summaryrefslogtreecommitdiff
path: root/include/fud_hash_map_impl.hpp
blob: e4b79c451ba1d1dd8194b381eeadd188d1040e30 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * libfud
 * Copyright 2025 Dominick Allen
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef FUD_HASH_MAP_IMPL_HPP
#define FUD_HASH_MAP_IMPL_HPP

#include "fud_hash.hpp"

namespace fud {

template <typename Key, typename Value, typename Hash = detail::DefaultHash<Key>>
class HashMap;

} // namespace fud

namespace fud::detail {

enum class BucketState : uint8_t
{
    Disengaged,
    Engaged,
    Tombstoned
};

template <typename Key, typename Value>
struct MapEntry {
    static_assert(!std::is_same_v<Key, option_detail::NullOptionType>);
    static_assert(!std::is_same_v<Value, option_detail::NullOptionType>);
    static_assert(!std::is_reference_v<Key>);
    static_assert(!std::is_reference_v<Value>);

    constexpr MapEntry() noexcept : m_state{BucketState::Disengaged}
    {
    }

    constexpr MapEntry(const Key& key, const Value& value) : m_state{BucketState::Engaged}
    {
        new (m_keyData.data()) Key(key);
        new (m_valueData.data()) Value(value);
    }

    constexpr MapEntry(Key&& key, const Value& value) : m_state{BucketState::Engaged}
    {
        new (m_keyData.data()) Key(std::move(key));
        new (m_valueData.data()) Value(value);
    }

    constexpr MapEntry(const Key& key, Value&& value) : m_state{BucketState::Engaged}
    {
        new (m_keyData.data()) Key(key);
        new (m_valueData.data()) Value(std::move(value));
    }

    constexpr MapEntry(Key&& key, Value&& value) : m_state{BucketState::Engaged}
    {
        new (m_keyData.data()) Key(std::move(key));
        new (m_valueData.data()) Value(std::move(value));
    }

    constexpr MapEntry(const MapEntry& rhs) noexcept :
        m_keyData(rhs.m_keyData), m_valueData(rhs.m_valueData), m_state(rhs.m_state)
    {
    }

    constexpr MapEntry(MapEntry&& rhs) noexcept :
        m_keyData(std::move(rhs.m_keyData)), m_valueData(std::move(rhs.m_valueData)), m_state(rhs.m_state)
    {
        rhs.destroy(false);
    }

    constexpr MapEntry& operator=(const MapEntry& rhs) noexcept
    {
        if (&rhs == this) {
            return *this;
        }
        destroy(false);
        m_keyData = rhs.m_keyData;
        m_valueData = rhs.m_valueData;
        m_state = rhs.m_state;
        return *this;
    }

    constexpr MapEntry& operator=(MapEntry&& rhs) noexcept
    {
        if (&rhs == this) {
            return *this;
        }
        destroy(false);
        m_keyData = rhs.m_keyData;
        m_valueData = rhs.m_valueData;
        m_state = rhs.m_state;
        rhs.destroy(false);
        return *this;
    }

    constexpr ~MapEntry() noexcept
    {
        destroy(false);
    }

    [[nodiscard]] constexpr bool hasValue() const
    {
        return m_state == BucketState::Engaged;
    }

    [[nodiscard]] constexpr bool isNone() const
    {
        return m_state == BucketState::Disengaged;
    }

    [[nodiscard]] constexpr bool isTombstone() const
    {
        return m_state == BucketState::Tombstoned;
    }

    [[nodiscard]] constexpr const Key& key() const&
    {
        fudAssert(hasValue());
        return *std::bit_cast<const Key*>(m_keyData.data());
    }

    [[nodiscard]] constexpr const Value& value() const&
    {
        fudAssert(hasValue());
        return *std::bit_cast<const Value*>(m_valueData.data());
    }

    [[nodiscard]] constexpr Key& key() &
    {
        fudAssert(hasValue());
        return *std::bit_cast<Key*>(m_keyData.data());
    }

    [[nodiscard]] constexpr Value& value() &
    {
        fudAssert(hasValue());
        return *std::bit_cast<Value*>(m_valueData.data());
    }

    constexpr void tombstone() noexcept
    {
        destroy(true);
    }

    constexpr void destroy(bool entomb) noexcept
    {
        if (m_state == BucketState::Engaged) {
            std::bit_cast<Key*>(m_keyData.data())->~Key();
            m_keyData.clear();
            std::bit_cast<Value*>(m_valueData.data())->~Value();
            m_valueData.clear();
            m_state = entomb ? BucketState::Tombstoned : BucketState::Disengaged;
        } else if (m_state == BucketState::Tombstoned and not entomb) {
            m_state = BucketState::Disengaged;
        }
    }

    static constexpr auto Align = max(alignof(Key), alignof(Value));
    alignas(alignof(Key)) option_detail::DataArray<sizeof(Key)> m_keyData{};
    alignas(alignof(Value)) option_detail::DataArray<sizeof(Value)> m_valueData{};
    BucketState m_state{BucketState::Disengaged};
};

} // namespace fud::detail

#endif