root/trunk/dglut/set.d

Revision 170, 0.8 kB (checked in by FeepingCreature, 1 year ago)
  • STUF! DEF IS BACK
Line 
1 module dglut.set;
2
3 // A delicious hack
4 struct Empty { string toString() { return ""; } } Empty empty;
5
6 import std.string: format, find, ifind;
7 struct Set(T) {
8   Empty[T] aa;
9   bool opIn_r(T what) { return (what in aa)!=null; }
10   void set(T what) { aa[what]=empty; }
11   void remove(T what) { aa.remove(what); }
12   int opApply(int delegate(ref T) dg) {
13     foreach (idx, bogus; aa) if (auto res=dg(idx)) return res;
14     return 0;
15   }
16   string toString() { return format(aa.keys); }
17   Set grep(bool delegate(T) dg) {
18     Set res;
19     foreach (entry; *this) {
20       if (dg(entry)) res.set(entry);
21     }
22     return res;
23   }
24   static if (is(T: string)) {
25     Set  grep(string what) { return grep((T t) { return  t.find(what)!=-1; }); }
26     Set igrep(string what) { return grep((T t) { return t.ifind(what)!=-1; }); }
27   }
28   size_t length() { return aa.length; }
29 }
Note: See TracBrowser for help on using the browser.