trie_matcher 1.0.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 +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +40 -0
- data/Rakefile +7 -0
- data/lib/trie_matcher/version.rb +3 -0
- data/lib/trie_matcher.rb +87 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/trie_matcher_spec.rb +63 -0
- data/trie_matcher.gemspec +24 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4825d60fa8f9fc8493b6ba2d420306ba24a157c5
|
4
|
+
data.tar.gz: 31cf3a39e649f988e711bbff24dc3af0318b9f45
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d0f0695f7791ab6c9d7e814ddc38119a9d13eb9786b8c51a93ec4f59807f709117c638f163c9d161a0aa86b6dcdffd47396b908b0e068348e9cd869bfbfc4850
|
7
|
+
data.tar.gz: 2311159b3187eae2a82650641a510f7830a21f2756b886a1971c754d1c4423fb7ca43bc808193dbc2e2409fcfc84bdb7a45fd7e564c4901c0bb9f443b0cebb06
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Steven Karas
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# TrieMatcher
|
2
|
+
|
3
|
+
Prefix matching in Ruby using a trie
|
4
|
+
|
5
|
+
I built this after needing to match user agents quickly, and found that existing trie implementations did not provide suitable interfaces.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'trie_matcher'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install trie_matcher
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
t = TrieMatcher.new
|
25
|
+
t["cat"] = "feline"
|
26
|
+
t["car"] = "automobile"
|
27
|
+
t["bar"] = "exam"
|
28
|
+
|
29
|
+
t["cat"] # "feline"
|
30
|
+
t["catch"] # "feline"
|
31
|
+
t["ca"] # nil
|
32
|
+
```
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
1. Fork it ( https://github.com/[my-github-username]/trie_matcher/fork )
|
37
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
38
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
39
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
40
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/trie_matcher.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require File.expand_path("trie_matcher/version", __dir__)
|
2
|
+
|
3
|
+
class TrieMatcher
|
4
|
+
def initialize
|
5
|
+
@root = { nodes: {}, value: nil}
|
6
|
+
end
|
7
|
+
|
8
|
+
def []=(key, value)
|
9
|
+
current = @root
|
10
|
+
current_key = key
|
11
|
+
|
12
|
+
while current_key != ""
|
13
|
+
current, current_key = find_canididate_insertion_node(current, current_key)
|
14
|
+
end
|
15
|
+
|
16
|
+
current[:value] = value
|
17
|
+
return value
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def [](key)
|
22
|
+
current = @root
|
23
|
+
current_key = key
|
24
|
+
|
25
|
+
while current != nil && current_key != ""
|
26
|
+
previous = current
|
27
|
+
current, current_key = next_node(current, current_key)
|
28
|
+
end
|
29
|
+
|
30
|
+
return current[:value] if current
|
31
|
+
return previous[:value]
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
def find_canididate_insertion_node(current, key)
|
36
|
+
# look for a common prefix
|
37
|
+
current[:nodes].keys.find do |prefix|
|
38
|
+
common_prefix = shared_prefix(key, prefix)
|
39
|
+
next unless common_prefix
|
40
|
+
|
41
|
+
if common_prefix == prefix
|
42
|
+
return current[:nodes][prefix], key[common_prefix.length..-1]
|
43
|
+
else
|
44
|
+
old = current[:nodes].delete(prefix)
|
45
|
+
new_node = {
|
46
|
+
nodes: {
|
47
|
+
prefix[common_prefix.length..-1] => old
|
48
|
+
},
|
49
|
+
value: nil
|
50
|
+
}
|
51
|
+
current[:nodes][common_prefix] = new_node
|
52
|
+
return new_node, key[common_prefix.length..-1]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
new_node = {
|
57
|
+
nodes: {},
|
58
|
+
value: nil
|
59
|
+
}
|
60
|
+
current[:nodes][key] = new_node
|
61
|
+
return new_node, ""
|
62
|
+
end
|
63
|
+
|
64
|
+
# find the next node from the current one based on the given key
|
65
|
+
def next_node(current, key)
|
66
|
+
key.length.times do |l|
|
67
|
+
if current[:nodes].has_key?(key[0..-l-1])
|
68
|
+
return current[:nodes][key[0..-l-1]], key[-l,l]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
return nil, nil
|
72
|
+
end
|
73
|
+
|
74
|
+
# finds a shared prefix between the two strings, or nil if there isn't any
|
75
|
+
def shared_prefix(a, b)
|
76
|
+
shared_prefix_length = [a.length, b.length].min
|
77
|
+
while shared_prefix_length >= 0
|
78
|
+
a_prefix = a[0..shared_prefix_length]
|
79
|
+
b_prefix = b[0..shared_prefix_length]
|
80
|
+
return a_prefix if a_prefix == b_prefix
|
81
|
+
|
82
|
+
shared_prefix_length -= 1
|
83
|
+
end
|
84
|
+
|
85
|
+
return nil
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TrieMatcher do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(TrieMatcher::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should store values' do
|
9
|
+
t = TrieMatcher.new
|
10
|
+
t["foo"] = "bar"
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should retrieve stored values' do
|
14
|
+
t = TrieMatcher.new
|
15
|
+
value = "bar"
|
16
|
+
t["foo"] = value
|
17
|
+
expect(t["foo"]).to be value
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should return the stored value' do
|
21
|
+
t = TrieMatcher.new
|
22
|
+
value = "bar"
|
23
|
+
expect(t["foo"] = value).to be value
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should store values with shared prefixes' do
|
27
|
+
t = TrieMatcher.new
|
28
|
+
t["cat"] = 1
|
29
|
+
t["car"] = 2
|
30
|
+
expect(t["cat"]).to be 1
|
31
|
+
expect(t["car"]).to be 2
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should store keys that are a prefix of other keys' do
|
35
|
+
t = TrieMatcher.new
|
36
|
+
t["catch"] = 1
|
37
|
+
t["cat"] = 2
|
38
|
+
expect(t["catch"]).to be 1
|
39
|
+
expect(t["cat"]).to be 2
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should store keys that have a prefix of another key' do
|
43
|
+
t = TrieMatcher.new
|
44
|
+
t["cat"] = 1
|
45
|
+
t["catch"] = 2
|
46
|
+
expect(t["cat"]).to be 1
|
47
|
+
expect(t["catch"]).to be 2
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should do prefix searching' do
|
51
|
+
t = TrieMatcher.new
|
52
|
+
t["cat"] = 1
|
53
|
+
expect(t["cats"]).to be 1
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should do partial prefix matching' do
|
57
|
+
t = TrieMatcher.new
|
58
|
+
t["cat"] = 1
|
59
|
+
t["cats in the cradle"] = 2
|
60
|
+
expect(t["cats"]).to be 1
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'trie_matcher/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "trie_matcher"
|
8
|
+
spec.version = TrieMatcher::VERSION
|
9
|
+
spec.authors = ["Steven Karas"]
|
10
|
+
spec.email = ["steven.karas@gmail.com"]
|
11
|
+
spec.summary = %q{Fast prefix matching}
|
12
|
+
spec.description = %q{Fast prefix matching using a trie-like structure}
|
13
|
+
spec.homepage = "https://github.com/stevenkaras/trie_matcher"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trie_matcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steven Karas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Fast prefix matching using a trie-like structure
|
56
|
+
email:
|
57
|
+
- steven.karas@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/trie_matcher.rb
|
70
|
+
- lib/trie_matcher/version.rb
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
- spec/trie_matcher_spec.rb
|
73
|
+
- trie_matcher.gemspec
|
74
|
+
homepage: https://github.com/stevenkaras/trie_matcher
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Fast prefix matching
|
98
|
+
test_files:
|
99
|
+
- spec/spec_helper.rb
|
100
|
+
- spec/trie_matcher_spec.rb
|