HashTableIter.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2017 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
30 template<class T, class Key, class Hash>
31 template<bool Const>
33 (
34  table_type* tbl,
35  const Key& key
36 )
37 :
38  entry_(nullptr),
39  container_(tbl),
40  index_(0)
41 {
42  if (tbl->size())
43  {
44  const label index = container_->hashKeyIndex(key);
45 
46  for (node_type* ep = container_->table_[index]; ep; ep = ep->next_)
47  {
48  if (key == ep->key())
49  {
50  entry_ = ep;
51  index_ = index;
52  break;
53  }
54  }
55  }
56 }
57 
58 
59 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
60 
61 //
62 // Any changes here may need changes in the iterator increment() method
63 //
64 template<class T, class Key, class Hash>
66 (
67  node_type*& entry,
68  label& index
69 )
70 {
71  // Safeguard against the following:
72  // - empty table
73  // - nullptr entry
74  // - end iterator (which is also a nullptr)
75  // - negative index from a previous erase. See comment below.
76  if (!size_ || !entry || index < 0)
77  {
78  return false;
79  }
80 
81  // Decrease count
82  --size_;
83 
84  // The previous element in the singly linked list
85  node_type* prev = nullptr;
86 
87  for (node_type* ep = table_[index]; ep; ep = ep->next_)
88  {
89  if (ep == entry)
90  {
91  break;
92  }
93  prev = ep;
94  }
95 
96  if (prev)
97  {
98  // Had previous element in linked list - reposition to there
99  prev->next_ = entry->next_;
100  delete entry;
101  entry = prev;
102 
103  return true;
104  }
105 
106  // Was first element on linked list
107  table_[index] = entry->next_;
108  delete entry;
109 
110  // Assign any non-nullptr value so it doesn't look like end()
111  entry = reinterpret_cast<node_type*>(this);
112 
113  // Mark the present index to continue and bring it back to the present
114  // location with the next index.
115  //
116  // Save: (-index-1), which has no ambiguity for index 0.
117  // Retrieve: (-(index+1))
118 
119  index = (-index - 1);
120 
121  return true;
122 }
123 
124 
125 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:67
Foam::glTF::key
auto key(const Type &t) -> typename std::enable_if< std::is_enum< Type >::value, typename std::underlying_type< Type >::type >::type
Definition: foamGltfBase.H:108
Foam::HashTable::iterator_erase
bool iterator_erase(node_type *&entry, label &index)
Low-level entry erasure using iterator internals.
Definition: HashTableIter.C:66
Foam::HashTable
A HashTable similar to std::unordered_map.
Definition: HashTable.H:105
Foam::HashTable::Iterator< true >::table_type
typename std::conditional< Const, const this_type, this_type >::type table_type
The HashTable container type.
Definition: HashTable.H:586