intersectedWith

@safe
intersectedWith
(
C1
C2
)
(
C1 x
,
auto ref C2 y
)
if (
isInstanceOf!(OpenHashMapOrSet, C1) &&
isInstanceOf!(OpenHashMapOrSet, C2)
)

Return Value

Type: auto

x eagerly intersected with y. TODO move to container_algorithm.d.

Examples

exercise opEquals

alias K = Nullable!(ulong, ulong.max);
alias X = OpenHashSet!(K, FNV!(64, true));

const n = 100;

X a;
foreach (const i_; 0 .. n)
{
    const i = 1113*i_;           // insert in order
    assert(!a.contains(K(i)));
    assert(!a.containsUsingLinearSearch(K(i)));
    assert(a.insertAndReturnElement(K(i)) == K(i));
    assert(a.contains(K(i)));
    assert(a.containsUsingLinearSearch(K(i)));
}

X b;
foreach (const i_; 0 .. n)
{
    const i = 1113*(n - 1 - i_);   // insert in reverse
    assert(!b.contains(K(i)));
    assert(!b.containsUsingLinearSearch(K(i)));
    assert(b.insertAndReturnElement(K(i)) == K(i));
    assert(b.contains(K(i)));
    assert(b.containsUsingLinearSearch(K(i)));
}

assert(a == b);

// bin storage must be deterministic
() @trusted { assert(a._store != b._store); }();

string as key

1 version(showEntries) dbg();
2 import nxt.digestx.fnv : FNV;
3 
4 alias X = OpenHashSet!(string, FNV!(64, true));
5 debug static assert(!mustAddGCRange!X);
6 debug static assert(X.sizeof == 24); // dynamic arrays also `hasAddressLikeKey`
7 
8 auto x = X();
9 
10 auto testEscapeShouldFail()() @safe pure
11 {
12     X x;
13     x.insert("a");
14     return x.byElement;
15 }
16 
17 auto testEscapeShouldFailFront()() @safe pure
18 {
19     X x;
20     x.insert("a");
21     return x.byElement.front;
22 }
23 
24 assert(&"a"[0] is &"a"[0]); // string literals are store in common place
25 
26 const aa = "aa";
27 
28 // string slices are equal when elements are equal regardless of position
29 // (.ptr) in memory
30 assert(x.insertAndReturnElement(aa[0 .. 1]) !is "a");
31 x.insert(aa[0 .. 1]);
32 assert(x.insertAndReturnElement(aa[0 .. 1]) is aa[0 .. 1]);
33 assert(x.contains(aa[1 .. 2]));
34 assert(x.containsUsingLinearSearch(aa[1 .. 2]));
35 
36 const(char)[] aa_ = "aa";
37 assert(x.contains(aa_[1 .. 2]));
38 assert(x.containsUsingLinearSearch(aa_[1 .. 2]));
39 assert(aa_[1 .. 2] in x);
40 
41 char[2] aa__; aa__ = "aa";
42 assert(x.contains(aa__[1 .. 2]));
43 assert(x.containsUsingLinearSearch(aa__[1 .. 2]));
44 assert(aa__[1 .. 2] in x);
45 
46 const bb = "bb";
47 
48 assert(x.insertAndReturnElement(bb[0 .. 1]) is bb[0 .. 1]); // returns newly added ref
49 assert(x.insertAndReturnElement(bb[0 .. 1]) !is "b");       // return other ref not equal new literal
50 x.insert(bb[0 .. 1]);
51 assert(x.contains(bb[1 .. 2]));
52 assert(x.containsUsingLinearSearch(bb[1 .. 2]));
53 
54 x.remove(aa[0 .. 1]);
55 assert(!x.contains(aa[1 .. 2]));
56 assert(!x.containsUsingLinearSearch(aa[1 .. 2]));
57 assert(x.contains(bb[1 .. 2]));
58 assert(x.containsUsingLinearSearch(bb[1 .. 2]));
59 
60 x.remove(bb[0 .. 1]);
61 assert(!x.contains(bb[1 .. 2]));
62 assert(!x.containsUsingLinearSearch(bb[1 .. 2]));
63 
64 x.insert("a");
65 x.insert("b");
66 assert(x.contains("a"));
67 assert(x.containsUsingLinearSearch("a"));
68 assert(x.contains("b"));
69 assert(x.containsUsingLinearSearch("b"));
70 
71 debug static assert(!__traits(compiles, { testEscapeShouldFail(); } ));
72 // TODO this should fail:
73 // TODO debug static assert(!__traits(compiles, { testEscapeShouldFailFront(); } ));

string as key

version(showEntries) dbg();
import nxt.digestx.fnv : FNV;
alias X = OpenHashSet!(string, FNV!(64, true));
auto x = X();

char[2] cc = "cc";          // mutable chars
assert(x.insertAndReturnElement(cc[]) !is cc[]); // will allocate new slice

const cc_ = "cc";           // immutable chars
assert(x.insertAndReturnElement(cc_[]) !is cc[]); // will not allocate

array container as value type

