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:

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.concat([6, 8])
s1.subset? s2 # => false
s2.subset? s1 # => true

Included Modules

Defined in:

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

Constructors

Instance Method Summary

Instance methods inherited from module Iterable(T)

chunk(reuse = false, &block : T -> U) forall U chunk, chunk_while(reuse : Bool | Array(T) = false, &block : T, T -> B) forall B chunk_while, cycle(n)
cycle
cycle
, each each, each_cons(count : Int, reuse = false) each_cons, each_slice(count : Int, reuse = false) each_slice, each_with_index(offset = 0) each_with_index, each_with_object(obj) each_with_object, slice_after(reuse : Bool | Array(T) = false, &block : T -> B) forall B
slice_after(pattern, reuse : Bool | Array(T) = false)
slice_after
, slice_before(reuse : Bool | Array(T) = false, &block : T -> B) forall B
slice_before(pattern, reuse : Bool | Array(T) = false)
slice_before
, slice_when(reuse : Bool | Array(T) = false, &block : T, T -> B) forall B slice_when

Instance methods inherited from module Enumerable(T)

all?(&)
all?(pattern)
all?
all?
, any?(&)
any?(pattern)
any?
any?
, chunks(&block : T -> U) forall U chunks, compact_map(&) compact_map, count(&)
count(item)
count
, cycle(n, &)
cycle(&)
cycle
, each(&block : T -> _) each, each_cons(count : Int, reuse = false, &) each_cons, each_cons_pair(& : T, T -> _) : Nil each_cons_pair, each_slice(count : Int, reuse = false, &) each_slice, each_with_index(offset = 0, &) each_with_index, each_with_object(obj, &) each_with_object, empty? empty?, find(if_none = nil, &) find, first(count : Int)
first
first
, first? first?, flat_map(&block : T -> Array(U) | Iterator(U) | U) forall U flat_map, grep(pattern) grep, group_by(&block : T -> U) forall U group_by, in_groups_of(size : Int, filled_up_with : U = nil) forall U
in_groups_of(size : Int, filled_up_with : U = nil, reuse = false, &) forall U
in_groups_of
, includes?(obj) includes?, index(&)
index(obj)
index
, index_by(&block : T -> U) forall U index_by, join(separator = "", &)
join(separator, io, &)
join(separator = "")
join(separator, io)
join
, map(&block : T -> U) forall U map, map_with_index(offset = 0, &block : T, Int32 -> U) forall U map_with_index, max max, max? max?, max_by(&block : T -> U) forall U max_by, max_by?(&block : T -> U) forall U max_by?, max_of(&block : T -> U) forall U max_of, max_of?(&block : T -> U) forall U max_of?, min min, min? min?, min_by(&block : T -> U) forall U min_by, min_by?(&block : T -> U) forall U min_by?, min_of(&block : T -> U) forall U min_of, min_of?(&block : T -> U) forall U min_of?, minmax minmax, minmax? minmax?, minmax_by(&block : T -> U) forall U minmax_by, minmax_by?(&block : T -> U) forall U minmax_by?, minmax_of(&block : T -> U) forall U minmax_of, minmax_of?(&block : T -> U) forall U minmax_of?, none?
none?(&)
none?(pattern)
none?
, one?(&)
one?(pattern)
one?
one?
, partition(&) partition, product
product(initial : Number)
product(initial : Number, &)
product(&)
product
, reduce(memo, &)
reduce(&)
reduce
, reduce?(&) reduce?, reject(pattern)
reject(type : U.class) forall U
reject(&block : T -> )
reject
, select(pattern)
select(type : U.class) forall U
select(&block : T -> )
select
, size size, skip(count : Int) skip, skip_while(&) skip_while, sum(initial)
sum
sum(initial, &)
sum(&)
sum
, take_while(&) take_while, tally : Hash(T, Int32) tally, to_a to_a, to_h
to_h(&block : T -> Tuple(K, V)) forall K, V
to_h
, to_set to_set, zip(*others : Indexable | Iterable | Iterator, &)
zip(*others : Indexable | Iterable | Iterator)
zip
, zip?(*others : Indexable | Iterable | Iterator, &)
zip?(*others : Indexable | Iterable | Iterator)
zip?

Instance methods inherited from struct Struct

==(other) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io : IO) : Nil to_s

Instance methods inherited from struct Value

==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup

Instance methods inherited from class Object

