Class: Meangirls::TwoPhaseSet
- Defined in:
- lib/meangirls/two_phase_set.rb
Instance Attribute Summary collapse
-
#a ⇒ Object
Returns the value of attribute a.
-
#r ⇒ Object
Returns the value of attribute r.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(e) ⇒ Object
(also: #add)
Inserts e into the set.
-
#==(other) ⇒ Object
Strict equality: both adds and removes for both 2p-sets are equal.
- #as_json ⇒ Object
- #bias ⇒ Object
- #clone ⇒ Object
-
#delete(e) ⇒ Object
Deletes e from self.
- #include?(e) ⇒ Boolean
-
#initialize(hash = nil) ⇒ TwoPhaseSet
constructor
A new instance of TwoPhaseSet.
-
#merge(other) ⇒ Object
Merge with another 2p-set.
- #to_set ⇒ Object
- #type ⇒ Object
Methods inherited from Set
#&, #+, #-, #===, #each, #empty?, #size, #to_a, #|
Methods inherited from CRDT
Constructor Details
#initialize(hash = nil) ⇒ TwoPhaseSet
Returns a new instance of TwoPhaseSet.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/meangirls/two_phase_set.rb', line 7 def initialize(hash = nil) if hash raise ArgumentError, "hash must contain a" unless hash['a'] raise ArgumentError, "hash must contain r" unless hash['r'] @a = Set.new hash['a'] @r = Set.new hash['r'] else # Empty set @a = Set.new @r = Set.new end end |
Instance Attribute Details
#a ⇒ Object
Returns the value of attribute a.
6 7 8 |
# File 'lib/meangirls/two_phase_set.rb', line 6 def a @a end |
#r ⇒ Object
Returns the value of attribute r.
6 7 8 |
# File 'lib/meangirls/two_phase_set.rb', line 6 def r @r end |
Class Method Details
.biases ⇒ Object
2 3 4 |
# File 'lib/meangirls/two_phase_set.rb', line 2 def self.biases ['r'] end |
Instance Method Details
#<<(e) ⇒ Object Also known as: add
Inserts e into the set. Raises ReinsertNotAllowed if e was previously deleted.
23 24 25 26 27 28 29 30 |
# File 'lib/meangirls/two_phase_set.rb', line 23 def <<(e) if @r.include? e raise Meangirls::ReinsertNotAllowed end @a << e self end |
#==(other) ⇒ Object
Strict equality: both adds and removes for both 2p-sets are equal.
34 35 36 37 38 |
# File 'lib/meangirls/two_phase_set.rb', line 34 def ==(other) other.kind_of? self.class and a == other.a and r == other.r end |
#as_json ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/meangirls/two_phase_set.rb', line 40 def as_json { 'type' => type, 'a' => a.to_a, 'r' => r.to_a } end |
#bias ⇒ Object
48 49 50 |
# File 'lib/meangirls/two_phase_set.rb', line 48 def bias 'r' end |
#clone ⇒ Object
52 53 54 55 56 57 |
# File 'lib/meangirls/two_phase_set.rb', line 52 def clone c = super c.a = a.clone c.r = r.clone c end |
#delete(e) ⇒ Object
Deletes e from self. Raises DeleteNotAllowed if e does not presently exist.
61 62 63 64 65 66 67 |
# File 'lib/meangirls/two_phase_set.rb', line 61 def delete(e) unless @a.include? e raise Meangirls::DeleteNotAllowed end @r << e e end |
#include?(e) ⇒ Boolean
81 82 83 |
# File 'lib/meangirls/two_phase_set.rb', line 81 def include?(e) @a.include? e and not @r.include? e end |
#merge(other) ⇒ Object
Merge with another 2p-set.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/meangirls/two_phase_set.rb', line 70 def merge(other) unless other.kind_of? self.class raise ArgumentError, "other must be a #{self.class}" end self.class.new( 'a' => (a | other.a), 'r' => (r | other.r) ) end |
#to_set ⇒ Object
85 86 87 |
# File 'lib/meangirls/two_phase_set.rb', line 85 def to_set @a - @r end |
#type ⇒ Object
89 90 91 |
# File 'lib/meangirls/two_phase_set.rb', line 89 def type '2p-set' end |