codders-trie 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/extensions/look_ahead_trie.rb +22 -0
- data/test/extensions/look_ahead_trie_tests.rb +52 -0
- data/trie.gemspec +2 -2
- metadata +3 -1
@@ -0,0 +1,22 @@
|
|
1
|
+
class LookAheadTrie < Trie
|
2
|
+
|
3
|
+
def paths
|
4
|
+
@children.collect { |c| c[0] + c[1].longest_path }
|
5
|
+
end
|
6
|
+
|
7
|
+
def steps
|
8
|
+
@children.collect { |c| c[0] }
|
9
|
+
end
|
10
|
+
|
11
|
+
def linear?
|
12
|
+
@children.size == 1 && @values.size == 0
|
13
|
+
end
|
14
|
+
|
15
|
+
def longest_path
|
16
|
+
return @compressed_key.join("") if @compressed_key.size > 0
|
17
|
+
return "" unless linear?
|
18
|
+
child = @children.first
|
19
|
+
return child[0] + child[1].longest_path
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
#!/usr/bin/ruby -w
|
2
|
+
#
|
3
|
+
# = Name
|
4
|
+
# TestTrie
|
5
|
+
#
|
6
|
+
# == Description
|
7
|
+
# This file contains unit tests for the Trie class.
|
8
|
+
#
|
9
|
+
# == Author
|
10
|
+
# Daniel Erat <dan-ruby@erat.org>
|
11
|
+
#
|
12
|
+
# == Copyright
|
13
|
+
# Copyright 2005 Daniel Erat
|
14
|
+
#
|
15
|
+
# == License
|
16
|
+
# GNU GPL; see COPYING
|
17
|
+
|
18
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "..", "lib")
|
19
|
+
|
20
|
+
require 'test/unit'
|
21
|
+
require 'trie'
|
22
|
+
|
23
|
+
# Unit tests for the Trie class.
|
24
|
+
class TestLookAheadTrie < Test::Unit::TestCase
|
25
|
+
# Check to see what the possible extensions are
|
26
|
+
def test_list_root_paths
|
27
|
+
t = LookAheadTrie.new
|
28
|
+
%w(radio ratio ration radon patio path q sunshine).each_with_index { |word, index| t.insert(word, index) }
|
29
|
+
assert_equal(%w(pat q ra sunshine), t.paths.sort)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_list_walked_paths
|
33
|
+
t = LookAheadTrie.new
|
34
|
+
%w(radio ratio ration radon patio path q).each_with_index { |word, index| t.insert(word, index) }
|
35
|
+
t2 = t.find_prefix("ra")
|
36
|
+
assert_equal(%w(d tio), t2.paths.sort)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_list_root_steps
|
40
|
+
t = LookAheadTrie.new
|
41
|
+
%w(radio ratio ration radon patio path q).each_with_index { |word, index| t.insert(word, index) }
|
42
|
+
assert_equal(%w(p q r), t.steps.sort)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_list_walked_steps
|
46
|
+
t = LookAheadTrie.new
|
47
|
+
%w(radio ratio ration radon patio path q).each_with_index { |word, index| t.insert(word, index) }
|
48
|
+
t2 = t.find_prefix("ra")
|
49
|
+
assert_equal(%w(d t), t2.steps.sort)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/trie.gemspec
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
SPEC = Gem::Specification.new do |s|
|
2
2
|
s.name = "codders-trie"
|
3
|
-
s.version = "0.0.
|
3
|
+
s.version = "0.0.4"
|
4
4
|
s.author = "Daniel Erat"
|
5
5
|
s.email = "dan-ruby@erat.org"
|
6
6
|
s.homepage = "http://www.erat.org/ruby/"
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.summary = "Implemention of a trie data structure"
|
9
|
-
candidates = %w(COPYING INSTALL MANIFEST README setup.rb trie.gemspec lib/trie.rb test/tests.rb)
|
9
|
+
candidates = %w(COPYING INSTALL MANIFEST README setup.rb trie.gemspec lib/trie.rb lib/extensions/look_ahead_trie.rb test/tests.rb test/extensions/look_ahead_trie_tests.rb)
|
10
10
|
s.files = candidates.delete_if {|i| i =~ /CVS/ }
|
11
11
|
s.require_path = "lib"
|
12
12
|
s.test_file = "test/tests.rb"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codders-trie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -24,7 +24,9 @@ files:
|
|
24
24
|
- setup.rb
|
25
25
|
- trie.gemspec
|
26
26
|
- lib/trie.rb
|
27
|
+
- lib/extensions/look_ahead_trie.rb
|
27
28
|
- test/tests.rb
|
29
|
+
- test/extensions/look_ahead_trie_tests.rb
|
28
30
|
homepage: http://www.erat.org/ruby/
|
29
31
|
licenses: []
|
30
32
|
post_install_message:
|