X-Git-Url: http://nitlanguage.org diff --git a/lib/counter.nit b/lib/counter.nit index df4beca..75278d2 100644 --- a/lib/counter.nit +++ b/lib/counter.nit @@ -52,7 +52,10 @@ 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) @@ -65,7 +68,7 @@ class Counter[E: Object] 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 @@ -170,8 +173,8 @@ class Counter[E: Object] 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 @@ -180,7 +183,7 @@ 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.sum)}%)" end