contrib/jwrapper: do not add `redef type SELF...` to generated extern classes
[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 fun avg_arr_len: Float do
25 var total = 0
26 var sum = 0
27 for i in arr_len.keys do
28 total += arr_len[i]
29 sum += arr_len[i] * i
30 end
31 return sum.to_f / total.to_f
32 end
33
34 fun avg_s_len: Float do
35 var total = 0
36 var sum = 0
37 for i in arr_s_len.keys do
38 total += arr_s_len[i]
39 sum += arr_s_len[i] * i
40 end
41 return sum.to_f / total.to_f
42 end
43
44 fun print_stats do
45 if arr_len.sum == 0 then
46 print "*** No Array stats ***"
47 return
48 end
49 print "*** Array Stats ***"
50 print "Number of calls to Array::to_s : {sys.arr_len.sum}"
51 print "Average number of elements in an Array (when calling to_s) : {sys.avg_arr_len}"
52 print "Average string size in Array : {sys.avg_s_len}"
53 print "*** End of Stats ***"
54 end
55
56 redef fun run do
57 super
58 print_stats
59 end
60 end
61
62 redef fun exit(i)
63 do
64 sys.print_stats
65 super
66 end
67
68 redef class Array[E]
69
70 redef fun to_s do
71 sys.arr_len.inc length
72 for i in self do
73 sys.arr_s_len.inc i.to_s.length
74 end
75 return super
76 end
77
78 end