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 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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkStringManager.h"
#include "vtkObjectFactory.h"
#include <algorithm>
#include <array>
#include <iostream>
VTK_ABI_NAMESPACE_BEGIN
constexpr vtkStringManager::Hash vtkStringManager::Invalid;
vtkStandardNewMacro(vtkStringManager);
void vtkStringManager::PrintSelf(ostream& os, vtkIndent indent)
{
LockGuard guard(this->WriteLock);
this->Superclass::PrintSelf(os, indent);
vtkIndent i2 = indent.GetNextIndent();
vtkIndent i3 = i2.GetNextIndent();
os << indent << "Data: " << this->Data.size() << " entries\n";
for (const auto& entry : this->Data)
{
os << i2 << entry.first << ": " << entry.second << '\n';
}
os << indent << "Sets: " << this->Sets.size() << " entries\n";
for (const auto& entry : this->Sets)
{
os << i2 << entry.first << ": " << entry.second.size() << " entries\n";
for (const auto& child : entry.second)
{
os << i3 << child << '\n';
}
}
}
vtkStringManager::Hash vtkStringManager::Manage(const std::string& ss)
{
std::pair<Hash, bool> hp;
{
LockGuard lock(this->WriteLock);
hp = this->ComputeInternalAndInsert(ss, lock);
}
return hp.first;
}
std::size_t vtkStringManager::Unmanage(Hash hh)
{
LockGuard writeGuard(this->WriteLock);
std::size_t num = this->UnmanageInternal(hh, writeGuard);
return num;
}
const std::string& vtkStringManager::Value(Hash hh) const
{
LockGuard guard(this->WriteLock);
static const std::string empty;
auto it = this->Data.find(hh);
if (it == this->Data.end())
{
static bool once = false;
if (!once)
{
once = true;
vtkWarningMacro("Hash " << hh << " is missing from manager. Returning empty string.");
}
return empty;
}
return it->second;
}
vtkStringManager::Hash vtkStringManager::Find(const std::string& ss) const
{
std::pair<Hash, bool> hh;
{
LockGuard lock(this->WriteLock);
hh = this->ComputeInternal(ss, lock);
}
return hh.second ? hh.first : Invalid;
}
vtkStringManager::Hash vtkStringManager::Compute(const std::string& ss) const
{
LockGuard lock(this->WriteLock);
return this->ComputeInternal(ss, lock).first;
}
vtkStringManager::Hash vtkStringManager::Insert(const std::string& ss, Hash hh)
{
LockGuard lock(this->WriteLock);
bool didInsert = false;
// Verify \a h is managed.
if (this->Data.find(hh) == this->Data.end())
{
return Invalid;
}
// Insert \a h into \a ss.
std::pair<Hash, bool> setHash{ Invalid, false };
{
setHash = this->ComputeInternalAndInsert(ss, lock);
didInsert = this->Sets[setHash.first].insert(hh).second;
(void)didInsert;
}
return setHash.first;
}
bool vtkStringManager::Insert(Hash ss, Hash hh)
{
LockGuard lock(this->WriteLock);
bool didInsert = false;
// Verify \a ss and \a h are managed.
if (this->Data.find(hh) == this->Data.end() || this->Data.find(ss) == this->Data.end())
{
return didInsert;
}
didInsert = this->Sets[ss].insert(hh).second;
return didInsert;
}
bool vtkStringManager::Remove(const std::string& ss, Hash hh)
{
bool didRemove = false;
LockGuard lock(this->WriteLock);
// Verify \a h is managed.
if (this->Data.find(hh) == this->Data.end())
{
return Invalid;
}
// Remove \a hh from \a ss.
auto setHash = this->ComputeInternalAndInsert(ss, lock);
auto it = this->Sets.find(setHash.first);
// Verify \a setHash is managed.
if (it == this->Sets.end())
{
return didRemove;
}
didRemove = this->Sets[setHash.first].erase(hh) > 0;
if (didRemove)
{
if (this->Sets[setHash.first].empty())
{
this->Sets.erase(setHash.first);
}
}
return didRemove;
}
bool vtkStringManager::Remove(Hash ss, Hash hh)
{
bool didRemove = false;
LockGuard lock(this->WriteLock);
auto hit = this->Data.find(hh);
auto sit = this->Sets.find(ss);
// Verify \a h is managed and \a ss is a set.
if (hit == this->Data.end() || sit == this->Sets.end())
{
return false;
}
// Remove \a h from \a ss.
didRemove = this->Sets[ss].erase(hh) > 0;
if (didRemove)
{
if (this->Sets[ss].empty())
{
this->Sets.erase(ss);
}
}
return didRemove;
}
bool vtkStringManager::Contains(const std::string& ss, Hash hh) const
{
LockGuard lock(this->WriteLock);
auto setHash = this->ComputeInternal(ss, lock);
auto sit = this->Sets.find(setHash.first);
return (sit != this->Sets.end() && sit->second.find(hh) != sit->second.end());
}
bool vtkStringManager::Contains(Hash ss, Hash hh) const
{
LockGuard lock(this->WriteLock);
if (ss == Invalid)
{
auto mit = this->Data.find(hh);
return mit != this->Data.end();
}
auto sit = this->Sets.find(ss);
return (sit != this->Sets.end() && sit->second.find(hh) != sit->second.end());
}
vtkStringManager::Visit vtkStringManager::VisitMembers(Visitor visitor, Hash ss) const
{
if (!visitor)
{
return vtkStringManager::Visit::Halt;
}
std::unordered_set<Hash> currentSet;
if (ss == Invalid)
{
// Copy keys from this->Data and iterate over them all.
{
LockGuard guard(this->WriteLock);
for (const auto& entry : this->Data)
{
currentSet.insert(entry.first);
}
}
// Iterate over this->Data.
for (const auto& entry : currentSet)
{
if (visitor(entry) == vtkStringManager::Visit::Halt)
{
return vtkStringManager::Visit::Halt;
}
}
return vtkStringManager::Visit::Continue;
}
// Copy values from this->Sets[hh] and iterate over them all.
{
LockGuard guard(this->WriteLock);
auto sit = this->Sets.find(ss);
if (sit == this->Sets.end())
{
return vtkStringManager::Visit::Continue;
}
currentSet = sit->second;
}
for (const auto& entry : currentSet)
{
if (visitor(entry) == vtkStringManager::Visit::Halt)
{
return vtkStringManager::Visit::Halt;
}
}
return vtkStringManager::Visit::Continue;
}
vtkStringManager::Visit vtkStringManager::VisitSets(Visitor visitor) const
{
if (!visitor)
{
return vtkStringManager::Visit::Halt;
}
std::unordered_set<Hash> setKeys;
{
LockGuard guard(this->WriteLock);
for (const auto& entry : this->Sets)
{
setKeys.insert(entry.first);
}
}
// Iterate over the keys.
for (const auto& key : setKeys)
{
if (visitor(key) == vtkStringManager::Visit::Halt)
{
return vtkStringManager::Visit::Halt;
}
}
return vtkStringManager::Visit::Continue;
}
void vtkStringManager::Reset()
{
LockGuard writeGuard(this->WriteLock);
this->Data.clear();
this->Sets.clear();
}
std::pair<vtkStringManager::Hash, bool> vtkStringManager::ComputeInternal(
const std::string& ss, const LockGuard& vtkNotUsed(proofOfLock)) const
{
/// Same as compute() but does not lock (you must hold WriteLock upon entry).
std::pair<Hash, bool> result{ vtkStringToken::StringHash(ss.data(), ss.size()), false };
while (true)
{
auto it = this->Data.find(result.first);
if (it == this->Data.end())
{
return result;
}
else if (it->second == ss)
{
result.second = true;
return result;
}
vtkWarningMacro(
"String token collision " << ss << " and " << it->second << " both " << it->first << ".");
++result.first;
}
}
std::pair<vtkStringManager::Hash, bool> vtkStringManager::ComputeInternalAndInsert(
const std::string& ss, const LockGuard& proofOfLock)
{
std::pair<Hash, bool> result = this->ComputeInternal(ss, proofOfLock);
if (result.first != Invalid)
{
this->Data[result.first] = ss;
result.second = true;
}
return result;
}
std::size_t vtkStringManager::UnmanageInternal(Hash hh, const LockGuard& proofOfLock)
{
std::size_t num = 0;
auto it = this->Data.find(hh);
if (it == this->Data.end())
{
return num;
}
auto members = this->Sets.find(hh);
if (members != this->Sets.end())
{
// Erase all sets contained in this set recursively.
for (auto member : members->second)
{
num += this->UnmanageInternal(member, proofOfLock);
}
}
num += this->Data.erase(hh);
return num;
}
VTK_ABI_NAMESPACE_END
|