HashTableI.H 5.95 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8 9
    Copyright (C) 2011-2016 OpenFOAM Foundation
    Copyright (C) 2017-2019 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
18 19 20 21 22 23 24

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
25
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
26 27 28 29 30

\*---------------------------------------------------------------------------*/

#include "error.H"

Mark Olesen's avatar
Mark Olesen committed
31 32 33 34 35 36
// * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //

template<class T, class Key, class Hash>
inline Foam::label
Foam::HashTable<T, Key, Hash>::hashKeyIndex(const Key& key) const
{
37 38
    // capacity is always a power of two - this is the modulus
    return Hash()(key) & (capacity_ - 1);
Mark Olesen's avatar
Mark Olesen committed
39 40 41
}


42 43
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //

Mark Olesen's avatar
Mark Olesen committed
44
template<class T, class Key, class Hash>
45
inline Foam::label Foam::HashTable<T, Key, Hash>::capacity() const noexcept
Mark Olesen's avatar
Mark Olesen committed
46
{
47
    return capacity_;
Mark Olesen's avatar
Mark Olesen committed
48 49 50
}


51
template<class T, class Key, class Hash>
52
inline Foam::label Foam::HashTable<T, Key, Hash>::size() const noexcept
53
{
54
    return size_;
55 56 57
}


58
template<class T, class Key, class Hash>
59
inline bool Foam::HashTable<T, Key, Hash>::empty() const noexcept
60
{
61 62 63 64
    return !size_;
}


65 66 67
template<class T, class Key, class Hash>
inline T& Foam::HashTable<T, Key, Hash>::at(const Key& key)
{
68
    iterator iter(this->find(key));
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

    if (!iter.good())
    {
        FatalErrorInFunction
            << key << " not found in table.  Valid entries: "
            << toc()
            << exit(FatalError);
    }

    return iter.val();
}


template<class T, class Key, class Hash>
inline const T& Foam::HashTable<T, Key, Hash>::at(const Key& key) const
{
    const const_iterator iter(this->cfind(key));

    if (!iter.good())
    {
        FatalErrorInFunction
            << key << " not found in table.  Valid entries: "
            << toc()
            << exit(FatalError);
    }

    return iter.val();
}


99
template<class T, class Key, class Hash>
100
inline bool Foam::HashTable<T, Key, Hash>::found(const Key& key) const
101 102 103
{
    if (size_)
    {
104
        return Iterator<true>(this, key).good();
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
    }

    return false;
}


template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::iterator
Foam::HashTable<T, Key, Hash>::find
(
    const Key& key
)
{
    if (size_)
    {
        return iterator(Iterator<false>(this, key));
    }

    return iterator();
}


template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::find
(
    const Key& key
) const
{
    return this->cfind(key);
}


template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::cfind
(
    const Key& key
) const
{
    if (size_)
    {
        return const_iterator(Iterator<true>(this, key));
    }

    return const_iterator();
151 152 153
}


154 155 156 157 158 159 160 161 162 163 164 165
template<class T, class Key, class Hash>
template<class... Args>
inline bool Foam::HashTable<T, Key, Hash>::emplace
(
    const Key& key,
    Args&&... args
)
{
    return this->setEntry(false, key, std::forward<Args>(args)...);
}


166
template<class T, class Key, class Hash>
167 168 169
inline bool Foam::HashTable<T, Key, Hash>::insert
(
    const Key& key,
170
    const T& obj
171
)
172
{
173
    return this->setEntry(false, key, obj);
174 175 176 177 178 179 180
}


template<class T, class Key, class Hash>
inline bool Foam::HashTable<T, Key, Hash>::insert
(
    const Key& key,
181
    T&& obj
182 183
)
{
184
    return this->setEntry(false, key, std::forward<T>(obj));
185 186 187 188
}


template<class T, class Key, class Hash>
189 190 191
inline bool Foam::HashTable<T, Key, Hash>::set
(
    const Key& key,
192
    const T& obj
193
)
194
{
195
    return this->setEntry(true, key, obj);   // Overwrite
196 197 198 199 200 201 202
}


template<class T, class Key, class Hash>
inline bool Foam::HashTable<T, Key, Hash>::set
(
    const Key& key,
203
    T&& obj
204 205
)
{
206
    return this->setEntry(true, key, std::forward<T>(obj));  // Overwrite
207 208
}

209

210 211 212 213 214 215 216
template<class T, class Key, class Hash>
inline const T& Foam::HashTable<T, Key, Hash>::lookup
(
    const Key& key,
    const T& deflt
) const
{
217
    const const_iterator iter(this->cfind(key));
218
    return iter.good() ? iter.val() : deflt;
219 220 221
}


222 223 224
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

template<class T, class Key, class Hash>
225
inline T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key)
226
{
227
    return this->at(key);
228 229
}

230

231
template<class T, class Key, class Hash>
232
inline const T& Foam::HashTable<T, Key, Hash>::operator[](const Key& key) const
233
{
234
    return this->at(key);
235 236 237 238
}


template<class T, class Key, class Hash>
239
inline T& Foam::HashTable<T, Key, Hash>::operator()(const Key& key)
240
{
241
    iterator iter(this->find(key));
242

243
    if (iter.good())
244
    {
245
        return iter.val();
246
    }
247

248
    this->emplace(key);
249
    return find(key).val();
250 251 252
}


253 254 255 256 257 258 259
template<class T, class Key, class Hash>
inline T& Foam::HashTable<T, Key, Hash>::operator()
(
    const Key& key,
    const T& deflt
)
{
260
    iterator iter(this->find(key));
261

262
    if (iter.good())
263
    {
264
        return iter.val();
265 266 267
    }

    this->insert(key, deflt);
268
    return find(key).val();
269 270 271
}


272
// ************************************************************************* //