Class: Meangirls::Set

Inherits:
CRDT
  • Object
show all
Includes:
Enumerable
Defined in:
lib/meangirls/set.rb

Direct Known Subclasses

LWWSet, ORSet, TwoPhaseSet

Instance Method Summary collapse

Methods inherited from CRDT

merge, #to_json

Instance Method Details

#&(other) ⇒ Object

Return a copy of self where [all elements not present in other] have been deleted.



17
18
19
20
21
22
# File 'lib/meangirls/set.rb', line 17

def &(other)
  (to_set - other.to_set).inject(clone) do |copy, e|
    copy.delete e
    copy
  end
end

#+(other) ⇒ Object

Add all elements of other to a copy of self.



25
26
27
28
29
# File 'lib/meangirls/set.rb', line 25

def +(other)
  other.inject(clone) do |copy, e|
    copy << e
  end
end

#-(other) ⇒ Object

Remove all elements of other from a copy of self.



32
33
34
35
36
37
# File 'lib/meangirls/set.rb', line 32

def -(other)
  other.inject(clone) do |copy, e|
    copy.delete e
    copy
  end
end

#===(other) ⇒ Object

Loose equality: present elements of each set are equal



40
41
42
# File 'lib/meangirls/set.rb', line 40

def ===(other)
  to_set == other.to_set
end

#each(&block) ⇒ Object

Iterate over all present elements



45
46
47
# File 'lib/meangirls/set.rb', line 45

def each(&block)
  to_set.each(&block)
end

#empty?Boolean

Are there no elements present?

Returns:

  • (Boolean)


50
51
52
# File 'lib/meangirls/set.rb', line 50

def empty?
  size == 0
end

#sizeObject

How many elements are present in the set?



55
56
57
# File 'lib/meangirls/set.rb', line 55

def size
  to_set.size
end

#to_aObject

Convert to an array



60
61
62
# File 'lib/meangirls/set.rb', line 60

def to_a
  to_set.to_a
end

#|(other) ⇒ Object

Add all elements of other to a copy of self.



9
10
11
12
13
# File 'lib/meangirls/set.rb', line 9

def |(other)
  other.inject(clone) do |copy, e|
    copy << e
  end
end