rambling-trie 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -1
- data/.travis.yml +4 -0
- data/Gemfile +4 -1
- data/README.markdown +4 -2
- data/Rakefile +1 -2
- data/lib/rambling-trie.rb +1 -27
- data/lib/rambling/trie.rb +33 -0
- data/lib/{rambling-trie → rambling/trie}/branches.rb +10 -2
- data/lib/{rambling-trie → rambling/trie}/children_hash_deferer.rb +0 -0
- data/lib/{rambling-trie → rambling/trie}/compressor.rb +2 -0
- data/lib/rambling/trie/enumerable.rb +20 -0
- data/lib/{rambling-trie → rambling/trie}/invalid_operation.rb +0 -0
- data/lib/{rambling-trie → rambling/trie}/node.rb +0 -0
- data/lib/{rambling-trie → rambling/trie}/root.rb +10 -0
- data/lib/{rambling-trie → rambling/trie}/tasks/gem.rb +0 -0
- data/lib/{rambling-trie → rambling/trie}/tasks/performance.rb +0 -0
- data/lib/{rambling-trie → rambling/trie}/version.rb +1 -1
- data/rambling-trie.gemspec +1 -1
- data/spec/lib/{rambling-trie → rambling/trie}/branches_spec.rb +0 -0
- data/spec/lib/{rambling-trie → rambling/trie}/children_hash_deferer_spec.rb +0 -0
- data/spec/lib/rambling/trie/enumerable_spec.rb +37 -0
- data/spec/lib/{rambling-trie → rambling/trie}/node_spec.rb +0 -0
- data/spec/lib/{rambling-trie → rambling/trie}/root_spec.rb +17 -7
- data/spec/lib/{rambling-trie_spec.rb → rambling/trie_spec.rb} +0 -0
- metadata +22 -18
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Rambling Trie
|
1
|
+
# Rambling Trie [![Build Status](https://secure.travis-ci.org/egonzalez0787/rambling-trie.png)](http://travis-ci.org/egonzalez0787/rambling-trie) [![Dependency Status](https://gemnasium.com/egonzalez0787/rambling-trie.png)](https://gemnasium.com/egonzalez0787/rambling-trie) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/egonzalez0787/rambling-trie)
|
2
2
|
|
3
3
|
The Rambling Trie is a custom implementation of the Trie data structure with Ruby, which includes compression abilities and is designed to be very fast to traverse.
|
4
4
|
|
@@ -28,9 +28,11 @@ gem 'rambling-trie'
|
|
28
28
|
|
29
29
|
## How to use the Rambling Trie
|
30
30
|
|
31
|
+
- - -
|
31
32
|
### Deprecation warning
|
32
33
|
|
33
34
|
Starting from version 0.4.0, `Rambling::Trie.new` is deprecated. Please use `Rambling::Trie.create` instead.
|
35
|
+
- - -
|
34
36
|
|
35
37
|
To create the trie, initialize it like this:
|
36
38
|
|
@@ -89,7 +91,7 @@ trie.compressed?
|
|
89
91
|
|
90
92
|
## Further Documentation
|
91
93
|
|
92
|
-
You can find further API documentation on the autogenerated [RubyDoc.info](http://rubydoc.info/gems/rambling-trie)
|
94
|
+
You can find further API documentation on the autogenerated [rambling-trie gem RubyDoc.info page](http://rubydoc.info/gems/rambling-trie) or if you want edge documentation, you can go the [GitHub project RubyDoc.info page](http://rubydoc.info/github/egonzalez0787/rambling-trie).
|
93
95
|
|
94
96
|
## Compatible Ruby and Rails versions
|
95
97
|
|
data/Rakefile
CHANGED
data/lib/rambling-trie.rb
CHANGED
@@ -1,27 +1 @@
|
|
1
|
-
|
2
|
-
'invalid_operation',
|
3
|
-
'children_hash_deferer',
|
4
|
-
'compressor',
|
5
|
-
'branches',
|
6
|
-
'node',
|
7
|
-
'root',
|
8
|
-
'version'
|
9
|
-
].map { |file| File.join('rambling-trie', file) }.each &method(:require)
|
10
|
-
|
11
|
-
module Rambling
|
12
|
-
module Trie
|
13
|
-
class << self
|
14
|
-
# Creates a new Trie. Entry point for the Rambling::Trie API.
|
15
|
-
# @param [String, nil] filename the file to load the words from (defaults to nil).
|
16
|
-
def create(*params)
|
17
|
-
Root.new *params
|
18
|
-
end
|
19
|
-
|
20
|
-
# @deprecated Please use {#create} instead
|
21
|
-
def new(*params)
|
22
|
-
warn '[DEPRECATION] `new` is deprecated. Please use `create` instead.'
|
23
|
-
create *params
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
1
|
+
require 'rambling/trie'
|
@@ -0,0 +1,33 @@
|
|
1
|
+
[
|
2
|
+
'invalid_operation',
|
3
|
+
'children_hash_deferer',
|
4
|
+
'compressor',
|
5
|
+
'branches',
|
6
|
+
'enumerable',
|
7
|
+
'node',
|
8
|
+
'root',
|
9
|
+
'version'
|
10
|
+
].map { |file| File.join 'rambling', 'trie', file }.each &method(:require)
|
11
|
+
|
12
|
+
# General namespace for all Rambling gems.
|
13
|
+
module Rambling
|
14
|
+
# Entry point for rambling-trie API.
|
15
|
+
module Trie
|
16
|
+
class << self
|
17
|
+
# Creates a new Trie. Entry point for the Rambling::Trie API.
|
18
|
+
# @param [String, nil] filename the file to load the words from (defaults to nil).
|
19
|
+
def create(filename = nil)
|
20
|
+
Root.new filename
|
21
|
+
end
|
22
|
+
|
23
|
+
# Creates a new Trie. Entry point for the Rambling::Trie API.
|
24
|
+
# @param [String, nil] filename the file to load the words from (defaults to nil).
|
25
|
+
# @deprecated Please use {.create} instead.
|
26
|
+
# @see .create
|
27
|
+
def new(filename = nil)
|
28
|
+
warn '[DEPRECATION] `new` is deprecated. Please use `create` instead.'
|
29
|
+
create filename
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -2,10 +2,11 @@ module Rambling
|
|
2
2
|
module Trie
|
3
3
|
# Provides the branching behavior for the Trie data structure.
|
4
4
|
module Branches
|
5
|
-
# Adds a branch to the trie based on the word
|
5
|
+
# Adds a branch to the current trie node based on the word
|
6
6
|
# @param [String] word the word to add the branch from.
|
7
7
|
# @return [Node] the just added branch's root node.
|
8
8
|
# @raise [InvalidOperation] if the trie is already compressed.
|
9
|
+
# @note This method clears the contents of the word variable.
|
9
10
|
def add_branch_from(word)
|
10
11
|
raise InvalidOperation.new('Cannot add branch to compressed trie') if compressed?
|
11
12
|
if word.empty?
|
@@ -25,7 +26,14 @@ module Rambling
|
|
25
26
|
end
|
26
27
|
end
|
27
28
|
|
28
|
-
alias_method
|
29
|
+
# Alias for {#add_branch_from}. Defined instead of simple `alias_method` for overriding purposes.
|
30
|
+
# @param [String] word the word to add the branch from.
|
31
|
+
# @return [Node] the just added branch's root node.
|
32
|
+
# @raise [InvalidOperation] if the trie is already compressed.
|
33
|
+
# @see #add_branch_from
|
34
|
+
def <<(word)
|
35
|
+
add_branch_from word
|
36
|
+
end
|
29
37
|
|
30
38
|
protected
|
31
39
|
|
File without changes
|
@@ -8,6 +8,8 @@ module Rambling
|
|
8
8
|
@parent.nil? ? false : @parent.compressed?
|
9
9
|
end
|
10
10
|
|
11
|
+
# Compressed the current node using redundant node elimination.
|
12
|
+
# @return [Root, Node] the compressed node.
|
11
13
|
def compress_tree!
|
12
14
|
if @children.size == 1 and not terminal? and not @letter.nil?
|
13
15
|
merge_with! @children.values.first
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Rambling
|
2
|
+
module Trie
|
3
|
+
# Provides enumerable behavior to the Trie data structure.
|
4
|
+
module Enumerable
|
5
|
+
include ::Enumerable
|
6
|
+
|
7
|
+
alias_method :size, :count
|
8
|
+
|
9
|
+
# Calls block once for each of the words contained in the trie. If no block given, an Enumerator is returned.
|
10
|
+
def each(&block)
|
11
|
+
enumerator = Enumerator.new do |words|
|
12
|
+
words << as_word if terminal?
|
13
|
+
children.each { |key, child| child.each { |word| words << word } }
|
14
|
+
end
|
15
|
+
|
16
|
+
block.nil? ? enumerator : enumerator.each(&block)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
File without changes
|
File without changes
|
@@ -41,6 +41,16 @@ module Rambling
|
|
41
41
|
|
42
42
|
alias_method :include?, :is_word?
|
43
43
|
|
44
|
+
# Adds a branch to the trie based on the word, without changing the passed word.
|
45
|
+
# @param [String] word the word to add the branch from.
|
46
|
+
# @return [Node] the just added branch's root node.
|
47
|
+
# @raise [InvalidOperation] if the trie is already compressed.
|
48
|
+
# @see Branches#add_branch_from
|
49
|
+
# @note Avoids clearing the contents of the word variable.
|
50
|
+
def add_branch_from(word)
|
51
|
+
super word.clone
|
52
|
+
end
|
53
|
+
|
44
54
|
private
|
45
55
|
def fulfills_condition(word, method)
|
46
56
|
method = compressed? ? "compressed_#{method}" : "uncompressed_#{method}"
|
File without changes
|
File without changes
|
data/rambling-trie.gemspec
CHANGED
File without changes
|
File without changes
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Rambling
|
4
|
+
module Trie
|
5
|
+
describe Enumerable do
|
6
|
+
let(:root) { Root.new }
|
7
|
+
let(:words) { %w(add some words and another word) }
|
8
|
+
|
9
|
+
before :each do
|
10
|
+
words.each { |word| root << word.clone }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#each' do
|
14
|
+
it 'returns an enumerator' do
|
15
|
+
root.each.should be_a(Enumerator)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'includes every word contained in the trie' do
|
19
|
+
root.each { |word| words.should include(word) }
|
20
|
+
root.count.should == words.count
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#size' do
|
25
|
+
it 'delegates to #count' do
|
26
|
+
root.size.should == words.size
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'includes the core Enumerable module' do
|
31
|
+
root.all? { |word| words.include? word }.should be_true
|
32
|
+
root.any? { |word| word.start_with? 's' }.should be_true
|
33
|
+
root.to_a.should =~ words
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
File without changes
|
@@ -3,10 +3,10 @@ require 'spec_helper'
|
|
3
3
|
module Rambling
|
4
4
|
module Trie
|
5
5
|
describe Root do
|
6
|
+
let(:root) { Root.new }
|
7
|
+
|
6
8
|
describe '.new' do
|
7
9
|
context 'without filename' do
|
8
|
-
let(:root) { Root.new }
|
9
|
-
|
10
10
|
it 'has no letter' do
|
11
11
|
root.letter.should be_nil
|
12
12
|
end
|
@@ -66,8 +66,6 @@ module Rambling
|
|
66
66
|
end
|
67
67
|
|
68
68
|
describe '#compress!' do
|
69
|
-
let(:root) { Root.new }
|
70
|
-
|
71
69
|
it 'returns itself marked as compressed' do
|
72
70
|
compressed_root = root.compress!
|
73
71
|
|
@@ -186,8 +184,6 @@ module Rambling
|
|
186
184
|
end
|
187
185
|
|
188
186
|
describe '#has_branch_for?' do
|
189
|
-
let(:root) { Root.new }
|
190
|
-
|
191
187
|
context 'word is contained' do
|
192
188
|
shared_examples_for 'word is found' do
|
193
189
|
it 'matches part of the word' do
|
@@ -246,7 +242,6 @@ module Rambling
|
|
246
242
|
end
|
247
243
|
|
248
244
|
describe '#include?' do
|
249
|
-
let(:root) { Root.new }
|
250
245
|
let(:word) { 'word' }
|
251
246
|
|
252
247
|
it 'delegates to #is_word?' do
|
@@ -256,6 +251,21 @@ module Rambling
|
|
256
251
|
end
|
257
252
|
end
|
258
253
|
end
|
254
|
+
|
255
|
+
describe '#add_branch_from' do
|
256
|
+
let(:original_word) { 'word' }
|
257
|
+
let(:word) { original_word.clone }
|
258
|
+
|
259
|
+
it 'does not change the original word' do
|
260
|
+
root.add_branch_from word
|
261
|
+
word.should == original_word
|
262
|
+
end
|
263
|
+
|
264
|
+
it 'is still aliased as #<<' do
|
265
|
+
root << word
|
266
|
+
word.should == original_word
|
267
|
+
end
|
268
|
+
end
|
259
269
|
end
|
260
270
|
end
|
261
271
|
end
|
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rambling-trie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -102,6 +102,7 @@ extra_rdoc_files: []
|
|
102
102
|
files:
|
103
103
|
- .gitignore
|
104
104
|
- .rspec
|
105
|
+
- .travis.yml
|
105
106
|
- Gemfile
|
106
107
|
- Guardfile
|
107
108
|
- LICENSE
|
@@ -109,23 +110,26 @@ files:
|
|
109
110
|
- Rakefile
|
110
111
|
- assets/dictionaries/words_with_friends.txt
|
111
112
|
- lib/rambling-trie.rb
|
112
|
-
- lib/rambling
|
113
|
-
- lib/rambling
|
114
|
-
- lib/rambling
|
115
|
-
- lib/rambling
|
116
|
-
- lib/rambling
|
117
|
-
- lib/rambling
|
118
|
-
- lib/rambling
|
119
|
-
- lib/rambling
|
120
|
-
- lib/rambling
|
113
|
+
- lib/rambling/trie.rb
|
114
|
+
- lib/rambling/trie/branches.rb
|
115
|
+
- lib/rambling/trie/children_hash_deferer.rb
|
116
|
+
- lib/rambling/trie/compressor.rb
|
117
|
+
- lib/rambling/trie/enumerable.rb
|
118
|
+
- lib/rambling/trie/invalid_operation.rb
|
119
|
+
- lib/rambling/trie/node.rb
|
120
|
+
- lib/rambling/trie/root.rb
|
121
|
+
- lib/rambling/trie/tasks/gem.rb
|
122
|
+
- lib/rambling/trie/tasks/performance.rb
|
123
|
+
- lib/rambling/trie/version.rb
|
121
124
|
- rambling-trie.gemspec
|
122
125
|
- reports/performance
|
123
126
|
- spec/assets/test_words.txt
|
124
|
-
- spec/lib/rambling
|
125
|
-
- spec/lib/rambling
|
126
|
-
- spec/lib/rambling
|
127
|
-
- spec/lib/rambling
|
128
|
-
- spec/lib/rambling
|
127
|
+
- spec/lib/rambling/trie/branches_spec.rb
|
128
|
+
- spec/lib/rambling/trie/children_hash_deferer_spec.rb
|
129
|
+
- spec/lib/rambling/trie/enumerable_spec.rb
|
130
|
+
- spec/lib/rambling/trie/node_spec.rb
|
131
|
+
- spec/lib/rambling/trie/root_spec.rb
|
132
|
+
- spec/lib/rambling/trie_spec.rb
|
129
133
|
- spec/spec_helper.rb
|
130
134
|
homepage: http://github.com/ramblinglabs/rambling-trie
|
131
135
|
licenses: []
|
@@ -141,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
141
145
|
version: '0'
|
142
146
|
segments:
|
143
147
|
- 0
|
144
|
-
hash:
|
148
|
+
hash: -1164707040702408249
|
145
149
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
150
|
none: false
|
147
151
|
requirements:
|
@@ -150,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
154
|
version: '0'
|
151
155
|
segments:
|
152
156
|
- 0
|
153
|
-
hash:
|
157
|
+
hash: -1164707040702408249
|
154
158
|
requirements: []
|
155
159
|
rubyforge_project:
|
156
160
|
rubygems_version: 1.8.24
|