std.set.Set
class pub Set[V: Equal[ref V] + Hash]
A hash set implemented using a Map.
The order of values in this Set are not guaranteed. For values to be stored in
a Set
they must implement the Hash
and Equal
traits.
Static methods
new
Show source codeHide source code
fn pub static new -> Set[V] {
Set(Map.new)
}
fn pub static new -> Set[V]
Instance methods
!=
Show source codeHide source code
fn pub !=(other: T) -> Bool {
(self == other).false?
}
fn pub !=(other: T) -> Bool
Returns true
if self
and the given object are not equal to each other.
==
Show source codeHide source code
fn pub ==(other: ref Set[V]) -> Bool {
if size != other.size { return false }
iter.all?(fn (val) { other.contains?(val) })
}
fn pub ==(other: ref Set[V]) -> Bool
Returns true
if self
and the given Set
are identical to each
other.
Examples
Comparing two Set
instances:
import std.set (Set)
let set1 = Set.new
let set2 = Set.new
set1.insert(10)
set2.insert(10)
set1 == set2 # => true
contains?
Show source codeHide source code
fn pub contains?(value: ref V) -> Bool {
@map.contains?(value)
}
fn pub contains?(value: ref V) -> Bool
Returns true
if this Set
contains the given value.
Examples
Checking if a Set
contains a value:
import std.set (Set)
let set = Set.new
set.contains?(10) # => false
set.insert(10)
set.contains?(10) # => true
fmt
Show source codeHide source code
fn pub fmt(formatter: mut Formatter) {
formatter.write('{')
iter.each_with_index(fn (index, value) {
if index > 0 { formatter.write(', ') }
value.fmt(formatter)
})
formatter.write('}')
}
fn pub fmt(formatter: mut Formatter)
if
V: Equal[ref V] + Hash + Format
Formats self
in a human-readable format for debugging purposes.
insert
Show source codeHide source code
fn pub mut insert(value: V) -> Bool {
@map.set(value, true).none?
}
fn pub mut insert(value: V) -> Bool
Inserts a new value into the Set
.
The returned value is true
if the value was inserted, false
otherwise.
Examples
Inserting a value into a Set
:
import std.set (Set)
let set = Set.new
set.insert(10)
iter
Show source codeHide source code
fn pub iter -> Stream[ref V] {
@map.keys
}
fn pub iter -> Stream[ref V]
Returns an Iter
over the values in this Set
.
Examples
Creating an Iter
for the values:
import std.set (Set)
let set = Set.new
set.insert(10)
set.insert(20)
set.iter.next # => 10
remove
Show source codeHide source code
fn pub mut remove(value: ref V) -> Bool {
@map.remove(value).some?
}
fn pub mut remove(value: ref V) -> Bool
Removes a value from this Set
.
If the value was removed true
is returned, otherwise false
is returned.
Examples
Removing an existing value from a Set
:
import std.set (Set)
let set = Set.new
set.insert(10)
set.remove(10) # => true
set.remove(10) # => false
Removing a non-existing value from a Set
:
import std.set (Set)
let set = Set.new
set.remove(10) # => false
size
Show source codeHide source code
fn pub size -> Int {
@map.size
}
fn pub size -> Int
Returns the number of values in this Set
.
Examples
Using an empty Set
:
import std.set (Set)
Set.new.size # => 0
Using a Set
with one value:
let set = Set.new
set.insert('Alice')
set.size # => 1
Implemented traits
Contains
impl Contains[V] for Set
Equal
impl Equal[ref Set[V]] for Set
Format
impl Format for Set
if
V: Equal[ref V] + Hash + Format