1 /**
2  * Associative Array utility functions
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.utils.aa;
15 
16 import std.algorithm;
17 import std.range;
18 import std.typecons;
19 
20 // ***************************************************************************
21 
22 /// Get a value from an AA, and throw an exception (not an error) if not found
23 ref auto aaGet(AA, K)(AA aa, K key)
24 	if (is(typeof(key in aa)))
25 {
26 	import std.conv;
27 
28 	auto p = key in aa;
29 	if (p)
30 		return *p;
31 	else
32 		static if (is(typeof(text(key))))
33 			throw new Exception("Absent value: " ~ text(key));
34 		else
35 			throw new Exception("Absent value");
36 }
37 
38 /// If key is not in aa, add it with defaultValue.
39 /// Returns a reference to the value corresponding to key.
40 ref V getOrAdd(K, V)(ref V[K] aa, K key, V defaultValue = V.init)
41 {
42 	auto p = key in aa;
43 	if (!p)
44 	{
45 		aa[key] = defaultValue;
46 		p = key in aa;
47 	}
48 	return *p;
49 }
50 
51 unittest
52 {
53 	int[int] aa;
54 	aa.getOrAdd(1, 2) = 3;
55 	assert(aa[1] == 3);
56 	assert(aa.getOrAdd(1, 4) == 3);
57 }
58 
59 struct KeyValuePair(K, V) { K key; V value; }
60 
61 /// Get key/value pairs from AA
62 KeyValuePair!(K, V)[] pairs(K, V)(V[K] aa)
63 {
64 	KeyValuePair!(K, V)[] result;
65 	foreach (key, value; aa)
66 		result ~= KeyValuePair!(K, V)(key, value);
67 	return result;
68 }
69 
70 /// Get key/value pairs from AA, sorted by keys
71 KeyValuePair!(K, V)[] sortedPairs(K, V)(V[K] aa)
72 {
73 	KeyValuePair!(K, V)[] result;
74 	foreach (key; aa.keys.sort)
75 		result ~= KeyValuePair!(K, V)(key, aa[key]);
76 	return result;
77 }
78 
79 /// Get values from AA, sorted by keys
80 V[] sortedValues(K, V)(in V[K] aa)
81 {
82 	V[] result;
83 	foreach (key; aa.keys.sort())
84 		result ~= aa[key];
85 	return result;
86 }
87 
88 /// Merge source into target. Return target.
89 V[K] merge(K, V)(auto ref V[K] target, in V[K] source)
90 {
91 	foreach (k, v; source)
92 		target[k] = v;
93 	return target;
94 }
95 
96 unittest
97 {
98 	int[int] target;
99 	int[int] source = [2:4];
100 	merge(target, source);
101 	assert(source == target);
102 
103 	target = [1:1, 2:2, 3:3];
104 	merge(target, source);
105 	assert(target == [1:1, 2:4, 3:3]);
106 
107 	assert(merge([1:1], [2:2]) == [1:1, 2:2]);
108 }
109 
110 /// Slurp a range of two elements (or two-element struct/class) into an AA.
111 auto toAA(R)(R r)
112 	if (is(typeof(r.front[1])))
113 {
114 	alias K = typeof(r.front[0]);
115 	alias V = typeof(r.front[1]);
116 	V[K] result;
117 	foreach (pair; r)
118 	{
119 		assert(pair.length == 2);
120 		result[pair[0]] = pair[1];
121 	}
122 	return result;
123 }
124 
125 /// ditto
126 auto toAA(R)(R r)
127 	if (is(typeof(r.front.tupleof)) && r.front.tupleof.length == 2 && !is(typeof(r.front[1])))
128 {
129 	return r.map!(el => tuple(el.tupleof)).toAA();
130 }
131 
132 unittest
133 {
134 	assert([[2, 4]].toAA() == [2:4]);
135 	assert([2:4].pairs.toAA() == [2:4]);
136 }
137 
138 // ***************************************************************************
139 
140 /// An associative array which retains the order in which elements were added.
141 struct OrderedMap(K, V)
142 {
143 	K[] keys;
144 	V[] values;
145 	size_t[K] index;
146 
147 	ref inout(V) opIndex()(auto ref K k) inout
148 	{
149 		return values[index[k]];
150 	}
151 
152 	ref V opIndexAssign()(auto ref V v, auto ref K k)
153 	{
154 		auto pi = k in index;
155 		if (pi)
156 		{
157 			auto pv = &values[*pi];
158 			*pv = v;
159 			return *pv;
160 		}
161 
162 		index[k] = values.length;
163 		keys ~= k;
164 		values ~= v;
165 		return values[$-1];
166 	}
167 
168 	ref V getOrAdd()(auto ref K k)
169 	{
170 		auto pi = k in index;
171 		V* pv;
172 		if (pi)
173 			pv = &values[*pi];
174 		else
175 		{
176 			index[k] = values.length;
177 			keys ~= k;
178 			values ~= V.init;
179 			pv = &values[$-1];
180 		}
181 		return *pv;
182 	}
183 
184 	ref V opIndexUnary(string op)(auto ref K k)
185 	{
186 		auto pv = &getOrAdd(k);
187 		mixin("(*pv) " ~ op ~ ";");
188 		return *pv;
189 	}
190 
191 	ref V opIndexOpAssign(string op)(auto ref V v, auto ref K k)
192 	{
193 		auto pv = &getOrAdd(k);
194 		mixin("(*pv) " ~ op ~ "= v;");
195 		return *pv;
196 	}
197 
198 	inout(V) get()(auto ref K k, inout(V) defaultValue) inout
199 	{
200 		auto p = k in index;
201 		return p ? values[*p] : defaultValue;
202 	}
203 
204 	inout(V)* opIn_r()(auto ref K k) inout
205 	{
206 		auto p = k in index;
207 		return p ? &values[*p] : null;
208 	}
209 
210 	void remove()(auto ref K k)
211 	{
212 		auto i = index[k];
213 		index.remove(k);
214 		keys = keys.remove(i);
215 		values = values.remove(i);
216 	}
217 
218 	@property size_t length() const { return values.length; }
219 
220 	int opApply(int delegate(ref K k, ref V v) dg)
221 	{
222 		int result = 0;
223 
224 		foreach (i, ref v; values)
225 		{
226 			result = dg(keys[i], v);
227 			if (result)
228 				break;
229 		}
230 		return result;
231 	}
232 }
233 
234 unittest
235 {
236 	OrderedMap!(string, int) m;
237 	m["a"] = 1;
238 	m["b"] = 2;
239 	m["c"] = 3;
240 	assert(m.length == 3);
241 	assert("a" in m);
242 	assert("d" !in m);
243 	m.remove("a");
244 	assert(m.length == 2);
245 	m["x"] -= 1;
246 	assert(m["x"] == -1);
247 	++m["y"];
248 	assert(m["y"] == 1);
249 }
250 
251 // ***************************************************************************
252 
253 /// Helper/wrapper for void[0][T]
254 struct HashSet(T)
255 {
256 	void[0][T] data;
257 
258 	alias data this;
259 
260 	this(R)(R r)
261 	{
262 		foreach (k; r)
263 			add(k);
264 	}
265 
266 	void add(T k)
267 	{
268 		void[0] v;
269 		data[k] = v;
270 	}
271 
272 	void remove(T k)
273 	{
274 		data.remove(k);
275 	}
276 
277 	@property HashSet!T dup() const
278 	{
279 		// Can't use .dup with void[0] value
280 		HashSet!T result;
281 		foreach (k, v; data)
282 			result.add(k);
283 		return result;
284 	}
285 
286 	int opApply(scope int delegate(ref T) dg)
287 	{
288 		int result;
289 		foreach (k, v; data)
290 			if ((result = dg(k)) != 0)
291 				break;
292 		return result;
293 	}
294 }
295 
296 unittest
297 {
298 	HashSet!int s;
299 	assert(s.length == 0);
300 	assert(!(1 in s));
301 	assert(1 !in s);
302 	s.add(1);
303 	assert(1 in s);
304 	assert(s.length == 1);
305 	foreach (k; s)
306 		assert(k == 1);
307 	s.remove(1);
308 	assert(s.length == 0);
309 
310 	s.add(1);
311 	auto t = s.dup;
312 	s.add(2);
313 	assert(t.length==1);
314 	t.remove(1);
315 	assert(t.length==0);
316 }
317 
318 auto toSet(R)(R r)
319 {
320 	alias E = ElementType!R;
321 	return HashSet!E(r);
322 }
323 
324 unittest
325 {
326 	auto set = [1, 2, 3].toSet();
327 	assert(2 in set);
328 	assert(4 !in set);
329 }
330 
331 // ***************************************************************************
332 
333 /// An object which acts mostly as an associative array,
334 /// with the added property of being able to hold keys with
335 /// multiple values. These are only exposed explicitly and
336 /// through iteration
337 struct MultiAA(K, V)
338 {
339 	V[][K] items;
340 
341 	/// If multiple items with this name are present,
342 	/// only the first one is returned.
343 	ref inout(V) opIndex(K key) inout
344 	{
345 		return items[key][0];
346 	}
347 
348 	V opIndexAssign(V value, K key)
349 	{
350 		items[key] = [value];
351 		return value;
352 	}
353 
354 	inout(V)* opIn_r(K key) inout @nogc
355 	{
356 		auto pvalues = key in items;
357 		if (pvalues && (*pvalues).length)
358 			return &(*pvalues)[0];
359 		return null;
360 	}
361 
362 	void remove(K key)
363 	{
364 		items.remove(key);
365 	}
366 
367 	// D forces these to be "ref"
368 	int opApply(int delegate(ref K key, ref V value) dg)
369 	{
370 		int ret;
371 		outer:
372 		foreach (key, values; items)
373 			foreach (ref value; values)
374 			{
375 				ret = dg(key, value);
376 				if (ret)
377 					break outer;
378 			}
379 		return ret;
380 	}
381 
382 	// Copy-paste because of https://issues.dlang.org/show_bug.cgi?id=7543
383 	int opApply(int delegate(ref const(K) key, ref const(V) value) dg) const
384 	{
385 		int ret;
386 		outer:
387 		foreach (key, values; items)
388 			foreach (ref value; values)
389 			{
390 				ret = dg(key, value);
391 				if (ret)
392 					break outer;
393 			}
394 		return ret;
395 	}
396 
397 	void add(K key, V value)
398 	{
399 		if (key !in items)
400 			items[key] = [value];
401 		else
402 			items[key] ~= value;
403 	}
404 
405 	V get(K key, lazy V def) const
406 	{
407 		auto pvalue = key in this;
408 		return pvalue ? *pvalue : def;
409 	}
410 
411 	inout(V)[] getAll(K key) inout
412 	{
413 		inout(V)[] result;
414 		foreach (ref value; items.get(key, null))
415 			result ~= value;
416 		return result;
417 	}
418 
419 	this(typeof(null) Null)
420 	{
421 	}
422 
423 	this(V[K] aa)
424 	{
425 		foreach (ref key, ref value; aa)
426 			add(key, value);
427 	}
428 
429 	this(V[][K] aa)
430 	{
431 		foreach (ref key, values; aa)
432 			foreach (ref value; values)
433 				add(key, value);
434 	}
435 
436 	@property auto keys() inout { return items.keys; }
437 
438 	// https://issues.dlang.org/show_bug.cgi?id=14626
439 
440 	@property V[] values()
441 	{
442 		return items.byValue.join;
443 	}
444 
445 	@property const(V)[] values() const
446 	{
447 		return items.byValue.join;
448 	}
449 
450 	@property typeof(V[K].init.pairs) pairs()
451 	{
452 		alias Pair = typeof(V[K].init.pairs[0]);
453 		Pair[] result;
454 		result.reserve(length);
455 		foreach (ref k, ref v; this)
456 			result ~= Pair(k, v);
457 		return result;
458 	}
459 
460 	@property size_t length() const { return items.byValue.map!(item => item.length).sum(); }
461 
462 	auto byKey() { return items.byKey(); }
463 	auto byValue() { return items.byValue().joiner(); }
464 
465 	bool opCast(T)() inout
466 		if (is(T == bool))
467 	{
468 		return !!items;
469 	}
470 
471 	/// Warning: discards repeating items
472 	V[K] opCast(T)() const
473 		if (is(T == V[K]))
474 	{
475 		V[K] result;
476 		foreach (key, value; this)
477 			result[key] = value;
478 		return result;
479 	}
480 
481 	V[][K] opCast(T)() inout
482 		if (is(T == V[][K]))
483 	{
484 		V[][K] result;
485 		foreach (k, v; this)
486 			result[k] ~= v;
487 		return result;
488 	}
489 }
490 
491 unittest
492 {
493 	MultiAA!(string, string) aa;
494 }