rambling-trie 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2011-2012 Rambling Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person
4
+ obtaining a copy of this software and associated documentation
5
+ files (the "Software"), to deal in the Software without
6
+ restriction, including without limitation the rights to use,
7
+ copy, modify, merge, publish, distribute, sublicense, and/or
8
+ sell copies of the Software, and to permit persons to whom
9
+ the Software is furnished to do so, subject to the following
10
+ conditions:
11
+
12
+ The above copyright notice and this permission notice shall
13
+ be included in all copies or substantial portions of the
14
+ Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18
+ WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19
+ PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23
+ USE OR OTHER DEALINGS IN THE SOFTWARE.
24
+
data/README.markdown ADDED
@@ -0,0 +1,111 @@
1
+ # Rambling Trie
2
+
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
+
5
+ ## Installing the Rambling Trie
6
+
7
+ ### Requirements
8
+
9
+ You will need:
10
+
11
+ * Ruby 1.9.2 or up
12
+ * RubyGems
13
+
14
+ See [RVM](http://beginrescueend.com) or [rbenv](https://github.com/sstephenson/rbenv) for more information on how to manage Ruby versions.
15
+
16
+ ### Installation
17
+
18
+ You can either install it manually with:
19
+
20
+ <pre><code>gem install rambling-trie
21
+ </code></pre>
22
+
23
+ Or, include it in your `Gemfile` and bundle it:
24
+
25
+ ``` ruby
26
+ gem 'rambling-trie'
27
+ ```
28
+
29
+ ## How to use the Rambling Trie
30
+
31
+ To create the trie, initialize it like this:
32
+
33
+ ``` ruby
34
+ trie = Rambling::Trie.new
35
+ ```
36
+
37
+ You can also provide a file which contains all the words to be added to the trie, and it will read the file and create the structure for you, like this:
38
+
39
+ ``` ruby
40
+ trie = Rambling::Trie.new('/path/to/file')
41
+ ```
42
+
43
+ To add new words to the trie, use `add_branch_from`:
44
+
45
+ ``` ruby
46
+ trie.add_branch_from('word')
47
+ ```
48
+
49
+ And to find out if a word already exists in the trie, use `is_word?`:
50
+
51
+ ``` ruby
52
+ trie.is_word?('word')
53
+ ```
54
+
55
+ If you wish to find if part of a word exists in the `Rambling::Trie` instance, you should call `has_branch_from?`:
56
+
57
+ ``` ruby
58
+ trie.has_branch_from?('partial_word')
59
+ ```
60
+
61
+ ### Compression
62
+
63
+ By default, the Rambling Trie works as a Standard Trie.
64
+ You can obtain a Compressed Trie from the Standard one, by using the compression feature.
65
+ Just call the `compress!` method on the `Rambling::Trie` instance:
66
+
67
+ ``` ruby
68
+ trie.compress!
69
+ ```
70
+
71
+ This will reduce the amount of Trie nodes by eliminating the redundant ones, which are the only-child non-terminal nodes.
72
+ This will also make the Trie faster to traverse.
73
+
74
+ __Note that the `compress!` method acts over the `Rambling::Trie` instance it belongs to.__
75
+ __Also, words added after compression will create new nodes and re-compression is not supported.__
76
+
77
+ You can find out if a `Rambling::Trie` instance is compressed by calling the `compressed?` method:
78
+
79
+ ``` ruby
80
+ trie.compressed?
81
+ ```
82
+
83
+ ## Compatible Ruby and Rails versions
84
+
85
+ The Rambling Trie has been tested with the following Ruby versions:
86
+
87
+ * 1.9.2
88
+ * 1.9.3
89
+
90
+ And the following Rails versions:
91
+
92
+ * 3.1.x
93
+ * 3.2.x
94
+
95
+ It's possible that Ruby 1.8.7 and Rails 3.0.x are supported, but there is no guarantee.
96
+
97
+ ## Contributing to Rambling Trie
98
+
99
+ If you want to contribute to this project, you will need RSpec to run the tests.
100
+ Also, be sure to add tests for any feature you may develop or bug you may fix.
101
+
102
+ ## License and copyright
103
+
104
+ Copyright (c) 2012 Rambling Labs
105
+
106
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
107
+
108
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
109
+
110
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
111
+
@@ -2,7 +2,7 @@ module Rambling
2
2
  module TrieCompressor
3
3
  def compress!
4
4
  if @children.size == 1 and not terminal?
5
- transfer_ownership_from(@children.values.first)
5
+ merge_with!(@children.values.first)
6
6
  compress!
7
7
  end
8
8
 
@@ -13,14 +13,26 @@ module Rambling
13
13
 
14
14
  private
15
15
 
16
- def transfer_ownership_from(child)
17
- @parent.delete(@letter) unless @parent.nil?
18
- @letter = (@letter.to_s + child.letter.to_s).to_sym
19
- @parent[@letter] = self unless @parent.nil?
16
+ def merge_with!(child)
17
+ new_letter = (@letter.to_s + child.letter.to_s).to_sym
18
+
19
+ rehash_on_parent!(@letter, new_letter)
20
+ redefine_self!(new_letter, child)
20
21
 
21
- @children = child.children
22
- @is_terminal = child.terminal?
23
22
  @children.values.each { |node| node.parent = self }
24
23
  end
24
+
25
+ def rehash_on_parent!(old_letter, new_letter)
26
+ return if @parent.nil?
27
+
28
+ @parent.delete(old_letter)
29
+ @parent[new_letter] = self
30
+ end
31
+
32
+ def redefine_self!(new_letter, merged_node)
33
+ @letter = new_letter
34
+ @children = merged_node.children
35
+ @is_terminal = merged_node.terminal?
36
+ end
25
37
  end
26
38
  end
data/lib/trie_node.rb CHANGED
@@ -11,7 +11,7 @@ module Rambling
11
11
  @is_terminal = false
12
12
  @children = {}
13
13
 
14
- unless word.nil?
14
+ unless word.nil? or word.empty?
15
15
  letter = word.slice!(0)
16
16
  @letter = letter.to_sym unless letter.nil?
17
17
  @is_terminal = word.empty?
@@ -28,21 +28,20 @@ module Rambling
28
28
  end
29
29
 
30
30
  def add_branch_from(word)
31
- unless word.empty?
32
- first_letter = word.slice(0).to_sym
33
-
34
- if @children.has_key?(first_letter)
35
- word.slice!(0)
36
- @children[first_letter].add_branch_from(word)
37
- else
38
- @children[first_letter] = TrieNode.new(word, self)
39
- end
31
+ return if word.empty?
32
+
33
+ first_letter = word.slice(0).to_sym
34
+
35
+ if @children.has_key?(first_letter)
36
+ word.slice!(0)
37
+ @children[first_letter].add_branch_from(word)
38
+ else
39
+ @children[first_letter] = TrieNode.new(word, self)
40
40
  end
41
41
  end
42
42
 
43
43
  def has_branch_for?(word)
44
- return true if word.empty?
45
- branch_exists_and(word) { |node, sliced_word| node.has_branch_for?(sliced_word) }
44
+ word.empty? or branch_exists_and(word, :has_branch_for?)
46
45
  end
47
46
 
48
47
  def as_word
@@ -50,18 +49,13 @@ module Rambling
50
49
  get_letter_string
51
50
  end
52
51
 
53
- def is_word?(word)
54
- return true if word.empty? and terminal?
55
- branch_exists_and(word) { |node, sliced_word| node.is_word?(sliced_word) }
52
+ def is_word?(word = '')
53
+ (word.empty? and terminal?) or branch_exists_and(word, :is_word?)
56
54
  end
57
55
 
58
56
  protected
59
57
  def get_letter_string
60
- if @parent.nil?
61
- @letter.to_s or ''
62
- else
63
- @parent.get_letter_string + @letter.to_s
64
- end
58
+ (@parent.nil? ? '' : @parent.get_letter_string) + @letter.to_s
65
59
  end
66
60
 
67
61
  def parent=(parent)
@@ -70,15 +64,13 @@ module Rambling
70
64
 
71
65
  private
72
66
 
73
- def branch_exists_and(word, &block)
67
+ def branch_exists_and(word, method)
74
68
  first_letter = word.slice!(0)
75
69
 
76
- unless first_letter.nil?
77
- first_letter = first_letter.to_sym
78
- return block.call(@children[first_letter], word) if @children.has_key?(first_letter)
79
- end
70
+ return false if first_letter.nil?
80
71
 
81
- false
72
+ first_letter_key = first_letter.to_sym
73
+ @children.has_key?(first_letter_key) ? @children[first_letter_key].send(method, word) : false
82
74
  end
83
75
  end
84
76
  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.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,9 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-07 00:00:00.000000000Z
13
- dependencies: []
14
- description: A custom implementation of the trie data structure
12
+ date: 2012-02-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &15464280 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *15464280
25
+ description: The Rambling Trie is a custom implementation of the Trie data structure
26
+ with Ruby, which includes compression abilities and is designed to be very fast
27
+ to traverse.
15
28
  email: development@ramblinglabs.com
16
29
  executables: []
17
30
  extensions: []
@@ -23,7 +36,9 @@ files:
23
36
  - ./lib/trie.rb
24
37
  - ./lib/trie_compressor.rb
25
38
  - ./lib/trie_node.rb
26
- homepage: http://rubygems.org/gems/rambling-trie
39
+ - LICENSE
40
+ - README.markdown
41
+ homepage: http://github.com/ramblinglabs/rambling-trie
27
42
  licenses: []
28
43
  post_install_message:
29
44
  rdoc_options: []
@@ -46,5 +61,5 @@ rubyforge_project:
46
61
  rubygems_version: 1.8.10
47
62
  signing_key:
48
63
  specification_version: 3
49
- summary: Rambling Trie
64
+ summary: A custom implementation of the trie data structure.
50
65
  test_files: []