super Map[E, Int]
# Total number of counted occurrences
- var total: Int = 0
+ var sum: Int = 0
private var map = new HashMap[E, Int]
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
fun inc(e: E)
do
self.map[e] = self[e] + 1
- total += 1
+ sum += 1
end
# Return an array of elements sorted by occurrences
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
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
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
# 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
+ return (sum / values.length).to_f
end
# The standard derivation of the counter values
var list = c.sort
(new ComparableSorter[Int]).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
redef fun clear do values_cache.clear
+ fun sum: Int do return values_cache.sum
+
# Return the couple with the highest value
fun max: Couple[ELM, Int] do
assert not values_cache.is_empty
redef fun clear do values_cache.clear
+ fun sum: Float do
+ var sum = 0.0
+ for v in values.values do sum += v
+ return sum
+ end
+
# Return the couple with the highest value
fun max: Couple[ELM, Float] do
assert not values.is_empty
redef fun avg do
if values.is_empty then return 0.0
- var sum = 0.0
- for value in values.values do
- sum += value
- end
return sum / values.length.to_f
end