/usr/include/ni/XnHashT.h is in libopenni-dev 1.5.4.0-8.
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 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 | #ifndef _XN_HASH_T_H_
#define _XN_HASH_T_H_
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnOS.h>
#include <XnListT.h>
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
typedef XnUInt8 XnHashCode;
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
template<class _TKey, class _TValue>
struct XnKeyValuePair
{
typedef _TKey TKey;
typedef _TValue TValue;
XnKeyValuePair() : key(TKey()), value(TValue()) {}
XnKeyValuePair(TKey key, TValue value) : key(key), value(value) {}
XnKeyValuePair(const XnKeyValuePair& other) : key(other.key), value(other.value) {}
public:
TKey const& Key() const { return key; }
TValue const& Value() const { return value; }
TValue& Value() { return value; }
private:
TKey key;
TValue value;
};
template<class TKey>
class XnDefaultKeyManagerT
{
public:
static XnHashCode Hash(TKey const& key)
{
return (((XnSizeT)key) & 0xff);
}
static XnInt32 Compare(TKey const& key1, TKey const& key2)
{
return XnInt32(XnSizeT(key1)-XnSizeT(key2));
}
};
template<class TKey,
class TValue,
class TKeyManager = XnDefaultKeyManagerT<TKey>,
class TAlloc = XnLinkedNodeDefaultAllocatorT<XnKeyValuePair<TKey, TValue> > >
class XnHashT
{
public:
typedef XnKeyValuePair<TKey, TValue> TPair;
typedef XnListT<TPair, TAlloc> TPairList;
enum
{
LAST_BIN = (1 << (sizeof(XnHashCode)*8)),
NUM_BINS = LAST_BIN + 1,
};
class ConstIterator
{
public:
ConstIterator() : m_ppBins(NULL), m_nCurrBin(0)
{}
ConstIterator(TPairList* const* apBins, XnUInt32 nCurrBin, typename TPairList::ConstIterator currIt)
: m_ppBins(apBins), m_nCurrBin(nCurrBin), m_currIt(currIt)
{
if (nCurrBin != LAST_BIN && m_currIt == m_ppBins[m_nCurrBin]->End())
{
// this does not point to an actual entry. run to next one.
++*this;
}
}
ConstIterator(const ConstIterator& other)
: m_ppBins(other.m_ppBins), m_nCurrBin(other.m_nCurrBin), m_currIt(other.m_currIt)
{}
/**
* Support ++iterator, go to the next object in the list
*/
ConstIterator& operator++()
{
XN_ASSERT(m_nCurrBin != LAST_BIN);
// increment internal bin iterator
if (m_currIt != m_ppBins[m_nCurrBin]->End())
{
++m_currIt;
}
// check if we need to move to next bin
if (m_currIt == m_ppBins[m_nCurrBin]->End())
{
// go forward through bins, until we either reach the end or a non-empty bin
do
{
++m_nCurrBin;
} while (m_nCurrBin < LAST_BIN &&
(m_ppBins[m_nCurrBin] == NULL || m_ppBins[m_nCurrBin]->IsEmpty()));
m_currIt = m_ppBins[m_nCurrBin]->Begin();
}
return *this;
}
/**
* Support iterator++, go to the next object in the list, returning the old value
*/
ConstIterator operator++(int)
{
ConstIterator retVal(*this);
++*this;
return retVal;
}
/**
* Support --iterator, go to the previous object in the hash
*/
ConstIterator& operator--()
{
XN_ASSERT(m_nCurrBin != LAST_BIN);
// decrement internal bin iterator
if (m_currIt != m_ppBins[m_nCurrBin]->ReverseEnd())
{
--m_currIt;
}
// check if we need to move to previous bin
if (m_currIt == m_ppBins[m_nCurrBin]->ReverseEnd())
{
// go backwards through bins, until we either reach the end or a non-empty bin
do
{
if (m_nCurrBin == 0)
{
m_nCurrBin = LAST_BIN;
break;
}
else
{
--m_nCurrBin;
}
} while (m_ppBins[m_nCurrBin] == NULL || m_ppBins[m_nCurrBin]->IsEmpty());
m_currIt = m_ppBins[m_nCurrBin]->Begin();
}
return *this;
}
/**
* Support iterator--, go to the previous object in the hash, returning the old value
*/
ConstIterator operator--(int)
{
ConstIterator retVal(*this);
--*this;
return retVal;
}
/**
* Operator to check if two iterators point to the same object
*
* @param other [in] instance to compare with
*/
inline XnBool operator==(const ConstIterator& other) const
{
return m_currIt == other.m_currIt;
}
/**
* Operator to check if two iterators point to different objects
*
* @param other [in] instance to compare with
*/
inline XnBool operator!=(const ConstIterator& other) const
{
return m_currIt != other.m_currIt;
}
/**
* Get the value of the current object (const version)
*/
inline TPair const& operator*() const
{
return *m_currIt;
}
/**
* Get a pointer to the value of the current object (const version)
*/
inline TPair const* operator->() const
{
return m_currIt.operator->();
}
protected:
friend class XnHashT;
TPairList* const* m_ppBins;
XnUInt32 m_nCurrBin;
typename TPairList::ConstIterator m_currIt;
};
class Iterator : public ConstIterator
{
public:
Iterator() : ConstIterator()
{}
Iterator(TPairList** apBins, XnUInt32 nCurrBin, typename TPairList::Iterator currIt)
: ConstIterator(apBins, nCurrBin, currIt)
{}
Iterator(const Iterator& other) : ConstIterator(other)
{}
/**
* Support ++iterator, go to the next object in the list
*/
Iterator& operator++()
{
++(*(ConstIterator*)this);
return (*this);
}
/**
* Support iterator++, go to the next object in the list, returning the old value
*/
inline Iterator operator++(int)
{
Iterator retVal(*this);
++*this;
return (retVal);
}
/**
* Support --iterator, go to the next object in the list
*/
inline Iterator& operator--()
{
--(*(ConstIterator*)this);
return (*this);
}
/**
* Support iterator--, go to the next object in the list, returning the old value
*/
inline Iterator operator--(int)
{
Iterator retVal(*this);
--*this;
return (retVal);
}
/**
* Get the value of the current object
*/
inline TPair& operator*() const
{
return const_cast<TPair&>(*this->m_currIt);
}
/**
* Get a pointer to the value of the current object
*/
inline TPair* operator->() const
{
return const_cast<TPair*>(this->m_currIt.operator->());
}
};
XnHashT()
{
Init();
}
XnHashT(const XnHashT& other)
{
Init();
*this = other;
}
XnHashT& operator=(const XnHashT& other)
{
Clear();
XnStatus nRetVal = XN_STATUS_OK;
for (ConstIterator it = other.Begin(); it != other.End(); ++it)
{
nRetVal = Set(it->Key(), it->Value());
XN_ASSERT(nRetVal == XN_STATUS_OK);
}
return *this;
}
~XnHashT()
{
// NOTE: we don't want to delete LAST_BIN (it points to the m_lastBin member)
for (XnUInt32 i = 0; i < LAST_BIN; ++i)
{
if (m_apBins[i] != NULL)
{
XN_DELETE(m_apBins[i]);
}
}
}
/**
* An iterator to the first entry of the list (non-const version)
*/
Iterator Begin()
{
return Iterator(m_apBins, m_nMinBin, m_apBins[m_nMinBin]->Begin());
}
/**
* An iterator to the first entry of the list (const version)
*/
ConstIterator Begin() const
{
return ConstIterator(m_apBins, m_nMinBin, m_apBins[m_nMinBin]->Begin());
}
/**
* An iterator 1to the end of the list (non-const version). The position is invalid.
*/
Iterator End()
{
return Iterator(m_apBins, LAST_BIN, m_apBins[LAST_BIN]->Begin());
}
/**
* An iterator to the end of the list (const version). The position is invalid.
*/
ConstIterator End() const
{
return ConstIterator(m_apBins, LAST_BIN, m_apBins[LAST_BIN]->Begin());
}
/**
* Set a new key-value entry. If key exists, will replace value.
*
* @param key [in] The key to which to associate the value
* @param value [in] The value to add to the XnHash
*/
XnStatus Set(const TKey& key, const TValue& value)
{
XnHashCode nHash = TKeyManager::Hash(key);
// check if bin exists
if (m_apBins[nHash] == NULL)
{
// create it
XN_VALIDATE_NEW(m_apBins[nHash], TPairList);
if (nHash < m_nMinBin)
{
m_nMinBin = nHash;
}
}
// now check if key is already in the bin
for (typename TPairList::Iterator it = m_apBins[nHash]->Begin(); it != m_apBins[nHash]->End(); ++it)
{
if (TKeyManager::Compare(it->Key(), key) == 0)
{
// replace it
it->Value() = value;
return (XN_STATUS_OK);
}
}
// if we got here, key is not in bin. Add it.
return m_apBins[nHash]->AddLast(TPair(key, value));
}
/**
* Get an iterator pointing to the pair in the hash (const version).
*
* @param key [in] The searched key
*
* @return End() if value doesn't exist
*/
ConstIterator Find(TKey const& key) const
{
XnUInt32 nBin = LAST_BIN;
typename TPairList::ConstIterator it;
if (TRUE == Find(key, nBin, it))
{
return ConstIterator(m_apBins, nBin, it);
}
else
{
return End();
}
}
/**
* Get an iterator pointing to the pair in the hash.
*
* @param key [in] The searched key
*
* @return End() if value doesn't exist
*/
Iterator Find(TKey const& key)
{
XnUInt32 nBin = LAST_BIN;
typename TPairList::Iterator it;
if (TRUE == Find(key, nBin, it))
{
return Iterator(m_apBins, nBin, it);
}
else
{
return End();
}
}
/**
* Get an iterator pointing to the pair in the hash (const version).
*
* @param key [in] The searched key
* @param it [out] An iterator to the entry in the hash.
*
* @return XN_STATUS_NO_MATCH if value doesn't exist
*/
XnStatus Find(TKey const& key, ConstIterator& it) const
{
it = Find(key);
return (it == End() ? XN_STATUS_NO_MATCH : XN_STATUS_OK);
}
/**
* Get an iterator pointing to the pair in the hash (const version).
*
* @param key [in] The searched key
* @param it [out] An iterator to the entry in the hash.
*
* @return XN_STATUS_NO_MATCH if value doesn't exist
*/
XnStatus Find(TKey const& key, Iterator& it)
{
it = Find(key);
return (it == End() ? XN_STATUS_NO_MATCH : XN_STATUS_OK);
}
/**
* Get the value associated with the supplied key
*
* @param key [in] The key of the entry
* @param value [out] The retrieved value
*
* @return XN_STATUS_NO_MATCH if no such key exists
*/
XnStatus Get(TKey const& key, TValue& value) const
{
ConstIterator it = Find(key);
if (it == End())
{
return XN_STATUS_NO_MATCH;
}
else
{
value = it->Value();
return XN_STATUS_OK;
}
}
/**
* Get a pointer to the value associated with the supplied key
*
* @param key [in] The key of the entry
* @param pValue [out] A const pointer to the value that is stored in the hash.
*
* @return XN_STATUS_NO_MATCH if no such key exists
*/
XnStatus Get(TKey const& key, TValue const*& pValue) const
{
ConstIterator it = Find(key);
if (it == End())
{
return XN_STATUS_NO_MATCH;
}
else
{
pValue = &it->Value();
return XN_STATUS_OK;
}
}
/**
* Get the value associated with the supplied key
*
* @param key [in] The key of the entry
* @param value [out] The retrieved value
*
* @return XN_STATUS_NO_MATCH if no such key exists
*/
XnStatus Get(TKey const& key, TValue& value)
{
Iterator it = Find(key);
if (it == End())
{
return XN_STATUS_NO_MATCH;
}
else
{
value = it->Value();
return XN_STATUS_OK;
}
}
/**
* Get a pointer to the value associated with the supplied key
*
* @param key [in] The key of the entry
* @param pValue [out] A pointer to the value that is stored in the hash
*
* @return XN_STATUS_NO_MATCH if no such key exists
*/
XnStatus Get(TKey const& key, TValue*& pValue)
{
Iterator it = Find(key);
if (it == End())
{
return XN_STATUS_NO_MATCH;
}
else
{
pValue = &it->Value();
return XN_STATUS_OK;
}
}
/**
* Gets a reference to the value of a specific key. If this key is not in the hash, it will be added.
*
* @param key [in] The key of the entry.
*/
TValue& operator[](TKey const& key)
{
XnStatus nRetVal = XN_STATUS_OK;
Iterator it = Find(key);
if (it == End())
{
nRetVal = Set(key, TValue());
XN_ASSERT(nRetVal == XN_STATUS_OK);
it = Find(key);
XN_ASSERT(it != End());
}
return it->Value();
}
XnStatus Remove(ConstIterator it)
{
// Verify iterator is valid
if (it == End())
{
XN_ASSERT(FALSE);
return XN_STATUS_ILLEGAL_POSITION;
}
XN_ASSERT(m_apBins == it.m_ppBins);
XN_ASSERT(m_apBins[it.m_nCurrBin] != NULL);
return m_apBins[it.m_nCurrBin]->Remove(it.m_currIt);
}
XnStatus Remove(TKey const& key)
{
ConstIterator it = Find(key);
if (it != End())
{
return Remove(it);
}
else
{
return XN_STATUS_NO_MATCH;
}
}
/**
* Remove all entries from the XnHash.
*/
XnStatus Clear()
{
while (Begin() != End())
Remove(Begin());
return XN_STATUS_OK;
}
/**
* Checks if hash is empty.
*/
XnBool IsEmpty() const
{
return (Begin() == End());
}
/**
* Gets the number of entries in the hash.
*/
XnUInt32 Size() const
{
XnUInt32 nSize = 0;
for (ConstIterator iter = Begin(); iter != End(); ++iter, ++nSize)
;
return nSize;
}
private:
XnBool Find(TKey const& key, XnUInt32& nBin, typename TPairList::ConstIterator& currIt) const
{
XnHashCode nHash = TKeyManager::Hash(key);
if (m_apBins[nHash] != NULL)
{
// look for value in bin
for (typename TPairList::ConstIterator it = m_apBins[nHash]->Begin(); it != m_apBins[nHash]->End(); ++it)
{
if (TKeyManager::Compare(it->Key(), key) == 0)
{
nBin = nHash;
currIt = it;
return TRUE;
}
}
}
// if we got here, key wasn't found
return FALSE;
}
void Init()
{
xnOSMemSet(m_apBins, 0, sizeof(m_apBins));
m_apBins[LAST_BIN] = &m_lastBin;
m_nMinBin = LAST_BIN;
}
TPairList* m_apBins[NUM_BINS];
TPairList m_lastBin;
XnUInt32 m_nMinBin;
};
#endif // _XN_HASH_T_H_
|