Advertisement
NLinker

Alexey's fix :-)

Jul 10th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 4.25 KB | None | 0 0
  1.   private def initCategoryList: IndexedSeq[(Double, Double)] = {
  2.     val ret = new ArrayBuffer[(Double, Double)]()
  3.  
  4.     var catsLeft = ncats
  5.  
  6.     if (min != vll.green) {
  7.       ret += ((roundify(min), roundify(vll.green)))
  8.       catsLeft -= 1
  9.     }
  10.  
  11.     val width = (max - vll.green) / catsLeft
  12.  
  13.     val distances = Array[Double](vll.yellow - vll.green, vll.red - vll.yellow, vll.darkred - vll.red, max - vll.darkred)
  14.     val preCatsList = distances map(distance => (distance, distance/width, Math.round(distance / width).toInt))
  15.     val preCatsPlusFraction = preCatsList map(cat => (cat._1, cat._2, cat._3, (cat._3 > cat._2) match {
  16.       case true => cat._2 + 1.0 - cat._3
  17.       case false => cat._2 - cat._3
  18.     })) // tuple (distance, double cats, rounded cats, fraction)
  19.  
  20.     val preCatsCount = preCatsPlusFraction.map(_._3).reduce((a, b) => a + b)
  21.  
  22.     var finalCats: Array[(Double, Double, Int, Double)] = null
  23.     val defect = catsLeft - preCatsCount
  24.     defect match {
  25.       case 0 => finalCats = preCatsPlusFraction //do nothing, preCatsCount are already correct
  26.       case 1 => {
  27.         val (fractionMax, maxCount) = preCatsPlusFraction.filter(_._3 > 1).map(x => (x._4, 1)).reduce((a, b) => {
  28.           val maxFraction = Math.max(a._1, b._1)
  29.           val count = (a._1 - b._1) match {
  30.             case 0 => a._2 + b._2
  31.             case x => if (x > 0) a._2 else b._2
  32.           }
  33.           (maxFraction, count)
  34.         })
  35.         var index = 0
  36.         maxCount match {
  37.           case 1 => {
  38.             index = preCatsPlusFraction.zipWithIndex.find(_._1._4 == fractionMax).get._2
  39.           }
  40.           case _ => {
  41.             index = preCatsPlusFraction.zipWithIndex.filter(_._1._4 == fractionMax).reduce((a, b) => {
  42.               (a._1._3 < b._1._3) match {
  43.                 case true => b
  44.                 case false => a
  45.               }
  46.             })._2
  47.           }
  48.         }
  49.         finalCats = preCatsPlusFraction.zipWithIndex.map(x => x._2 == index match {
  50.           case true =>  (x._1._1, x._1._2, x._1._3 + 1, x._1._4)
  51.           case false => x._1
  52.         })
  53.       }
  54.       case -1 => {
  55.         var (fractionMin, minCount) = preCatsPlusFraction.filter(_._3 > 1).map(x => (x._4, 1)).reduce((a, b) => {
  56.           val minFraction = Math.min(a._1, b._1)
  57.           val count = (a._1 - b._1) match {
  58.             case 0 => a._2 + b._2
  59.             case x => if (x > 0) b._2 else a._2
  60.           }
  61.           (minFraction, count)
  62.         })
  63.         var index = 0
  64.         minCount match {
  65.           case 1 => {
  66.             index = preCatsPlusFraction.zipWithIndex.find(_._1._4 == fractionMin).get._2
  67.           }
  68.           case _ => {
  69.             index = preCatsPlusFraction.zipWithIndex.filter(_._1._4 == fractionMin).reduce((a, b) => {
  70.               (a._1._3 < b._1._3) match {
  71.                 case true => b
  72.                 case false => a
  73.               }
  74.             })._2
  75.           }
  76.         }
  77.         finalCats = preCatsPlusFraction.zipWithIndex.map(x => x._2 == index match {
  78.           case true => (x._1._1, x._1._2, x._1._3 - 1, x._1._4)
  79.           case false => x._1
  80.         })
  81.       }
  82.       case -2 => { // this is very unlikely to happen
  83.  
  84.         var indices = preCatsPlusFraction.zipWithIndex.filter(x=> x._1._3 > 1).sortWith((a, b) => a._1._4 < b._1._4 || (a._1._4 == b._1._4 && a._1._3 > b._1._3)).slice(0, 2).map(_._2)
  85.         finalCats = preCatsPlusFraction.zipWithIndex.map(x => indices.contains(x._2) match {
  86.           case true => (x._1._1, x._1._2, x._1._3 -1, x._1._4)
  87.           case false => x._1
  88.         })
  89.       }
  90.       case _ => throw new RuntimeException("Problem with building LinearDoubleCategory")
  91.     }
  92.  
  93.     val catsWithLevels = finalCats.zipWithIndex.map(x => {
  94.       x._2 match {
  95.         case 0 => (x._1, vll.green)
  96.         case 1 => (x._1, vll.yellow)
  97.         case 2 => (x._1, vll.red)
  98.         case 3 => (x._1, vll.darkred)
  99.       }
  100.     })
  101.  
  102.     for (x <- catsWithLevels) {
  103.       var tmp = x._2
  104.       val colorWidth = x._1._1 / x._1._3
  105.       for (i <- 0 until x._1._3 - 1) {
  106.         ret += ((roundify(tmp + colorWidth * i), roundify(tmp + colorWidth * (i + 1))))
  107.       }
  108.       if (x._1._3 > 0)
  109.         ret += ((roundify(tmp + colorWidth * Math.max(x._1._3 - 1, 0)), roundify(tmp + x._1._1)))
  110.     }
  111.     ret
  112.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement