Advertisement
jules0707

PercoScala

Nov 23rd, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.48 KB | None | 0 0
  1. /* An attempt to make Percolation.java more functional but possibly not the best example as Array is preferred here over other immutable collections
  2. Scala code remains very imperative...some conciseness gained but that's it */
  3.  
  4. import edu.princeton.cs.algs4.WeightedQuickUnionUF
  5.  
  6. /* primary constructor shorthand, n parameter without val or var
  7.  are private values so not accessible from the
  8.  outside and immutable*/
  9.  
  10. class PercoScala(n: Int) {
  11.   // default member visibility is public
  12.   val grid_size: Int = n * n
  13.   val wquf: WeightedQuickUnionUF = new WeightedQuickUnionUF(grid_size + 2)
  14.   val top = 0
  15.   val bottom: Int = grid_size + 1
  16.  
  17.   // Arrays as size is fixed and fast random access important
  18.   val isSiteOpen: Array[Boolean] = new Array[Boolean](grid_size + 2)
  19.  
  20.   // top is always open
  21.   isSiteOpen(top) = true
  22.   isSiteOpen(bottom) = true
  23.  
  24.   // var are just simple enough for a global counter
  25.   var openSites: Int = 0
  26.  
  27.   def validate(i: Int, j: Int) =
  28.     if (i <= 0 || i > n || j <= 0 || j > n) throw new IllegalArgumentException(
  29.       "indices (" + i + "," + j + ") are not between 1 and " + n)
  30.  
  31.   def xyTo1D(i: Int, j: Int) = j + n * (i - 1)
  32.  
  33.    def open(i: Int, j: Int) = {
  34.     validate(i, j)
  35.     val site = xyTo1D(i, j)
  36.     if (!isSiteOpen(site)) {
  37.       isSiteOpen(site) = true
  38.       openSites += 1
  39.       connectAdjacentSites(i, j)
  40.     }
  41.   }
  42.  
  43.   def connectAdjacentSites(i: Int, j: Int): Unit = {
  44.     val site = xyTo1D(i, j)
  45.  
  46.     // connect to top sites on the first row
  47.     if (i == 1) wquf.union(site, top)
  48.  
  49.     // above site exits only if i>1
  50.     if (i > 1) {
  51.       val adjacent = xyTo1D(i - 1, j)
  52.       if (isSiteOpen(adjacent)) wquf.union(site, adjacent)
  53.     }
  54.  
  55.     // left site
  56.     if (j > 1 && j <= n) {
  57.       val adjacent = xyTo1D(i, j - 1)
  58.       if (isSiteOpen(adjacent)) wquf.union(site, adjacent)
  59.     }
  60.  
  61.     // right site
  62.     if (j >= 1 && j < n) {
  63.       val adjacent = xyTo1D(i, j + 1)
  64.       if (isSiteOpen(adjacent)) wquf.union(site, adjacent)
  65.     }
  66.  
  67.  
  68.     // below site exists only if i<n
  69.     if (i != n) {
  70.       val adjacent = xyTo1D(i + 1, j)
  71.       if (isSiteOpen(adjacent)) wquf.union(site, adjacent)
  72.     }
  73.  
  74.     // all bottom sites are connected to bottom !
  75.     if (i == n) wquf.union(site, bottom);
  76.  
  77.   }
  78.  
  79.   def isOpen(i: Int, j: Int) = isSiteOpen(xyTo1D(i, j))
  80.  
  81.   def isFull(i: Int, j: Int) = wquf.connected(xyTo1D(i, j), 0)
  82.  
  83.   def numberOfOpenSites = openSites
  84.  
  85.   def percolates = wquf.connected(top, bottom)
  86.  
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement