class SetC(T)

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:

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

Defined in:

setc.cr

Constructors

Instance Method Summary

Instance methods inherited from module Enumerable(T)

to_setc to_setc

Constructor Detail

def self.new(other : Indexable(T)) #

Optimized version of .new used when other is also an Indexable


[View source]
def self.new(enumerable : Enumerable(T)) #

Creates a new setc from the elements in enumerable.

a = [1, 3, 5]
s = SetC.new a
s.empty? # => false

[View source]
def self.new(initial_capacity = nil) #

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.


[View source]

Instance Method Detail

def &(other : SetC) #

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'}

[View source]
def -(other : SetC) #

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'}

[View source]
def -(other : Enumerable) #

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'}

[View source]
def <<(object : T) #

Alias for #add


[View source]
def ==(other : SetC) #

Returns true if both setcs have the same elements.

SetC{1, 5} == SetC{1, 5} # => true

[View source]
def ^(other : SetC(U)) forall U #

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'}

[View source]
def ^(other : Enumerable(U)) forall U #

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'}

[View source]
def |(other : SetC(U)) forall U #

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.


[View source]
def add(object : T) #

Adds object to the setc and returns self.

s = SetC{1, 5}
s.includes? 8 # => false
s << 8
s.includes? 8 # => true

[View source]
def clear #

Removes all elements in the setc, and returns self.

s = SetC{1, 5}
s.size # => 2
s.clear
s.size # => 0

[View source]
def clone #

Returns a new SetC with all of the elements cloned.


[View source]
def concat(elems) #

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.


[View source]
def delete(object) #

Removes the object from the setc and returns self.

s = SetC{1, 5}
s.includes? 5 # => true
s.delete 5
s.includes? 5 # => false

[View source]
def dup #

Returns a new SetC with all of the same elements.


[View source]
def each(&) #

Yields each element of the setc, and returns self.


[View source]
def each #

Returns an iterator for each element of the setc.


[View source]
def empty? #

Returns true if the setc is empty.

s = SetC(Int32).new
s.empty? # => true
s << 3
s.empty? # => false

[View source]
def hash #
Description copied from class Object

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.


[View source]
def includes?(object) #

Returns true if object exists in the setc.

s = SetC{1, 5}
s.includes? 5 # => true
s.includes? 9 # => false

[View source]
def inspect(io) #

Alias of #to_s.


[View source]
def intersects?(other : SetC) #

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

[View source]
def pretty_print(pp) : Nil #

[View source]
def proper_subset?(other : SetC) #

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

[View source]
def proper_superset?(other : SetC) #

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

[View source]
def size #

Returns the number of elements in the setc.

s = SetC{1, 5}
s.size # => 2

[View source]
def subset?(other : SetC) #

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

[View source]
def subtract(other : Enumerable) #

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}

[View source]
def superset?(other : SetC) #

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

[View source]
def to_a #

Returns the elements as an Array.

SetC{1, 5}.to_a # => [1,5]

[View source]
def to_s(io) #

Writes a string representation of the setc to io.


[View source]