rambling-trie 1.0.2 → 2.2.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 +5 -5
- data/Gemfile +6 -3
- data/Guardfile +3 -1
- data/README.md +30 -12
- data/Rakefile +8 -0
- data/lib/rambling-trie.rb +2 -0
- data/lib/rambling/trie.rb +48 -26
- data/lib/rambling/trie/comparable.rb +6 -3
- data/lib/rambling/trie/compressible.rb +16 -0
- data/lib/rambling/trie/compressor.rb +39 -24
- data/lib/rambling/trie/configuration.rb +3 -1
- data/lib/rambling/trie/configuration/properties.rb +18 -9
- data/lib/rambling/trie/configuration/provider_collection.rb +38 -17
- data/lib/rambling/trie/container.rb +123 -36
- data/lib/rambling/trie/enumerable.rb +6 -4
- data/lib/rambling/trie/inspectable.rb +2 -0
- data/lib/rambling/trie/invalid_operation.rb +3 -1
- data/lib/rambling/trie/nodes.rb +13 -0
- data/lib/rambling/trie/nodes/compressed.rb +98 -0
- data/lib/rambling/trie/nodes/missing.rb +12 -0
- data/lib/rambling/trie/nodes/node.rb +183 -0
- data/lib/rambling/trie/nodes/raw.rb +82 -0
- data/lib/rambling/trie/readers.rb +3 -1
- data/lib/rambling/trie/readers/plain_text.rb +3 -11
- data/lib/rambling/trie/serializers.rb +3 -1
- data/lib/rambling/trie/serializers/file.rb +2 -0
- data/lib/rambling/trie/serializers/marshal.rb +15 -5
- data/lib/rambling/trie/serializers/yaml.rb +21 -5
- data/lib/rambling/trie/serializers/zip.rb +15 -8
- data/lib/rambling/trie/stringifyable.rb +8 -2
- data/lib/rambling/trie/version.rb +3 -1
- data/rambling-trie.gemspec +21 -10
- data/spec/assets/test_words.es_DO.txt +1 -0
- data/spec/integration/rambling/trie_spec.rb +44 -35
- data/spec/lib/rambling/trie/comparable_spec.rb +8 -15
- data/spec/lib/rambling/trie/compressor_spec.rb +90 -13
- data/spec/lib/rambling/trie/configuration/properties_spec.rb +21 -13
- data/spec/lib/rambling/trie/configuration/provider_collection_spec.rb +18 -34
- data/spec/lib/rambling/trie/container_spec.rb +183 -217
- data/spec/lib/rambling/trie/enumerable_spec.rb +14 -9
- data/spec/lib/rambling/trie/inspectable_spec.rb +36 -11
- data/spec/lib/rambling/trie/nodes/compressed_spec.rb +37 -0
- data/spec/lib/rambling/trie/nodes/node_spec.rb +9 -0
- data/spec/lib/rambling/trie/nodes/raw_spec.rb +179 -0
- data/spec/lib/rambling/trie/readers/plain_text_spec.rb +3 -1
- data/spec/lib/rambling/trie/serializers/file_spec.rb +6 -4
- data/spec/lib/rambling/trie/serializers/marshal_spec.rb +5 -7
- data/spec/lib/rambling/trie/serializers/yaml_spec.rb +5 -7
- data/spec/lib/rambling/trie/serializers/zip_spec.rb +18 -20
- data/spec/lib/rambling/trie/stringifyable_spec.rb +14 -11
- data/spec/lib/rambling/trie_spec.rb +18 -11
- data/spec/spec_helper.rb +10 -5
- data/spec/support/config.rb +10 -0
- data/spec/support/helpers/add_word.rb +20 -0
- data/spec/support/helpers/one_line_heredoc.rb +11 -0
- data/spec/support/shared_examples/a_compressible_trie.rb +40 -0
- data/spec/support/shared_examples/a_serializable_trie.rb +10 -6
- data/spec/support/shared_examples/a_serializer.rb +9 -1
- data/spec/support/shared_examples/a_trie_data_structure.rb +2 -0
- data/spec/support/shared_examples/a_trie_node.rb +127 -0
- data/spec/{lib/rambling/trie/compressed_node_spec.rb → support/shared_examples/a_trie_node_implementation.rb} +25 -72
- metadata +42 -31
- data/lib/rambling/trie/compressable.rb +0 -14
- data/lib/rambling/trie/compressed_node.rb +0 -120
- data/lib/rambling/trie/missing_node.rb +0 -8
- data/lib/rambling/trie/node.rb +0 -97
- data/lib/rambling/trie/raw_node.rb +0 -96
- data/spec/lib/rambling/trie/node_spec.rb +0 -86
- data/spec/lib/rambling/trie/raw_node_spec.rb +0 -389
- data/spec/support/shared_examples/a_compressable_trie.rb +0 -26
@@ -1,43 +1,9 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
describe '#compressed?' do
|
7
|
-
it 'returns true' do
|
8
|
-
expect(node).to be_compressed
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe '.new' do
|
13
|
-
context 'with no parent' do
|
14
|
-
let(:node) { Rambling::Trie::CompressedNode.new }
|
15
|
-
|
16
|
-
it 'is marked as root' do
|
17
|
-
expect(node).to be_root
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'with a specified' do
|
22
|
-
let(:node) { Rambling::Trie::CompressedNode.new double(:root) }
|
23
|
-
|
24
|
-
it 'is not marked as root' do
|
25
|
-
expect(node).not_to be_root
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
describe '#add' do
|
31
|
-
it 'raises an error' do
|
32
|
-
expect { node.add 'restaurant' }.to raise_error Rambling::Trie::InvalidOperation
|
33
|
-
end
|
34
|
-
end
|
3
|
+
shared_examples_for 'a trie node implementation' do
|
4
|
+
it_behaves_like 'a trie node'
|
35
5
|
|
36
6
|
describe '#partial_word?' do
|
37
|
-
let(:raw_node) { Rambling::Trie::RawNode.new }
|
38
|
-
let(:compressor) { Rambling::Trie::Compressor.new }
|
39
|
-
let(:node) { compressor.compress raw_node }
|
40
|
-
|
41
7
|
context 'when the chars array is empty' do
|
42
8
|
it 'returns true' do
|
43
9
|
expect(node.partial_word? []).to be true
|
@@ -47,7 +13,7 @@ describe Rambling::Trie::CompressedNode do
|
|
47
13
|
context 'when the chars array is not empty' do
|
48
14
|
context 'when the node has a tree that matches the characters' do
|
49
15
|
before do
|
50
|
-
|
16
|
+
add_word_to_tree 'abc'
|
51
17
|
end
|
52
18
|
|
53
19
|
it 'returns true' do
|
@@ -59,7 +25,7 @@ describe Rambling::Trie::CompressedNode do
|
|
59
25
|
|
60
26
|
context 'when the node has a tree that does not match the characters' do
|
61
27
|
before do
|
62
|
-
|
28
|
+
add_word_to_tree 'cba'
|
63
29
|
end
|
64
30
|
|
65
31
|
it 'returns false' do
|
@@ -72,10 +38,6 @@ describe Rambling::Trie::CompressedNode do
|
|
72
38
|
end
|
73
39
|
|
74
40
|
describe '#word?' do
|
75
|
-
let(:raw_node) { Rambling::Trie::RawNode.new }
|
76
|
-
let(:compressor) { Rambling::Trie::Compressor.new }
|
77
|
-
let(:node) { compressor.compress raw_node }
|
78
|
-
|
79
41
|
context 'when the chars array is empty' do
|
80
42
|
context 'when the node is terminal' do
|
81
43
|
before do
|
@@ -97,32 +59,28 @@ describe Rambling::Trie::CompressedNode do
|
|
97
59
|
context 'when the chars array is not empty' do
|
98
60
|
context 'when the node has a tree that matches all the characters' do
|
99
61
|
before do
|
100
|
-
|
62
|
+
add_word_to_tree 'abc'
|
101
63
|
end
|
102
64
|
|
103
65
|
it 'returns true' do
|
104
|
-
expect(node.word? %w(a b c)).to be true
|
66
|
+
expect(node.word? %w(a b c).map(&:dup)).to be true
|
105
67
|
end
|
106
68
|
end
|
107
69
|
|
108
|
-
context 'when the node
|
70
|
+
context 'when the node subtree does not match all the characters' do
|
109
71
|
before do
|
110
|
-
|
72
|
+
add_word_to_tree 'abc'
|
111
73
|
end
|
112
74
|
|
113
75
|
it 'returns false' do
|
114
|
-
expect(node.word? %w(a)).to be false
|
115
|
-
expect(node.word? %w(a b)).to be false
|
76
|
+
expect(node.word? %w(a).map(&:dup)).to be false
|
77
|
+
expect(node.word? %w(a b).map(&:dup)).to be false
|
116
78
|
end
|
117
79
|
end
|
118
80
|
end
|
119
81
|
end
|
120
82
|
|
121
83
|
describe '#scan' do
|
122
|
-
let(:raw_node) { Rambling::Trie::RawNode.new }
|
123
|
-
let(:compressor) { Rambling::Trie::Compressor.new }
|
124
|
-
let(:node) { compressor.compress raw_node }
|
125
|
-
|
126
84
|
context 'when the chars array is empty' do
|
127
85
|
it 'returns itself' do
|
128
86
|
expect(node.scan []).to eq node
|
@@ -131,44 +89,39 @@ describe Rambling::Trie::CompressedNode do
|
|
131
89
|
|
132
90
|
context 'when the chars array is not empty' do
|
133
91
|
before do
|
134
|
-
|
92
|
+
add_words_to_tree %w(cba ccab)
|
135
93
|
end
|
136
94
|
|
137
95
|
context 'when the chars are found' do
|
138
96
|
it 'returns the found child' do
|
139
|
-
expect(node.scan %w(c)).to
|
140
|
-
expect(node.scan %w(c b)).to
|
141
|
-
expect(node.scan %w(c b a)).to
|
97
|
+
expect(node.scan %w(c)).to match_array %w(cba ccab)
|
98
|
+
expect(node.scan %w(c b)).to match_array %w(cba)
|
99
|
+
expect(node.scan %w(c b a)).to match_array %w(cba)
|
142
100
|
end
|
143
101
|
end
|
144
102
|
|
145
103
|
context 'when the chars are not found' do
|
146
|
-
it 'returns a
|
147
|
-
expect(node.scan %w(a)).to be_a Rambling::Trie::
|
148
|
-
expect(node.scan %w(a b)).to be_a Rambling::Trie::
|
149
|
-
expect(node.scan %w(a b c)).to be_a Rambling::Trie::
|
150
|
-
expect(node.scan %w(c
|
104
|
+
it 'returns a Nodes::Missing' do
|
105
|
+
expect(node.scan %w(a)).to be_a Rambling::Trie::Nodes::Missing
|
106
|
+
expect(node.scan %w(a b)).to be_a Rambling::Trie::Nodes::Missing
|
107
|
+
expect(node.scan %w(a b c)).to be_a Rambling::Trie::Nodes::Missing
|
108
|
+
expect(node.scan %w(c a)).to be_a Rambling::Trie::Nodes::Missing
|
109
|
+
expect(node.scan %w(c c b)).to be_a Rambling::Trie::Nodes::Missing
|
110
|
+
expect(node.scan %w(c b a d)).to be_a Rambling::Trie::Nodes::Missing
|
151
111
|
end
|
152
112
|
end
|
153
113
|
end
|
154
114
|
end
|
155
115
|
|
156
116
|
describe '#match_prefix' do
|
157
|
-
let(:raw_node) { Rambling::Trie::RawNode.new }
|
158
|
-
let(:compressor) { Rambling::Trie::Compressor.new }
|
159
|
-
let(:node) { compressor.compress raw_node }
|
160
|
-
|
161
117
|
before do
|
162
|
-
|
163
|
-
|
164
|
-
raw_node.add 'mport'
|
165
|
-
raw_node.add 'mportant'
|
166
|
-
raw_node.add 'mportantly'
|
118
|
+
assign_letter :i
|
119
|
+
add_words_to_tree %w(gnite mport mportant mportantly)
|
167
120
|
end
|
168
121
|
|
169
122
|
context 'when the node is terminal' do
|
170
123
|
before do
|
171
|
-
|
124
|
+
node.terminal!
|
172
125
|
end
|
173
126
|
|
174
127
|
it 'adds itself to the words' do
|
metadata
CHANGED
@@ -1,60 +1,60 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rambling-trie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edgar Gonzalez
|
8
8
|
- Lilibeth De La Cruz
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2021-01-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: rake
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
18
|
- - "~>"
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: '
|
20
|
+
version: '13.0'
|
21
21
|
type: :development
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - "~>"
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
27
|
+
version: '13.0'
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
|
-
name:
|
29
|
+
name: rspec
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
31
31
|
requirements:
|
32
32
|
- - "~>"
|
33
33
|
- !ruby/object:Gem::Version
|
34
|
-
version: '
|
34
|
+
version: '3.9'
|
35
35
|
type: :development
|
36
36
|
prerelease: false
|
37
37
|
version_requirements: !ruby/object:Gem::Requirement
|
38
38
|
requirements:
|
39
39
|
- - "~>"
|
40
40
|
- !ruby/object:Gem::Version
|
41
|
-
version: '
|
41
|
+
version: '3.9'
|
42
42
|
- !ruby/object:Gem::Dependency
|
43
43
|
name: yard
|
44
44
|
requirement: !ruby/object:Gem::Requirement
|
45
45
|
requirements:
|
46
46
|
- - "~>"
|
47
47
|
- !ruby/object:Gem::Version
|
48
|
-
version: 0.9.
|
48
|
+
version: 0.9.25
|
49
49
|
type: :development
|
50
50
|
prerelease: false
|
51
51
|
version_requirements: !ruby/object:Gem::Requirement
|
52
52
|
requirements:
|
53
53
|
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
|
-
version: 0.9.
|
56
|
-
description: The Rambling Trie is a Ruby implementation of the trie data structure,
|
57
|
-
which includes compression abilities and is designed to be very fast to traverse.
|
55
|
+
version: 0.9.25
|
56
|
+
description: 'The Rambling Trie is a Ruby implementation of the trie data structure,
|
57
|
+
which includes compression abilities and is designed to be very fast to traverse. '
|
58
58
|
email:
|
59
59
|
- edggonzalezg@gmail.com
|
60
60
|
- lilibethdlc@gmail.com
|
@@ -70,8 +70,7 @@ files:
|
|
70
70
|
- lib/rambling-trie.rb
|
71
71
|
- lib/rambling/trie.rb
|
72
72
|
- lib/rambling/trie/comparable.rb
|
73
|
-
- lib/rambling/trie/
|
74
|
-
- lib/rambling/trie/compressed_node.rb
|
73
|
+
- lib/rambling/trie/compressible.rb
|
75
74
|
- lib/rambling/trie/compressor.rb
|
76
75
|
- lib/rambling/trie/configuration.rb
|
77
76
|
- lib/rambling/trie/configuration/properties.rb
|
@@ -80,9 +79,11 @@ files:
|
|
80
79
|
- lib/rambling/trie/enumerable.rb
|
81
80
|
- lib/rambling/trie/inspectable.rb
|
82
81
|
- lib/rambling/trie/invalid_operation.rb
|
83
|
-
- lib/rambling/trie/
|
84
|
-
- lib/rambling/trie/
|
85
|
-
- lib/rambling/trie/
|
82
|
+
- lib/rambling/trie/nodes.rb
|
83
|
+
- lib/rambling/trie/nodes/compressed.rb
|
84
|
+
- lib/rambling/trie/nodes/missing.rb
|
85
|
+
- lib/rambling/trie/nodes/node.rb
|
86
|
+
- lib/rambling/trie/nodes/raw.rb
|
86
87
|
- lib/rambling/trie/readers.rb
|
87
88
|
- lib/rambling/trie/readers/plain_text.rb
|
88
89
|
- lib/rambling/trie/serializers.rb
|
@@ -97,15 +98,15 @@ files:
|
|
97
98
|
- spec/assets/test_words.es_DO.txt
|
98
99
|
- spec/integration/rambling/trie_spec.rb
|
99
100
|
- spec/lib/rambling/trie/comparable_spec.rb
|
100
|
-
- spec/lib/rambling/trie/compressed_node_spec.rb
|
101
101
|
- spec/lib/rambling/trie/compressor_spec.rb
|
102
102
|
- spec/lib/rambling/trie/configuration/properties_spec.rb
|
103
103
|
- spec/lib/rambling/trie/configuration/provider_collection_spec.rb
|
104
104
|
- spec/lib/rambling/trie/container_spec.rb
|
105
105
|
- spec/lib/rambling/trie/enumerable_spec.rb
|
106
106
|
- spec/lib/rambling/trie/inspectable_spec.rb
|
107
|
-
- spec/lib/rambling/trie/
|
108
|
-
- spec/lib/rambling/trie/
|
107
|
+
- spec/lib/rambling/trie/nodes/compressed_spec.rb
|
108
|
+
- spec/lib/rambling/trie/nodes/node_spec.rb
|
109
|
+
- spec/lib/rambling/trie/nodes/raw_spec.rb
|
109
110
|
- spec/lib/rambling/trie/readers/plain_text_spec.rb
|
110
111
|
- spec/lib/rambling/trie/serializers/file_spec.rb
|
111
112
|
- spec/lib/rambling/trie/serializers/marshal_spec.rb
|
@@ -115,16 +116,20 @@ files:
|
|
115
116
|
- spec/lib/rambling/trie_spec.rb
|
116
117
|
- spec/spec_helper.rb
|
117
118
|
- spec/support/config.rb
|
118
|
-
- spec/support/
|
119
|
+
- spec/support/helpers/add_word.rb
|
120
|
+
- spec/support/helpers/one_line_heredoc.rb
|
121
|
+
- spec/support/shared_examples/a_compressible_trie.rb
|
119
122
|
- spec/support/shared_examples/a_serializable_trie.rb
|
120
123
|
- spec/support/shared_examples/a_serializer.rb
|
121
124
|
- spec/support/shared_examples/a_trie_data_structure.rb
|
125
|
+
- spec/support/shared_examples/a_trie_node.rb
|
126
|
+
- spec/support/shared_examples/a_trie_node_implementation.rb
|
122
127
|
- spec/tmp/.gitkeep
|
123
128
|
homepage: http://github.com/gonzedge/rambling-trie
|
124
129
|
licenses:
|
125
130
|
- MIT
|
126
131
|
metadata: {}
|
127
|
-
post_install_message:
|
132
|
+
post_install_message:
|
128
133
|
rdoc_options: []
|
129
134
|
require_paths:
|
130
135
|
- lib
|
@@ -132,16 +137,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
132
137
|
requirements:
|
133
138
|
- - ">="
|
134
139
|
- !ruby/object:Gem::Version
|
135
|
-
version: '
|
140
|
+
version: '2.5'
|
141
|
+
- - "<="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: 3.0.0
|
136
144
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
145
|
requirements:
|
138
146
|
- - ">="
|
139
147
|
- !ruby/object:Gem::Version
|
140
148
|
version: '0'
|
141
149
|
requirements: []
|
142
|
-
|
143
|
-
|
144
|
-
signing_key:
|
150
|
+
rubygems_version: 3.2.3
|
151
|
+
signing_key:
|
145
152
|
specification_version: 4
|
146
153
|
summary: A Ruby implementation of the trie data structure.
|
147
154
|
test_files:
|
@@ -149,15 +156,15 @@ test_files:
|
|
149
156
|
- spec/assets/test_words.es_DO.txt
|
150
157
|
- spec/integration/rambling/trie_spec.rb
|
151
158
|
- spec/lib/rambling/trie/comparable_spec.rb
|
152
|
-
- spec/lib/rambling/trie/compressed_node_spec.rb
|
153
159
|
- spec/lib/rambling/trie/compressor_spec.rb
|
154
160
|
- spec/lib/rambling/trie/configuration/properties_spec.rb
|
155
161
|
- spec/lib/rambling/trie/configuration/provider_collection_spec.rb
|
156
162
|
- spec/lib/rambling/trie/container_spec.rb
|
157
163
|
- spec/lib/rambling/trie/enumerable_spec.rb
|
158
164
|
- spec/lib/rambling/trie/inspectable_spec.rb
|
159
|
-
- spec/lib/rambling/trie/
|
160
|
-
- spec/lib/rambling/trie/
|
165
|
+
- spec/lib/rambling/trie/nodes/compressed_spec.rb
|
166
|
+
- spec/lib/rambling/trie/nodes/node_spec.rb
|
167
|
+
- spec/lib/rambling/trie/nodes/raw_spec.rb
|
161
168
|
- spec/lib/rambling/trie/readers/plain_text_spec.rb
|
162
169
|
- spec/lib/rambling/trie/serializers/file_spec.rb
|
163
170
|
- spec/lib/rambling/trie/serializers/marshal_spec.rb
|
@@ -167,8 +174,12 @@ test_files:
|
|
167
174
|
- spec/lib/rambling/trie_spec.rb
|
168
175
|
- spec/spec_helper.rb
|
169
176
|
- spec/support/config.rb
|
170
|
-
- spec/support/
|
177
|
+
- spec/support/helpers/add_word.rb
|
178
|
+
- spec/support/helpers/one_line_heredoc.rb
|
179
|
+
- spec/support/shared_examples/a_compressible_trie.rb
|
171
180
|
- spec/support/shared_examples/a_serializable_trie.rb
|
172
181
|
- spec/support/shared_examples/a_serializer.rb
|
173
182
|
- spec/support/shared_examples/a_trie_data_structure.rb
|
183
|
+
- spec/support/shared_examples/a_trie_node.rb
|
184
|
+
- spec/support/shared_examples/a_trie_node_implementation.rb
|
174
185
|
- spec/tmp/.gitkeep
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module Rambling
|
2
|
-
module Trie
|
3
|
-
# Provides the compressable behavior for the trie data structure.
|
4
|
-
module Compressable
|
5
|
-
# Indicates if the current {Rambling::Trie::Node Node} can be compressed
|
6
|
-
# or not.
|
7
|
-
# @return [Boolean] `true` for non-{Node#terminal? terminal} nodes with
|
8
|
-
# one child, `false` otherwise.
|
9
|
-
def compressable?
|
10
|
-
!(root? || terminal?) && children_tree.size == 1
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,120 +0,0 @@
|
|
1
|
-
module Rambling
|
2
|
-
module Trie
|
3
|
-
# A representation of a node in an compressed trie data structure.
|
4
|
-
class CompressedNode < Rambling::Trie::Node
|
5
|
-
# Always raises {Rambling::Trie::InvalidOperation InvalidOperation} when
|
6
|
-
# trying to add a word to the current compressed trie node
|
7
|
-
# @param [String] word the word to add to the trie.
|
8
|
-
# @raise [InvalidOperation] if the trie is already compressed.
|
9
|
-
# @return [nil] this never returns as it always raises an exception.
|
10
|
-
def add word
|
11
|
-
raise Rambling::Trie::InvalidOperation, 'Cannot add word to compressed trie'
|
12
|
-
end
|
13
|
-
|
14
|
-
# Checks if a path for set a of characters exists in the trie.
|
15
|
-
# @param [Array<String>] chars the characters to look for in the trie.
|
16
|
-
# @return [Boolean] `true` if the characters are found, `false` otherwise.
|
17
|
-
def partial_word? chars
|
18
|
-
chars.empty? || has_partial_word?(chars)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Checks if a path for set of characters represents a word in the trie.
|
22
|
-
# @param [Array<String>] chars the characters to look for in the trie.
|
23
|
-
# @return [Boolean] `true` if the characters are found and form a word,
|
24
|
-
# `false` otherwise.
|
25
|
-
def word? chars
|
26
|
-
chars.empty? ? terminal? : has_word?(chars)
|
27
|
-
end
|
28
|
-
|
29
|
-
# Returns the node that starts with the specified characters.
|
30
|
-
# @param [Array<String>] chars the characters to look for in the trie.
|
31
|
-
# @return [Node] the node that matches the specified characters.
|
32
|
-
# {MissingNode MissingNode} when not found.
|
33
|
-
def scan chars
|
34
|
-
chars.empty? ? self : closest_node(chars)
|
35
|
-
end
|
36
|
-
|
37
|
-
# Always return `true` for a compressed node.
|
38
|
-
# @return [Boolean] always `true` for a compressed node.
|
39
|
-
def compressed?
|
40
|
-
true
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def has_partial_word? chars
|
46
|
-
recursive_get(:partial_word?, chars) || false
|
47
|
-
end
|
48
|
-
|
49
|
-
def has_word? chars
|
50
|
-
current_key = nil
|
51
|
-
|
52
|
-
while !chars.empty?
|
53
|
-
if current_key
|
54
|
-
current_key << chars.slice!(0)
|
55
|
-
else
|
56
|
-
current_key = chars.slice!(0)
|
57
|
-
end
|
58
|
-
|
59
|
-
child = children_tree[current_key.to_sym]
|
60
|
-
return child.word? chars if child
|
61
|
-
end
|
62
|
-
|
63
|
-
false
|
64
|
-
end
|
65
|
-
|
66
|
-
def closest_node chars
|
67
|
-
recursive_get(:scan, chars) || Rambling::Trie::MissingNode.new
|
68
|
-
end
|
69
|
-
|
70
|
-
def children_match_prefix chars
|
71
|
-
return enum_for :children_match_prefix, chars unless block_given?
|
72
|
-
|
73
|
-
current_key = nil
|
74
|
-
|
75
|
-
while !chars.empty?
|
76
|
-
if current_key
|
77
|
-
current_key << chars.slice!(0)
|
78
|
-
else
|
79
|
-
current_key = chars.slice!(0)
|
80
|
-
end
|
81
|
-
|
82
|
-
child = children_tree[current_key.to_sym]
|
83
|
-
|
84
|
-
if child
|
85
|
-
child.match_prefix chars do |word|
|
86
|
-
yield word
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
def recursive_get method, chars
|
93
|
-
current_length = 0
|
94
|
-
current_key = current_key chars.slice!(0)
|
95
|
-
|
96
|
-
begin
|
97
|
-
current_length += 1
|
98
|
-
|
99
|
-
if current_key && (current_key.length == current_length || chars.empty?)
|
100
|
-
return children_tree[current_key.to_sym].send method, chars
|
101
|
-
end
|
102
|
-
end while current_key && current_key[current_length] == chars.slice!(0)
|
103
|
-
end
|
104
|
-
|
105
|
-
def current_key letter
|
106
|
-
current_key = nil
|
107
|
-
|
108
|
-
children_tree.keys.each do |key|
|
109
|
-
key_string = key.to_s
|
110
|
-
if key_string.start_with? letter
|
111
|
-
current_key = key_string
|
112
|
-
break
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
current_key
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|