18b19e36ec20a0e34807d279f2af45a1e03fe698
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Helpers for various statistics tools.
22 redef class ToolContext
25 var opt_all
= new OptionBool("Compute all metrics", "--all")
28 var opt_refinement
= new OptionBool("Compute metrics about refinement usage", "--refinement")
30 var opt_self
= new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
32 var opt_nullables
= new OptionBool("Compute metrics on nullables send", "--nullables")
34 var opt_static_types
= new OptionBool("Compute explicit static types metrics", "--static-types")
36 var opt_tables
= new OptionBool("Compute tables metrics", "--tables")
38 var opt_rta
= new OptionBool("Compute RTA metrics", "--rta")
40 var opt_generate_hyperdoc
= new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
42 var opt_dir
= new OptionString("Directory where some statistics files are generated", "-d", "--dir")
43 var output_dir
: String = "."
48 self.option_context
.add_option
(opt_all
)
49 self.option_context
.add_option
(opt_refinement
)
50 self.option_context
.add_option
(opt_self
)
51 self.option_context
.add_option
(opt_nullables
)
52 self.option_context
.add_option
(opt_static_types
)
53 self.option_context
.add_option
(opt_tables
)
54 self.option_context
.add_option
(opt_rta
)
55 self.option_context
.add_option
(opt_generate_hyperdoc
)
56 self.option_context
.add_option
(opt_dir
)
59 redef fun process_options
62 var val
= self.opt_dir
.value
64 val
= val
.simplify_path
71 # A counter counts occurence of things
72 # Use this instead of a HashMap[E, Int]
73 class Counter[E
: Object]
74 # Total number of counted occurences
77 private var map
= new HashMap[E
, Int]
79 # The number of counted occurences of `e'
83 if map
.has_key
(e
) then return map
[e
]
87 # Count one more occurence of `e'
90 self.map
[e
] = self[e
] + 1
94 # Return an array of elements sorted by occurences
97 var res
= map
.keys
.to_a
98 var sorter
= new CounterSorter[E
](self)
100 #res.sort !cmp a, b = map[a] <=> map[b]
105 private class CounterSorter[E
: Object]
106 super AbstractSorter[E
]
107 var counter
: Counter[E
]
108 redef fun compare
(a
,b
) do return self.counter
.map
[a
] <=> self.counter
.map
[b
]
111 # Helper function to display n/d and handle division by 0
112 fun div
(n
: Int, d
: Int): String
114 if d
== 0 then return "na"
115 return ((100*n
/d
).to_f
/100.0).to_precision
(2)