Class: Meangirls::TwoPhaseSet

Inherits:
Set
  • Object
show all
Defined in:
lib/meangirls/two_phase_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Set

#&, #+, #-, #===, #each, #empty?, #size, #to_a, #|

Methods inherited from CRDT

merge, #to_json

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

#aObject

Returns the value of attribute a.



6
7
8
# File 'lib/meangirls/two_phase_set.rb', line 6

def a
  @a
end

#rObject

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

.biasesObject



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_jsonObject



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

#biasObject



48
49
50
# File 'lib/meangirls/two_phase_set.rb', line 48

def bias
  'r'
end

#cloneObject



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

Returns:

  • (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_setObject



85
86
87
# File 'lib/meangirls/two_phase_set.rb', line 85

def to_set
  @a - @r
end

#typeObject



89
90
91
# File 'lib/meangirls/two_phase_set.rb', line 89

def type
  '2p-set'
end