metrics: move counter to its own module
authorJean Privat <jean@pryen.org>
Tue, 23 Jul 2013 15:01:13 +0000 (11:01 -0400)
committerJean Privat <jean@pryen.org>
Tue, 23 Jul 2013 15:54:59 +0000 (11:54 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/counter.nit [new file with mode: 0644]
src/metrics/metrics_base.nit

diff --git a/src/counter.nit b/src/counter.nit
new file mode 100644 (file)
index 0000000..e7a7a9d
--- /dev/null
@@ -0,0 +1,63 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Simple numerical statistical analysis and presentation
+module counter
+
+# A counter counts occurence of things
+# Use this instead of a HashMap[E, Int]
+class Counter[E: Object]
+       # Total number of counted occurences
+       var total: Int = 0
+
+       private var map = new HashMap[E, Int]
+
+       # The number of counted occurences of `e'
+       fun [](e: E): Int
+       do
+               var map = self.map
+               if map.has_key(e) then return map[e]
+               return 0
+       end
+
+       # Count one more occurence of `e'
+       fun inc(e: E)
+       do
+               self.map[e] = self[e] + 1
+               total += 1
+       end
+
+       # Return an array of elements sorted by occurences
+       fun sort: Array[E]
+       do
+               var res = map.keys.to_a
+               var sorter = new CounterSorter[E](self)
+               sorter.sort(res)
+               #res.sort !cmp a, b = map[a] <=> map[b]
+               return res
+       end
+end
+
+private class CounterSorter[E: Object]
+       super AbstractSorter[E]
+       var counter: Counter[E]
+       redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
+end
+
+# Helper function to display n/d and handle division by 0
+fun div(n: Int, d: Int): String
+do
+       if d == 0 then return "na"
+       return ((100*n/d).to_f/100.0).to_precision(2)
+end
index 058d47f..c7e381b 100644 (file)
@@ -19,6 +19,7 @@ module metrics_base
 
 import modelbuilder
 import csv
+import counter
 
 redef class ToolContext
 
@@ -134,50 +135,3 @@ redef class MModule
                return not self.model.std_modules.has(self.name)
        end
 end
-
-# A counter counts occurence of things
-# Use this instead of a HashMap[E, Int]
-class Counter[E: Object]
-       # Total number of counted occurences
-       var total: Int = 0
-
-       private var map = new HashMap[E, Int]
-
-       # The number of counted occurences of `e'
-       fun [](e: E): Int
-       do
-               var map = self.map
-               if map.has_key(e) then return map[e]
-               return 0
-       end
-
-       # Count one more occurence of `e'
-       fun inc(e: E)
-       do
-               self.map[e] = self[e] + 1
-               total += 1
-       end
-
-       # Return an array of elements sorted by occurences
-       fun sort: Array[E]
-       do
-               var res = map.keys.to_a
-               var sorter = new CounterSorter[E](self)
-               sorter.sort(res)
-               #res.sort !cmp a, b = map[a] <=> map[b]
-               return res
-       end
-end
-
-private class CounterSorter[E: Object]
-       super AbstractSorter[E]
-       var counter: Counter[E]
-       redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
-end
-
-# Helper function to display n/d and handle division by 0
-fun div(n: Int, d: Int): String
-do
-       if d == 0 then return "na"
-       return ((100*n/d).to_f/100.0).to_precision(2)
-end