X-Git-Url: http://nitlanguage.org diff --git a/lib/counter.nit b/lib/counter.nit index 4c7b6d6..75278d2 100644 --- a/lib/counter.nit +++ b/lib/counter.nit @@ -23,7 +23,7 @@ class Counter[E: Object] super Map[E, Int] # Total number of counted occurrences - var total: Int = 0 + var sum: Int = 0 private var map = new HashMap[E, Int] @@ -39,9 +39,9 @@ class Counter[E: Object] redef fun []=(e: E, value: Int) do - total -= self[e] + sum -= self[e] self.map[e] = value - total += value + sum += value end redef fun keys do return map.keys @@ -52,20 +52,23 @@ class Counter[E: Object] redef fun is_empty do return map.is_empty - redef fun clear do map.clear + redef fun clear do + sum = 0 + map.clear + end # Count one more occurrence of `e` fun inc(e: E) do self.map[e] = self[e] + 1 - total += 1 + sum += 1 end # Return an array of elements sorted by occurrences fun sort: Array[E] do var res = map.keys.to_a - var sorter = new CounterSorter[E](self) + var sorter = new CounterComparator[E](self) sorter.sort(res) return res end @@ -85,15 +88,15 @@ class Counter[E: Object] if list.is_empty then return print " minimum value: {self[list.first]}" print " maximum value: {self[list.last]}" - print " total value: {self.total}" - print " average value: {div(self.total,list.length)}" + print " total value: {self.sum}" + print " average value: {div(self.sum,list.length)}" print " distribution:" var count = 0 var sum = 0 var limit = self[list.first] for t in list do if self[t] > limit then - print " <={limit}: sub-population={count} ({div(count*100,list.length)}%); cumulated value={sum} ({div(sum*100,self.total)}%)" + print " <={limit}: sub-population={count} ({div(count*100,list.length)}%); cumulated value={sum} ({div(sum*100,self.sum)}%)" count = 0 sum = 0 while self[t] > limit do @@ -104,7 +107,7 @@ class Counter[E: Object] count += 1 sum += self[t] end - print " <={limit}: sub-population={count} ({div(count*100,list.length)}%); cumulated value={sum} ({div(sum*100,self.total)}%)" + print " <={limit}: sub-population={count} ({div(count*100,list.length)}%); cumulated value={sum} ({div(sum*100,self.sum)}%)" end # Display up to `count` most used elements and `count` least used elements @@ -117,13 +120,13 @@ class Counter[E: Object] if list.length <= count*2 then min = list.length for i in [0..min[ do var t = list[list.length-i-1] - print " {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.total)}%)" + print " {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.sum)}%)" end if list.length <= count*2 then return print " ..." for i in [0..min[ do var t = list[min-i-1] - print " {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.total)}%)" + print " {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.sum)}%)" end end @@ -156,16 +159,22 @@ class Counter[E: Object] # Values average fun avg: Float do if values.is_empty then return 0.0 - var sum = 0 + return (sum / values.length).to_f + end + + # The standard derivation of the counter values + fun std_dev: Float do + var avg = self.avg + var sum = 0.0 for value in map.values do - sum += value + sum += (value.to_f - avg).pow(2.0) end - return sum.to_f / values.length.to_f + return (sum / map.length.to_f).sqrt end end -private class CounterSorter[E: Object] - super AbstractSorter[E] +private class CounterComparator[E: Object] + super Comparator[E] var counter: Counter[E] redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b] end @@ -174,9 +183,9 @@ redef class POSet[E] private fun show_counter(c: Counter[Int]) do var list = c.sort - (new ComparableSorter[Int]).sort(list) + default_comparator.sort(list) for e in list do - print " {e} -> {c[e]} times ({div(c[e]*100, c.total)}%)" + print " {e} -> {c[e]} times ({div(c[e]*100, c.sum)}%)" end end