summaryrefslogtreecommitdiff
path: root/source/tanya/container/entry.d
blob: 1aea4783e5bfd59a067ca48cd4253928e3141b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Internal package used by containers that rely on entries/nodes.
 *
 * Copyright: Eugene Wissner 2016-2017.
 * License: $(LINK2 https://www.mozilla.org/en-US/MPL/2.0/,
 *                  Mozilla Public License, v. 2.0).
 * Authors: $(LINK2 mailto:info@caraus.de, Eugene Wissner)
 * Source: $(LINK2 https://github.com/caraus-ecms/tanya/blob/master/source/tanya/container/entry.d,
 *                 tanya/container/entry.d)
 */
module tanya.container.entry;

import std.traits;
import tanya.typecons;

package struct SEntry(T)
{
    // Item content.
    T content;

    // Next item.
    SEntry* next;
}

package struct DEntry(T)
{
    // Item content.
    T content;

    // Previous and next item.
    DEntry* next, prev;
}

package struct HashEntry(K, V)
{
    this(ref K key, ref V value)
    {
        this.pair = Pair!(K, V)(key, value);
    }

    Pair!(K, V) pair;
    HashEntry* next;
}

package enum BucketStatus : byte
{
    deleted = -1,
    empty = 0,
    used = 1,
}

package struct Bucket(T)
{
    @property void content(ref T content)
    {
        this.content_ = content;
        this.status = BucketStatus.used;
    }

    @property ref inout(T) content() inout
    {
        return this.content_;
    }

    bool opEquals(ref T content)
    {
        if (this.status == BucketStatus.used && this.content == content)
        {
            return true;
        }
        return false;
    }

    bool opEquals(ref const T content) const
    {
        if (this.status == BucketStatus.used && this.content == content)
        {
            return true;
        }
        return false;
    }

    bool opEquals(ref typeof(this) that)
    {
        return this.content == that.content && this.status == that.status;
    }

    bool opEquals(ref typeof(this) that) const
    {
        return this.content == that.content && this.status == that.status;
    }

    void remove()
    {
        static if (hasElaborateDestructor!T)
        {
            destroy(this.content);
        }
        this.status = BucketStatus.deleted;
    }

    T content_;
    BucketStatus status = BucketStatus.empty;
}