struct Pf::Set(T)
- Pf::Set(T)
- Struct
- Value
- Object
Overview
A thread-safe, persistent, unordered set.
See also: Map
.
Included Modules
Defined in:
permafrost/set.crConstructors
- .additive_identity : self
-
.new(enumerable : Enumerable(T)) : Set(T)
Returns a new set containing elements from enumerable.
-
.new : Set(T)
Returns a new, empty set.
Class Method Summary
-
.[](*elements)
Returns a new set with the given elements.
-
.transaction(& : Commit(T) -> ) : Set(T)
Shorthand for
new.transaction
.
Instance Method Summary
-
#&(other : Set(T))
Returns a new set containing elements common to this and other sets.
-
#+(other) : Set(T)
Shorthand for
#concat
. -
#-(other : Set(T)) : Set(T)
Returns a new set containing elements in this set that are not present in other.
-
#==(other : Set)
Returns
true
if the sets are equal. -
#===(object : T) : Bool
Same as
Set#===
. -
#add(element : T) : Set(T)
Returns a copy of this set that includes element.
-
#concat(other : Enumerable(T)) : Set(T)
Returns a copy of this set that also includes elements from other enumerable.
-
#delete(element : T) : Set(T)
Returns a copy of this set that is guaranteed not to include element.
-
#each(& : T -> ) : Nil
Yields each element from this set.
-
#empty? : Bool
Returns
true
if this set is empty. -
#fmap(& : T -> U) : Set(U) forall U
Yields each element from this set to the block and constructs a new set from block return results.
-
#hash(hasher)
See
Object#hash(hasher)
. -
#includes?(element : T) : Bool
Returns
true
if element is present in this set. -
#inspect(io : IO) : Nil
Alias of
#to_s
. - #pretty_print(pp) : Nil
-
#reject(& : T -> ) : Set(T)
Returns a new set that includes only elements for which the block is falsey.
-
#same?(other : Set(T))
Returns
true
ifself
and other refer to the same set in memory. -
#select(& : T -> ) : Set(T)
Returns a new set that includes only elements for which the block is truthy.
-
#size : Int32
Returns the number of elements in this set.
- #to_s(io)
-
#transaction(& : Commit(T) -> ) : Set(T)
Yields a
Commit
object which you can populate by multiple edits of this set.
Instance methods inherited from module Enumerable(T)
to_pf_bidi
to_pf_bidi,
to_pf_map(& : T -> Tuple(K, V)) : Pf::Map(K, V) forall K, Vto_pf_map to_pf_map, to_pf_set : Pf::Set(T) to_pf_set
Constructor Detail
Returns a new set containing elements from enumerable.
set = Pf::Set.new([1, 2, 3])
set # => Pf::Set[1, 2, 3]
Returns a new, empty set.
set = Pf::Set(Int32).new
set.empty? => true
Class Method Detail
Returns a new set with the given elements.
The type of the returned set is the union of the types of elements.
set = Pf::Set[1, "Hello", 3, true]
set # => Pf::Set[1, "Hello", 3, true]
typeof(set) # => Pf::Set(String | Int32 | Bool)
Instance Method Detail
Returns a new set containing elements common to this and other sets.
a = Pf::Set[1, 2, 3]
b = Pf::Set[4, 5, 1, 6, 2]
a & b # => Pf::Set[1, 2]
Returns a new set containing elements in this set that are not present in other.
Supports value equality.
Pf::Set[:a, :b, :c, :d] - Pf::Set[:a, :c] # => Pf::Set[:b, :d]
Same as Set#===
.
reds = Pf::Set["red", "pink", "violet"]
blues = Pf::Set["blue", "azure", "violet"]
both = red = blue = false
case "violet"
when reds & blues
both = true
when reds
red = true
when blues
blue = true
end
both # => true
red # => false
blue # => false
Returns a copy of this set that includes element.
Supports value equality.
set = Pf::Set[100, 200]
set.add(300) # => Pf::Set[100, 200, 300]
set.add(400) # => Pf::Set[100, 200, 400]
Returns a copy of this set that also includes elements from other enumerable.
Supports value equality.
a = Pf::Set[1, 2, 3]
a.concat([4, 5, 1, 2]) # => Pf::Set[1, 2, 3, 4, 5]
Returns a copy of this set that is guaranteed not to include element.
Supports value equality.
set = Pf::Set[100, 200, 300]
set.delete(100) # => Pf::Set[200, 300]
set.delete(200) # => Pf::Set[100, 300]
Returns true
if this set is empty.
set = Pf::Set(Int32).new
set.empty? # => true
newset = set.add(100)
newset.empty? # => false
Yields each element from this set to the block and constructs a new set from block return results.
Supports value equality if T == U
.
There is no shortcut in terms of performance. Even if all elements produced
by the block are already in this set, a new set is created and populated
anyway. We do keep track of changes, and if none were made return self
;
the new set is then simply discarded.
set = Pf::Set[1, 2, 3]
set.fmap(&.succ.to_s) # => Pf::Set["2", "3", "4"]
set.fmap(&.succ.pred).same?(set) # => true
Returns true
if element is present in this set.
set = Pf::Set[1, 2, 3]
1.in?(set) # => true
2.in?(set) # => true
3.in?(set) # => true
100.in?(set) # => false
"foobar".in?(set) # => false
Returns a new set that includes only elements for which the block is falsey.
Supports value equality.
set = (0...10).to_pf_set
set.reject(&.even?) # => Pf::Set[1, 3, 5, 7, 9]
Returns true
if self
and other refer to the same set in memory.
Due to the way Set
is implemented, this method can be used as
a cheap way to detect changes.
set1 = Pf::Set[1, 2, 3]
set2 = set1.add(1).add(2).add(3)
set1.same?(set2) # => true
Returns a new set that includes only elements for which the block is truthy.
Supports value equality.
set = (0...10).to_pf_set
set.select(&.even?) # => Pf::Set[0, 2, 4, 6, 8]
Yields a Commit
object which you can populate by multiple edits of
this set. Applies the commit to a copy of this set. Returns the copy.
-
The commit object is marked as resolved after the block. You should not retain it. If you do, all operations on the object (including readonly ones) will raise
ResolvedError
. -
If you pass the commit object to another fiber in the block, e.g. via a channel, and fiber yield immediately after that, the commit obviously would not be marked as resolved as the resolution code would not have been reached yet. However, if you then attempt to call mutation methods on the commit, another error,
ReadonlyError
, will be raised. In other words, the yielded commit object is readonly for any other fiber except for the fiber that it was originally yielded to.
Returns self
if the transaction did not touch the set. If the set was
changed but then the changes were reverted this method will return a new
set (one equal to self
).
set1 = Pf::Set[1, 2, 3]
set2 = set1.transaction do |commit|
commit.add(4)
commit.delete(2) if 4.in?(commit)
if 2.in?(commit)
commit.delete(4)
commit.add(6)
else
commit.delete(4)
commit.add(2)
commit.add(5)
end
end
set1 # => Pf::Set[1, 2, 3]
set2 # => Pf::Set[1, 2, 3, 5]