cohen-carlisle / bihash Goto Github PK
View Code? Open in Web Editor NEWImplements a bidirectional map in Ruby
License: MIT License
Implements a bidirectional map in Ruby
License: MIT License
Decide if Bihash should have merge methods.
If so, decide how to resolve collisions.
Merge methods:
#merge!
#update (alias of #merge!)
#merge
suggested changing impl to:
def fetch(key, *args, &block)
(@forward.key?(key) ? @forward : @reverse).fetch(key, *args, &block)
end
Someday maybe improve serialization to not include redundant data.
Add logic to dup and freeze them instead of relying on the internal hashes to do it.
Current readme is unireadme
Suggested by Spike@StackExchange {
def select!(&block)
if block_given?
raise_error_if_frozen
old_size = size
@forward.each { |k,v| delete(k) if !block.call(k,v) }
old_size == size ? nil : self
else
to_enum(:select!)
end
end
The @forward.each line can be changed to keep_if(&block). Similarly for reject! and delete_if
}
However, this would also make redundant #block_given? and #raise_error_if_frozen checks
[:compact, :compact!]
bh = Bihash[1 => 2, 2=> 3]
bh[2] # => 3 ... but could equal 1 as well
found an edge case with rehash. Hash#rehash merges two pairs if their keys become equal, but not if their values become equal. Therefore the following code leaves a Bihash in an inconsistent state:
b = Bihash.new
key_a = ["a"]
key_b = ["b"]
key_c = ["c"]
b[key_a] = key_b
b[key_c] = key_c
key_a[0] = "c" # make key_a equal to key_c
b.rehash # @forward.size is now 1, but @reverse.size is still 2
puts b[["b"]].inspect # => ["c"] # found in @reverse
puts b[["c"]].inspect # => ["c"] # found in @forward
Bihash[1 => :one] == Bihash[:one => 1] should be true
Also fix #hash, #eql?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.