Skip to content

Instantly share code, notes, and snippets.

@PetarKirov
Created December 21, 2021 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PetarKirov/76e3bd512466286cfb7ae5c1f772572b to your computer and use it in GitHub Desktop.
Save PetarKirov/76e3bd512466286cfb7ae5c1f772572b to your computer and use it in GitHub Desktop.
Trivial subset implementation in D
bool isSubsetOf(S1, S2)(S1 subset, S2 set)
{
import std.algorithm : all;
return all!(elem => elem in set)(subset[]);
}
void test(alias set)()
{
auto s1 = set(5, 2, 1);
auto s2 = set(3, 1, 4, 2, 5);
import std.stdio : writefln;
"Is { %(%s, %) } ⊆ { %(%s, %) }? %s."
.writefln(s1[], s2[], s1.isSubsetOf(s2) ? "Yes" : "No");
}
void main()
{
import std.container : redBlackTree;
test!redBlackTree;
static auto set(T)(T[] elems...)
{
struct Set
{
private bool[T] _data;
alias _data this;
auto opSlice() const { return _data.byKey; }
}
import std.array : assocArray;
import std.range : repeat;
return Set(assocArray(elems, true.repeat(elems.length)));
}
test!set;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment