1 /**
2 Copyright: Copyright (c) 2017-2018 Andrey Penechko.
3 License: $(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0).
4 Authors: Andrey Penechko.
5 */
6 module voxelman.container.hash.keybucket;
7
8 struct MetaKeyBucket(Key)
9 {
10 enum BucketType : ubyte {
11 empty,
12 used,
13 deleted
14 }
15
16 BucketType type;
17 Key key;
18
19 bool empty() const { return type == BucketType.empty; }
20 bool used() const { return type == BucketType.used; }
21 bool deleted() const { return type == BucketType.deleted; }
22 bool corrupted() const { return type > cast(BucketType)2; }
23
24 bool canInsert(Key key) const {
25 return type == BucketType.empty || this.key == key;
26 }
27
28 void markAsDeleted() {
29 type = BucketType.deleted;
30 }
31 void assignKey(Key key) {
32 this.key = key;
33 type = BucketType.used;
34 }
35
36 static void clearBuckets(typeof(this)[] keyBuckets)
37 {
38 auto buf = cast(ubyte[])keyBuckets;
39 buf[] = 0; // set KeyBucket.type to empty
40 }
41 }
42
43 /// Uses 2 special key values to mark empty and deleted buckets
44 struct KeyBucket(Key, Key emptyKey, Key deletedKey)
45 {
46 Key key = emptyKey;
47
48 bool empty() const { return key == emptyKey; }
49 bool used() const { return key != emptyKey && key != deletedKey; }
50 bool deleted() const { return key == deletedKey; }
51 bool corrupted() const { return false; }
52
53 bool canInsert(Key key) const {
54 return this.key == emptyKey || this.key == key;
55 }
56
57 void markAsDeleted() {
58 key = deletedKey;
59 }
60 void assignKey(Key key) {
61 this.key = key;
62 }
63
64 static void clearBuckets(typeof(this)[] keyBuckets)
65 {
66 keyBuckets[] = typeof(this).init;
67 }
68 }