Class: StringTree::Tree
- Inherits:
-
Object
- Object
- StringTree::Tree
- Defined in:
- lib/stringtree/tree.rb
Overview
Tree represents a complete StringTree, and has functionality resembling a Hash.
Instance Attribute Summary collapse
-
#root ⇒ Object
The root StringTree::Node, or nil if empty.
Instance Method Summary collapse
-
#add(key, value) ⇒ Object
(also: #[]=)
Add a key and value to this Tree.
-
#clear ⇒ Object
Clear the Tree (Remove all keys/values).
-
#delete(key) ⇒ Object
Delete a key.
-
#find(key) ⇒ Object
(also: #[])
Find a specified key in the Tree, and return the value, or nil if not found.
-
#has_key?(key) ⇒ Boolean
(also: #include?)
Return true if the given key exists.
-
#initialize ⇒ Tree
constructor
Create a new empty Tree.
-
#match_all(data, &block) ⇒ Object
Tokenize the string Data by finding all instances of any key in the Tree.
-
#match_count(data, list = {}) ⇒ Object
Return a Hash of terminating nodes to Integer counts for a given String data, i.e.
-
#optimize! ⇒ Object
Rebalance the tree for faster access.
-
#partials(key) ⇒ Object
Return an Array of Strings representing all partial matches forward of key in the Tree.
Constructor Details
#initialize ⇒ Tree
Create a new empty Tree
8 9 10 |
# File 'lib/stringtree/tree.rb', line 8 def initialize clear end |
Instance Attribute Details
#root ⇒ Object
The root StringTree::Node, or nil if empty
5 6 7 |
# File 'lib/stringtree/tree.rb', line 5 def root @root end |
Instance Method Details
#add(key, value) ⇒ Object Also known as: []=
Add a key and value to this Tree
13 14 15 16 |
# File 'lib/stringtree/tree.rb', line 13 def add(key,value) @root = Node.new(key[0], nil) if (@root == nil) @root.add_vertical(key,value) end |
#clear ⇒ Object
Clear the Tree (Remove all keys/values)
21 22 23 |
# File 'lib/stringtree/tree.rb', line 21 def clear @root = nil end |
#delete(key) ⇒ Object
Delete a key
41 42 43 44 45 46 47 |
# File 'lib/stringtree/tree.rb', line 41 def delete(key) node = find_node(key) return false if node.nil? node.value = nil node.prune true end |
#find(key) ⇒ Object Also known as: []
Find a specified key in the Tree, and return the value, or nil if not found.
26 27 28 29 |
# File 'lib/stringtree/tree.rb', line 26 def find(key) node = find_node(key) (node == nil ? nil : node.value) end |
#has_key?(key) ⇒ Boolean Also known as: include?
Return true if the given key exists
34 35 36 |
# File 'lib/stringtree/tree.rb', line 34 def has_key?(key) !find_node(key).nil? end |
#match_all(data, &block) ⇒ Object
Tokenize the string Data by finding all instances of any key in the Tree. yields each instance as a StringTree::Item.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/stringtree/tree.rb', line 70 def match_all(data, &block) return nil if @root == nil i=0 while (i<data.length) node = @root.find_forward(data, i, data.length-i) if (node!=nil && node.value!=nil) yield Item.new(i, true, node) i += node.length else i += 1 end end end |
#match_count(data, list = {}) ⇒ Object
Return a Hash of terminating nodes to Integer counts for a given String data, i.e. Find the count of instances of each String in the tree in the given data.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/stringtree/tree.rb', line 86 def match_count(data, list = {}) return nil if @root == nil i=0 while (i<data.length) node = @root.find_forward(data, i, data.length-i) if (node!=nil && node.value!=nil) if (!list.has_key?(node)) list[node] = 1 else list[node] += 1 end i += node.length else i += 1 end end list end |
#optimize! ⇒ Object
Rebalance the tree for faster access.
62 63 64 65 66 |
# File 'lib/stringtree/tree.rb', line 62 def optimize! return nil if @root == nil @root.prune @root = @root.balance end |
#partials(key) ⇒ Object
Return an Array of Strings representing all partial matches forward of key in the Tree. Please note the key itself is not included, even if it exists as a value.
E.g.: A tree containing ‘ant’,‘antler’,‘deer’,‘anthropic’,‘beer’ tree.partials(‘ant’) would return [‘antler’,‘anthropic’]
54 55 56 57 58 59 |
# File 'lib/stringtree/tree.rb', line 54 def partials(key) return nil if @root == nil node = @root.find_vertical(key) return nil if node == nil node.all_partials(key) end |