struct Set(T)

Overview

Set implements a collection of unordered values with no duplicates.

An Enumerable object can be converted to Set using the #to_set method.

Set uses Hash as storage, so you must note the following points:

Object#hash.

stored. Modifying an element of a set will render the set to an unreliable state.

Example

s1 = Set{1, 2}
s2 = [1, 2].to_set
s3 = Set.new [1, 2]
s1 == s2         # => true
s1 == s3         # => true
s1.add(2)
s1.merge([6,8])
s1.subset? s2    # => false
s2.subset? s1    # => true

Included Modules

Defined in:

set.cr
json/to_json.cr
yaml/to_yaml.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from module Iterable

cycle
cycle(n)
cycle
, each each, each_cons(count : Int) each_cons, each_slice(count : Int) each_slice, each_with_index(offset = 0) each_with_index, each_with_object(obj) each_with_object

Instance methods inherited from module Enumerable(T)

all?
all?(&block)
all?
, any?
any?(&block)
any?
, compact_map(&block) compact_map, count(item)
count(&block)
count
, cycle(&block)
cycle(n, &block)
cycle
, each(&block : T -> _) each, each_cons(count : Int, &block) each_cons, each_slice(count : Int, &block) each_slice, each_with_index(offset = 0, &block) each_with_index, each_with_object(obj, &block) each_with_object, find(if_none = nil, &block) find, first
first(count : Int)
first
, first? first?, flat_map(&block : T -> Array(U)) flat_map, grep(pattern) grep, group_by(&block : T -> U) group_by, in_groups_of(size : Int, filled_up_with : U = nil, &block)
in_groups_of(size : Int, filled_up_with : U = nil)
in_groups_of
, includes?(obj) includes?, index(obj)
index(&block)
index
, index_by(&block : T -> U) index_by, join(separator = "")
join(separator, io, &block)
join(separator = "", &block)
join(separator, io)
join
, map(&block : T -> U) map, map_with_index(&block : T, Int32 -> U) map_with_index, max max, max? max?, max_by(&block : T -> U) max_by, max_by?(&block : T -> U) max_by?, max_of(&block : T -> U) max_of, max_of?(&block : T -> U) max_of?, min min, min? min?, min_by(&block : T -> U) min_by, min_by?(&block : T -> U) min_by?, min_of(&block : T -> U) min_of, min_of?(&block : T -> U) min_of?, minmax minmax, minmax? minmax?, minmax_by(&block : T -> U) minmax_by, minmax_by?(&block : T -> U) minmax_by?, minmax_of(&block : T -> U) minmax_of, minmax_of?(&block : T -> U) minmax_of?, none?(&block)
none?
none?
, one?(&block) one?, partition(&block) partition, product(initial : Number, &block)
product
product(initial : Number)
product(&block)
product
, reduce(memo, &block)
reduce(&block)
reduce
, reject(&block : T -> ) reject, select(&block : T -> ) select, size size, skip(count : Int) skip, skip_while(&block) skip_while, sum
sum(initial)
sum(&block)
sum(initial, &block)
sum
, take_while(&block) take_while, to_a to_a, to_h to_h, to_set to_set

Instance methods inherited from struct Struct

==(other : self) : Bool ==, hash : Int32 hash, inspect(io : IO) : Nil inspect, to_s(io) to_s

Instance methods inherited from struct Value

==(other) ==

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other)
===(other : YAML::Any)
===(other : JSON::Any)
===
, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO)
inspect
, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try

Class methods inherited from class Object

==(other : Class) ==, ===(other) ===, cast(other) : self cast, from_json(string_or_io) : self from_json, from_yaml(string : String) : self from_yaml, hash hash, inspect(io) inspect, name : String name, to_s(io) to_s, |(other : U.class) |

Class Method Detail

def self.new(pull : JSON::PullParser) #

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

Creates a new set from the elements in enumerable

s = Set.new [1,3,5]
s.empty? => false

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

Creates a new, empty Set

s = Set(Int32).new
set.empty? # => true

An initial capacity can be specified, and it will be set as the initial capacity of the internal Hash.


[View source]

Instance Method Detail

def &(other : Set) #

Intersection: returns a new set containing elements common to both sets.

Set.new([1,1,3,5]) & Set.new([1,2,3])               #=> Set{1, 3}
Set.new(['a','b','b','z']) & Set.new(['a','b','c']) #=> Set{'a', 'b'}

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

Difference: returns a new set containing elements in this set that are not present in the other.

