Struct Set

Set structure

struct Set(K) ;

Methods

NameDescription
add add element to set
create Fill set from range
difference create difference of two sets
intersection create intersection of two sets
iterate iterate over items
join join other set to this set
length number of items in set
opBinaryRight if element present in set
remove remove element from set

Example

import std.stdio;

Set!string s;
s.add("hello");
assert(s.length == 1);
assert(equal(s.iterate, only("hello")));
s.remove("hello");
assert(s.length == 0);
s.remove("hello");
assert(s.length == 0);

s.create(only("hello", "hello", "world"));
assert(s.length == 2);

s.join(set(only("and", "bye")));
assert(s.length == 4);

auto other = set(only("and", "bye", "!"));
auto cross0 = s.intersection(other);
assert("bye" in cross0);
assert("!"  !in cross0);
auto cross1 = other.intersection(s);
assert(cross0.length == cross1.length);
assert("and" in cross0 && "and" in cross1);
assert("bye" in cross0 && "bye" in cross1);

auto nums = set(iota(10));
auto someNums = nums.difference(set(only(1,2,3)));
assert(0 in someNums);
assert(1 !in someNums);

bool f(const Set!string s) {
    return "yes" in s;
}

Set!string ss;
ss.add("yes");
f(ss);
assert("yes" in ss);