Skip to content

Commit

Permalink
Refactoring, fixed bug in empty?
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdowad committed Feb 22, 2012
1 parent 5372ff9 commit e270e6a
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions ruby-threads/concurrent_hash.rb
Expand Up @@ -48,34 +48,22 @@ def include?(k); read_hash(k) { |h| h.include? k }; end
def has_key?(k); read_hash(k) { |h| h.has_key? k }; end
def has_value?(k); read_hash(k) { |h| h.has_value? k }; end

def key(v)
read_each_hash do |h|
if key = h.key(v)
return key
end
end
false
end
def assoc(k)
read_each_hash do |h|
if pair = h.assoc(k)
return pair
end
end
false
end
def rassoc(v)
read_each_hash do |h|
if pair = h.rassoc(v)
return pair
['key','assoc','rassoc'] do |method|
class_eval <<DEF
def #{method}(x)
read_each_hash do |h|
if result = h.#{method}(x)
return result
end
end
false
end
nil
DEF
end

def empty?
read_each_hash { |h| return true if h.empty? }
false
read_each_hash { |h| return false if not h.empty? }
true
end

# Benchmark these iterators and compare performance with the implementation
Expand Down

0 comments on commit e270e6a

Please sign in to comment.