7bb23d9cca2f33abf0323911271a0d0e7b00032e
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_generate_hyperdoc
= new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
27 var opt_dir
= new OptionString("Directory where some statistics files are generated", "-d", "--dir")
28 var output_dir
: String = "."
33 self.option_context
.add_option
(opt_generate_hyperdoc
)
34 self.option_context
.add_option
(opt_dir
)
37 redef fun process_options
40 var val
= self.opt_dir
.value
42 val
= val
.simplify_path
49 # A counter counts occurence of things
50 # Use this instead of a HashMap[E, Int]
51 class Counter[E
: Object]
52 # Total number of counted occurences
55 private var map
= new HashMap[E
, Int]
57 # The number of counted occurences of `e'
61 if map
.has_key
(e
) then return map
[e
]
65 # Count one more occurence of `e'
68 self.map
[e
] = self[e
] + 1
72 # Return an array of elements sorted by occurences
75 var res
= map
.keys
.to_a
76 var sorter
= new CounterSorter[E
](self)
78 #res.sort !cmp a, b = map[a] <=> map[b]
83 private class CounterSorter[E
: Object]
84 super AbstractSorter[E
]
85 var counter
: Counter[E
]
86 redef fun compare
(a
,b
) do return self.counter
.map
[a
] <=> self.counter
.map
[b
]
89 # Helper function to display n/d and handle division by 0
90 fun div
(n
: Int, d
: Int): String
92 if d
== 0 then return "na"
93 return ((100*n
/d
).to_f
/100.0).to_precision
(2)