1 module collections.commons;
2 
3 import std.digest.murmurhash;
4 
5 ///Contains all possible basic attributes that might be used in a foreach situation
6 static immutable string[] attrList = ["@safe", "@nogc", "nothrow", "pure",
7 	"@nogc nothrow", "@nogc pure", "nothrow pure", "@nogc nothrow pure", "@safe nothrow",
8 	"@safe @nogc nothrow", "@safe @nogc pure", "@safe nothrow pure", "@safe @nogc nothrow pure"];
9 ///Generates overrides to be mixed in with regular code
10 string generateOverrides()(){
11 	string result;
12 	foreach (attr; attrList)
13 		result ~= Func(attr);
14 	return result;
15 }
16 ///Inserts code only if it compiles.
17 string ifCompiles(string code)
18 {
19     return "static if (__traits(compiles, " ~ code ~ ")) " ~ code ~ ";\n";
20 }
21 ///Thrown if an element is nog found
22 public class ElementNotFoundException : Exception {
23 	@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null) {
24 		super(msg, file, line, nextInChain);
25 	}
26 
27 	@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__)
28 	{
29 		super(msg, file, line, nextInChain);
30 	}
31 }
32 ///Thrown if incorrect arguments are passed to a function
33 public class IncorrectArgumentsException : Exception {
34 	@nogc @safe pure nothrow this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable nextInChain = null) {
35 		super(msg, file, line, nextInChain);
36 	}
37 
38 	@nogc @safe pure nothrow this(string msg, Throwable nextInChain, string file = __FILE__, size_t line = __LINE__)
39 	{
40 		super(msg, file, line, nextInChain);
41 	}
42 }
43 /**
44  * Standard digest function for hashing, using the MurMurHash3/32 algorithm
45  */
46 uint defaultHash(R)(R src) @nogc @trusted pure nothrow {
47 	const (ubyte)[] helperFunc() @nogc @system pure nothrow {
48 		return cast(const (ubyte)[])(cast(const (void)[])src);
49 	}
50 	MurmurHash3!32 hashFunc;
51 	hashFunc.put(helperFunc);
52 	hashFunc.finish();
53 	return hashFunc.get();
54 }
55 /**
56  * Standard digest function for hashing, using the MurMurHash3/128 algorithm
57  */
58 ubyte[16] defaultHash128(R)(R src) @nogc @trusted pure nothrow {
59 	const (ubyte)[] helperFunc() @nogc @system pure nothrow {
60 		return cast(const (ubyte)[])(cast(const (void)[])src);
61 	}
62 	MurmurHash3!128 hashFunc;
63 	hashFunc.put(helperFunc);
64 	return hashFunc.finish();
65 }
66 /** 
67  * Intended for unittest of 
68  */
69 package struct TestStructWithKey {
70 	int			key;
71 	ubyte[]		foo;
72 	string		bar;
73 	int opCmp(const TestStructWithKey other) @nogc @safe pure nothrow const {
74 		if (this.key > other.key) return 1;
75 		else if (this.key < other.key) return -1;
76 		else return 0;
77 	}
78 	int opCmp(R)(const R other) @nogc @safe pure nothrow const {
79 		if (this.key > other) return 1;
80 		else if (this.key < other) return -1;
81 		else return 0;
82 	}
83 	bool opEquals(const TestStructWithKey other) @nogc @safe pure nothrow const {
84 		return this.key == other.key;
85 	}
86 	bool opEquals(R)(const R other) const {
87 		return this.key == other;
88 	}
89 	string toString() const @safe pure nothrow {
90 		import std.conv : to;
91 		return key.to!string();
92 	}
93 }