stringtree 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +2 -2
- data/lib/stringtree/node.rb +11 -1
- data/lib/stringtree/tree.rb +32 -16
- data/lib/stringtree/version.rb +1 -1
- data/spec/tree_spec.rb +46 -0
- metadata +1 -3
data/README.md
CHANGED
@@ -16,7 +16,7 @@ This has become my 'hello world' over the years with any new language. I use it
|
|
16
16
|
|
17
17
|
## Installation
|
18
18
|
|
19
|
-
```
|
19
|
+
```bash
|
20
20
|
gem install stringtree
|
21
21
|
```
|
22
22
|
|
@@ -87,7 +87,7 @@ Type 'exit' to finish the demo.
|
|
87
87
|
|
88
88
|
## Code of Conduct
|
89
89
|
|
90
|
-
The StringTree project is committed to the [Contributor Covenant](http://contributor-covenant.org). Please read [CODE_OF_CONDUCT.md] before making any contributions or comments.
|
90
|
+
The StringTree project is committed to the [Contributor Covenant](http://contributor-covenant.org). Please read [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) before making any contributions or comments.
|
91
91
|
|
92
92
|
## References
|
93
93
|
|
data/lib/stringtree/node.rb
CHANGED
@@ -94,7 +94,7 @@ module StringTree
|
|
94
94
|
# and repeating with the next character and so on. Return the last node if found, or nil if any
|
95
95
|
# horizontal search fails.
|
96
96
|
# Optionally, set the offset into the string and its length
|
97
|
-
def find_vertical(str, offset = 0, length = str.length)
|
97
|
+
def find_vertical(str, offset = 0, length = str.length-offset)
|
98
98
|
node = nil
|
99
99
|
i = offset
|
100
100
|
while (i<offset+length)
|
@@ -212,5 +212,15 @@ module StringTree
|
|
212
212
|
@down.walk(str) { |str| list << str } unless @down.nil?
|
213
213
|
list
|
214
214
|
end
|
215
|
+
|
216
|
+
# Prune the tree back from this node onward so that all leaves have values.
|
217
|
+
def prune
|
218
|
+
return true if !@down.nil? && down.prune
|
219
|
+
return true if !@left.nil? && left.prune
|
220
|
+
return true if !@right.nil? && right.prune
|
221
|
+
return true unless @value.nil?
|
222
|
+
up.down = nil if !up.nil? && left.nil? && right.nil?
|
223
|
+
false
|
224
|
+
end
|
215
225
|
end
|
216
226
|
end
|
data/lib/stringtree/tree.rb
CHANGED
@@ -6,7 +6,7 @@ module StringTree
|
|
6
6
|
|
7
7
|
# Create a new empty Tree
|
8
8
|
def initialize
|
9
|
-
|
9
|
+
clear
|
10
10
|
end
|
11
11
|
|
12
12
|
# Add a key and value to this Tree
|
@@ -15,18 +15,34 @@ module StringTree
|
|
15
15
|
@root.add_vertical(key,value)
|
16
16
|
end
|
17
17
|
|
18
|
+
alias []= add
|
19
|
+
|
20
|
+
# Clear the Tree (Remove all keys/values)
|
21
|
+
def clear
|
22
|
+
@root = nil
|
23
|
+
end
|
24
|
+
|
18
25
|
# Find a specified key in the Tree, and return the value, or nil if not found.
|
19
26
|
def find(key)
|
20
|
-
|
21
|
-
node = @root.find_vertical(key)
|
27
|
+
node = find_node(key)
|
22
28
|
(node == nil ? nil : node.value)
|
23
29
|
end
|
24
30
|
|
31
|
+
alias [] find
|
32
|
+
|
25
33
|
# Return true if the given key exists
|
26
34
|
def has_key?(key)
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
!find_node(key).nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
alias include? has_key?
|
39
|
+
|
40
|
+
# Delete a key
|
41
|
+
def delete(key)
|
42
|
+
node = find_node(key)
|
43
|
+
return false if node.nil?
|
44
|
+
node.value = nil
|
45
|
+
node.prune
|
30
46
|
true
|
31
47
|
end
|
32
48
|
|
@@ -45,6 +61,7 @@ module StringTree
|
|
45
61
|
# Rebalance the tree for faster access.
|
46
62
|
def optimize!
|
47
63
|
return nil if @root == nil
|
64
|
+
@root.prune
|
48
65
|
@root = @root.balance
|
49
66
|
end
|
50
67
|
|
@@ -64,16 +81,6 @@ module StringTree
|
|
64
81
|
end
|
65
82
|
end
|
66
83
|
|
67
|
-
# Alias for find
|
68
|
-
def [](key)
|
69
|
-
find(key)
|
70
|
-
end
|
71
|
-
|
72
|
-
# Alias for add
|
73
|
-
def []=(key,value)
|
74
|
-
add(key,value)
|
75
|
-
end
|
76
|
-
|
77
84
|
# Return a Hash of terminating nodes to Integer counts for a given String data,
|
78
85
|
# i.e. Find the count of instances of each String in the tree in the given data.
|
79
86
|
def match_count(data, list = {})
|
@@ -94,5 +101,14 @@ module StringTree
|
|
94
101
|
end
|
95
102
|
list
|
96
103
|
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
# Find a node by its key
|
108
|
+
def find_node(key)
|
109
|
+
return nil if @root == nil
|
110
|
+
node = @root.find_vertical(key)
|
111
|
+
(node.nil? || node.value.nil? ? nil : node)
|
112
|
+
end
|
97
113
|
end
|
98
114
|
end
|
data/lib/stringtree/version.rb
CHANGED
data/spec/tree_spec.rb
CHANGED
@@ -262,5 +262,51 @@ describe StringTree::Tree do
|
|
262
262
|
end
|
263
263
|
end
|
264
264
|
end
|
265
|
+
|
266
|
+
describe '#delete' do
|
267
|
+
it 'should return false on empty tree' do
|
268
|
+
inst = StringTree::Tree.new
|
269
|
+
|
270
|
+
expect(inst.delete("test")).to be(false)
|
271
|
+
end
|
272
|
+
it 'should delete only the correct key and return true' do
|
273
|
+
inst = StringTree::Tree.new
|
274
|
+
|
275
|
+
inst["OneX"] = 5
|
276
|
+
inst["TwoX"] = 3
|
277
|
+
inst["TenX"] = 10
|
278
|
+
|
279
|
+
expect(inst.delete("TwoX")).to be(true)
|
280
|
+
|
281
|
+
expect(inst.has_key?("OneX")).to be(true)
|
282
|
+
expect(inst.has_key?("TenX")).to be(true)
|
283
|
+
expect(inst.has_key?("TwoX")).to be(false)
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'should return false if key not found' do
|
287
|
+
inst = StringTree::Tree.new
|
288
|
+
|
289
|
+
inst["OneX"] = 5
|
290
|
+
inst["TwoX"] = 3
|
291
|
+
inst["TenX"] = 10
|
292
|
+
|
293
|
+
expect(inst.delete("FiveX")).to be(false)
|
294
|
+
expect(inst.delete("OneX1")).to be(false)
|
295
|
+
expect(inst.delete("Two")).to be(false)
|
296
|
+
end
|
297
|
+
|
298
|
+
it 'should not corrupt tree' do
|
299
|
+
inst = StringTree::Tree.new
|
300
|
+
|
301
|
+
inst["super"] = 5
|
302
|
+
inst["supercala"] = 3
|
303
|
+
inst["supercalafragilistic"] = 10
|
304
|
+
|
305
|
+
expect(inst.delete("super")).to be(true)
|
306
|
+
expect(inst.has_key?("super")).to be(false)
|
307
|
+
expect(inst.has_key?("supercala")).to be(true)
|
308
|
+
expect(inst.has_key?("supercalafragilistic")).to be(true)
|
309
|
+
end
|
310
|
+
end
|
265
311
|
end
|
266
312
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stringtree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -176,7 +176,6 @@ files:
|
|
176
176
|
- examples/dictionary.txt
|
177
177
|
- examples/hamlet.txt
|
178
178
|
- examples/warandpeace.txt
|
179
|
-
- hamlet.tokens.txt
|
180
179
|
- lib/stringtree.rb
|
181
180
|
- lib/stringtree/item.rb
|
182
181
|
- lib/stringtree/node.rb
|
@@ -187,7 +186,6 @@ files:
|
|
187
186
|
- spec/spec_helper.rb
|
188
187
|
- spec/tree_spec.rb
|
189
188
|
- stringtree.gemspec
|
190
|
-
- warandpeace.tokens.txt
|
191
189
|
homepage: http://github.com/tomdionysus/stringtree-ruby
|
192
190
|
licenses: []
|
193
191
|
post_install_message:
|