Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_algorithm_setops.d

raw
1 @safe unittest
2 {
3 import std.algorithm.setops;
4
5 import std.algorithm.searching : canFind;
6 import std.range;
7 import std.typecons : tuple;
8
9 auto N = sequence!"n"(0); // the range of natural numbers
10 auto N2 = cartesianProduct(N, N); // the range of all pairs of natural numbers
11
12 // Various arbitrary number pairs can be found in the range in finite time.
13 assert(canFind(N2, tuple(0, 0)));
14 assert(canFind(N2, tuple(123, 321)));
15 assert(canFind(N2, tuple(11, 35)));
16 assert(canFind(N2, tuple(279, 172)));
17 }
18
19 @safe unittest
20 {
21 import std.algorithm.setops;
22
23 import std.algorithm.searching : canFind;
24 import std.typecons : tuple;
25
26 auto B = [ 1, 2, 3 ];
27 auto C = [ 4, 5, 6 ];
28 auto BC = cartesianProduct(B, C);
29
30 foreach (n; [[1, 4], [2, 4], [3, 4], [1, 5], [2, 5], [3, 5], [1, 6],
31 [2, 6], [3, 6]])
32 {
33 assert(canFind(BC, tuple(n[0], n[1])));
34 }
35 }
36
37 @safe unittest
38 {
39 import std.algorithm.setops;
40
41 import std.algorithm.comparison : equal;
42 import std.typecons : tuple;
43
44 auto A = [ 1, 2, 3 ];
45 auto B = [ 'a', 'b', 'c' ];
46 auto C = [ "x", "y", "z" ];
47 auto ABC = cartesianProduct(A, B, C);
48
49 assert(ABC.equal([
50 tuple(1, 'a', "x"), tuple(1, 'a', "y"), tuple(1, 'a', "z"),
51 tuple(1, 'b', "x"), tuple(1, 'b', "y"), tuple(1, 'b', "z"),
52 tuple(1, 'c', "x"), tuple(1, 'c', "y"), tuple(1, 'c', "z"),
53 tuple(2, 'a', "x"), tuple(2, 'a', "y"), tuple(2, 'a', "z"),
54 tuple(2, 'b', "x"), tuple(2, 'b', "y"), tuple(2, 'b', "z"),
55 tuple(2, 'c', "x"), tuple(2, 'c', "y"), tuple(2, 'c', "z"),
56 tuple(3, 'a', "x"), tuple(3, 'a', "y"), tuple(3, 'a', "z"),
57 tuple(3, 'b', "x"), tuple(3, 'b', "y"), tuple(3, 'b', "z"),
58 tuple(3, 'c', "x"), tuple(3, 'c', "y"), tuple(3, 'c', "z")
59 ]));
60 }
61
62 @system unittest
63 {
64 import std.algorithm.setops;
65
66 import std.typecons : tuple, Tuple;
67
68 // Figure which number can be found in most arrays of the set of
69 // arrays below.
70 double[][] a =
71 [
72 [ 1, 4, 7, 8 ],
73 [ 1, 7 ],
74 [ 1, 7, 8],
75 [ 4 ],
76 [ 7 ],
77 ];
78 auto b = new Tuple!(double, uint)[1];
79 // it will modify the input range, hence we need to create a duplicate
80 largestPartialIntersection(a.dup, b);
81 // First member is the item, second is the occurrence count
82 assert(b[0] == tuple(7.0, 4u));
83 // 7.0 occurs in 4 out of 5 inputs, more than any other number
84
85 // If more of the top-frequent numbers are needed, just create a larger
86 // tgt range
87 auto c = new Tuple!(double, uint)[2];
88 largestPartialIntersection(a, c);
89 assert(c[0] == tuple(1.0, 3u));
90 // 1.0 occurs in 3 inputs
91
92 // multiset
93 double[][] x =
94 [
95 [1, 1, 1, 1, 4, 7, 8],
96 [1, 7],
97 [1, 7, 8],
98 [4, 7],
99 [7]
100 ];
101 auto y = new Tuple!(double, uint)[2];
102 largestPartialIntersection(x.dup, y);
103 // 7.0 occurs 5 times
104 assert(y[0] == tuple(7.0, 5u));
105 // 1.0 occurs 6 times
106 assert(y[1] == tuple(1.0, 6u));
107 }
108
109 @system unittest
110 {
111 import std.algorithm.setops;
112
113 import std.typecons : tuple, Tuple;
114
115 // Figure which number can be found in most arrays of the set of
116 // arrays below, with specific per-element weights
117 double[][] a =
118 [
119 [ 1, 4, 7, 8 ],
120 [ 1, 7 ],
121 [ 1, 7, 8],
122 [ 4 ],
123 [ 7 ],
124 ];
125 auto b = new Tuple!(double, uint)[1];
126 double[double] weights = [ 1:1.2, 4:2.3, 7:1.1, 8:1.1 ];
127 largestPartialIntersectionWeighted(a, b, weights);
128 // First member is the item, second is the occurrence count
129 assert(b[0] == tuple(4.0, 2u));
130 // 4.0 occurs 2 times -> 4.6 (2 * 2.3)
131 // 7.0 occurs 3 times -> 4.4 (3 * 1.1)
132
133 // multiset
134 double[][] x =
135 [
136 [ 1, 1, 1, 4, 7, 8 ],
137 [ 1, 7 ],
138 [ 1, 7, 8],
139 [ 4 ],
140 [ 7 ],
141 ];
142 auto y = new Tuple!(double, uint)[1];
143 largestPartialIntersectionWeighted(x, y, weights);
144 assert(y[0] == tuple(1.0, 5u));
145 // 1.0 occurs 5 times -> 1.2 * 5 = 6
146 }
147
148 @system unittest
149 {
150 import std.algorithm.setops;
151
152 import std.algorithm.comparison : equal;
153
154 double[][] a =
155 [
156 [ 1, 4, 7, 8 ],
157 [ 1, 7 ],
158 [ 1, 7, 8],
159 [ 4 ],
160 [ 7 ],
161 ];
162 auto witness = [
163 1, 1, 1, 4, 4, 7, 7, 7, 7, 8, 8
164 ];
165 assert(equal(multiwayMerge(a), witness));
166
167 double[][] b =
168 [
169 // range with duplicates
170 [ 1, 1, 4, 7, 8 ],
171 [ 7 ],
172 [ 1, 7, 8],
173 [ 4 ],
174 [ 7 ],
175 ];
176 // duplicates are propagated to the resulting range
177 assert(equal(multiwayMerge(b), witness));
178 }
179
180 @system unittest
181 {
182 import std.algorithm.setops;
183
184 import std.algorithm.comparison : equal;
185
186 // sets
187 double[][] a =
188 [
189 [ 1, 4, 7, 8 ],
190 [ 1, 7 ],
191 [ 1, 7, 8],
192 [ 4 ],
193 [ 7 ],
194 ];
195
196 auto witness = [1, 4, 7, 8];
197 assert(equal(multiwayUnion(a), witness));
198
199 // multisets
200 double[][] b =
201 [
202 [ 1, 1, 1, 4, 7, 8 ],
203 [ 1, 7 ],
204 [ 1, 7, 7, 8],
205 [ 4 ],
206 [ 7 ],
207 ];
208 assert(equal(multiwayUnion(b), witness));
209
210 double[][] c =
211 [
212 [9, 8, 8, 8, 7, 6],
213 [9, 8, 6],
214 [9, 8, 5]
215 ];
216 auto witness2 = [9, 8, 7, 6, 5];
217 assert(equal(multiwayUnion!"a > b"(c), witness2));
218 }
219
220 @safe unittest
221 {
222 import std.algorithm.setops;
223
224 import std.algorithm.comparison : equal;
225 import std.range.primitives : isForwardRange;
226
227 //sets
228 int[] a = [ 1, 2, 4, 5, 7, 9 ];
229 int[] b = [ 0, 1, 2, 4, 7, 8 ];
230 assert(equal(setDifference(a, b), [5, 9]));
231 static assert(isForwardRange!(typeof(setDifference(a, b))));
232
233 // multisets
234 int[] x = [1, 1, 1, 2, 3];
235 int[] y = [1, 1, 2, 4, 5];
236 auto r = setDifference(x, y);
237 assert(equal(r, [1, 3]));
238 assert(setDifference(r, x).empty);
239 }
240
241 @safe unittest
242 {
243 import std.algorithm.setops;
244
245 import std.algorithm.comparison : equal;
246
247 // sets
248 int[] a = [ 1, 2, 4, 5, 7, 9 ];
249 int[] b = [ 0, 1, 2, 4, 7, 8 ];
250 int[] c = [ 0, 1, 4, 5, 7, 8 ];
251 assert(equal(setIntersection(a, a), a));
252 assert(equal(setIntersection(a, b), [1, 2, 4, 7]));
253 assert(equal(setIntersection(a, b, c), [1, 4, 7]));
254
255 // multisets
256 int[] d = [ 1, 1, 2, 2, 7, 7 ];
257 int[] e = [ 1, 1, 1, 7];
258 assert(equal(setIntersection(a, d), [1, 2, 7]));
259 assert(equal(setIntersection(d, e), [1, 1, 7]));
260 }
261
262 @safe unittest
263 {
264 import std.algorithm.setops;
265
266 import std.algorithm.comparison : equal;
267 import std.range.primitives : isForwardRange;
268
269 // sets
270 int[] a = [ 1, 2, 4, 5, 7, 9 ];
271 int[] b = [ 0, 1, 2, 4, 7, 8 ];
272 assert(equal(setSymmetricDifference(a, b), [0, 5, 8, 9][]));
273 static assert(isForwardRange!(typeof(setSymmetricDifference(a, b))));
274
275 //mutisets
276 int[] c = [1, 1, 1, 1, 2, 2, 2, 4, 5, 6];
277 int[] d = [1, 1, 2, 2, 2, 2, 4, 7, 9];
278 assert(equal(setSymmetricDifference(c, d), setSymmetricDifference(d, c)));
279 assert(equal(setSymmetricDifference(c, d), [1, 1, 2, 5, 6, 7, 9]));
280 }
281