Set.new([1,2,3,4,5]) - Set.new([2,4])               #=> Set{1, 3, 5}
Set.new(['a','b','b','z']) - Set.new(['a','b','c']) #=> Set{'z'}

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

Difference: returns a new set containing elements in this set that are not present in the other enumerable.

Set.new([1,2,3,4,5]) - [2,4]               #=> Set{1, 3, 5}
Set.new(['a','b','b','z']) - ['a','b','c'] #=> Set{'z'}

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

Alias for #add


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

Returns true if both sets have the same elements

Set.new([1,5]) == Set.new([1,5]) # => true

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

Symmetric Difference: returns a new set (self - other) | (other - self). Equivalently, returns (self | other) - (self & other).

Set.new([1,2,3,4,5]) ^ [2,4,6]             #=> Set{1, 3, 5, 6}
Set.new(['a','b','b','z']) ^ ['a','b','c'] #=> Set{'z', 'c'}

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

Symmetric Difference: returns a new set (self - other) | (other - self). Equivalently, returns (self | other) - (self & other).

Set.new([1,2,3,4,5]) ^ Set.new([2,4,6])             #=> Set{1, 3, 5, 6}
Set.new(['a','b','b','z']) ^ Set.new(['a','b','c']) #=> Set{'z', 'c'}

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

Adds object to the set and returns self

s = Set.new [1,5]
s.includes? 8     # => false
s << 8
s.includes? 8     # => true

[View source]
def clear #

Removes all elements in the set, and returns self.

s = Set.new [1,5]
s.size  # => 2
s.clear
s.size  # => 0

[View source]
def delete(object) #

Removes the object from the set and returns self.

s = Set.new [1,5]
s.includes? 5  # => true
s.delete 5
s.includes? 5  # => false

[View source]
def dup #

Returns a new set with all of the same elements


[View source]
def each #

Returns an iterator for each element of the set.


[View source]
def each(&block) #

Yields each element of the set, and returns self.


[View source]
def empty? #

Returns true if the set is empty.

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

[View source]
def hash #

[View source]
def includes?(object) #

Returns true if object exists in the set.

s = Set.new [1,5]
s.includes? 5  # => true
s.includes? 9  # => false

[View source]
def inspect(io) #

Alias of #to_s


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

Returns true if the set and the given set have at least one element in common.

Set{1, 2, 3}.intersects? Set{4, 5} # => false
Set{1, 2, 3}.intersects? Set{3, 4} # => true

[View source]
def merge(elems) #

Adds #each element of elems to the set and returns self.

s = Set.new [1,5]
s.merge [5,5,8,9]
s.size            # => 4

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

Returns true if the set is a proper subset of the other set

This set must have fewer elements than the other set, and all of elements in this set must be present in the other set.

Set.new([1,5]).subset? Set.new([1,3,5])   # => true
Set.new([1,3,5]).subset? Set.new([1,3,5]) # => false

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

Returns true if the set is a superset of the other set

The other must have the same or fewer elements than this set, and all of elements in the other set must be present in this set.

Set.new([1,3,5]).superset? Set.new([1,5])   # => true
Set.new([1,3,5]).superset? Set.new([1,3,5]) # => false

[View source]
def size #

Returns the number of elements in the set.

s = Set.new [1,5]
s.size  # => 2

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

Returns true if the set is a subset of the other set

This set must have the same or fewer elements than the other set, and all of elements in this set must be present in the other set.

Set.new([1,5]).subset? Set.new([1,3,5])   # => true
Set.new([1,3,5]).subset? Set.new([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.

Set.new(['a','b','b','z']).subtract Set.new(['a','b','c']) #=> Set{'z'}
Set.new([1,2,3,4,5]).subtract [2,4,6] #=> Set{1, 3, 5}

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

Returns true if the set is a superset of the other set

The other must have the same or fewer elements than this set, and all of elements in the other set must be present in this set.

Set.new([1,3,5]).superset? Set.new([1,5])   # => true
Set.new([1,3,5]).superset? Set.new([1,3,5]) # => true

[View source]
def to_a #

Returns the elements as an array

Set.new([1,5]).to_a  # => [1,5]

[View source]
def to_json(io) #

[View source]
def to_s(io) #

Writes a string representation of the set to io


[View source]
def to_yaml(yaml : YAML::Generator) #

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

Union: returns a new set containing all unique elements from both sets.

Set.new([1,1,3,5]) | Set.new([1,2,3])               #=> Set{1, 3, 5, 2}
Set.new(['a','b','b','z']) | Set.new(['a','b','c']) #=> Set{'a', 'b', 'z', 'c'}

[View source]