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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6826addd0962f940a478110fdd7f434a47576822
4
- data.tar.gz: cbc905b0ad75449a9b8fa06794b76b2bfe9b2d6f
3
+ metadata.gz: be6709d49263fbdc6baf9dcf04766f2e44710216
4
+ data.tar.gz: 65705c4113f69078469d79a97967cb25ec513329
5
5
  SHA512:
6
- metadata.gz: 81d3a5f4b73e38debef8881db1a3095c60adbb7b9bedcb3070281a1616419fb13e68198b13e970a74bbe4998126228dc5cd40ba12321307cf7359114746b1ac7
7
- data.tar.gz: 05dba91f89e9d004ac456744e12a19263dd2951a56152e2d39556655723c36d165bd9ad5867c145f2cd0ab89bb417ccf1d2a97a011d3d4db045e346f7add3ee3
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
@@ -1,3 +1,3 @@
1
1
  class TrieMatcher
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -9,88 +9,103 @@ describe TrieMatcher do
9
9
  expect(TrieMatcher::VERSION).not_to be nil
10
10
  end
11
11
 
12
- it 'should store values' do
13
- @t["foo"] = "bar"
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
- it 'should retrieve stored values' do
17
- value = "bar"
18
- @t["foo"] = value
19
- expect(@t["foo"]).to be value
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
- it 'should return the stored value' do
23
- value = "bar"
24
- expect(@t["foo"] = value).to be value
25
- end
26
-
27
- it 'should store values with shared prefixes' do
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
- expect(@t.match("catcher")).to eq []
108
+ expect(@t["catch"]).to eq [1,2,3]
109
+ end
95
110
  end
96
111
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trie_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steven Karas