Sha256: a9e8ab8f4f2cf08f4c992859ad24f4db7c810bb07d400515ea6243d4a4ad9db0
Contents?: true
Size: 1.82 KB
Versions: 1
Compression:
Stored size: 1.82 KB
Contents
class FuzzyHash Version = '0.0.1' def initialize @hash = {} @regexes = [] @hash_reverse = {} @regexes_reverse = {} end def clear @hash.clear @regexes.clear @hash_reverse.clear @regexes_reverse.clear end def ==(o) o.instance_variable_get(:@hash) == @hash && o.instance_variable_get(:@regexes) == @regexes end def empty? @hash.empty? && @regexes.empty? end def keys @hash.keys + @regexes.collect{|r| r.first} end def values @hash.values + @regexes.collect{|r| r.last} end def each @hash.each{|k,v| yield k,v } @regexes.each{|v| yield v.first, v.last } end def delete_value(value) @hash.delete(@hash_reverse[value]) || ((rr = @regexes_reverse[value]) && @regexes.delete_at(rr[0])) end def []=(key, value) case key when Regexp @regexes << [key, value] @regex_test = nil @regexes_reverse[value] = [@regexes.size - 1, key, value] else @hash[key] = value @hash_reverse[value] = key end end def replace(src, dest) if @hash_reverse.key?(src) key = @hash_reverse[src] @hash[key] = dest @hash_reverse.delete(src) @hash_reverse[dest] = key elsif @regexes_reverse.key?(src) key = @regexes_reverse[src] @regexes[rkey[0]] = [rkey[1], dest] @regexes_reverse.delete(src) @regexes_reverse[dest] = [rkey[0], rkey[1], dest] end end def [](key) @hash[key] || regex_lookup(key) end private def regex_test @regex_test ||= Regexp.new(@regexes.collect{|r| "(#{r[0]})"} * '|') end def regex_lookup(key) if !@regexes.empty? && key.is_a?(String) && (data = regex_test.match(key)) (data_array = data.to_a).each_index do |i| break @regexes[i].last if data_array.at(i+1) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
joshbuddy-fuzzy_hash-0.0.1 | lib/fuzzy_hash.rb |