HashTableIterI.H 6.78 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
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7
-------------------------------------------------------------------------------
8
    Copyright (C) 2017-2020 OpenCFD Ltd.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    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.

    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
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

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

// * * * * * * * * * * * * * * * iterator base * * * * * * * * * * * * * * * //

template<class T, class Key, class Hash>
template<bool Const>
32 33
inline constexpr
Foam::HashTable<T, Key, Hash>::Iterator<Const>::Iterator() noexcept
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
:
    entry_(nullptr),
    container_(nullptr),
    index_(0)
{}


template<class T, class Key, class Hash>
template<bool Const>
inline Foam::HashTable<T, Key, Hash>::Iterator<Const>::Iterator
(
    table_type* tbl
)
:
    entry_(nullptr),
    container_(tbl),
    index_(0)
{
    if (container_ && container_->size_)
    {
        // Locate the first non-nullptr table entry
        while
        (
            !(entry_ = container_->table_[index_])
         && ++index_ < container_->capacity_
        )
        {}

        if (index_ >= container_->capacity_)
        {
            // Nothing found - make it an end iterator
            entry_ = nullptr;
            index_ = 0;
        }
    }
}


//
// Any changes here may need changes in iterator_erase() method too
//
template<class T, class Key, class Hash>
template<bool Const>
inline void
Foam::HashTable<T, Key, Hash>::Iterator<Const>::increment()
{
    if (index_ < 0)
    {
        // Negative index is a special value from erase
        //
        // Saved as (-index-1), retrieved as (-(index-1)) but to with an
        // extra (-1) to compensate for the ++ in the following while loop
        index_ = -(index_+1) - 1;
    }
    else if (index_ < container_->capacity_ && entry_ && entry_->next_)
    {
        // Move to next element on the linked-list
        entry_ = entry_->next_;
        return;
    }

    // Move to the next non-nullptr table entry
    while
    (
        ++index_ < container_->capacity_
     && !(entry_ = container_->table_[index_])
    )
    {}

    if (index_ >= container_->capacity_)
    {
        // Nothing found - make it an end iterator
        entry_ = nullptr;
        index_ = 0;
    }
}


112 113 114
template<class T, class Key, class Hash>
template<bool Const>
inline bool
115
Foam::HashTable<T, Key, Hash>::Iterator<Const>::good() const noexcept
116 117 118 119 120
{
    return entry_;
}


121 122 123
template<class T, class Key, class Hash>
template<bool Const>
inline bool
124
Foam::HashTable<T, Key, Hash>::Iterator<Const>::found() const noexcept
125 126 127 128 129 130 131 132 133 134 135 136 137
{
    return entry_;
}


template<class T, class Key, class Hash>
template<bool Const>
inline const Key& Foam::HashTable<T, Key, Hash>::Iterator<Const>::key() const
{
    return entry_->key();
}


138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
template<class T, class Key, class Hash>
template<bool Const>
inline Foam::Ostream& Foam::HashTable<T, Key, Hash>::Iterator<Const>::print
(
    Ostream& os
) const
{
    if (entry_)
    {
        entry_->print(os);
    }
    return os;
}


153 154 155 156 157 158 159 160 161
template<class T, class Key, class Hash>
template<bool Const>
inline Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator
bool() const noexcept
{
    return entry_;
}


162 163
template<class T, class Key, class Hash>
template<bool Const>
164
template<bool Any>
165 166
inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator==
(
167 168
    const Iterator<Any>& iter
) const noexcept
169 170 171 172 173 174 175
{
    return entry_ == iter.entry_;
}


template<class T, class Key, class Hash>
template<bool Const>
176
template<bool Any>
177 178
inline bool Foam::HashTable<T, Key, Hash>::Iterator<Const>::operator!=
(
179 180
    const Iterator<Any>& iter
) const noexcept
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
{
    return entry_ != iter.entry_;
}


// * * * * * * * * * * * * * * * * STL iterator  * * * * * * * * * * * * * * //

template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::iterator&
Foam::HashTable<T, Key, Hash>::iterator::operator++()
{
    this->increment();
    return *this;
}


template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::iterator
Foam::HashTable<T, Key, Hash>::iterator::operator++(int)
{
    iterator iter(*this);
    this->increment();
    return iter;
}


// * * * * * * * * * * * * * * * STL const_iterator  * * * * * * * * * * * * //

template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator&
Foam::HashTable<T, Key, Hash>::const_iterator::operator++()
{
    this->increment();
    return *this;
}


template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::const_iterator::operator++(int)
{
    const_iterator iter(*this);
    this->increment();
    return iter;
}


// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::iterator
Foam::HashTable<T, Key, Hash>::begin()
{
234
    return iterator(Iterator<false>(this));
235 236 237 238 239 240 241
}


template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::begin() const
{
242
    return const_iterator(Iterator<true>(this));
243 244 245 246 247 248 249
}


template<class T, class Key, class Hash>
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::cbegin() const
{
250
    return const_iterator(Iterator<true>(this));
251 252 253 254
}


template<class T, class Key, class Hash>
255 256
inline typename Foam::HashTable<T, Key, Hash>::iterator
Foam::HashTable<T, Key, Hash>::end() noexcept
257
{
258
    return iterator();
259 260 261 262
}


template<class T, class Key, class Hash>
263 264
inline typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::end() const noexcept
265
{
266
    return const_iterator();
267 268 269 270
}


template<class T, class Key, class Hash>
271 272
inline constexpr typename Foam::HashTable<T, Key, Hash>::const_iterator
Foam::HashTable<T, Key, Hash>::cend() const noexcept
273
{
274
    return const_iterator();
275 276 277 278
}


// ************************************************************************* //