/usr/include/ni/XnHash.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 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 | /****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
#ifndef _XN_HASH_H
#define _XN_HASH_H
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnList.h"
//---------------------------------------------------------------------------
// Defines
//---------------------------------------------------------------------------
#define XN_HASH_LAST_BIN 256
#define XN_HASH_NUM_BINS (XN_HASH_LAST_BIN + 1)
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
/**
* The key to the hash
*/
typedef XnValue XnKey;
/**
* The hash value - the output of the hash function
*/
typedef XnUInt8 XnHashValue;
/**
* Default Hash function
*/
static XnHashValue XnDefaultHashFunction(const XnKey& key)
{
return (XnSizeT(key) & 0xff);
}
/**
* Default Compare function
*/
static XnInt32 XnDefaultCompareFunction(const XnKey& key1, const XnKey& key2)
{
return XnInt32(XnSizeT(key1)-XnSizeT(key2));
}
/**
* The hash - associative array
*/
class XnHash
{
public:
/**
* Iterator to the XnHash
*/
class ConstIterator
{
public:
friend class XnHash;
/**
* Copy constructor
*
* @param other [in] instance to copy from
*/
ConstIterator(const ConstIterator& other) :
m_pHash(other.m_pHash), m_nCurrentBin(other.m_nCurrentBin), m_Iterator(other.m_Iterator) {}
/**
* Support ++iterator, go to the next object in the hash
*/
ConstIterator& operator++()
{
++m_Iterator;
while (m_Iterator == m_pHash->m_Bins[m_nCurrentBin]->end() &&
m_Iterator != m_pHash->m_Bins[XN_HASH_LAST_BIN]->end())
{
do
{
m_nCurrentBin++;
} while (m_pHash->m_Bins[m_nCurrentBin] == NULL);
m_Iterator = m_pHash->m_Bins[m_nCurrentBin]->begin();
}
return *this;
}
/**
* Support iterator++, go to the next object in the hash, returning the old value
*/
ConstIterator operator++(int)
{
XnHash::ConstIterator other(*this);
++*this;
return other;
}
/**
* Support --iterator, go to the previous object in the hash
*/
ConstIterator& operator--()
{
--m_Iterator;
while (m_Iterator == m_pHash->m_Bins[m_nCurrentBin]->end() &&
m_Iterator != m_pHash->m_Bins[XN_HASH_LAST_BIN]->end())
{
do
{
if (m_nCurrentBin == 0)
{
m_nCurrentBin = XN_HASH_LAST_BIN;
m_Iterator = m_pHash->m_Bins[XN_HASH_LAST_BIN]->end();
return *this;
}
m_nCurrentBin--;
} while (m_pHash->m_Bins[m_nCurrentBin] == NULL);
m_Iterator = m_pHash->m_Bins[m_nCurrentBin]->rbegin();
}
return *this;
}
/**
* Support iterator--, go to the previous object in the hash, returning the old value
*/
ConstIterator operator--(int)
{
ConstIterator other(*this);
--*this;
return other;
}
/**
* Operator to check if 2 iterators point to the same object
*
* @param other [in] instance to compare with
*/
XnBool operator==(const ConstIterator& other) const
{
return m_Iterator == other.m_Iterator;
}
/**
* Operator to check if 2 iterators point to different objects
*
* @param other [in] instance to compare with
*/
XnBool operator!=(const ConstIterator& other) const
{
return m_Iterator != other.m_Iterator;
}
/**
* Get the key of the current object (const version)
*/
const XnKey& Key() const
{
return ((XnNode*)(*m_Iterator))->Data();
}
/**
* Get the value of the current object (const version)
*/
const XnValue& Value() const
{
return ((XnNode*)(*m_Iterator))->Next()->Data();
}
/**
* Get the entire current object (non-const version)
*/
XnNode* GetNode()
{
return m_Iterator.GetNode();
}
/**
* Get the entire current object (const version)
*/
const XnNode* GetNode() const
{
return m_Iterator.GetNode();
}
protected:
/**
* constructor to be used from inside the XnHash
*
* @param pHash [in] The hash to which the iterator belongs
* @param nBin [in] The bin of the current object
* @param listIterator [in] Iterator on the bin (each bin is a XnList)
*/
ConstIterator(const XnHash* pHash, XnUInt16 nBin, XnList::Iterator listIterator) :
m_pHash(pHash), m_nCurrentBin(nBin), m_Iterator(listIterator)
{
// Find the first valid
while (m_Iterator == m_pHash->m_Bins[m_nCurrentBin]->end() &&
m_Iterator != m_pHash->m_Bins[XN_HASH_LAST_BIN]->end())
{
do
{
m_nCurrentBin++;
} while (m_pHash->m_Bins[m_nCurrentBin] == NULL);
m_Iterator = m_pHash->m_Bins[m_nCurrentBin]->begin();
}
}
/**
* constructor to be used from inside the XnHash. It points to the first value in the hash.
*
* @param pHash [in] The hash to which the iterator belongs
*/
ConstIterator(const XnHash* pHash) :
m_pHash(pHash), m_nCurrentBin(0), m_Iterator(m_pHash->m_Bins[XN_HASH_LAST_BIN]->end()) {}
/** The hash to which the iterator belongs */
const XnHash* m_pHash;
/** The bin of the current object */
XnUInt16 m_nCurrentBin;
/** Iterator for the specific bin */
XnList::Iterator m_Iterator;
};
/**
* Iterator to the XnHash
*/
class Iterator : public ConstIterator
{
public:
friend class XnHash;
/**
* Copy constructor
*
* @param other [in] instance to copy from
*/
inline Iterator(const Iterator& other) : ConstIterator(other) {}
/**
* 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 result = *this;
++*this;
return (result);
}
/**
* 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 result = *this;
--*this;
return (result);
}
/**
* Get the key of the current object (const version)
*/
XnKey& Key() const { return (XnKey&)ConstIterator::Key(); }
/**
* Get the value of the current object (const version)
*/
XnValue& Value() const { return (XnValue&)ConstIterator::Value(); }
protected:
/**
* constructor to be used from inside the XnHash
*
* @param pHash [in] The hash to which the iterator belongs
* @param nBin [in] The bin of the current object
* @param listIterator [in] Iterator on the bin (each bin is a XnList)
*/
Iterator(const XnHash* pHash, XnUInt16 nBin, XnList::Iterator listIterator) :
ConstIterator(pHash, nBin, listIterator)
{}
/**
* constructor to be used from inside the XnHash. It points to the first value in the hash.
*
* @param pHash [in] The hash to which the iterator belongs
*/
Iterator(const XnHash* pHash) : ConstIterator(pHash) {}
Iterator(const ConstIterator& other) : ConstIterator(other) {}
};
friend class ConstIterator;
public:
/**
* Definition of a hash function - receive key, and outputs HashValue
*/
typedef XnHashValue (*XnHashFunction)(const XnKey& key);
/**
* Definition of comparison function - receives 2 keys, returns 0 on equality
*/
typedef XnInt32 (*XnCompareFunction)(const XnKey& key1, const XnKey& key2);
/**
* Constructor. Initializes internal representations.
*/
XnHash()
{
m_nInitStatus = Init();
}
/**
* Destructor. Destroys internal representations.
*/
virtual ~XnHash()
{
if (m_Bins != NULL)
{
for (int i = 0; i < XN_HASH_NUM_BINS; ++i)
{
XN_DELETE(m_Bins[i]);
}
XN_DELETE_ARR(m_Bins);
}
}
/**
* Returns the status of the initialization of the hash.
* @returns XN_STATUS_OK if the hash was initialized successfully, or an error code otherwise
* (e.g. if memory could not be allocated).
*/
XnStatus GetInitStatus() const
{
return m_nInitStatus;
}
/**
* 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 XnKey& key, const XnValue& value)
{
XnHashValue HashValue = (*m_HashFunction)(key);
// Check if key already exists
if (m_Bins[HashValue] != NULL)
{
Iterator hiter(this);
if (Find(key, HashValue, hiter) == XN_STATUS_OK)
{
// Replace value
hiter.Value() = value;
return XN_STATUS_OK;
}
}
else
{
// First time trying to access this bin, create it.
m_Bins[HashValue] = XN_NEW(XnList);
if (m_Bins[HashValue] == NULL)
{
return XN_STATUS_ALLOC_FAILED;
}
if (HashValue < m_nMinBin)
m_nMinBin = HashValue;
}
// Get a new node for the key
XnNode* pKeyNode = XnNode::Allocate();
if (pKeyNode == NULL)
{
return XN_STATUS_ALLOC_FAILED;
}
pKeyNode->Data() = key;
// Get a new node for the value
XnNode* pValueNode = XnNode::Allocate();
if (pValueNode == NULL)
{
XnNode::Deallocate(pKeyNode);
return XN_STATUS_ALLOC_FAILED;
}
pValueNode->Data() = value;
// Concatenate the value node to the key node
pKeyNode->Next() = pValueNode;
pValueNode->Next() = NULL;
// Add the 2 nodes as the value to the key's list
XnStatus ListStatus = m_Bins[HashValue]->AddLast(XnValue(pKeyNode));
if (ListStatus != XN_STATUS_OK)
{
// Add failed. return the 2 nodes to the pool
XnNode::Deallocate(pKeyNode);
XnNode::Deallocate(pValueNode);
return ListStatus;
}
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(const XnKey& key, XnValue& value) const
{
// Check if key exists
Iterator hiter(this);
XnStatus FindStatus = Find(key, hiter);
if (FindStatus != XN_STATUS_OK)
{
// Key doesn't exist!
return FindStatus;
}
value = hiter.Value();
return XN_STATUS_OK;
}
/**
* Remove a key-value entry from the XnHash
*
* @param key [in] The key of the entry
* @param value [out] The value that was in the removed entry
* @return XN_STATUS_NO_MATCH if no such key exists
*/
XnStatus Remove(const XnKey& key, XnValue& value)
{
// find the entry to which the key belongs
Iterator hiter(this);
XnStatus FindStatus = Find(key, hiter);
if (FindStatus != XN_STATUS_OK)
{
// no such entry!
return FindStatus;
}
// Remove by iterator
value = hiter.Value();
return Remove(hiter);
}
/**
* Remove an entry from the XnHash by iterator
*
* @param iter [in] Iterator pointing to an entry in the hash
* @param key [out] The key that was in the removed entry
* @param value [out] The value that was in the removed entry
*
* @return XN_STATUS_ILLEGAL_POSITION if iterator is invalid
*/
XnStatus Remove(ConstIterator iter, XnKey& key, XnValue& value)
{
if (iter == end())
{
// Can't remove invalid node
return XN_STATUS_ILLEGAL_POSITION;
}
// Get value and key, to return to the caller
value = iter.Value();
key = iter.Key();
return Remove(iter);
}
/**
* Remove an entry from the XnHash by iterator
*
* @param iter [in] Iterator pointing to an entry in the hash
*
* @return XN_STATUS_ILLEGAL_POSITION if iterator is invalid
*/
virtual XnStatus Remove(ConstIterator iter)
{
if (iter == end())
{
// Can't remove invalid node
return XN_STATUS_ILLEGAL_POSITION;
}
XnNode* pNode = iter.GetNode();
XnNode* pKeyNode = (XnNode*)(pNode->Data());
XnNode* pValueNode = pKeyNode->Next();
// Return the nodes to the pool
XnNode::Deallocate(pKeyNode);
XnNode::Deallocate(pValueNode);
pNode->Previous()->Next() = pNode->Next();
pNode->Next()->Previous() = pNode->Previous();
XnNode::Deallocate(pNode);
return XN_STATUS_OK;
}
/**
* 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 (Iterator iter = begin(); iter != end(); ++iter, ++nSize)
;
return nSize;
}
/**
* Get an iterator pointing to the entry to which the key belongs
*
* @param key [in] The key of the entry
* @param hiter [out] Iterator to the entry described by key
*
* @return XN_STATUS_NO_MATCH No such key in the XnHash
*/
XnStatus Find(const XnKey& key, ConstIterator& hiter) const
{
return ConstFind(key, hiter);
}
/**
* Get an iterator pointing to the entry to which the key belongs
*
* @param key [in] The key of the entry
* @param hiter [out] Iterator to the entry described by key
*
* @return XN_STATUS_NO_MATCH No such key in the XnHash
*/
XnStatus Find(const XnKey& key, Iterator& hiter)
{
XnStatus nRetVal = XN_STATUS_OK;
ConstIterator& it = hiter;
nRetVal = ConstFind(key, it);
XN_IS_STATUS_OK(nRetVal);
return (XN_STATUS_OK);
}
/**
* Get an iterator to the beginning of the XnHash (non-const version)
*/
Iterator begin()
{
return Iterator(this, m_nMinBin, m_Bins[m_nMinBin]->begin());
}
/**
* Get an iterator to the beginning of the XnHash (const version)
*/
ConstIterator begin() const
{
return ConstIterator(this, m_nMinBin, m_Bins[m_nMinBin]->begin());
}
/**
* Get an iterator to the end of the XnHash (position is invalid) (non-const version)
*/
Iterator end()
{
return Iterator(this, XN_HASH_LAST_BIN, m_Bins[XN_HASH_LAST_BIN]->end());
}
/**
* Get an iterator to the end of the XnHash (position is invalid) (const version)
*/
ConstIterator end() const
{
return ConstIterator(this, XN_HASH_LAST_BIN, m_Bins[XN_HASH_LAST_BIN]->end());
}
/**
* Change the hash function. The XnHash must be empty for this to succeed.
*
* @param hashFunction [in] The new hash function
*
* @return XN_STATUS_IS_NOT_EMPTY if the XnHash isn't empty
*/
XnStatus SetHashFunction(XnHashFunction hashFunction)
{
if (begin() != end())
{
return XN_STATUS_IS_NOT_EMPTY;
}
m_HashFunction = hashFunction;
return XN_STATUS_OK;
}
/**
* Change the comparison function. The XnHash must be empty for this to succeed.
*
* @param compareFunction [in] The new hash function
*
* @return XN_STATUS_IS_NOT_EMPTY if the XnHash isn't empty
*/
XnStatus SetCompareFunction(XnCompareFunction compareFunction)
{
if (begin() != end())
{
return XN_STATUS_IS_NOT_EMPTY;
}
m_CompareFunction = compareFunction;
return XN_STATUS_OK;
}
protected:
XnStatus Init()
{
m_Bins = XN_NEW_ARR(XnList*, XN_HASH_NUM_BINS);
XN_VALIDATE_ALLOC_PTR(m_Bins);
for (int i = 0; i < XN_HASH_NUM_BINS; i++)
{
m_Bins[i] = NULL;
}
m_Bins[XN_HASH_LAST_BIN] = XN_NEW(XnList); // We need this for an end() iterator
m_nMinBin = XN_HASH_LAST_BIN;
XN_VALIDATE_ALLOC_PTR(m_Bins[XN_HASH_LAST_BIN]);
m_CompareFunction = &XnDefaultCompareFunction;
m_HashFunction = &XnDefaultHashFunction;
return XN_STATUS_OK;
}
/**
* Get an iterator pointing to the entry to which the key belongs
*
* @param key [in] The key of the entry
* @param hashValue [in] The hash value of the key
* @param hiter [out] Iterator to the entry described by key
*
* @return XN_STATUS_NO_MATCH No such key in the XnHash
*/
XnStatus Find(const XnKey& key, XnHashValue hashValue, ConstIterator& hiter) const
{
if (m_Bins[hashValue] != NULL)
{
hiter = ConstIterator(this, hashValue, m_Bins[hashValue]->begin());
for (XnList::ConstIterator iter = m_Bins[hashValue]->begin();
iter != m_Bins[hashValue]->end(); ++iter, ++hiter)
{
if ((*m_CompareFunction)(key, hiter.Key()) == 0)
return XN_STATUS_OK;
}
}
return XN_STATUS_NO_MATCH;
}
/** Each bin is a XnList */
XnList** m_Bins;
XnUInt16 m_nMinBin;
/* Status of initialization - could be an error if memory could not be allocated. */
XnStatus m_nInitStatus;
/** The current hash function */
XnHashFunction m_HashFunction;
/** The current comparison function */
XnCompareFunction m_CompareFunction;
private:
XN_DISABLE_COPY_AND_ASSIGN(XnHash);
XnStatus ConstFind(const XnKey& key, ConstIterator& hiter) const
{
XnHashValue HashValue = (*m_HashFunction)(key);
return Find(key, HashValue, hiter);
}
};
/**
* Declares a default Key Manager for type @a KeyType. It is given the name @a ClassName.
* The manager uses the translator @a KeyTranslator, and is declared using the @a decl declspec.
*/
#define XN_DECLARE_DEFAULT_KEY_MANAGER_DECL(decl, KeyType, ClassName, KeyTranslator) \
class decl ClassName \
{ \
public: \
inline static XnHashValue Hash(KeyType const& key) \
{ \
const XnKey _key = KeyTranslator::GetAsValue(key); \
return XnDefaultHashFunction(_key); \
} \
inline static XnInt32 Compare(KeyType const& key1, KeyType const& key2) \
{ \
const XnKey _key1 = KeyTranslator::GetAsValue(key1); \
const XnKey _key2 = KeyTranslator::GetAsValue(key2); \
return XnDefaultCompareFunction(_key1, _key2); \
} \
};
/**
* Declares a default Key Manager for type @a KeyType. It is given the name @a ClassName.
* The manager uses the translator @a KeyTranslator.
*/
#define XN_DECLARE_DEFAULT_KEY_MANAGER(KeyType, ClassName, KeyTranslator) \
XN_DECLARE_DEFAULT_KEY_MANAGER_DECL(, KeyType, ClassName, KeyTranslator)
/**
* Declares a hash table from @a KeyType to @a ValueType named @a ClassName. The hash table uses
* @a KeyTranslator and @a ValueTranslator for storing keys and values, and @a KeyManager for managing the keys.
* It is declared using the @a decl declspec.
*/
#define XN_DECLARE_HASH_DECL(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, KeyManager) \
class decl ClassName : public XnHash \
{ \
public: \
class decl ConstIterator : public XnHash::ConstIterator \
{ \
public: \
friend class ClassName; \
inline ConstIterator(const ConstIterator& other) : XnHash::ConstIterator(other) {} \
inline ConstIterator& operator++() \
{ \
++(*(XnHash::ConstIterator*)this); \
return (*this); \
} \
inline ConstIterator operator++(int) \
{ \
ConstIterator result = *this; \
++*this; \
return result; \
} \
inline ConstIterator& operator--() \
{ \
--(*(XnHash::ConstIterator*)this); \
return (*this); \
} \
inline ConstIterator operator--(int) \
{ \
ConstIterator result = *this; \
--*this; \
return result; \
} \
inline KeyType const& Key() const \
{ \
return KeyTranslator::GetFromValue(XnHash::ConstIterator::Key()); \
} \
inline ValueType const& Value() const \
{ \
return ValueTranslator::GetFromValue(XnHash::ConstIterator::Value()); \
} \
protected: \
inline ConstIterator(const XnHash::ConstIterator& other) : \
XnHash::ConstIterator(other) {} \
}; \
class decl Iterator : public ConstIterator \
{ \
public: \
friend class ClassName; \
inline Iterator(const Iterator& other) : ConstIterator(other) {} \
inline Iterator& operator++() \
{ \
++(*(ConstIterator*)this); \
return (*this); \
} \
inline Iterator operator++(int) \
{ \
Iterator result = *this; \
++*this; \
return result; \
} \
inline Iterator& operator--() \
{ \
--(*(ConstIterator*)this); \
return (*this); \
} \
inline Iterator operator--(int) \
{ \
Iterator result = *this; \
--*this; \
return result; \
} \
inline KeyType& Key() const \
{ \
return (KeyType&)ConstIterator::Key(); \
} \
inline ValueType& Value() const \
{ \
return (ValueType&)ConstIterator::Value(); \
} \
protected: \
inline Iterator(const XnHash::Iterator& other) : ConstIterator(other) {} \
}; \
public: \
ClassName() \
{ \
SetHashFunction(Hash); \
SetCompareFunction(Compare); \
} \
virtual ~ClassName() \
{ \
while (!IsEmpty()) \
Remove(begin()); \
} \
XnStatus Set(KeyType const& key, ValueType const& value) \
{ \
Iterator oldIt = begin(); \
if (Find(key, oldIt) == XN_STATUS_OK) \
{ \
oldIt.Value() = value; \
} \
else \
{ \
XnKey _key = KeyTranslator::CreateValueCopy(key); \
XnValue _value = ValueTranslator::CreateValueCopy(value); \
XnStatus nRetVal = XnHash::Set(_key, _value); \
if (nRetVal != XN_STATUS_OK) \
{ \
KeyTranslator::FreeValue(_key); \
ValueTranslator::FreeValue(_value); \
return (nRetVal); \
} \
} \
return XN_STATUS_OK; \
} \
XnStatus Get(KeyType const& key, ValueType& value) const \
{ \
XnKey _key = KeyTranslator::GetAsValue(key); \
XnValue _value; \
XnStatus nRetVal = XnHash::Get(_key, _value); \
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
value = ValueTranslator::GetFromValue(_value); \
return XN_STATUS_OK; \
} \
XnStatus Get(KeyType const& key, ValueType*& pValue) const \
{ \
XnKey _key = KeyTranslator::GetAsValue(key); \
XnValue _value; \
XnStatus nRetVal = XnHash::Get(_key, _value); \
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
pValue = &ValueTranslator::GetFromValue(_value); \
return XN_STATUS_OK; \
} \
XnStatus Remove(KeyType const& key) \
{ \
ValueType dummy; \
return Remove(key, dummy); \
} \
XnStatus Remove(KeyType const& key, ValueType& value) \
{ \
ConstIterator it = end(); \
XnStatus nRetVal = Find(key, it); \
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
value = it.Value(); \
return Remove(it); \
} \
inline XnStatus Remove(ConstIterator iter) \
{ \
XnKey key = KeyTranslator::GetAsValue(iter.Key()); \
XnValue value = ValueTranslator::GetAsValue(iter.Value()); \
XnStatus nRetVal = XnHash::Remove(iter); \
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
KeyTranslator::FreeValue(key); \
ValueTranslator::FreeValue(value); \
return XN_STATUS_OK; \
} \
XnStatus Find(KeyType const& key, ConstIterator& hiter) const \
{ \
XnKey _key = KeyTranslator::GetAsValue(key); \
XnHash::ConstIterator it = XnHash::end(); \
XnStatus nRetVal = XnHash::Find(_key, it); \
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
hiter = it; \
return XN_STATUS_OK; \
} \
XnStatus Find(KeyType const& key, Iterator& hiter) \
{ \
XnKey _key = KeyTranslator::GetAsValue(key); \
XnHash::Iterator it = XnHash::end(); \
XnStatus nRetVal = XnHash::Find(_key, it); \
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
hiter = it; \
return XN_STATUS_OK; \
} \
inline Iterator begin() { return XnHash::begin(); } \
inline ConstIterator begin() const { return XnHash::begin(); } \
inline Iterator end() { return XnHash::end(); } \
inline ConstIterator end() const { return XnHash::end(); } \
protected: \
virtual XnStatus Remove(XnHash::ConstIterator iter) \
{ \
return Remove(ConstIterator(iter)); \
} \
inline static XnHashValue Hash(const XnKey& key) \
{ \
KeyType const& _key = KeyTranslator::GetFromValue(key); \
return KeyManager::Hash(_key); \
} \
inline static XnInt32 Compare(const XnKey& key1, const XnKey& key2) \
{ \
KeyType const _key1 = KeyTranslator::GetFromValue(key1); \
KeyType const _key2 = KeyTranslator::GetFromValue(key2); \
return KeyManager::Compare(_key1, _key2); \
} \
private: \
XN_DISABLE_COPY_AND_ASSIGN(ClassName); \
};
/**
* Declares a hash table from @a KeyType to @a ValueType named @a ClassName. The hash table uses
* @a KeyTranslator and @a ValueTranslator for storing keys and values, and @a KeyManager for managing the keys.
*/
#define XN_DECLARE_HASH(KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, KeyManager) \
XN_DECLARE_HASH_DECL(, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, KeyManager)
#define _XN_DEFAULT_KEY_MANAGER_NAME(ClassName) _##ClassName##Manager
/**
* Declares a hash table from @a KeyType to @a ValueType named @a ClassName. The hash table uses
* @a KeyTranslator and @a ValueTranslator for storing keys and values. It declares and uses a default manager.
* It is declared using the @a decl declspec.
*/
#define XN_DECLARE_DEFAULT_MANAGER_HASH_DECL(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator) \
XN_DECLARE_DEFAULT_KEY_MANAGER_DECL(decl, KeyType, _XN_DEFAULT_KEY_MANAGER_NAME(ClassName), KeyTranslator) \
XN_DECLARE_HASH_DECL(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, _XN_DEFAULT_KEY_MANAGER_NAME(ClassName))
/**
* Declares a hash table from @a KeyType to @a ValueType named @a ClassName. The hash table uses
* @a KeyTranslator and @a ValueTranslator for storing keys and values. It declares and uses a default manager.
*/
#define XN_DECLARE_DEFAULT_MANAGER_HASH(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator) \
XN_DECLARE_DEFAULT_MANAGER_HASH_DECL(, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator)
#define _XN_DEFAULT_KEY_TRANSLATOR(ClassName) _##ClassName##KeyTranslator
#define _XN_DEFAULT_VALUE_TRANSLATOR(ClassName) _##ClassName##ValueTranslator
/**
* Declares a hash table from @a KeyType to @a ValueType named @a ClassName. The hash table uses
* default translators for storing keys and values. It declares and uses a default manager.
* It is declared using the @a decl declspec.
*/
#define XN_DECLARE_DEFAULT_HASH_DECL(decl, KeyType, ValueType, ClassName) \
XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, KeyType, _XN_DEFAULT_KEY_TRANSLATOR(ClassName)) \
XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, ValueType, _XN_DEFAULT_VALUE_TRANSLATOR(ClassName)) \
XN_DECLARE_DEFAULT_MANAGER_HASH_DECL(decl, KeyType, ValueType, ClassName, _XN_DEFAULT_KEY_TRANSLATOR(ClassName), _XN_DEFAULT_VALUE_TRANSLATOR(ClassName))
/**
* Declares a hash table from @a KeyType to @a ValueType named @a ClassName. The hash table uses
* default translators for storing keys and values. It declares and uses a default manager.
*/
#define XN_DECLARE_DEFAULT_HASH(KeyType, ValueType, ClassName) \
XN_DECLARE_DEFAULT_HASH_DECL(, KeyType, ValueType, ClassName)
#endif // _XN_HASH_H
|