! : Bool !, !=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, as(type : Class) as, as?(type : Class) as?, class class, dup dup, hash(hasher)
hash
hash
, in?(*values : Object) : Bool
in?(collection) : Bool
in?
, inspect : String
inspect(io : IO) : Nil
inspect
, is_a?(type : Class) : Bool is_a?, itself itself, nil? : Bool nil?, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, responds_to?(name : Symbol) : Bool responds_to?, tap(&) tap, to_json(io : IO)
to_json
to_json
, to_pretty_json(io : IO, indent : String = " ")
to_pretty_json(indent : String = " ")
to_pretty_json
, to_s : String
to_s(io : IO) : Nil
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&) try, unsafe_as(type : T.class) forall T unsafe_as

Class methods inherited from class Object

from_json(string_or_io, root : String)
from_json(string_or_io)
from_json
, from_yaml(string_or_io : String | IO) from_yaml

Constructor Detail

def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node) #

[View source]
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 set from the elements in enumerable.

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

[View source]
def self.new(pull : JSON::PullParser) #

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

Creates a new, empty Set.

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

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


[View source]
def self.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node, &) #

[View source]

Instance Method Detail

def &(other : Set) #

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

Set{1, 1, 3, 5} & Set{1, 2, 3}               # => Set{1, 3}
Set{'a', 'b', 'b', 'z'} & Set{'a', 'b', 'c'} # => Set{'a', 'b'}

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

Addition: returns a new set containing the unique elements from both sets.

Set{1, 1, 2, 3} + Set{3, 4, 5} # => Set{1, 2, 3, 4, 5}

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

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

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

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

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

Set{1, 2, 3, 4, 5} - Set{2, 4}               # => Set{1, 3, 5}
Set{'a', 'b', 'b', 'z'} - Set{'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{1, 5} == Set{1, 5} # => true

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

Same as #includes?.

It is for convenience with using on case statement.

red_like = Set{"red", "pink", "violet"}
blue_like = Set{"blue", "azure", "violet"}

case "violet"
when red_like & blue_like
  puts "red & blue like color!"
when red_like
  puts "red like color!"
when blue_like
  puts "blue like color!"
end

See also: Object#===.


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

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

Set{1, 2, 3, 4, 5} ^ Set{2, 4, 6}            # => Set{1, 3, 5, 6}
Set{'a', 'b', 'b', 'z'} ^ Set{'a', 'b', 'c'} # => Set{'z', 'c'}

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

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

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

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

Adds object to the set and returns self.

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

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

Adds object to the set and returns true on success and false if the value was already in the set.

s = Set{1, 5}
s.add? 8 # => true
s.add? 8 # => false

[View source]
def clear #

Removes all elements in the set, and returns self.

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

[View source]
def clone #

Returns a new Set with all of the elements cloned.


[View source]
def compare_by_identity #

Makes this set compare objects using their object identity (object_id) for types that define such method (Reference types, but also structs that might wrap other Reference types and delegate the object_id method to them).

s = Set{"foo", "bar"}
s.includes?("fo" + "o") # => true

s.compare_by_identity
s.compare_by_identity?  # => true
s.includes?("fo" + "o") # => false # not the same String instance

[View source]
def compare_by_identity? #

Returns true of this Set is comparing objects by object_id.

See #compare_by_identity.


[View source]
def concat(elems) #

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

s = Set{1, 5}
s.concat [5, 5, 8, 9]
s.size # => 4

See also: #| to merge two sets and return a new one.


[View source]
def delete(object) #

Removes the object from the set and returns self.

s = Set{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(&) #

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(hasher) #

[View source]
def includes?(object) #

Returns true if object exists in the set.

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

[View source]
def inspect(io : IO) : Nil #

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 pretty_print(pp) : Nil #

[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{1, 5}.proper_subset? Set{1, 3, 5}    # => true
Set{1, 3, 5}.proper_subset? Set{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{1, 3, 5}.proper_superset? Set{1, 5}    # => true
Set{1, 3, 5}.proper_superset? Set{1, 3, 5} # => false

[View source]
def size #

Returns the number of elements in the set.

s = Set{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{1, 5}.subset? Set{1, 3, 5}    # => true
Set{1, 3, 5}.subset? Set{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{'a', 'b', 'b', 'z'}.subtract Set{'a', 'b', 'c'} # => Set{'z'}
Set{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{1, 3, 5}.superset? Set{1, 5}    # => true
Set{1, 3, 5}.superset? Set{1, 3, 5} # => true

[View source]
def to_a #

Returns the elements as an Array.

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

[View source]
def to_json(json : JSON::Builder) #

[View source]
def to_s(io : IO) : Nil #

Writes a string representation of the set to io.


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

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

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

Set{1, 1, 3, 5} | Set{1, 2, 3}               # => Set{1, 3, 5, 2}
Set{'a', 'b', 'b', 'z'} | Set{'a', 'b', 'c'} # => Set{'a', 'b', 'z', 'c'}

See also: #concat to add elements from a set to self.


[View source]