array_debug :: array_debug $ Array
Resizable one dimension array of objects.array_debug :: array_debug $ Array
Resizable one dimension array of objects.serialization :: serialization_core
Abstract services to serialize Nit objects to different formatscore :: union_find
union–find algorithm using an efficient disjoint-set data structure
# Exposes functions to help profile or debug Arrays.
module array_debug
import counter
redef class Sys
# Tracks the average length of the Strings of an array when calling to_s
var arr_s_len = new Counter[Int]
# Keeps the average length of an Array when calling to_s
var arr_len = new Counter[Int]
# Compute the average array length.
fun avg_arr_len: Float do
var total = 0
var sum = 0
for i in arr_len.keys do
total += arr_len[i]
sum += arr_len[i] * i
end
return sum.to_f / total.to_f
end
# Compute the average string length.
fun avg_s_len: Float do
var total = 0
var sum = 0
for i in arr_s_len.keys do
total += arr_s_len[i]
sum += arr_s_len[i] * i
end
return sum.to_f / total.to_f
end
# Display statistics in standard output.
fun print_stats do
if arr_len.sum == 0 then
print "*** No Array stats ***"
return
end
print "*** Array Stats ***"
print "Number of calls to Array::to_s : {sys.arr_len.sum}"
print "Average number of elements in an Array (when calling to_s) : {sys.avg_arr_len}"
print "Average string size in Array : {sys.avg_s_len}"
print "*** End of Stats ***"
end
redef fun run do
super
print_stats
end
end
redef fun exit(i)
do
sys.print_stats
super
end
redef class Array[E]
redef fun to_s do
sys.arr_len.inc length
for i in self do
sys.arr_s_len.inc i.to_s.length
end
return super
end
end
lib/array_debug/array_debug.nit:11,1--81,3