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
-
#[](key) ⇒ Object
Alias for find.
-
#[]=(key, value) ⇒ Object
Alias for add.
-
#add(key, value) ⇒ Object
Add a key and value to this Tree.
-
#find(key) ⇒ Object
Find a specified key in the Tree, and return the value, or nil if not found.
-
#has_key?(key) ⇒ Boolean
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 @root = nil 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
#[](key) ⇒ Object
Alias for find
68 69 70 |
# File 'lib/stringtree/tree.rb', line 68 def [](key) find(key) end |
#[]=(key, value) ⇒ Object
Alias for add
73 74 75 |
# File 'lib/stringtree/tree.rb', line 73 def []=(key,value) add(key,value) end |
#add(key, value) ⇒ Object
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 |
#find(key) ⇒ Object
Find a specified key in the Tree, and return the value, or nil if not found.
19 20 21 22 23 |
# File 'lib/stringtree/tree.rb', line 19 def find(key) return nil if @root == nil node = @root.find_vertical(key) (node == nil ? nil : node.value) end |
#has_key?(key) ⇒ Boolean
Return true if the given key exists
26 27 28 29 30 31 |
# File 'lib/stringtree/tree.rb', line 26 def has_key?(key) return false if @root == nil node = @root.find_vertical(key) return false if node.nil? or node.value.nil? true 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.
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/stringtree/tree.rb', line 53 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.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/stringtree/tree.rb', line 79 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.
46 47 48 49 |
# File 'lib/stringtree/tree.rb', line 46 def optimize! return nil if @root == nil @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’]
38 39 40 41 42 43 |
# File 'lib/stringtree/tree.rb', line 38 def partials(key) return nil if @root == nil node = @root.find_vertical(key) return nil if node == nil node.all_partials(key) end |