remove closures from code
[nit.git] / lib / counter.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Simple numerical statistical analysis and presentation
16 module counter
17
18 import poset
19
20 # A counter counts occurrences of things
21 # Use this instead of a `HashMap[E, Int]`
22 class Counter[E: Object]
23 super Map[E, Int]
24
25 # Total number of counted occurrences
26 var total: Int = 0
27
28 private var map = new HashMap[E, Int]
29
30 # The number of counted occurrences of `e`
31 redef fun [](e: E): Int
32 do
33 var map = self.map
34 if map.has_key(e) then return map[e]
35 return 0
36 end
37
38 redef fun []=(e: E, value: Int)
39 do
40 total -= self[e]
41 self.map[e] = value
42 total += value
43 end
44
45 redef fun keys do return map.keys
46
47 redef fun values do return map.values
48
49 # Count one more occurrence of `e`
50 fun inc(e: E)
51 do
52 self.map[e] = self[e] + 1
53 total += 1
54 end
55
56 # Return an array of elements sorted by occurrences
57 fun sort: Array[E]
58 do
59 var res = map.keys.to_a
60 var sorter = new CounterSorter[E](self)
61 sorter.sort(res)
62 return res
63 end
64
65 # The method used to display an element
66 # @toimplement by default just call `to_s` on the element
67 protected fun element_to_s(e: E): String
68 do
69 do return e.to_s
70 end
71
72 # Display statistical information
73 fun print_summary
74 do
75 var list = self.sort
76 print " population: {list.length}"
77 if list.is_empty then return
78 print " minimum value: {self[list.first]}"
79 print " maximum value: {self[list.last]}"
80 print " total value: {self.total}"
81 print " average value: {div(self.total,list.length)}"
82 print " distribution:"
83 var count = 0
84 var sum = 0
85 var limit = self[list.first]
86 for t in list do
87 if self[t] > limit then
88 print " <={limit}: sub-population={count} ({div(count*100,list.length)}%); cumulated value={sum} ({div(sum*100,self.total)}%)"
89 count = 0
90 sum = 0
91 while self[t] > limit do
92 limit = limit * 2
93 if limit == 0 then limit = 1
94 end
95 end
96 count += 1
97 sum += self[t]
98 end
99 print " <={limit}: sub-population={count} ({div(count*100,list.length)}%); cumulated value={sum} ({div(sum*100,self.total)}%)"
100 end
101
102 # Display up to `count` most used elements and `count` least used elements
103 # Use `element_to_s` to display the element
104 fun print_elements(count: Int)
105 do
106 print " list:"
107 var list = self.sort
108 var min = count
109 if list.length <= count*2 then min = list.length
110 for i in [0..min[ do
111 var t = list[list.length-i-1]
112 print " {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.total)}%)"
113 end
114 if list.length <= count*2 then return
115 print " ..."
116 for i in [0..min[ do
117 var t = list[min-i-1]
118 print " {element_to_s(t)}: {self[t]} ({div(self[t]*100,self.total)}%)"
119 end
120 end
121 end
122
123 private class CounterSorter[E: Object]
124 super AbstractSorter[E]
125 var counter: Counter[E]
126 redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
127 end
128
129 redef class POSet[E]
130 private fun show_counter(c: Counter[Int])
131 do
132 var list = c.sort
133 (new ComparableSorter[Int]).sort(list)
134 for e in list do
135 print " {e} -> {c[e]} times ({div(c[e]*100, c.total)}%)"
136 end
137 end
138
139 # Display exhaustive metrics about the poset
140 fun print_metrics
141 do
142 var nb_greaters = new Counter[E]
143 var nb_direct_greaters = new Counter[E]
144 var nb_smallers = new Counter[E]
145 var nb_direct_smallers = new Counter[E]
146 var nb_direct_edges = 0
147 var nb_edges = 0
148 for n in self do
149 var ne = self[n]
150 nb_edges += ne.greaters.length
151 nb_direct_edges += ne.direct_greaters.length
152 nb_greaters[n] = ne.greaters.length
153 nb_direct_greaters[n] = ne.direct_greaters.length
154 nb_smallers[n] = ne.smallers.length
155 nb_direct_smallers[n] = ne.direct_smallers.length
156 end
157 print "Number of nodes: {self.length}"
158 print "Number of edges: {nb_edges} ({div(nb_edges,self.length)} per node)"
159 print "Number of direct edges: {nb_direct_edges} ({div(nb_direct_edges,self.length)} per node)"
160 print "Distribution of greaters"
161 nb_greaters.print_summary
162 print "Distribution of direct greaters"
163 nb_direct_greaters.print_summary
164 print "Distribution of smallers"
165 nb_smallers.print_summary
166 print "Distribution of direct smallers"
167 nb_direct_smallers.print_summary
168 end
169 end
170
171 # Helper function to display `n/d` and handle division by 0
172 fun div(n: Int, d: Int): String
173 do
174 if d == 0 then return "na"
175 return ((100*n/d).to_f/100.0).to_precision(2)
176 end