containers.simdset

SIMD-accelerated Set

Authors

Brian Schott

License

  • Declaration

    struct SimdSet(T, Allocator = Mallocator) if (T.sizeof == 1 || T.sizeof == 2 || T.sizeof == 4 || T.sizeof == 8);

    Set implementation that is well suited for small sets and simple items.

    Discussion

    Uses SSE instructions to compare multiple elements simultaneously, but has linear time complexity.

    Note: Only works on x86_64. Does NOT add GC ranges. Do not store pointers in this container unless they are also stored somewhere else.

    Parameters

    T

    the element type

    Allocator

    the allocator to use. Defaults to Mallocator.

    Examples

    1. import std.string : format; void testSimdSet(T)() { SimdSet!T set; assert(set.insert(1)); assert(set.length == 1); assert(set.contains(1)); assert(!set.insert(1)); set.insert(0); set.insert(20); assert(set.contains(1)); assert(set.contains(0)); assert(!set.contains(10)); assert(!set.contains(50)); assert(set.contains(20)); foreach (T i; 28 .. 127) set.insert(i); foreach (T i; 28 .. 127) assert(set.contains(i), "%d".format(i)); foreach (T i; 28 .. 127) assert(set.remove(i)); assert(set.length == 3, "%d".format(set.length)); assert(set.contains(0)); assert(set.contains(1)); assert(set.contains(20)); assert(!set.contains(28)); } testSimdSet!ubyte(); testSimdSet!ushort(); testSimdSet!uint(); testSimdSet!ulong(); testSimdSet!byte(); testSimdSet!short(); testSimdSet!int(); testSimdSet!long();

    • Declaration

      this();

      No default construction if an allocator must be provided.

    • Declaration

      this(Allocator allocator);

      Use the given allocator for allocations.

    • Declaration

      const pure nothrow @nogc @trusted bool contains(T item);
      const pure nothrow @nogc @safe bool opBinaryRight(string op)(T item) if (op == "in");

      Parameters

      T item

      the item to check

      Return Value

      true if the set contains the given item

    • Declaration

      bool insert(T item);
      bool opOpAssign(string op)(T item) if (op == "~");
      alias insertAnywhere = insert;
      alias put = insert;

      Inserts the given item into the set.

      Parameters

      T item

      the item to insert

      Return Value

      true if the item was inserted or false if it was already present

    • Declaration

      bool remove(T item);

      Removes the given item from the set.

      Parameters

      T item

      the time to remove

      Return Value

      true if the item was removed, false if it was not present

    • Declaration

      auto opSlice(this This)();

      Slice operator

    • Declaration

      const pure nothrow @nogc @property size_t length();

      Return Value

      the number of items in the set