array_trie 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +55 -4
- data/lib/array_trie.rb +2 -0
- data/lib/array_trie/prefix_trie.rb +2 -0
- data/lib/array_trie/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 15499f92722e8290a8de0c8c2a17f3bae3d77102
|
4
|
+
data.tar.gz: fc76470fe8932414955e48ab9099ede0548fda50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 59ef3cff51b614e7a4928ebae233c7113c5aaf657a0012e9f8063fed6f10f0710bbb1c7ac58815882d9d9d1c7eb563144d18034f161aefe069daf64018d3f072
|
7
|
+
data.tar.gz: 36488d8d2ff5ff712b01944b101f01c0bc12ca6e95cc80f91bc3729763e5d758873e1e56c7def000ef22ced7065fba58359dd40dedae6c28d55638ad376eb9d2
|
data/README.md
CHANGED
@@ -1,8 +1,11 @@
|
|
1
1
|
# ArrayTrie
|
2
2
|
|
3
|
-
|
3
|
+
[![Ruby gem](https://img.shields.io/gem/v/array_trie.svg)](https://rubygems.org/gems/array_trie)
|
4
|
+
[API docs](https://www.rubydoc.info/gems/array_trie)
|
4
5
|
|
5
|
-
|
6
|
+
Trie-like, prefix-tree data structures. First, a prefix-tree based on Arrays, which differs from a traditional trie, which maps strings to values. Second, a more general prefix-tree data structure that works for any type of keys, provided those keys can be transformed to and from an array.
|
7
|
+
|
8
|
+
Both of these data structures are implemented in terms of hashes.
|
6
9
|
|
7
10
|
## Installation
|
8
11
|
|
@@ -22,11 +25,59 @@ Or install it yourself as:
|
|
22
25
|
|
23
26
|
## Usage
|
24
27
|
|
25
|
-
|
28
|
+
Use the base `ArrayTrie` class if you want to work with an array-keyed trie.
|
29
|
+
The more general `ArrayTrie::PrefixTrie` class can be used with any sort of
|
30
|
+
ordered key.
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'array_trie/prefix_trie'
|
34
|
+
|
35
|
+
# You can store any sort of ordered key in a PrefixTrie, provided you can
|
36
|
+
# convert to and from arrays in a stable way.
|
37
|
+
path_to_a = -> (path) { path.split('/') }
|
38
|
+
a_to_path = -> (array) { array.join('/') }
|
39
|
+
paths = ArrayTrie::PrefixTrie.new(path_to_a, a_to_path)
|
40
|
+
|
41
|
+
# Store some keys in the trie
|
42
|
+
paths['/usr/local/bin/ruby'] = 'executable'
|
43
|
+
paths['/usr/local/etc/nginx/nginx.cfg'] = 'config file'
|
44
|
+
paths['/bin/bash'] = 'executable'
|
45
|
+
|
46
|
+
# Tries have efficient prefix queries
|
47
|
+
paths.include_prefix?('/usr/local')
|
48
|
+
# => true
|
49
|
+
paths.count_prefix('/usr/local')
|
50
|
+
# => 2
|
51
|
+
|
52
|
+
# You can obtain a subtrie to operate on a subsection of your trie
|
53
|
+
usr_local = paths.subtrie('/usr/local')
|
54
|
+
usr_local['bin/ruby']
|
55
|
+
# => 'executable'
|
56
|
+
|
57
|
+
usr_local['bin/fish'] = 'executable'
|
58
|
+
paths['/usr/local/bin/fish']
|
59
|
+
# => executable
|
60
|
+
|
61
|
+
# Use #breadth_first and #depth_first to enumarate your keys and values
|
62
|
+
paths.breadth_first do |k, v|
|
63
|
+
puts "Path #{k} is of type #{v}"
|
64
|
+
end
|
65
|
+
# STDOUT: Path /bin/bash is of type executable
|
66
|
+
# STDOUT: Path /usr/local/bin/ruby is of type executable
|
67
|
+
# STDOUT: Path /usr/local/etc/nginx/nginx.cfg is of type config file
|
68
|
+
|
69
|
+
# These methods return Enumerators, so you can use them with #map, etc.
|
70
|
+
enum = paths.depth_first
|
71
|
+
as_hash = Hash[enum.to_a]
|
72
|
+
# => {
|
73
|
+
# "/usr/local/bin/ruby"=>"executable",
|
74
|
+
# "/usr/local/etc/nginx/nginx.cfg"=>"config file",
|
75
|
+
# "/bin/bash"=>"executable"}
|
76
|
+
```
|
26
77
|
|
27
78
|
## Development
|
28
79
|
|
29
|
-
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
80
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. Finally, use `bin/test` to run the tests.
|
30
81
|
|
31
82
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
83
|
|
data/lib/array_trie.rb
CHANGED
@@ -9,6 +9,8 @@
|
|
9
9
|
# {ArrayTrie::PrefixTrie}, which can map arbitrary keys to values, so long as
|
10
10
|
# your keys can be converted to and from arrays.
|
11
11
|
class ArrayTrie
|
12
|
+
require 'array_trie/version'
|
13
|
+
|
12
14
|
# Just a unique marker value.
|
13
15
|
# Using Class.new is better than using Object.new, because it makes sense
|
14
16
|
# when inspected.
|
data/lib/array_trie/version.rb
CHANGED