Advertisement
pvanheus

Untitled

May 12th, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.76 KB | None | 0 0
  1. package marching_band
  2.  
  3. /**
  4.  * Created by pvh on 2015/05/12.
  5.  */
  6. class Band(r: Int, c: Int, init_matrix: Vector[Vector[Int]] = Vector(Vector()),
  7.            init_transformations: List[(Int, Int) => (Int, Int)] = List(),
  8.            init_r: Int = 0,
  9.            init_c: Int = 0) {
  10.  
  11.   val init_rows = if (init_r == 0) r else init_r
  12.   val init_cols = if (init_r == 0) c else init_c
  13.   val matrix = if (init_matrix == Vector(Vector()))
  14.     (1 to r map { x => (1 to c map { y => ((x-1)*c) + y }).toVector }).toVector
  15.   else
  16.     init_matrix
  17.   val transformations = init_transformations
  18.  
  19.   def at(x: Int, y: Int) = { matrix(x-1)(y-1) }
  20.  
  21.   def find_helper(x: Int, y: Int,
  22.                   t: List[(Int, Int) => (Int, Int)]): (Int, Int) = t match {
  23.     case List() => (x,y)
  24.     case _ => {
  25.       val new_pos = t.head(x, y)
  26.       find_helper(new_pos._1, new_pos._2, t.tail)
  27.     }
  28.   }
  29.  
  30.   def find(member: Int) = {
  31.     val init_row = ((member - 1)/ init_cols) + 1
  32.     val init_column = ((member - 1) % init_cols) + 1
  33.     find_helper(init_row, init_column, transformations)
  34.   }
  35.  
  36.   def reverse_columns() = {
  37.     val new_matrix = (matrix map { row: Vector[Int] => row.reverse.toVector }).toVector
  38.     new Band(r, c, new_matrix, transformations :+ ((x: Int, y: Int) => (x,c-y+1)))
  39.   }
  40.  
  41.   def reverse_rows() = {
  42.     val new_matrix = (r to 1 by -1 map { r => matrix(r-1) }).toVector
  43.     new Band(r, c, new_matrix, transformations :+ ((x: Int, y: Int) => (r-x+1, y)))
  44.   }
  45.  
  46.   def swap_rows(r1: Int, r2: Int) = {
  47.     val new_matrix = (1 to r map {
  48.       case `r1` => matrix(r2 - 1)
  49.       case `r2` => matrix(r1 - 1)
  50.       case num => matrix(num - 1)
  51.     }).toVector
  52.     new Band(r, c, new_matrix, transformations :+ (
  53.       (x: Int, y: Int) => x match {
  54.         case `r1` => (r2, y)
  55.         case `r2` => (r1, y)
  56.         case _ => (x, y)
  57.       }))
  58.   }
  59.  
  60.   def swap_columns(c1: Int, c2: Int) = {
  61.     val new_matrix = (1 to r map {
  62.       row_num => {
  63.         (1 to c map {
  64.           case `c1` => matrix(row_num - 1)(c2 - 1)
  65.           case `c2` => matrix(row_num - 1)(c1 - 1)
  66.           case col_num => matrix(row_num - 1)(col_num - 1)
  67.         }).toVector
  68.       }
  69.     }).toVector
  70.     new Band(r, c, new_matrix, transformations :+ (
  71.       (x: Int, y: Int) => y match {
  72.         case `c1` => (x, c2)
  73.         case `c2` => (x, c1)
  74.         case _ => (x,y)
  75.       }))
  76.   }
  77.  
  78.   def transpose() = {
  79.     new Band(c, r, matrix.transpose, transformations :+ ((x: Int, y: Int) => { (y, x) }), r, c )
  80.   }
  81.  
  82.   override def toString = {
  83.     val width = scala.math.log10(r*c).toInt
  84.     val format_str = "%" + (if (width > 0) width.toString else "") + "d"
  85.     matrix map {
  86.       case row => (row map { case num => format_str format num }) mkString " "
  87.     } mkString "\n"
  88.   }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement