trie_matcher 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/trie_matcher.rb +17 -0
- data/lib/trie_matcher/version.rb +1 -1
- data/spec/trie_matcher_spec.rb +93 -78
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be6709d49263fbdc6baf9dcf04766f2e44710216
|
4
|
+
data.tar.gz: 65705c4113f69078469d79a97967cb25ec513329
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61a82d7c000c5c076f240c8ea4221554006de143917a9e1e5210ab820c628ff640d88d3f70ce3a3df35497c03afd25915357fe3304707e67d145aad1abbfd1a5
|
7
|
+
data.tar.gz: f26bbae5d67f77d84665253312b9f1c342bc4523f8d1bbf4cd30c81f94c1e952504f3ec41faee6a67c5a1a98c039bbf6183c68ff70409234e09db24aa2f54985
|
data/lib/trie_matcher.rb
CHANGED
@@ -44,6 +44,23 @@ class TrieMatcher
|
|
44
44
|
return previous[:value]
|
45
45
|
end
|
46
46
|
|
47
|
+
# Set a value in the trie if it isn't null. Can be used to initialize collections as values
|
48
|
+
#
|
49
|
+
# @param word [String] The word to set the value for
|
50
|
+
# @param value [Object] the value to set, if the value for the word is nil
|
51
|
+
# @return [Object] the value associated with the word
|
52
|
+
def set_if_nil(word, value)
|
53
|
+
current = @root
|
54
|
+
current_prefix = word
|
55
|
+
|
56
|
+
while current_prefix != ""
|
57
|
+
current, current_prefix = find_canididate_insertion_node(current, current_prefix)
|
58
|
+
end
|
59
|
+
|
60
|
+
current[:value] ||= value
|
61
|
+
return current[:value]
|
62
|
+
end
|
63
|
+
|
47
64
|
# Perform a prefix search, and return all values in the trie that have this prefix
|
48
65
|
#
|
49
66
|
# @param prefix [String] the prefix to search the trie with
|
data/lib/trie_matcher/version.rb
CHANGED
data/spec/trie_matcher_spec.rb
CHANGED
@@ -9,88 +9,103 @@ describe TrieMatcher do
|
|
9
9
|
expect(TrieMatcher::VERSION).not_to be nil
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
describe "Hash access" do
|
13
|
+
it 'should store values' do
|
14
|
+
@t["foo"] = "bar"
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should retrieve stored values' do
|
18
|
+
value = "bar"
|
19
|
+
@t["foo"] = value
|
20
|
+
expect(@t["foo"]).to be value
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should return the stored value' do
|
24
|
+
value = "bar"
|
25
|
+
expect(@t["foo"] = value).to be value
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should store values with shared prefixes' do
|
29
|
+
@t["cat"] = 1
|
30
|
+
@t["car"] = 2
|
31
|
+
expect(@t["cat"]).to be 1
|
32
|
+
expect(@t["car"]).to be 2
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'should store keys that are a prefix of other keys' do
|
36
|
+
@t["catch"] = 1
|
37
|
+
@t["cat"] = 2
|
38
|
+
expect(@t["catch"]).to be 1
|
39
|
+
expect(@t["cat"]).to be 2
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should store keys that have a prefix of another key' do
|
43
|
+
@t["cat"] = 1
|
44
|
+
@t["catch"] = 2
|
45
|
+
expect(@t["cat"]).to be 1
|
46
|
+
expect(@t["catch"]).to be 2
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should do prefix searching' do
|
50
|
+
@t["cat"] = 1
|
51
|
+
expect(@t["cats"]).to be 1
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should do partial prefix matching' do
|
55
|
+
@t["cat"] = 1
|
56
|
+
@t["cats in the cradle"] = 2
|
57
|
+
expect(@t["cats"]).to be 1
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should return the more specific prefix value' do
|
61
|
+
@t["cat"] = 1
|
62
|
+
@t["catch"] = 2
|
63
|
+
expect(@t["catcher"]).to be 2
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'should not give a longer prefix value' do
|
67
|
+
@t["catch"] = 2
|
68
|
+
expect(@t["cat"]).to be nil
|
69
|
+
end
|
14
70
|
end
|
15
71
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
72
|
+
describe "prefix matches" do
|
73
|
+
it 'should return all values that are a prefix' do
|
74
|
+
@t["cats"] = 1
|
75
|
+
@t["foobar"] = 2
|
76
|
+
@t["foobaz"] = 3
|
77
|
+
@t["foobars"] = 4
|
78
|
+
@t["food"] = 5
|
79
|
+
|
80
|
+
expect(@t.match("foo").sort).to eq [2,3,4,5]
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return a single value when there is only one match' do
|
84
|
+
@t["cats"] = 1
|
85
|
+
@t["dogs"] = 2
|
86
|
+
@t["birds"] = 3
|
87
|
+
@t["bees"] = 4
|
88
|
+
|
89
|
+
expect(@t.match("dog")).to eq [2]
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should return an empty result when there are no prefix matches' do
|
93
|
+
@t["cat"] = 1
|
94
|
+
@t["dog"] = 2
|
95
|
+
@t["catch"] = 3
|
96
|
+
|
97
|
+
expect(@t.match("catcher")).to eq []
|
98
|
+
end
|
20
99
|
end
|
21
100
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@t["cat"] = 1
|
29
|
-
@t["car"] = 2
|
30
|
-
expect(@t["cat"]).to be 1
|
31
|
-
expect(@t["car"]).to be 2
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'should store keys that are a prefix of other keys' do
|
35
|
-
@t["catch"] = 1
|
36
|
-
@t["cat"] = 2
|
37
|
-
expect(@t["catch"]).to be 1
|
38
|
-
expect(@t["cat"]).to be 2
|
39
|
-
end
|
40
|
-
|
41
|
-
it 'should store keys that have a prefix of another key' do
|
42
|
-
@t["cat"] = 1
|
43
|
-
@t["catch"] = 2
|
44
|
-
expect(@t["cat"]).to be 1
|
45
|
-
expect(@t["catch"]).to be 2
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'should do prefix searching' do
|
49
|
-
@t["cat"] = 1
|
50
|
-
expect(@t["cats"]).to be 1
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'should do partial prefix matching' do
|
54
|
-
@t["cat"] = 1
|
55
|
-
@t["cats in the cradle"] = 2
|
56
|
-
expect(@t["cats"]).to be 1
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'should return the more specific prefix value' do
|
60
|
-
@t["cat"] = 1
|
61
|
-
@t["catch"] = 2
|
62
|
-
expect(@t["catcher"]).to be 2
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should not give a longer prefix value' do
|
66
|
-
@t["catch"] = 2
|
67
|
-
expect(@t["cat"]).to be nil
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'should return all values that are a prefix' do
|
71
|
-
@t["cats"] = 1
|
72
|
-
@t["foobar"] = 2
|
73
|
-
@t["foobaz"] = 3
|
74
|
-
@t["foobars"] = 4
|
75
|
-
@t["food"] = 5
|
76
|
-
|
77
|
-
expect(@t.match("foo").sort).to eq [2,3,4,5]
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'should return a single value when there is only one match' do
|
81
|
-
@t["cats"] = 1
|
82
|
-
@t["dogs"] = 2
|
83
|
-
@t["birds"] = 3
|
84
|
-
@t["bees"] = 4
|
85
|
-
|
86
|
-
expect(@t.match("dog")).to eq [2]
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'should return an empty result when there are no prefix matches' do
|
90
|
-
@t["cat"] = 1
|
91
|
-
@t["dog"] = 2
|
92
|
-
@t["catch"] = 3
|
101
|
+
describe "set_if_nil" do
|
102
|
+
it "should store a collection" do
|
103
|
+
@t["cat"] = 1
|
104
|
+
@t.set_if_nil("catch", []) << 1
|
105
|
+
@t.set_if_nil("catch", []) << 2
|
106
|
+
@t.set_if_nil("catch", []) << 3
|
93
107
|
|
94
|
-
|
108
|
+
expect(@t["catch"]).to eq [1,2,3]
|
109
|
+
end
|
95
110
|
end
|
96
111
|
end
|