Constructor, sets the number of buckets to n * load_factor
Constructor.
Calculates the hash value from key. Uses the identity since key is expected to be a suitable hash value.
HashSet unittest.
1 version ( UnitTestVerbose ) 2 { 3 Stdout.formatln("{} unittest ---------------", 4 typeof(this).stringof); 5 scope ( exit ) Stdout.formatln("{} unittest ---------------", 6 typeof(this).stringof); 7 } 8 9 scope set = new HashSet(10); 10 11 version ( UnitTestVerbose ) void printState ( ) 12 { 13 Stdout.formatln(" :: len={}, load={}, max_load={}, pool={} ({} busy)", 14 set.length, set.load, set.max_load, 15 set.bucket_elements.length, set.bucket_elements.num_busy); 16 } 17 18 bool lengthIs ( size_t expected ) 19 { 20 test(set.length == expected); 21 22 int c; 23 foreach ( k; set ) 24 { 25 c++; 26 } 27 return c == expected; 28 } 29 30 void put ( hash_t key, bool should_exist ) 31 { 32 auto len = set.length; 33 34 test(!!(key in set) == should_exist); 35 36 auto e = set.put(key); 37 version ( UnitTestVerbose ) 38 { 39 Stdout.format("put {}: {}", key, e); 40 printState(); 41 } 42 43 test(key in set); 44 test(lengthIs(len + (should_exist ? 0 : 1))); 45 } 46 47 void remove ( hash_t key, bool should_exist ) 48 { 49 auto len = set.length; 50 auto pool_len = set.bucket_info.num_buckets; 51 52 test(!!(key in set) == should_exist); 53 54 auto e = set.remove(key); 55 version ( UnitTestVerbose ) 56 { 57 Stdout.format("remove {}: {}", key, e); 58 printState(); 59 } 60 61 test(!(key in set)); 62 test(lengthIs(len - (should_exist ? 1 : 0))); 63 test(pool_len == set.bucket_info.num_buckets); 64 } 65 66 void clear ( ) 67 { 68 auto pool_len = set.bucket_info.num_buckets; 69 70 set.clear(); 71 version ( UnitTestVerbose ) 72 { 73 Stdout.format("clear"); 74 printState(); 75 } 76 77 test(lengthIs(0)); 78 79 test(pool_len == set.bucket_info.num_buckets); 80 } 81 82 put(4711, false); // put 83 put(4711, true); // double put 84 put(23, false); // put 85 put(12, false); // put 86 remove(23, true); // remove 87 remove(23, false); // double remove 88 put(23, false); // put 89 put(23, true); // double put 90 91 clear(); 92 93 put(4711, false); // put 94 put(11, false); // put 95 put(11, true); // double put 96 put(12, false); // put 97 remove(23, false); // remove 98 put(23, false); // put 99 put(23, true); // double put 100 101 clear();
HashSet class. Manages a set of hash_t's with fast lookup.