-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathset.d
66 lines (51 loc) · 1.16 KB
/
set.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Written in the D programming language
/**
A simple set module.
License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
Authors: Philippe Sigaud
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
module dranges.set;
import std.conv,
std.exception;
///
struct Set(T) {
bool[T] data;
///
this(T t) {
put(t);
}
///
void put(T t) { data[t] = true;}
///
void put(T[] tarr) { foreach(t; tarr) put(t);}
///
bool opIn_r(T t) {
return (t in data) ? true : false;
}
///
size_t length() {
return data.length;
}
///
void remove(T t) {
enforce((t in data), "Value: " ~ to!string(t) ~ " is not in the set.");
data.remove(t);
}
///
string toString() {
return to!string(data.keys);
}
///
T[] array() {
return data.keys;
}
}
/// merge two sets together
Set!(T) fusion(T)(Set!(T) s1, Set!(T) s2) {
auto result = s1;
foreach(t; s2.array) result.put(t);
result.data.rehash;
return result;
}