I was fishing around in D's standard library looking for a Set implementation, and I only found these:
- BinaryHeap
- RedBlackTree
These both would work fine, if I could only figure out how to use them. I started with the RedBlackTree (because I'm already familiar with how they work), and this is what I came up with:
auto rbt = redBlackTree!string();
foreach(s; setOfStrings) {
rbt.insert(s);
}
foreach(s; rbt) {
if (s[0 .. 3] == "sth") {
rbt.removeKey(s);
}
}
I know I could have done the condition in the first foreach, but it's just an example showing that I need to add and remove elements from the Set. This would work, but I get compile errors:
Error: template std.container.RedBlackTree!(string).RedBlackTree.removeKey(U) if (isImplicitlyConvertible!(U,Elem)) does not match any function template declaration
Error: template std.container.RedBlackTree!(string).RedBlackTree.removeKey(U) if (isImplicitlyConvertible!(U,Elem)) cannot deduce template function from argument types !()(string
I don't need a red-black tree (anything without duplicates), and speed isn't too important. I could do something like this:
string[] arr;
foreach(s; setOfStrings) {
// check for duplicate code here...
arr ~= s;
}
for(int i = 0; i < arr.length; i++) {
if (s[0 .. 3] == "sth") {
arr = arr[0 .. i] ~ arr[i + 1 .. $];
i++;
}
}
Is there anything in the standard library for a simple Set?
removeKey([s])
, but that didn't work so well. I totally spaced about the second, which should work (removeKey!string(s)
). Thanks for the info! – Calumnious