1 version(showEntries) dbg();
2 
3 import nxt.dynamic_array : Array = DynamicArray;
4 
5 alias K = Nullable!(uint, uint.max);
6 
7 alias VE = Nullable!(uint, uint.max);
8 alias V = OpenHashSet!(VE, FNV!(64, true));
9 
10 debug static assert(!mustAddGCRange!V);
11 
12 foreach (X; AliasSeq!(OpenHashMap!(K, V, FNV!(64, true))))
13 {
14     const VE n = 600;
15 
16     auto x = X();
17 
18     {                       // scoped range
19         auto xkeys = x.byKey;
20         assert(xkeys.length == 0);
21         foreach (ref key; xkeys)
22         {
23             debug static assert(is(typeof(key) == const(K)));
24             assert(0);
25         }
26         foreach (ref key; X().byKey)
27         {
28             debug static assert(is(typeof(key) == const(K)));
29             assert(0);
30         }
31     }
32 
33     foreach (immutable i; 0 .. n)
34     {
35         assert(x.length == i);
36 
37         auto key = K(i);
38         auto value = V.withElements([VE(i)].s);
39 
40         x[key] = value.dup;
41         assert(x.length == i + 1);
42         assert(x.contains(key));
43         // TODO assert(x.containsUsingLinearSearch(key));
44         {
45             auto valuePtr = key in x;
46             assert(valuePtr && *valuePtr == value);
47         }
48 
49         x.remove(key);
50         assert(x.length == i);
51         assert(!x.contains(key));
52         assert(key !in x);
53 
54         x[key] = value.dup;
55         assert(x.length == i + 1);
56         assert(x.contains(key));
57         {
58             auto valuePtr = key in x;
59             assert(valuePtr && *valuePtr == value);
60         }
61     }
62 
63     assert(x is x);
64 
65     x = x.dup;
66 
67     auto y = x.dup;
68     assert(x !is y);
69     assert(x.length == y.length);
70 
71     assert(y == x);
72     assert(x == y);
73 
74     foreach (ref key; x.byKey)
75     {
76         assert(x.contains(key));
77     }
78 
79     foreach (ref keyValue; x.byKeyValue)
80     {
81         assert(x.contains(keyValue.key));
82         auto keyValuePtr = keyValue.key in x;
83         assert(keyValuePtr &&
84                *keyValuePtr == keyValue.value);
85     }
86 
87     foreach (immutable i; 0 .. n)
88     {
89         assert(x.length == n - i);
90 
91         auto key = K(i);
92         auto value = V.withElements([VE(i)].s);
93 
94         assert(x.contains(key));
95         {
96             auto valuePtr = key in x;
97             assert(valuePtr && *valuePtr == value);
98         }
99 
100         x.remove(key);
101         assert(!x.contains(key));
102         assert(key !in x);
103     }
104 
105     auto z = y.dup;
106     assert(y == z);
107 
108     /* remove all elements in `y` using `removeAllMatching` and all elements
109      * in `z` using `removeAllMatching` */
110     foreach (immutable i; 0 .. n)
111     {
112         assert(y.length == n - i);
113         assert(z.length == n - i);
114 
115         auto key = K(i);
116         auto value = V.withElements([VE(i)].s);
117 
118         assert(y.contains(key));
119         {
120             auto valuePtr = key in y;
121             assert(valuePtr && *valuePtr == value);
122         }
123         assert(z.contains(key));
124         {
125             auto valuePtr = key in z;
126             assert(valuePtr && *valuePtr == value);
127         }
128 
129         y.remove(key);
130         assert(z.removeAllMatching!((const scope ref element) => element.key is key) == 1);
131         assert(y == z);
132 
133         assert(!y.contains(key));
134         assert(!z.contains(key));
135 
136         assert(key !in y);
137         assert(key !in z);
138     }
139 }

r-value and l-value intersection

version(showEntries) dbg();
alias K = Nullable!(uint, uint.max);
alias X = OpenHashSet!(K, FNV!(64, true));

auto x = X();

{                           // scoped range
    foreach (ref xe; x.byElement) { assert(0); }
}

auto x0 = X.init;
assert(x0.length == 0);
assert(x0._store.length == 0);
assert(!x0.contains(K(1)));

auto x1 = X.withElements([K(12)].s);
assert(x1.length == 1);
assert(x1.contains(K(12)));

auto x2 = X.withElements([K(10), K(12)].s);
assert(x2.length == 2);
assert(x2.contains(K(10)));
assert(x2.contains(K(12)));

auto x3 = X.withElements([K(12), K(13), K(14)].s);
assert(x3.length == 3);
assert(x3.contains(K(12)));
assert(x3.contains(K(13)));
assert(x3.contains(K(14)));

auto z = X.withElements([K(10), K(12), K(13), K(15)].s);
assert(z.length == 4);
assert(z.contains(K(10)));
assert(z.contains(K(12)));
assert(z.contains(K(13)));
assert(z.contains(K(15)));

auto y = move(z).intersectedWith(x2);
assert(y.length == 2);
assert(y.contains(K(10)));
assert(y.contains(K(12)));
assert(y.containsUsingLinearSearch(K(10)));
assert(y.containsUsingLinearSearch(K(12)));

r-value and r-value intersection

version(showEntries) dbg();
alias K = Nullable!(uint, uint.max);
alias X = OpenHashSet!(K, FNV!(64, true));

auto y = X.withElements([K(10), K(12), K(13), K(15)].s).intersectedWith(X.withElements([K(12), K(13)].s));
assert(y.length == 2);
assert(y.contains(K(12)));
assert(y.contains(K(13)));
assert(y.containsUsingLinearSearch(K(12)));
assert(y.containsUsingLinearSearch(K(13)));

Meta