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 CHANGED
@@ -16,4 +16,8 @@ reports/
16
16
 
17
17
  # Bundler
18
18
  .bundle
19
- pkg
19
+ pkg/
20
+
21
+ # Yard
22
+ doc/
23
+ .yardoc/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
data/Gemfile CHANGED
@@ -2,9 +2,12 @@ source 'http://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'perftools.rb'
5
+ group :development do
6
+ gem 'perftools.rb'
7
+ end
6
8
 
7
9
  group :test do
10
+ gem 'rake'
8
11
  gem 'guard-rspec'
9
12
  gem 'simplecov', require: false
10
13
  end
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
@@ -3,8 +3,7 @@ Bundler::GemHelper.install_tasks
3
3
 
4
4
  require 'rspec/core/rake_task'
5
5
  require 'rambling-trie'
6
- require 'rambling-trie/version'
7
- require 'rambling-trie/tasks/performance'
6
+ require 'rambling/trie/tasks/performance'
8
7
 
9
8
  RSpec::Core::RakeTask.new(:spec)
10
9
 
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 :<<, :add_branch_from
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
 
@@ -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
@@ -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
@@ -1,6 +1,6 @@
1
1
  module Rambling
2
2
  module Trie
3
3
  # Current version of the rambling-trie.
4
- VERSION = '0.4.1'
4
+ VERSION = '0.4.2'
5
5
  end
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path('../lib', __FILE__)
3
- require 'rambling-trie/version'
3
+ require 'rambling/trie/version'
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.authors = ['Rambling Labs']
@@ -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
@@ -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
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.1
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-20 00:00:00.000000000 Z
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-trie/branches.rb
113
- - lib/rambling-trie/children_hash_deferer.rb
114
- - lib/rambling-trie/compressor.rb
115
- - lib/rambling-trie/invalid_operation.rb
116
- - lib/rambling-trie/node.rb
117
- - lib/rambling-trie/root.rb
118
- - lib/rambling-trie/tasks/gem.rb
119
- - lib/rambling-trie/tasks/performance.rb
120
- - lib/rambling-trie/version.rb
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-trie/branches_spec.rb
125
- - spec/lib/rambling-trie/children_hash_deferer_spec.rb
126
- - spec/lib/rambling-trie/node_spec.rb
127
- - spec/lib/rambling-trie/root_spec.rb
128
- - spec/lib/rambling-trie_spec.rb
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: 1932693806363009255
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: 1932693806363009255
157
+ hash: -1164707040702408249
154
158
  requirements: []
155
159
  rubyforge_project:
156
160
  rubygems_version: 1.8.24