misc/vim: inform the user when no results are found
[nit.git] / lib / array_debug.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Exposes functions to help profile or debug Arrays.
12 module array_debug
13
14 import counter
15
16 redef class Sys
17
18 # Tracks the average length of the Strings of an array when calling to_s
19 var arr_s_len = new Counter[Int]
20
21 # Keeps the average length of an Array when calling to_s
22 var arr_len = new Counter[Int]
23
24 # Compute the average array length.
25 fun avg_arr_len: Float do
26 var total = 0
27 var sum = 0
28 for i in arr_len.keys do
29 total += arr_len[i]
30 sum += arr_len[i] * i
31 end
32 return sum.to_f / total.to_f
33 end
34
35 # Compute the average string length.
36 fun avg_s_len: Float do
37 var total = 0
38 var sum = 0
39 for i in arr_s_len.keys do
40 total += arr_s_len[i]
41 sum += arr_s_len[i] * i
42 end
43 return sum.to_f / total.to_f
44 end
45
46 # Display statistics in standard output.
47 fun print_stats do
48 if arr_len.sum == 0 then
49 print "*** No Array stats ***"
50 return
51 end
52 print "*** Array Stats ***"
53 print "Number of calls to Array::to_s : {sys.arr_len.sum}"
54 print "Average number of elements in an Array (when calling to_s) : {sys.avg_arr_len}"
55 print "Average string size in Array : {sys.avg_s_len}"
56 print "*** End of Stats ***"
57 end
58
59 redef fun run do
60 super
61 print_stats
62 end
63 end
64
65 redef fun exit(i)
66 do
67 sys.print_stats
68 super
69 end
70
71 redef class Array[E]
72
73 redef fun to_s do
74 sys.arr_len.inc length
75 for i in self do
76 sys.arr_s_len.inc i.to_s.length
77 end
78 return super
79 end
80
81 end