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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c5ee196913bc37a7c274bc7d36689a40d19838a6
4
- data.tar.gz: b4a0dc46652a9964f29db20def6b0d648407c933
3
+ metadata.gz: 15499f92722e8290a8de0c8c2a17f3bae3d77102
4
+ data.tar.gz: fc76470fe8932414955e48ab9099ede0548fda50
5
5
  SHA512:
6
- metadata.gz: 4694fea10a3c317b20e026deb9ff1dd57fe6640b038b6d79622dc8ee8cc9713f87a6b79555627a01cd8c3df104a357826428f01ff45a2b534fee63ec96cd2b8e
7
- data.tar.gz: 0258d42deceb72fc9c0df2c97c4b3212b0573291359554040be3775aa48a170a61513d04a62b2f6c42d868c3d75dded782fbe2a35d02e47a51a87aa5c22c6da4
6
+ metadata.gz: 59ef3cff51b614e7a4928ebae233c7113c5aaf657a0012e9f8063fed6f10f0710bbb1c7ac58815882d9d9d1c7eb563144d18034f161aefe069daf64018d3f072
7
+ data.tar.gz: 36488d8d2ff5ff712b01944b101f01c0bc12ca6e95cc80f91bc3729763e5d758873e1e56c7def000ef22ced7065fba58359dd40dedae6c28d55638ad376eb9d2
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # ArrayTrie
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/array_trie`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Delete this and the text above, and describe your gem
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
- TODO: Write usage instructions here
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
 
@@ -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.
@@ -1,3 +1,5 @@
1
+ require 'array_trie'
2
+
1
3
  class ArrayTrie
2
4
  # A trie-like, prefix-tree data structure that maps arbitrary keys to values,
3
5
  # provided that the keys may be transformed to and from arrays, as each
@@ -1,3 +1,3 @@
1
1
  class ArrayTrie
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: array_trie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Teton-Landis