Permalink
Browse files

Start on promoting mutable versions out of experimental. Still needs …

…tests and to wrap results in mutable variants where appropriate
  • Loading branch information...
1 parent 365be74 commit bebd212ca038eaf65764fa8e7967baa367dbe8e2 @harukizaemon harukizaemon committed Jan 9, 2013
View
@@ -6,4 +6,5 @@
require 'hamster/hash'
require 'hamster/set'
require 'hamster/vector'
+require 'hamster/mutable_hash'
require 'hamster/version'
@@ -1,6 +1,6 @@
require 'forwardable'
require 'hamster/queue'
-require 'hamster/experimental/read_copy_update'
+require 'hamster/read_copy_update'
module Hamster
@@ -22,10 +22,10 @@ def enqueue(item)
def dequeue
head = nil
- transform { |queue|
+ transform do |queue|
head = queue.head
queue.dequeue
- }
+ end
head
end
@@ -1,6 +1,6 @@
require 'forwardable'
require 'hamster/set'
-require 'hamster/experimental/read_copy_update'
+require 'hamster/read_copy_update'
module Hamster
@@ -21,10 +21,10 @@ def add(item)
def add?(item)
added = false
- transform { |set|
+ transform do |set|
added = !set.include?(item)
set.add(item)
- }
+ end
added
end
@@ -34,10 +34,10 @@ def delete(item)
def delete?(item)
deleted = false
- transform { |set|
+ transform do |set|
deleted = set.include?(item)
set.delete(item)
- }
+ end
deleted
end
@@ -1,6 +1,6 @@
require 'forwardable'
require 'hamster/stack'
-require 'hamster/experimental/read_copy_update'
+require 'hamster/read_copy_update'
module Hamster
@@ -22,10 +22,10 @@ def push(item)
def pop
top = nil
- transform { |stack|
+ transform do |stack|
top = stack.peek
stack.pop
- }
+ end
top
end
def_delegator :self, :pop, :dequeue
@@ -1,5 +1,6 @@
+require 'forwardable'
require 'hamster/hash'
-require 'hamster/experimental/read_copy_update'
+require 'hamster/read_copy_update'
module Hamster
@@ -9,23 +10,26 @@ def self.mutable_hash(pairs = {}, &block)
class MutableHash
+ extend Forwardable
+
include ReadCopyUpdate
def put(key, value = Undefined, &block)
- old_value = nil
- transform { |hash|
- old_value = hash.get(key)
- hash.put(key, value, &block)
- }
- old_value
+ transform { |hash| hash.put(key, value, &block) }
+ end
+
+ def store(key, value)
+ put(key, value)
+ value
end
+ def_delegator :self, :store, :[]=
def delete(key)
old_value = nil
- transform { |hash|
+ transform do |hash|
old_value = hash.get(key)
hash.delete(key)
- }
+ end
old_value
end
@@ -17,10 +17,15 @@ def eql?(other)
end
def_delegator :self, :eql?, :==
+ def_delegator :@content, :inspect
+ def_delegator :@content, :to_s
+
protected
def transform
- @lock.synchronize { @content = yield(@content) }
+ @lock.synchronize do
+ @content = yield(@content)
+ end
self
end

0 comments on commit bebd212

Please sign in to comment.