1 /**
2 * Copyright © DiamondMVC 2018
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.core.collections;
7 
8 /// A simple hash set implementation using the build-in AA.
9 final class HashSet(T)
10 {
11   private:
12   /// The build-in AA.
13   ubyte[T] _set;
14 
15   public:
16   final:
17   /// Creates a new hash set.
18   this() { }
19 
20   /**
21   * Adds a value to the hash set.
22   * Params:
23   *   value = The value added to the hash set.
24   */
25   void add(T value)
26   {
27     _set[value] = 0;
28   }
29 
30   /**
31   * Checks whether the hash set contains the given value.
32   * Params:
33   *   value = The value to check for existence within the set.
34   * Returns:
35   *   True if the set contains the given value.
36   */
37   bool has(T value)
38   {
39     if (!_set)
40     {
41       return false;
42     }
43 
44     if (value in _set)
45     {
46       return true;
47     }
48 
49     return false;
50   }
51 
52   /**
53   * Operator overload for accessing the set like an array.
54   * This calls "bool has(T value);"
55   */
56   bool opIndex(T value)
57   {
58     return has(value);
59   }
60 }