fbxset.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /****************************************************************************************
  2. Copyright (C) 2015 Autodesk, Inc.
  3. All rights reserved.
  4. Use of this software is subject to the terms of the Autodesk license agreement
  5. provided at the time of installation or download, or which otherwise accompanies
  6. this software in either electronic or hard copy form.
  7. ****************************************************************************************/
  8. //! \file fbxset.h
  9. #ifndef _FBXSDK_CORE_BASE_SET_H_
  10. #define _FBXSDK_CORE_BASE_SET_H_
  11. #include <fbxsdk/fbxsdk_def.h>
  12. #include <fbxsdk/core/base/fbxredblacktree.h>
  13. #include <fbxsdk/core/base/fbxmap.h>
  14. #include <fbxsdk/fbxsdk_nsbegin.h>
  15. /** This class implements an efficient set based on value comparison, which stores values.
  16. * It executes insertion, deletion and query operations in O(log(n)) time. */
  17. template <typename Type, typename Compare=FbxLessCompare<Type>, typename Allocator=FbxBaseAllocator> class FbxSet
  18. {
  19. protected:
  20. //! This class defines the value type used by the set.
  21. class Value
  22. {
  23. /*****************************************************************************************************************************
  24. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  25. *****************************************************************************************************************************/
  26. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  27. public:
  28. typedef const Type KeyType;
  29. typedef const Type ConstKeyType;
  30. typedef const Type ValueType;
  31. typedef const Type ConstValueType;
  32. inline Value(const Type& pValue) : mValue(pValue){}
  33. inline KeyType& GetKey() const { return mValue; }
  34. inline ConstKeyType& GetKey(){ return mValue; }
  35. inline ValueType& GetValue() const { return mValue; }
  36. inline ConstValueType& GetValue(){ return mValue; }
  37. protected:
  38. ValueType mValue;
  39. private:
  40. Value& operator=(const Value&);
  41. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  42. };
  43. //! Declaration of the storage type used by the set.
  44. typedef FbxRedBlackTree<Value, Compare, Allocator> StorageType;
  45. public:
  46. typedef Type ValueType;
  47. typedef typename StorageType::RecordType RecordType;
  48. typedef typename StorageType::IteratorType Iterator;
  49. typedef typename StorageType::ConstIteratorType ConstIterator;
  50. /** Preallocate memory.
  51. * \param pRecordCount The number of elements.
  52. */
  53. inline void Reserve(unsigned int pRecordCount)
  54. {
  55. mTree.Reserve(pRecordCount);
  56. }
  57. //! Retrieve the number of values it holds.
  58. inline int GetSize() const
  59. {
  60. return mTree.GetSize();
  61. }
  62. /** Insert a value.
  63. * \param pValue The value.
  64. * \return If the value is already present in the map, returns the existing value and false; else returns the pointer to the new value and true. */
  65. inline FbxPair<RecordType*, bool> Insert(const ValueType& pValue)
  66. {
  67. return mTree.Insert(Value(pValue));
  68. }
  69. /** Delete a value.
  70. * \param pValue The value.
  71. * \return \c true if success, \c false if value is not found. */
  72. inline int Remove(const ValueType& pValue)
  73. {
  74. return mTree.Remove(pValue);
  75. }
  76. //! Clear the set.
  77. inline void Clear()
  78. {
  79. mTree.Clear();
  80. }
  81. //! Query whether the set is empty.
  82. inline bool Empty() const
  83. {
  84. return mTree.Empty();
  85. }
  86. //! Retrieve the begin iterator of the set.
  87. Iterator Begin()
  88. {
  89. return Iterator(Minimum());
  90. }
  91. //! Retrieve the end iterator of the set.
  92. Iterator End()
  93. {
  94. return Iterator();
  95. }
  96. //! Retrieve the begin iterator of the set.
  97. ConstIterator Begin() const
  98. {
  99. return ConstIterator(Minimum());
  100. }
  101. //! Retrieve the end iterator of the set.
  102. ConstIterator End() const
  103. {
  104. return ConstIterator();
  105. }
  106. /** Find a given value in the set.
  107. * \param pValue The value to find.
  108. * \return The value in the set, or NULL if the value is not found in the set. */
  109. inline const RecordType* Find(const ValueType& pValue) const
  110. {
  111. return mTree.Find(pValue);
  112. }
  113. /** Find a given value in the set.
  114. * \param pValue The value to find.
  115. * \return The value in the set, or NULL if the value is not found in the set. */
  116. inline RecordType* Find(const ValueType& pValue)
  117. {
  118. return mTree.Find(pValue);
  119. }
  120. //! Retrieve the minimum value in the set.
  121. inline const RecordType* Minimum() const
  122. {
  123. return mTree.Minimum();
  124. }
  125. //! Retrieve the minimum value in the set.
  126. inline RecordType* Minimum()
  127. {
  128. return mTree.Minimum();
  129. }
  130. //! Retrieve the maximum value in the set.
  131. inline const RecordType* Maximum() const
  132. {
  133. return mTree.Maximum();
  134. }
  135. //! Retrieve the maximum value in the set.
  136. inline RecordType* Maximum()
  137. {
  138. return mTree.Maximum();
  139. }
  140. //! Equality operator.
  141. inline bool operator==(const FbxSet<Type, Compare, Allocator>& pOther) const
  142. {
  143. return (this == &pOther) || (mTree == pOther.mTree);
  144. }
  145. //! Inequality operator.
  146. inline bool operator != (const FbxSet<Type, Compare, Allocator>& pOther) const
  147. {
  148. return !(*this == pOther);
  149. }
  150. /** Intersect with another set.
  151. * \param pOther The other set.
  152. * \return The intersection set of the two sets. */
  153. inline FbxSet Intersect(const FbxSet& pOther) const
  154. {
  155. FbxSet lReturn;
  156. ConstIterator lBegin = Begin();
  157. for (; lBegin != End(); ++lBegin)
  158. {
  159. if (pOther.Find(lBegin->GetValue()) != NULL)
  160. lReturn.Insert(lBegin->GetValue());
  161. }
  162. return lReturn;
  163. }
  164. /** Unite with another set.
  165. * \param pOther The other set.
  166. * \return The union set of the two sets (no duplicated items). */
  167. inline FbxSet Union(const FbxSet& pOther) const
  168. {
  169. FbxSet lReturn(*this);
  170. ConstIterator lBegin = pOther.Begin();
  171. for (; lBegin != End(); ++lBegin)
  172. {
  173. if (Find(lBegin->GetValue()) == NULL)
  174. lReturn.Insert(lBegin->GetValue());
  175. }
  176. return lReturn;
  177. }
  178. /*****************************************************************************************************************************
  179. ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
  180. *****************************************************************************************************************************/
  181. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  182. inline FbxSet(){}
  183. inline FbxSet(const FbxSet& pSet) : mTree(pSet.mTree){}
  184. inline ~FbxSet(){ Clear(); }
  185. private:
  186. StorageType mTree;
  187. #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
  188. };
  189. #include <fbxsdk/fbxsdk_nsend.h>
  190. #endif /* _FBXSDK_CORE_BASE_SET_H_ */