Advertisement
Guest User

Untitled

a guest
May 4th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.87 KB | None | 0 0
  1. struct KV(K, V, string keyName = "key", string valueName = "value") {
  2.   mixin("K " ~ keyName ~ ";");
  3.   mixin("V " ~ valueName ~ ";");
  4.  
  5.   bool opEquals()(auto const ref KV s) const {
  6.     return mixin(keyName) == mixin("s." ~ keyName);
  7.   }
  8.  
  9.   int opCmp(const ref KV s) const {
  10.     return cmp(mixin(keyName), mixin("s." ~ keyName));
  11.   }
  12. }
  13.  
  14. auto createMap(K, V, alias less = "a < b", alias keyName = "key", alias valueName = "value", bool allowDups = false)() {
  15.   return new RedBlackTree!(KV!(K, V, keyName, valueName), less, allowDups);
  16. }
  17.  
  18. unittest {
  19.   auto map = createMap!(string, int, "a < b", "str", "bob");
  20.   map.stableInsert(map.Elem("hello", 5));
  21.   map.stableInsert(map.Elem("zbd", 3));
  22.   map.stableInsert(map.Elem("abc", 9));
  23.  
  24.   auto rng = map[];
  25.   assert(rng.front == map.Elem("abc", 9));
  26.   assert(rng.front.str == "abc");
  27.   assert(rng.front.bob == 9);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement