/usr/include/oce/NCollection_IndexedDataMap.hxx is in liboce-foundation-dev 0.17.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 | // Created on: 2002-04-24
// Created by: Alexander KARTOMIN (akm)
// Copyright (c) 2002-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef NCollection_IndexedDataMap_HeaderFile
#define NCollection_IndexedDataMap_HeaderFile
#include <NCollection_BaseMap.hxx>
#include <NCollection_TListNode.hxx>
#include <Standard_TypeMismatch.hxx>
#include <Standard_NoSuchObject.hxx>
#include <NCollection_StlIterator.hxx>
#include <NCollection_DefaultHasher.hxx>
#include <Standard_OutOfRange.hxx>
/**
* Purpose: An indexed map is used to store keys and to bind
* an index to them. Each new key stored in the map
* gets an index. Index are incremented as keys are
* stored in the map. A key can be found by the index
* and an index by the key. No key but the last can
* be removed so the indices are in the range 1..
* Extent. An Item is stored with each key.
*
* This class is similar to IndexedMap from
* NCollection with the Item as a new feature. Note
* the important difference on the operator (). In
* the IndexedMap this operator returns the Key. In
* the IndexedDataMap this operator returns the Item.
*
* See the class Map from NCollection for a
* discussion about the number of buckets.
*/
template < class TheKeyType,
class TheItemType,
class Hasher = NCollection_DefaultHasher<TheKeyType> >
class NCollection_IndexedDataMap : public NCollection_BaseMap
{
//! Adaptation of the TListNode to the INDEXEDDatamap
private:
class IndexedDataMapNode : public NCollection_TListNode<TheItemType>
{
public:
//! Constructor with 'Next'
IndexedDataMapNode (const TheKeyType& theKey1,
const Standard_Integer theKey2,
const TheItemType& theItem,
NCollection_ListNode* theNext1,
NCollection_ListNode* theNext2) :
NCollection_TListNode<TheItemType>(theItem,theNext1),
myKey1(theKey1),
myKey2(theKey2),
myNext2((IndexedDataMapNode*)theNext2)
{
}
//! Key1
TheKeyType& Key1 (void)
{ return myKey1; }
//! Key2
const Standard_Integer& Key2 (void)
{ return myKey2; }
//! Next2
IndexedDataMapNode*& Next2 (void)
{ return myNext2; }
//! Static deleter to be passed to BaseList
static void delNode (NCollection_ListNode * theNode,
Handle(NCollection_BaseAllocator)& theAl)
{
((IndexedDataMapNode *) theNode)->~IndexedDataMapNode();
theAl->Free(theNode);
}
private:
TheKeyType myKey1;
Standard_Integer myKey2;
IndexedDataMapNode * myNext2;
};
public:
//! Implementation of the Iterator interface.
class Iterator
{
public:
//! Empty constructor
Iterator (void) :
myMap(NULL),
myIndex(0) {}
//! Constructor
Iterator (const NCollection_IndexedDataMap& theMap)
#if defined(__BORLANDC__) || defined(__hpux)
: myMap((NCollection_IndexedDataMap <TheKeyType, TheItemType, Hasher> *) &theMap),
#else
: myMap ((NCollection_IndexedDataMap* )&theMap),
#endif
myNode (myMap->nodeFromIndex (1)),
myIndex (1) {}
//! Query if the end of collection is reached by iterator
Standard_Boolean More(void) const
{ return (myMap != NULL) && (myIndex <= myMap->Extent()); }
//! Make a step along the collection
void Next(void)
{
myNode = myMap->nodeFromIndex (++myIndex);
}
//! Value access
const TheItemType& Value(void) const
{
Standard_NoSuchObject_Raise_if(!More(), "NCollection_IndexedDataMap::Iterator::Value");
return myNode->Value();
}
//! ChangeValue access
TheItemType& ChangeValue(void) const
{
Standard_NoSuchObject_Raise_if(!More(), "NCollection_IndexedDataMap::Iterator::ChangeValue");
return myNode->ChangeValue();
}
//! Key
const TheKeyType& Key() const
{
Standard_NoSuchObject_Raise_if(!More(), "NCollection_IndexedDataMap::Iterator::Key");
return myNode->Key1();
}
//! Performs comparison of two iterators.
Standard_Boolean IsEqual (const Iterator& theOther) const
{
return myMap == theOther.myMap &&
myNode == theOther.myNode &&
myIndex == theOther.myIndex;
}
private:
NCollection_IndexedDataMap* myMap; //!< Pointer to the map being iterated
IndexedDataMapNode* myNode; //!< Current node
Standard_Integer myIndex; //!< Current index
};
//! Shorthand for a regular iterator type.
typedef NCollection_StlIterator<std::forward_iterator_tag, Iterator, TheItemType, false> iterator;
//! Shorthand for a constant iterator type.
typedef NCollection_StlIterator<std::forward_iterator_tag, Iterator, TheItemType, true> const_iterator;
//! Returns an iterator pointing to the first element in the map.
iterator begin() const { return Iterator (*this); }
//! Returns an iterator referring to the past-the-end element in the map.
iterator end() const { return Iterator(); }
//! Returns a const iterator pointing to the first element in the map.
const_iterator cbegin() const { return Iterator (*this); }
//! Returns a const iterator referring to the past-the-end element in the map.
const_iterator cend() const { return Iterator(); }
public:
// ---------- PUBLIC METHODS ------------
//! Constructor
NCollection_IndexedDataMap (const Standard_Integer NbBuckets=1,
const Handle(NCollection_BaseAllocator)& theAllocator = 0L)
: NCollection_BaseMap (NbBuckets, Standard_False, theAllocator) {}
//! Copy constructor
NCollection_IndexedDataMap (const NCollection_IndexedDataMap& theOther)
: NCollection_BaseMap (theOther.NbBuckets(), Standard_False, theOther.myAllocator)
{ *this = theOther; }
//! Exchange the content of two maps without re-allocations.
//! Notice that allocators will be swapped as well!
void Exchange (NCollection_IndexedDataMap& theOther)
{
this->exchangeMapsData (theOther);
}
//! Assignment.
//! This method does not change the internal allocator.
NCollection_IndexedDataMap& Assign (const NCollection_IndexedDataMap& theOther)
{
if (this == &theOther)
return *this;
Clear();
ReSize (theOther.Extent()-1);
Standard_Integer i;
for (i=1; i<=theOther.Extent(); i++)
{
TheKeyType aKey1 = theOther.FindKey(i);
TheItemType anItem = theOther.FindFromIndex(i);
Standard_Integer iK1 = Hasher::HashCode (aKey1, NbBuckets());
Standard_Integer iK2 = ::HashCode (i, NbBuckets());
IndexedDataMapNode * pNode =
new (this->myAllocator) IndexedDataMapNode (aKey1, i, anItem,
myData1[iK1], myData2[iK2]);
myData1[iK1] = pNode;
myData2[iK2] = pNode;
Increment();
}
return *this;
}
//! Assignment operator
NCollection_IndexedDataMap& operator= (const NCollection_IndexedDataMap& theOther)
{
return Assign (theOther);
}
//! ReSize
void ReSize (const Standard_Integer N)
{
NCollection_ListNode** ppNewData1 = NULL;
NCollection_ListNode** ppNewData2 = NULL;
Standard_Integer newBuck;
if (BeginResize (N, newBuck, ppNewData1, ppNewData2))
{
if (myData1)
{
IndexedDataMapNode *p, *q;
Standard_Integer i, iK1, iK2;
for (i = 0; i <= NbBuckets(); i++)
{
if (myData1[i])
{
p = (IndexedDataMapNode *) myData1[i];
while (p)
{
iK1 = Hasher::HashCode (p->Key1(), newBuck);
iK2 = ::HashCode (p->Key2(), newBuck);
q = (IndexedDataMapNode*) p->Next();
p->Next() = ppNewData1[iK1];
p->Next2() = (IndexedDataMapNode*)ppNewData2[iK2];
ppNewData1[iK1] = p;
ppNewData2[iK2] = p;
p = q;
}
}
}
}
EndResize (N, newBuck, ppNewData1, ppNewData2);
}
}
//! Add
Standard_Integer Add (const TheKeyType& theKey1, const TheItemType& theItem)
{
if (Resizable())
ReSize(Extent());
Standard_Integer iK1 = Hasher::HashCode (theKey1, NbBuckets());
IndexedDataMapNode * pNode;
pNode = (IndexedDataMapNode *) myData1[iK1];
while (pNode)
{
if (Hasher::IsEqual (pNode->Key1(), theKey1))
return pNode->Key2();
pNode = (IndexedDataMapNode *) pNode->Next();
}
Increment();
Standard_Integer iK2 = ::HashCode(Extent(),NbBuckets());
pNode = new (this->myAllocator) IndexedDataMapNode (theKey1, Extent(), theItem,
myData1[iK1], myData2[iK2]);
myData1[iK1] = pNode;
myData2[iK2] = pNode;
return Extent();
}
//! Contains
Standard_Boolean Contains (const TheKeyType& theKey1) const
{
if (IsEmpty())
return Standard_False;
Standard_Integer iK1 = Hasher::HashCode (theKey1, NbBuckets());
IndexedDataMapNode * pNode1;
pNode1 = (IndexedDataMapNode *) myData1[iK1];
while (pNode1)
{
if (Hasher::IsEqual(pNode1->Key1(), theKey1))
return Standard_True;
pNode1 = (IndexedDataMapNode *) pNode1->Next();
}
return Standard_False;
}
//! Substitute
void Substitute (const Standard_Integer theIndex,
const TheKeyType& theKey1,
const TheItemType& theItem)
{
Standard_OutOfRange_Raise_if (theIndex < 1 || theIndex > Extent(), "NCollection_IndexedDataMap::Substitute");
IndexedDataMapNode * p;
// check if theKey1 is not already in the map
Standard_Integer iK1 = Hasher::HashCode (theKey1, NbBuckets());
p = (IndexedDataMapNode *) myData1[iK1];
while (p)
{
if (Hasher::IsEqual (p->Key1(), theKey1))
Standard_DomainError::Raise("NCollection_IndexedDataMap::Substitute");
p = (IndexedDataMapNode *) p->Next();
}
// Find the node for the index I
Standard_Integer iK2 = ::HashCode (theIndex, NbBuckets());
p = (IndexedDataMapNode *) myData2[iK2];
while (p)
{
if (p->Key2() == theIndex)
break;
p = (IndexedDataMapNode*) p->Next2();
}
// remove the old key
Standard_Integer iK = Hasher::HashCode (p->Key1(), NbBuckets());
IndexedDataMapNode * q = (IndexedDataMapNode *) myData1[iK];
if (q == p)
myData1[iK] = (IndexedDataMapNode *) p->Next();
else
{
while (q->Next() != p)
q = (IndexedDataMapNode *) q->Next();
q->Next() = p->Next();
}
// update the node
p->Key1() = theKey1;
p->ChangeValue() = theItem;
p->Next() = myData1[iK1];
myData1[iK1] = p;
}
//! RemoveLast
void RemoveLast (void)
{
Standard_OutOfRange_Raise_if (Extent() == 0, "NCollection_IndexedDataMap::RemoveLast");
IndexedDataMapNode * p, * q;
// Find the node for the last index and remove it
Standard_Integer iK2 = ::HashCode (Extent(), NbBuckets());
p = (IndexedDataMapNode *) myData2[iK2];
q = NULL;
while (p)
{
if (p->Key2() == Extent())
break;
q = p;
p = (IndexedDataMapNode*) p->Next2();
}
if (q == NULL)
myData2[iK2] = (IndexedDataMapNode *) p->Next2();
else
q->Next2() = p->Next2();
// remove the key
Standard_Integer iK1 = Hasher::HashCode (p->Key1(), NbBuckets());
q = (IndexedDataMapNode *) myData1[iK1];
if (q == p)
myData1[iK1] = (IndexedDataMapNode *) p->Next();
else
{
while (q->Next() != p)
q = (IndexedDataMapNode *) q->Next();
q->Next() = p->Next();
}
p->~IndexedDataMapNode();
this->myAllocator->Free(p);
Decrement();
}
//! FindKey
const TheKeyType& FindKey (const Standard_Integer theKey2) const
{
Standard_OutOfRange_Raise_if (theKey2 < 1 || theKey2 > Extent(), "NCollection_IndexedDataMap::FindKey");
IndexedDataMapNode* aNode = nodeFromIndex (theKey2);
if (aNode == NULL)
{
Standard_NoSuchObject::Raise ("NCollection_IndexedDataMap::FindKey");
}
return aNode->Key1();
}
//! FindFromIndex
const TheItemType& FindFromIndex (const Standard_Integer theKey2) const
{
Standard_OutOfRange_Raise_if (theKey2 < 1 || theKey2 > Extent(), "NCollection_IndexedDataMap::FindFromIndex");
IndexedDataMapNode* aNode = nodeFromIndex (theKey2);
if (aNode == NULL)
{
Standard_NoSuchObject::Raise ("NCollection_IndexedDataMap::FindFromIndex");
}
return aNode->Value();
}
//! operator ()
const TheItemType& operator() (const Standard_Integer theKey2) const
{ return FindFromIndex (theKey2); }
//! ChangeFromIndex
TheItemType& ChangeFromIndex (const Standard_Integer theKey2)
{
Standard_OutOfRange_Raise_if (theKey2 < 1 || theKey2 > Extent(), "NCollection_IndexedDataMap::ChangeFromIndex");
IndexedDataMapNode* aNode = nodeFromIndex (theKey2);
if (aNode == NULL)
{
Standard_NoSuchObject::Raise ("NCollection_IndexedDataMap::ChangeFromIndex");
}
return aNode->ChangeValue();
}
//! operator ()
TheItemType& operator() (const Standard_Integer theKey2)
{ return ChangeFromIndex (theKey2); }
//! FindIndex
Standard_Integer FindIndex(const TheKeyType& theKey1) const
{
if (IsEmpty()) return 0;
IndexedDataMapNode * pNode1 =
(IndexedDataMapNode *) myData1[Hasher::HashCode(theKey1,NbBuckets())];
while (pNode1)
{
if (Hasher::IsEqual (pNode1->Key1(), theKey1))
return pNode1->Key2();
pNode1 = (IndexedDataMapNode*) pNode1->Next();
}
return 0;
}
//! FindFromKey
const TheItemType& FindFromKey(const TheKeyType& theKey1) const
{
Standard_NoSuchObject_Raise_if (IsEmpty(), "NCollection_IndexedDataMap::FindFromKey");
IndexedDataMapNode * pNode1 =
(IndexedDataMapNode *) myData1[Hasher::HashCode(theKey1,NbBuckets())];
while (pNode1)
{
if (Hasher::IsEqual (pNode1->Key1(), theKey1))
return pNode1->Value();
pNode1 = (IndexedDataMapNode*) pNode1->Next();
}
Standard_NoSuchObject::Raise("NCollection_IndexedDataMap::FindFromKey");
return pNode1->Value();
}
//! ChangeFromKey
TheItemType& ChangeFromKey (const TheKeyType& theKey1)
{
Standard_NoSuchObject_Raise_if (IsEmpty(), "NCollection_IndexedDataMap::ChangeFromKey");
IndexedDataMapNode * pNode1 =
(IndexedDataMapNode *) myData1[Hasher::HashCode(theKey1,NbBuckets())];
while (pNode1)
{
if (Hasher::IsEqual (pNode1->Key1(), theKey1))
return pNode1->ChangeValue();
pNode1 = (IndexedDataMapNode*) pNode1->Next();
}
Standard_NoSuchObject::Raise("NCollection_IndexedDataMap::ChangeFromKey");
return pNode1->ChangeValue();
}
//! Find value for key with copying.
//! @return true if key was found
Standard_Boolean FindFromKey (const TheKeyType& theKey1,
TheItemType& theValue) const
{
if (IsEmpty())
{
return Standard_False;
}
for (IndexedDataMapNode* aNode = (IndexedDataMapNode* )myData1[Hasher::HashCode (theKey1, NbBuckets())];
aNode != NULL; aNode = (IndexedDataMapNode* )aNode->Next())
{
if (Hasher::IsEqual (aNode->Key1(), theKey1))
{
theValue = aNode->Value();
return Standard_True;
}
}
return Standard_False;
}
//! Clear data. If doReleaseMemory is false then the table of
//! buckets is not released and will be reused.
void Clear(const Standard_Boolean doReleaseMemory = Standard_True)
{ Destroy (IndexedDataMapNode::delNode, doReleaseMemory); }
//! Clear data and reset allocator
void Clear (const Handle(NCollection_BaseAllocator)& theAllocator)
{
Clear();
this->myAllocator = ( ! theAllocator.IsNull() ? theAllocator :
NCollection_BaseAllocator::CommonBaseAllocator() );
}
//! Destructor
~NCollection_IndexedDataMap (void)
{ Clear(); }
//! Size
Standard_Integer Size(void) const
{ return Extent(); }
private:
// ----------- PRIVATE METHODS -----------
//! Find map node associated with specified index.
//! Return NULL if not found (exception-free internal implementation).
IndexedDataMapNode* nodeFromIndex (const Standard_Integer theKey2) const
{
if (Extent() == 0)
{
return NULL;
}
for (IndexedDataMapNode* aNode = (IndexedDataMapNode* )myData2[::HashCode (theKey2, NbBuckets())];
aNode != NULL; aNode = (IndexedDataMapNode* )aNode->Next2())
{
if (aNode->Key2() == theKey2)
{
return aNode;
}
}
return NULL;
}
};
#endif
|