class SetC(T)
- SetC(T)
- Reference
- Object
Overview
SetC
implements a collection of unordered values with no duplicates.
An Enumerable
object can be converted to SetC
using the #to_setc
method.
SetC
uses Hash
as storage, so you must note the following points:
- Equality of elements is determined according to
Object#==
andObject#hash
. SetC
assumes that the identity of each element does not change while it is stored. Modifying an element of a setc will render the setc to an unreliable state.
Example
s1 = SetC{1, 2}
s2 = [1, 2].to_setc
s3 = SetC.new [1, 2]
s1 == s2 # => true
s1 == s3 # => true
s1.add(2)
s1.concat([6, 8])
s1.subset? s2 # => false
s2.subset? s1 # => true
Included Modules
- Enumerable(T)
- Iterable(T)
Defined in:
setc.crConstructors
-
.new(other : Indexable(T))
Optimized version of
.new
used when other is also anIndexable
-
.new(enumerable : Enumerable(T))
Creates a new setc from the elements in enumerable.
-
.new(initial_capacity = nil)
Creates a new, empty
SetC
.
Instance Method Summary
-
#&(other : SetC)
Intersection: returns a new setc containing elements common to both setcs.
-
#-(other : SetC)
Difference: returns a new setc containing elements in this setc that are not present in the other.
-
#-(other : Enumerable)
Difference: returns a new setc containing elements in this setc that are not present in the other enumerable.
-
#<<(object : T)
Alias for
#add
-
#==(other : SetC)
Returns
true
if both setcs have the same elements. -
#^(other : SetC(U)) forall U
Symmetric Difference: returns a new setc
(self - other) | (other - self)
. -
#^(other : Enumerable(U)) forall U
Symmetric Difference: returns a new setc
(self - other) | (other - self)
. -
#|(other : SetC(U)) forall U
Union: returns a new setc containing all unique elements from both setcs.
-
#add(object : T)
Adds object to the setc and returns
self
. -
#clear
Removes all elements in the setc, and returns
self
. -
#clone
Returns a new
SetC
with all of the elements cloned. -
#concat(elems)
Adds
#each
element of elems to the setc and returnsself
. -
#delete(object)
Removes the object from the setc and returns
self
. -
#dup
Returns a new
SetC
with all of the same elements. -
#each(&)
Yields each element of the setc, and returns
self
. -
#each
Returns an iterator for each element of the setc.
-
#empty?
Returns
true
if the setc is empty. -
#hash
Generates an
UInt64
hash value for this object. -
#includes?(object)
Returns
true
if object exists in the setc. -
#inspect(io)
Alias of
#to_s
. -
#intersects?(other : SetC)
Returns
true
if the setc and the given setc have at least one element in common. - #pretty_print(pp) : Nil
-
#proper_subset?(other : SetC)
Returns
true
if the setc is a proper subset of the other setc. -
#proper_superset?(other : SetC)
Returns
true
if the setc is a superset of the other setc. -
#size
Returns the number of elements in the setc.
-
#subset?(other : SetC)
Returns
true
if the setc is a subset of the other setc. -
#subtract(other : Enumerable)
Returns
self
after removing from it those elements that are present in the given enumerable. -
#superset?(other : SetC)
Returns
true
if the setc is a superset of the other setc. -
#to_a
Returns the elements as an
Array
. -
#to_s(io)
Writes a string representation of the setc to io.
Instance methods inherited from module Enumerable(T)
to_setc
to_setc
Constructor Detail
Creates a new setc from the elements in enumerable.
a = [1, 3, 5]
s = SetC.new a
s.empty? # => false
Creates a new, empty SetC
.
s = SetC(Int32).new
s.empty? # => true
An initial capacity can be specified, and it will be setc as the initial capacity
of the internal Hash
.
Instance Method Detail
Intersection: returns a new setc containing elements common to both setcs.
SetC{1, 1, 3, 5} & SetC{1, 2, 3} # => SetC{1, 3}
SetC{'a', 'b', 'b', 'z'} & SetC{'a', 'b', 'c'} # => SetC{'a', 'b'}
Difference: returns a new setc containing elements in this setc that are not present in the other.
SetC{1, 2, 3, 4, 5} - SetC{2, 4} # => SetC{1, 3, 5}
SetC{'a', 'b', 'b', 'z'} - SetC{'a', 'b', 'c'} # => SetC{'z'}
Difference: returns a new setc containing elements in this setc that are not present in the other enumerable.
SetC{1, 2, 3, 4, 5} - [2, 4] # => SetC{1, 3, 5}
SetC{'a', 'b', 'b', 'z'} - ['a', 'b', 'c'] # => SetC{'z'}
Returns true
if both setcs have the same elements.
SetC{1, 5} == SetC{1, 5} # => true
Symmetric Difference: returns a new setc (self - other) | (other - self)
.
Equivalently, returns (self | other) - (self & other)
.
SetC{1, 2, 3, 4, 5} ^ SetC{2, 4, 6} # => SetC{1, 3, 5, 6}
SetC{'a', 'b', 'b', 'z'} ^ SetC{'a', 'b', 'c'} # => SetC{'z', 'c'}
Symmetric Difference: returns a new setc (self - other) | (other - self)
.
Equivalently, returns (self | other) - (self & other)
.
SetC{1, 2, 3, 4, 5} ^ [2, 4, 6] # => SetC{1, 3, 5, 6}
SetC{'a', 'b', 'b', 'z'} ^ ['a', 'b', 'c'] # => SetC{'z', 'c'}
Union: returns a new setc containing all unique elements from both setcs.
SetC{1, 1, 3, 5} | SetC{1, 2, 3} # => SetC{1, 3, 5, 2}
SetC{'a', 'b', 'b', 'z'} | SetC{'a', 'b', 'c'} # => SetC{'a', 'b', 'z', 'c'}
See also: #concat
to add elements from a setc to self
.
Adds object to the setc and returns self
.
s = SetC{1, 5}
s.includes? 8 # => false
s << 8
s.includes? 8 # => true
Removes all elements in the setc, and returns self
.
s = SetC{1, 5}
s.size # => 2
s.clear
s.size # => 0
Adds #each
element of elems to the setc and returns self
.
s = SetC{1, 5}
s.concat [5, 5, 8, 9]
s.size # => 4
See also: #|
to merge two setcs and return a new one.
Removes the object from the setc and returns self
.
s = SetC{1, 5}
s.includes? 5 # => true
s.delete 5
s.includes? 5 # => false
Returns true
if the setc is empty.
s = SetC(Int32).new
s.empty? # => true
s << 3
s.empty? # => false
Generates an UInt64
hash value for this object.
This method must have the property that a == b
implies a.hash == b.hash
.
The hash value is used along with #==
by the Hash
class to determine if two objects
reference the same hash key.
Subclasses must not override this method. Instead, they must define hash(hasher)
,
though usually the macro def_hash
can be used to generate this method.
Returns true
if object exists in the setc.
s = SetC{1, 5}
s.includes? 5 # => true
s.includes? 9 # => false
Returns true
if the setc and the given setc have at least one element in
common.
SetC{1, 2, 3}.intersects? SetC{4, 5} # => false
SetC{1, 2, 3}.intersects? SetC{3, 4} # => true
Returns true
if the setc is a proper subset of the other setc.
This setc must have fewer elements than the other setc, and all of elements in this setc must be present in the other setc.
SetC{1, 5}.proper_subset? SetC{1, 3, 5} # => true
SetC{1, 3, 5}.proper_subset? SetC{1, 3, 5} # => false
Returns true
if the setc is a superset of the other setc.
The other must have the same or fewer elements than this setc, and all of elements in the other setc must be present in this setc.
SetC{1, 3, 5}.proper_superset? SetC{1, 5} # => true
SetC{1, 3, 5}.proper_superset? SetC{1, 3, 5} # => false
Returns true
if the setc is a subset of the other setc.
This setc must have the same or fewer elements than the other setc, and all of elements in this setc must be present in the other setc.
SetC{1, 5}.subset? SetC{1, 3, 5} # => true
SetC{1, 3, 5}.subset? SetC{1, 3, 5} # => true
Returns self
after removing from it those elements that are present in
the given enumerable.
SetC{'a', 'b', 'b', 'z'}.subtract SetC{'a', 'b', 'c'} # => SetC{'z'}
SetC{1, 2, 3, 4, 5}.subtract [2, 4, 6] # => SetC{1, 3, 5}
Returns true
if the setc is a superset of the other setc.
The other must have the same or fewer elements than this setc, and all of elements in the other setc must be present in this setc.
SetC{1, 3, 5}.superset? SetC{1, 5} # => true
SetC{1, 3, 5}.superset? SetC{1, 3, 5} # => true