33 #define XN_HASH_LAST_BIN 256
34 #define XN_HASH_NUM_BINS (XN_HASH_LAST_BIN + 1)
53 return (XnSizeT(key) & 0xff);
59 static XnInt32 XnDefaultCompareFunction(
const XnKey& key1,
const XnKey& key2)
61 return XnInt32(XnSizeT(key1)-XnSizeT(key2));
357 if (other.
m_Bins[i] != NULL)
407 if (
m_Bins[HashValue] != NULL)
413 hiter.
Value() = value;
421 if (
m_Bins[HashValue] == NULL)
423 return XN_STATUS_ALLOC_FAILED;
431 if (pKeyNode == NULL)
433 return XN_STATUS_ALLOC_FAILED;
435 pKeyNode->
Data() = key;
439 if (pValueNode == NULL)
442 return XN_STATUS_ALLOC_FAILED;
444 pValueNode->
Data() = value;
447 pKeyNode->
Next() = pValueNode;
448 pValueNode->
Next() = NULL;
481 value = hiter.
Value();
507 value = hiter.
Value();
525 return XN_STATUS_ILLEGAL_POSITION;
529 value = iter.
Value();
547 return XN_STATUS_ILLEGAL_POSITION;
609 return ConstFind(key, hiter);
625 nRetVal = ConstFind(key, it);
674 return XN_STATUS_IS_NOT_EMPTY;
691 return XN_STATUS_IS_NOT_EMPTY;
729 if (
m_Bins[hashValue] != NULL)
733 iter !=
m_Bins[hashValue]->
end(); ++iter, ++hiter)
740 return XN_STATUS_NO_MATCH;
761 return Find(key, HashValue, hiter);
769 #define XN_DECLARE_DEFAULT_KEY_MANAGER_DECL(decl, KeyType, ClassName, KeyTranslator) \
770 class decl ClassName \
773 inline static XnHashValue Hash(KeyType const& key) \
775 const XnKey _key = KeyTranslator::GetAsValue(key); \
776 return XnDefaultHashFunction(_key); \
778 inline static XnInt32 Compare(KeyType const& key1, KeyType const& key2) \
780 const XnKey _key1 = KeyTranslator::GetAsValue(key1); \
781 const XnKey _key2 = KeyTranslator::GetAsValue(key2); \
782 return XnDefaultCompareFunction(_key1, _key2); \
790 #define XN_DECLARE_DEFAULT_KEY_MANAGER(KeyType, ClassName, KeyTranslator) \
791 XN_DECLARE_DEFAULT_KEY_MANAGER_DECL(, KeyType, ClassName, KeyTranslator)
798 #define XN_DECLARE_HASH_DECL(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, KeyManager) \
799 class decl ClassName : public XnHash \
802 class decl ConstIterator : public XnHash::ConstIterator \
805 friend class ClassName; \
806 inline ConstIterator(const ConstIterator& other) : XnHash::ConstIterator(other) {} \
807 inline ConstIterator& operator++() \
809 ++(*(XnHash::ConstIterator*)this); \
812 inline ConstIterator operator++(int) \
814 ConstIterator result = *this; \
818 inline ConstIterator& operator--() \
820 --(*(XnHash::ConstIterator*)this); \
823 inline ConstIterator operator--(int) \
825 ConstIterator result = *this; \
829 inline KeyType const& Key() const \
831 return KeyTranslator::GetFromValue(XnHash::ConstIterator::Key()); \
833 inline ValueType const& Value() const \
835 return ValueTranslator::GetFromValue(XnHash::ConstIterator::Value()); \
838 inline ConstIterator(const XnHash::ConstIterator& other) : \
839 XnHash::ConstIterator(other) {} \
841 class decl Iterator : public ConstIterator \
844 friend class ClassName; \
845 inline Iterator(const Iterator& other) : ConstIterator(other) {} \
846 inline Iterator& operator++() \
848 ++(*(ConstIterator*)this); \
851 inline Iterator operator++(int) \
853 Iterator result = *this; \
857 inline Iterator& operator--() \
859 --(*(ConstIterator*)this); \
862 inline Iterator operator--(int) \
864 Iterator result = *this; \
868 inline KeyType& Key() const \
870 return (KeyType&)ConstIterator::Key(); \
872 inline ValueType& Value() const \
874 return (ValueType&)ConstIterator::Value(); \
877 inline Iterator(const XnHash::Iterator& other) : ConstIterator(other) {} \
882 SetHashFunction(Hash); \
883 SetCompareFunction(Compare); \
885 ClassName(const ClassName& other) \
887 SetHashFunction(Hash); \
888 SetCompareFunction(Compare); \
891 virtual ~ClassName() \
896 ClassName& operator=(const ClassName& other) \
899 for (ConstIterator it = other.begin(); it != other.end(); it++) \
901 m_nInitStatus = Set(it.Key(), it.Value()); \
902 if (m_nInitStatus != XN_STATUS_OK) \
909 XnStatus Set(KeyType const& key, ValueType const& value) \
911 Iterator oldIt = begin(); \
912 if (Find(key, oldIt) == XN_STATUS_OK) \
914 oldIt.Value() = value; \
918 XnKey _key = KeyTranslator::CreateValueCopy(key); \
919 XnValue _value = ValueTranslator::CreateValueCopy(value); \
920 XnStatus nRetVal = XnHash::Set(_key, _value); \
921 if (nRetVal != XN_STATUS_OK) \
923 KeyTranslator::FreeValue(_key); \
924 ValueTranslator::FreeValue(_value); \
928 return XN_STATUS_OK; \
930 XnStatus Get(KeyType const& key, ValueType& value) const \
932 XnKey _key = KeyTranslator::GetAsValue(key); \
934 XnStatus nRetVal = XnHash::Get(_key, _value); \
935 if (nRetVal != XN_STATUS_OK) return (nRetVal); \
936 value = ValueTranslator::GetFromValue(_value); \
937 return XN_STATUS_OK; \
939 XnStatus Get(KeyType const& key, ValueType*& pValue) const \
941 XnKey _key = KeyTranslator::GetAsValue(key); \
943 XnStatus nRetVal = XnHash::Get(_key, _value); \
944 if (nRetVal != XN_STATUS_OK) return (nRetVal); \
945 pValue = &ValueTranslator::GetFromValue(_value); \
946 return XN_STATUS_OK; \
948 XnStatus Remove(KeyType const& key) \
951 return Remove(key, dummy); \
953 XnStatus Remove(KeyType const& key, ValueType& value) \
955 ConstIterator it = end(); \
956 XnStatus nRetVal = Find(key, it); \
957 if (nRetVal != XN_STATUS_OK) return (nRetVal); \
958 value = it.Value(); \
961 inline XnStatus Remove(ConstIterator iter) \
963 XnKey key = KeyTranslator::GetAsValue(iter.Key()); \
964 XnValue value = ValueTranslator::GetAsValue(iter.Value()); \
965 XnStatus nRetVal = XnHash::Remove(iter); \
966 if (nRetVal != XN_STATUS_OK) return (nRetVal); \
967 KeyTranslator::FreeValue(key); \
968 ValueTranslator::FreeValue(value); \
969 return XN_STATUS_OK; \
971 XnStatus Find(KeyType const& key, ConstIterator& hiter) const \
973 XnKey _key = KeyTranslator::GetAsValue(key); \
974 XnHash::ConstIterator it = XnHash::end(); \
975 XnStatus nRetVal = XnHash::Find(_key, it); \
976 if (nRetVal != XN_STATUS_OK) return (nRetVal); \
978 return XN_STATUS_OK; \
980 XnStatus Find(KeyType const& key, Iterator& hiter) \
982 XnKey _key = KeyTranslator::GetAsValue(key); \
983 XnHash::Iterator it = XnHash::end(); \
984 XnStatus nRetVal = XnHash::Find(_key, it); \
985 if (nRetVal != XN_STATUS_OK) return (nRetVal); \
987 return XN_STATUS_OK; \
989 inline Iterator begin() { return XnHash::begin(); } \
990 inline ConstIterator begin() const { return XnHash::begin(); } \
991 inline Iterator end() { return XnHash::end(); } \
992 inline ConstIterator end() const { return XnHash::end(); } \
994 virtual XnStatus Remove(XnHash::ConstIterator iter) \
996 return Remove(ConstIterator(iter)); \
998 inline static XnHashValue Hash(const XnKey& key) \
1000 KeyType const& _key = KeyTranslator::GetFromValue(key); \
1001 return KeyManager::Hash(_key); \
1003 inline static XnInt32 Compare(const XnKey& key1, const XnKey& key2) \
1005 KeyType const _key1 = KeyTranslator::GetFromValue(key1); \
1006 KeyType const _key2 = KeyTranslator::GetFromValue(key2); \
1007 return KeyManager::Compare(_key1, _key2); \
1015 #define XN_DECLARE_HASH(KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, KeyManager) \
1016 XN_DECLARE_HASH_DECL(, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, KeyManager)
1018 #define _XN_DEFAULT_KEY_MANAGER_NAME(ClassName) _##ClassName##Manager
1025 #define XN_DECLARE_DEFAULT_MANAGER_HASH_DECL(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator) \
1026 XN_DECLARE_DEFAULT_KEY_MANAGER_DECL(decl, KeyType, _XN_DEFAULT_KEY_MANAGER_NAME(ClassName), KeyTranslator) \
1027 XN_DECLARE_HASH_DECL(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator, _XN_DEFAULT_KEY_MANAGER_NAME(ClassName))
1033 #define XN_DECLARE_DEFAULT_MANAGER_HASH(decl, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator) \
1034 XN_DECLARE_DEFAULT_MANAGER_HASH_DECL(, KeyType, ValueType, ClassName, KeyTranslator, ValueTranslator)
1036 #define _XN_DEFAULT_KEY_TRANSLATOR(ClassName) _##ClassName##KeyTranslator
1037 #define _XN_DEFAULT_VALUE_TRANSLATOR(ClassName) _##ClassName##ValueTranslator
1044 #define XN_DECLARE_DEFAULT_HASH_DECL(decl, KeyType, ValueType, ClassName) \
1045 XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, KeyType, _XN_DEFAULT_KEY_TRANSLATOR(ClassName)) \
1046 XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, ValueType, _XN_DEFAULT_VALUE_TRANSLATOR(ClassName)) \
1047 XN_DECLARE_DEFAULT_MANAGER_HASH_DECL(decl, KeyType, ValueType, ClassName, _XN_DEFAULT_KEY_TRANSLATOR(ClassName), _XN_DEFAULT_VALUE_TRANSLATOR(ClassName))
1053 #define XN_DECLARE_DEFAULT_HASH(KeyType, ValueType, ClassName) \
1054 XN_DECLARE_DEFAULT_HASH_DECL(, KeyType, ValueType, ClassName)
1056 #endif // _XN_HASH_H