-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole_util.rb
65 lines (56 loc) · 1.8 KB
/
console_util.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module ConsoleUtil
def byte_label(byte)
return '---' unless byte
case c = [byte].pack('c')
when /[\d\w \~\`\!\@\#\$\%\^\&\*\(\)\-\_\+\=\\\|\;\:\'\"\,\<\.\>\/\?\[\{\]\}]/
"'#{c}'"
when "\n"
"'\\n'"
when "\r"
"'\\r'"
when "\t"
"'\\t'"
else
format('?%02X',byte)
end
end
def dump_heap(heap,index = 0,level = 0,levels = [])
if index < heap.length
(levels[level] ||= []) << [heap[index].byte,heap[index].count]
left_child_index,right_child_index = heap_child_indices(index)
dump_heap(heap,left_child_index,level + 1,levels)
dump_heap(heap,right_child_index,level + 1,levels)
end
levels
end
def dump_tree(node)
return [[node]] if node.byte
left_paths = dump_tree_children(node.left_child,'-^ ',false)
right_paths = dump_tree_children(node.right_child,'-v ',true)
all_paths = left_paths + [[nil,' +-',node]] + right_paths
max_length = all_paths.collect{|p| p.length}.max
all_paths.collect do |path|
leaf = path[0,1]
insertion = leaf[0] ? ['---'] : [nil]
path = path[0,1] + insertion + path[1..-1] while path.length < max_length
path
end
end
def dump_tree_children(node,corner_string,before)
insertion,pending = before ? [' | ',nil] : [nil,' | ']
(node ? dump_tree(node) : [[nil]]).each do |p|
if p.last
p << corner_string
insertion = pending
else
p << insertion
end
p << nil
end
end
def dump_nodes(nodes,total_bytes)
symbol_count = 0
nodes.each{|node| next unless node; symbol_count += 1; puts format('%s => %d (%0.06f) %s (%d)',node.label,node.count,node.count.to_f/total_bytes,node.code || '?',node.code.to_s.length)}
puts "TOTAL:#{total_bytes} SYMBOLS:#{symbol_count}"
end
end