+ print " {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.sum)}%)"
+ end
+ end
+
+ # Return the element with the highest value (aka. the mode)
+ #
+ # ~~~
+ # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+ # assert c.max == "b"
+ # ~~~
+ #
+ # If more than one max exists, the first one is returned.
+ 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
+ #
+ # ~~~
+ # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+ # assert c.min == "c"
+ # ~~~
+ #
+ # If more than one min exists, the first one is returned.
+ 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 (aka. arithmetic mean)
+ #
+ # ~~~
+ # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+ # assert c.avg == 2.0
+ # ~~~
+ fun avg: Float do
+ if values.is_empty then return 0.0
+ return (sum / values.length).to_f
+ end
+
+ # The standard derivation of the counter values
+ #
+ # ~~~
+ # var c = new Counter[String].from(["a", "a", "b", "b", "b", "c"])
+ # assert c.std_dev > 0.81
+ # assert c.std_dev < 0.82
+ # ~~~
+ fun std_dev: Float do
+ var avg = self.avg
+ var sum = 0.0
+ for value in map.values do
+ sum += (value.to_f - avg).pow(2.0)