lib: added some useful methods in counter
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 25 Feb 2014 01:55:56 +0000 (20:55 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Fri, 28 Feb 2014 18:35:15 +0000 (13:35 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

lib/counter.nit

index 0aaa711..4c7b6d6 100644 (file)
@@ -27,6 +27,8 @@ class Counter[E: Object]
 
        private var map = new HashMap[E, Int]
 
+       redef fun iterator do return map.iterator
+
        # The number of counted occurrences of `e`
        redef fun [](e: E): Int
        do
@@ -46,6 +48,12 @@ class Counter[E: Object]
 
        redef fun values do return map.values
 
+       redef fun length do return map.length
+
+       redef fun is_empty do return map.is_empty
+
+       redef fun clear do map.clear
+
        # Count one more occurrence of `e`
        fun inc(e: E)
        do
@@ -118,6 +126,42 @@ class Counter[E: Object]
                        print "  {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.total)}%)"
                end
        end
+
+       # Return the element with the highest value
+       fun max: nullable E do
+               var max: nullable Int = null
+               var elem: nullable E = null
+               for e, v in map do
+                       if max == null or v > max then
+                               max = v
+                               elem = e
+                       end
+               end
+               return elem
+       end
+
+       # Return the couple with the lowest value
+       fun min: nullable E do
+               var min: nullable Int = null
+               var elem: nullable E = null
+               for e, v in map do
+                       if min == null or v < min then
+                               min = v
+                               elem = e
+                       end
+               end
+               return elem
+       end
+
+       # Values average
+       fun avg: Float do
+               if values.is_empty then return 0.0
+               var sum = 0
+               for value in map.values do
+                       sum += value
+               end
+               return sum.to_f / values.length.to_f
+       end
 end
 
 private class CounterSorter[E: Object]