Try online at: https://run.dlang.io/
Created
December 21, 2021 08:49
-
-
Save PetarKirov/76e3bd512466286cfb7ae5c1f772572b to your computer and use it in GitHub Desktop.
Trivial subset implementation in D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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