37a56beffe50936c975cbcc7d3c9086998f7386b
[nit.git] / src / metrics / metrics_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Helpers for various statistics tools.
18 module metrics_base
19
20 import modelbuilder
21
22 redef class ToolContext
23
24 # --nullables
25 var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
26 # --static-types
27 var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
28 # --tables
29 var opt_tables = new OptionBool("Compute tables metrics", "--tables")
30 # --rta
31 var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
32 # --generate_hyperdoc
33 var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
34
35 var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
36 var output_dir: String = "."
37
38 redef init
39 do
40 super
41 self.option_context.add_option(opt_nullables)
42 self.option_context.add_option(opt_static_types)
43 self.option_context.add_option(opt_tables)
44 self.option_context.add_option(opt_rta)
45 self.option_context.add_option(opt_generate_hyperdoc)
46 self.option_context.add_option(opt_dir)
47 end
48
49 redef fun process_options
50 do
51 super
52 var val = self.opt_dir.value
53 if val != null then
54 val = val.simplify_path
55 val.mkdir
56 self.output_dir = val
57 end
58 end
59 end
60
61 # A counter counts occurence of things
62 # Use this instead of a HashMap[E, Int]
63 class Counter[E: Object]
64 # Total number of counted occurences
65 var total: Int = 0
66
67 private var map = new HashMap[E, Int]
68
69 # The number of counted occurences of `e'
70 fun [](e: E): Int
71 do
72 var map = self.map
73 if map.has_key(e) then return map[e]
74 return 0
75 end
76
77 # Count one more occurence of `e'
78 fun inc(e: E)
79 do
80 self.map[e] = self[e] + 1
81 total += 1
82 end
83
84 # Return an array of elements sorted by occurences
85 fun sort: Array[E]
86 do
87 var res = map.keys.to_a
88 var sorter = new CounterSorter[E](self)
89 sorter.sort(res)
90 #res.sort !cmp a, b = map[a] <=> map[b]
91 return res
92 end
93 end
94
95 private class CounterSorter[E: Object]
96 super AbstractSorter[E]
97 var counter: Counter[E]
98 redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
99 end
100
101 # Helper function to display n/d and handle division by 0
102 fun div(n: Int, d: Int): String
103 do
104 if d == 0 then return "na"
105 return ((100*n/d).to_f/100.0).to_precision(2)
106 end