bihash 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +25 -0
- data/Rakefile +10 -0
- data/bihash.gemspec +24 -0
- data/bin/console +6 -0
- data/bin/setup +6 -0
- data/lib/bihash.rb +311 -0
- data/lib/bihash/version.rb +3 -0
- data/spec/bihash_spec.rb +782 -0
- data/spec/spec_helper.rb +5 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e5d8345deb718d95a3241b798749ff0b1a1eb571
|
4
|
+
data.tar.gz: 38d06e9a1d0478fe5caaaacd788efe2ec8f029ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0bb47fb6f7d979f9afccf4743fd380c3f5c87668782c5f72ac8c7004249b062bc4278bc1f16d668100d7f6439574938f7aa2cf3544ce59fd25739a299e2fea6d
|
7
|
+
data.tar.gz: 7b8801d5e0d0f3bd19dc4f98d3c8025ae4e2f50c0d70655597845272086801bf92a0ae1fad07508f41eb02b8594e17c3fce28191f295e9b357f65df60f4b5efc
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at cohen.carlisle@gmail.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Cohen Carlisle
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# Bihash
|
2
|
+
|
3
|
+
A simple gem that implements a bidrectional hash
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Use as a hash, except that keys and values are interchangeable.
|
8
|
+
|
9
|
+
## Development
|
10
|
+
|
11
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
12
|
+
`rake spec` to run the specs. You can also run `bin/console` for an interactive
|
13
|
+
prompt that will allow you to experiment.
|
14
|
+
|
15
|
+
## Contributing
|
16
|
+
|
17
|
+
Bug reports and pull requests are welcome on GitHub at
|
18
|
+
https://github.com/Cohen-Carlisle/bihash. This project is intended to be a safe,
|
19
|
+
welcoming space for collaboration, and contributors are expected to adhere to
|
20
|
+
the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
21
|
+
|
22
|
+
## License
|
23
|
+
|
24
|
+
The gem is available as open source under the terms of the
|
25
|
+
[MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bihash.gemspec
ADDED
@@ -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 'bihash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'bihash'
|
8
|
+
s.version = Bihash::VERSION
|
9
|
+
s.authors = ['Cohen Carlisle']
|
10
|
+
s.email = ['cohen.carlisle@gmail.com']
|
11
|
+
|
12
|
+
s.summary = 'Bidirectional Hash'
|
13
|
+
s.description = 'A simple gem that implements a bidrectional hash'
|
14
|
+
s.homepage = 'http://rubygems.org/gems/bihash'
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.require_paths = ['lib']
|
19
|
+
|
20
|
+
s.add_development_dependency 'bundler', '~> 1.11'
|
21
|
+
s.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
s.add_development_dependency 'minitest', '~> 5.5'
|
23
|
+
s.add_development_dependency 'pry', '~> 0.10'
|
24
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/bihash.rb
ADDED
@@ -0,0 +1,311 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require 'bihash/version'
|
3
|
+
|
4
|
+
class Bihash
|
5
|
+
include Enumerable
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def self.[](*args)
|
9
|
+
new_from_hash(Hash[*args])
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.try_convert(arg)
|
13
|
+
h = Hash.try_convert(arg)
|
14
|
+
h && new_from_hash(h)
|
15
|
+
end
|
16
|
+
|
17
|
+
def ==(rhs)
|
18
|
+
rhs.is_a?(self.class) && rhs.send(:merged_hash_attrs) == merged_hash_attrs
|
19
|
+
end
|
20
|
+
|
21
|
+
def [](key)
|
22
|
+
if @forward.key?(key)
|
23
|
+
@forward[key]
|
24
|
+
elsif @reverse.key?(key)
|
25
|
+
@reverse[key]
|
26
|
+
else
|
27
|
+
default_value(key)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def []=(key1, key2)
|
32
|
+
raise_error_if_frozen
|
33
|
+
delete(key1)
|
34
|
+
delete(key2)
|
35
|
+
@reverse[key2] = key1
|
36
|
+
@forward[key1] = key2
|
37
|
+
end
|
38
|
+
|
39
|
+
def assoc(key)
|
40
|
+
@forward.assoc(key) || @reverse.assoc(key)
|
41
|
+
end
|
42
|
+
|
43
|
+
def clear
|
44
|
+
raise_error_if_frozen
|
45
|
+
@forward.clear
|
46
|
+
@reverse.clear
|
47
|
+
self
|
48
|
+
end
|
49
|
+
|
50
|
+
def compare_by_identity
|
51
|
+
raise_error_if_frozen
|
52
|
+
@forward.compare_by_identity
|
53
|
+
@reverse.compare_by_identity
|
54
|
+
self
|
55
|
+
end
|
56
|
+
|
57
|
+
def_delegator :@forward, :compare_by_identity?
|
58
|
+
|
59
|
+
def default(*args)
|
60
|
+
case args.count
|
61
|
+
when 0
|
62
|
+
@default
|
63
|
+
when 1
|
64
|
+
default_value(args[0])
|
65
|
+
else
|
66
|
+
raise ArgumentError, "wrong number of arguments (#{args.size} for 0..1)"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def default=(default)
|
71
|
+
raise_error_if_frozen
|
72
|
+
@default_proc = nil
|
73
|
+
@default = default
|
74
|
+
end
|
75
|
+
|
76
|
+
attr_reader :default_proc
|
77
|
+
|
78
|
+
def default_proc=(arg)
|
79
|
+
raise_error_if_frozen
|
80
|
+
if !arg.nil?
|
81
|
+
if !arg.is_a?(Proc)
|
82
|
+
raise TypeError, "wrong default_proc type #{arg.class} (expected Proc)"
|
83
|
+
end
|
84
|
+
if arg.lambda? && arg.arity != 2
|
85
|
+
raise TypeError, "default_proc takes two arguments (2 for #{arg.arity})"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
@default = nil
|
89
|
+
@default_proc = arg
|
90
|
+
end
|
91
|
+
|
92
|
+
def delete(key)
|
93
|
+
raise_error_if_frozen
|
94
|
+
if @forward.key?(key)
|
95
|
+
@reverse.delete(@forward[key])
|
96
|
+
@forward.delete(key)
|
97
|
+
elsif @reverse.key?(key)
|
98
|
+
@forward.delete(@reverse[key])
|
99
|
+
@reverse.delete(key)
|
100
|
+
else
|
101
|
+
yield(key) if block_given?
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def delete_if
|
106
|
+
if block_given?
|
107
|
+
raise_error_if_frozen
|
108
|
+
@forward.each { |k,v| delete(k) if yield(k,v) }
|
109
|
+
self
|
110
|
+
else
|
111
|
+
to_enum(:delete_if)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def each(&block)
|
116
|
+
if block_given?
|
117
|
+
@forward.each(&block)
|
118
|
+
self
|
119
|
+
else
|
120
|
+
to_enum(:each)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
alias :each_pair :each
|
125
|
+
|
126
|
+
def_delegator :@forward, :empty?
|
127
|
+
|
128
|
+
alias :eql? :==
|
129
|
+
|
130
|
+
def fetch(key, *default, &block)
|
131
|
+
(@forward.key?(key) ? @forward : @reverse).fetch(key, *default, &block)
|
132
|
+
end
|
133
|
+
|
134
|
+
def_delegator :@forward, :flatten
|
135
|
+
|
136
|
+
def has_key?(arg)
|
137
|
+
@forward.has_key?(arg) || @reverse.has_key?(arg)
|
138
|
+
end
|
139
|
+
|
140
|
+
def hash
|
141
|
+
self.class.hash ^ merged_hash_attrs.hash
|
142
|
+
end
|
143
|
+
|
144
|
+
alias :include? :has_key?
|
145
|
+
|
146
|
+
def inspect
|
147
|
+
"Bihash[#{@forward.to_s[1..-2]}]"
|
148
|
+
end
|
149
|
+
|
150
|
+
def keep_if
|
151
|
+
if block_given?
|
152
|
+
raise_error_if_frozen
|
153
|
+
@forward.each { |k,v| delete(k) unless yield(k,v) }
|
154
|
+
self
|
155
|
+
else
|
156
|
+
to_enum(:keep_if)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
alias :key? :has_key?
|
161
|
+
|
162
|
+
def_delegator :@forward, :length
|
163
|
+
|
164
|
+
alias :member? :has_key?
|
165
|
+
|
166
|
+
def merge(other_bh)
|
167
|
+
dup.merge!(other_bh)
|
168
|
+
end
|
169
|
+
|
170
|
+
def merge!(other_bh)
|
171
|
+
raise_error_if_frozen
|
172
|
+
raise_error_unless_bihash(other_bh)
|
173
|
+
other_bh.each { |k,v| store(k,v) }
|
174
|
+
self
|
175
|
+
end
|
176
|
+
|
177
|
+
def rehash
|
178
|
+
raise_error_if_frozen
|
179
|
+
if illegal_state?
|
180
|
+
raise 'Cannot rehash while a key is duplicated outside its own pair'
|
181
|
+
end
|
182
|
+
@forward.rehash
|
183
|
+
@reverse.rehash
|
184
|
+
self
|
185
|
+
end
|
186
|
+
|
187
|
+
def reject(&block)
|
188
|
+
if block_given?
|
189
|
+
dup.delete_if(&block)
|
190
|
+
else
|
191
|
+
to_enum(:reject)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
def reject!(&block)
|
196
|
+
if block_given?
|
197
|
+
raise_error_if_frozen
|
198
|
+
old_size = size
|
199
|
+
delete_if(&block)
|
200
|
+
old_size == size ? nil : self
|
201
|
+
else
|
202
|
+
to_enum(:reject!)
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
def replace(other_bh)
|
207
|
+
raise_error_if_frozen
|
208
|
+
raise_error_unless_bihash(other_bh)
|
209
|
+
@forward.replace(other_bh.instance_variable_get(:@forward))
|
210
|
+
@reverse.replace(other_bh.instance_variable_get(:@reverse))
|
211
|
+
self
|
212
|
+
end
|
213
|
+
|
214
|
+
def select(&block)
|
215
|
+
if block_given?
|
216
|
+
dup.keep_if(&block)
|
217
|
+
else
|
218
|
+
to_enum(:select)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def select!(&block)
|
223
|
+
if block_given?
|
224
|
+
raise_error_if_frozen
|
225
|
+
old_size = size
|
226
|
+
keep_if(&block)
|
227
|
+
old_size == size ? nil : self
|
228
|
+
else
|
229
|
+
to_enum(:select!)
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
def shift
|
234
|
+
raise_error_if_frozen
|
235
|
+
if empty?
|
236
|
+
default_value(nil)
|
237
|
+
else
|
238
|
+
@reverse.shift
|
239
|
+
@forward.shift
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
def_delegator :@forward, :size
|
244
|
+
|
245
|
+
alias :store :[]=
|
246
|
+
|
247
|
+
def to_h
|
248
|
+
@forward.dup
|
249
|
+
end
|
250
|
+
|
251
|
+
alias :to_s :inspect
|
252
|
+
|
253
|
+
alias :update :merge!
|
254
|
+
|
255
|
+
def values_at(*keys)
|
256
|
+
keys.map { |key| self[key] }
|
257
|
+
end
|
258
|
+
|
259
|
+
private
|
260
|
+
|
261
|
+
def self.new_from_hash(h)
|
262
|
+
bihash = new
|
263
|
+
bihash.instance_variable_set(:@reverse, h.invert)
|
264
|
+
bihash.instance_variable_set(:@forward, h)
|
265
|
+
if bihash.send(:illegal_state?)
|
266
|
+
raise ArgumentError, 'A key would be duplicated outside its own pair'
|
267
|
+
end
|
268
|
+
bihash
|
269
|
+
end
|
270
|
+
private_class_method :new_from_hash
|
271
|
+
|
272
|
+
def default_value(key)
|
273
|
+
@default_proc ? @default_proc.call(self, key) : @default
|
274
|
+
end
|
275
|
+
|
276
|
+
def illegal_state?
|
277
|
+
fw = @forward
|
278
|
+
(fw.keys | fw.values).size + fw.select { |k,v| k == v }.size < fw.size * 2
|
279
|
+
end
|
280
|
+
|
281
|
+
def initialize(*args, &block)
|
282
|
+
raise_error_if_frozen
|
283
|
+
if block_given? && !args.empty?
|
284
|
+
raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
|
285
|
+
elsif args.size > 1
|
286
|
+
raise ArgumentError, "wrong number of arguments (#{args.size} for 0..1)"
|
287
|
+
end
|
288
|
+
super()
|
289
|
+
@forward, @reverse = {}, {}
|
290
|
+
@default, @default_proc = args[0], block
|
291
|
+
end
|
292
|
+
|
293
|
+
def initialize_copy(source)
|
294
|
+
super
|
295
|
+
@forward, @reverse = @forward.dup, @reverse.dup
|
296
|
+
end
|
297
|
+
|
298
|
+
def merged_hash_attrs
|
299
|
+
@reverse.merge(@forward)
|
300
|
+
end
|
301
|
+
|
302
|
+
def raise_error_if_frozen
|
303
|
+
raise "can't modify frozen Bihash" if frozen?
|
304
|
+
end
|
305
|
+
|
306
|
+
def raise_error_unless_bihash(obj)
|
307
|
+
unless obj.is_a?(Bihash)
|
308
|
+
raise TypeError, "wrong argument type #{obj.class} (expected Bihash)"
|
309
|
+
end
|
310
|
+
end
|
311
|
+
end
|