Guest User

Cache.scala

a guest
Apr 23rd, 2012
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.70 KB | None | 0 0
  1. /**
  2.  * To remove duplicate elements from verArr and compacting the array, the algo
  3.  * uses two hash maps:
  4.  * uniqueMap, contains all the unique vertices as key and their respective
  5.  *            indices in final compacted verArr as value.
  6.  * redirectionMap, duplicate vertex entries go in this map, with their indices
  7.  *                 as key and value would be their index in final compacted
  8.  *                 verArr.
  9.  * using these two maps, verArr is compacted and entries would be unique.
  10.  * similarly facArr is changed to point to correct indices corresponding to
  11.  * compacted verArr.
  12.  *
  13.  * Time Complexity: O(n + m), where n and m are lengths of verArr and facArr.
  14.  * Assuming array index starting from 0.
  15.  */
  16.  
  17. import scala.collection.mutable.HashMap
  18.  
  19. // Vertex and Face classes.
  20. case class Vertex(a: Int, b: Int, c: Int)
  21. case class Face(var a: Int, var b: Int, var c: Int)
  22.  
  23. class Duplicates(verArr: Array[Vertex], facArr: Array[Face]) {
  24.   private val uniqueMap = new HashMap[Vertex, Int]
  25.   private val redirectionMap = new HashMap[Int, Int]
  26.   // diff is used to get the new index in the compact verArr for the unique
  27.   // verices.
  28.   private var diff = 0
  29.  
  30.   // Compacts the verArr and changes the facArr correspondingly.
  31.   def remove = {
  32.     for (i <- verArr.indices) {
  33.       val vertex = verArr(i)
  34.       if (uniqueMap contains vertex) {
  35.         // vertex is not unique, put it in redirection map with correct
  36.         // redirection.
  37.         redirectionMap += (i -> uniqueMap.apply(vertex))
  38.         diff += 1
  39.       }
  40.       // vertex is unique, put it in unique map with corresponding final verArr
  41.       // index.
  42.       else uniqueMap += (vertex -> (i - diff))
  43.     }
  44.  
  45.     // Change the facArr to point to correct entries in compacted verArr.
  46.     for (face <- facArr) {
  47.       face.a = if (redirectionMap contains face.a) redirectionMap.apply(face.a) else uniqueMap.apply(verArr(face.a))
  48.       face.b = if (redirectionMap contains face.b) redirectionMap.apply(face.b) else uniqueMap.apply(verArr(face.b))
  49.       face.c = if (redirectionMap contains face.c) redirectionMap.apply(face.c) else uniqueMap.apply(verArr(face.c))
  50.     }
  51.  
  52.     // Change the values in verArr, first verArrLength entries would be unique
  53.     // and corresponding to comapcted verArr. These values can be stored in some
  54.     // other collection also.
  55.     for ((k, v) <- uniqueMap) verArr(v) = k
  56.   }
  57.  
  58.   // Returns the facArr corresponding to squeezed verArr.
  59.   def getFacArr = facArr
  60.   // Length of this array would be same as i/p verArr but first verArrLength
  61.   // entries would be unique and stored in the order according to required
  62.   // compact verArr.
  63.   def getVerArr = verArr
  64.   def verArrLength = uniqueMap.size
  65. }
Advertisement
Add Comment
Please, Sign In to add comment