Class: Derailleur::HashTrieNode
Instance Attribute Summary collapse
Attributes inherited from TrieNode
#content, #name, #parent
Instance Method Summary
collapse
Methods inherited from TrieNode
#<=>, #absorbent?, #compatible_graft?, #compatible_handoff?, #normal?, #root, #root?, #useless?, #verify_graft, #verify_hand_off_to, #wildcard?
Constructor Details
#initialize(name, parent = nil) ⇒ HashTrieNode
Returns a new instance of HashTrieNode.
8
9
10
11
|
# File 'lib/derailleur/core/hash_trie.rb', line 8
def initialize(name, parent=nil)
super
@children_hash = {}
end
|
Instance Attribute Details
#children_hash ⇒ Object
Returns the value of attribute children_hash.
6
7
8
|
# File 'lib/derailleur/core/hash_trie.rb', line 6
def children_hash
@children_hash
end
|
Instance Method Details
#<<(name) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/derailleur/core/hash_trie.rb', line 19
def << name
node = exact_child_for_name(name)
if node
node
else
new_node = HashTrieNode.new(name, self)
ret = if new_node.normal?
children_hash[name] = new_node
elsif children_hash.default
raise ArgumentError, "there already is a fallback child in #{self}" unless children_hash.default.name == new_node.name
children_hash.default
else
children_hash.default = new_node
end
ret
end
end
|
#child_for_name(name) ⇒ Object
41
42
43
|
# File 'lib/derailleur/core/hash_trie.rb', line 41
def child_for_name(name)
children_hash[name]
end
|
#children ⇒ Object
13
14
15
16
17
|
# File 'lib/derailleur/core/hash_trie.rb', line 13
def children
ret = @children_hash.values
ret << @children_hash.default if @children_hash.default
ret
end
|
#exact_child_for_name(name) ⇒ Object
37
38
39
|
# File 'lib/derailleur/core/hash_trie.rb', line 37
def exact_child_for_name(name)
children_hash.values.find{|v| v.name == name}
end
|
#graft!(other) ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/derailleur/core/hash_trie.rb', line 82
def graft!(other)
verify_graft(other)
other.parent = parent
if other.normal?
parent.children_hash[name] = other
else
parent.children_hash.default = other
end
end
|
#hand_off_to!(other) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/derailleur/core/hash_trie.rb', line 66
def hand_off_to!(other)
verify_hand_off_to(other)
other.children_hash.replace @children_hash
@children_hash = {}
if parent
if normal?
parent.children_hash[name] = other
else
parent.children_hash.default = @children_hash.default
end
other.parent = parent
@parent = nil
end
end
|
#prune! ⇒ Object
pruning an already pruned node will crash: it has no parent already also prunes recursively on useless parents will stop naturally on roots because they have an overloaded prune!
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/derailleur/core/hash_trie.rb', line 54
def prune!
return if root?
if normal?
parent.children_hash.delete(name)
else
parent.children_hash.default = nil
end
old_parent = parent
@parent = nil
old_parent.prune! if old_parent.useless?
end
|
#tree_map(&blk) ⇒ Object
45
46
47
48
49
|
# File 'lib/derailleur/core/hash_trie.rb', line 45
def tree_map(&blk)
children_hash.values.inject([yield(self)]) do |trunk, child|
trunk << child.tree_map(&blk)
end
end
|