fbxsdk/core/base/fbxmap.h Source File
 
 
 
fbxsdk/core/base/fbxmap.h
Go to the documentation of this file.
00001 /****************************************************************************************
00002  
00003    Copyright (C) 2013 Autodesk, Inc.
00004    All rights reserved.
00005  
00006    Use of this software is subject to the terms of the Autodesk license agreement
00007    provided at the time of installation or download, or which otherwise accompanies
00008    this software in either electronic or hard copy form.
00009  
00010 ****************************************************************************************/
00011 
00013 #ifndef _FBXSDK_CORE_BASE_MAP_H_
00014 #define _FBXSDK_CORE_BASE_MAP_H_
00015 
00016 #include <fbxsdk/fbxsdk_def.h>
00017 
00018 #include <fbxsdk/core/base/fbxstring.h>
00019 #include <fbxsdk/core/base/fbxredblacktree.h>
00020 
00021 #include <fbxsdk/fbxsdk_nsbegin.h>
00022 
00023 class FbxObject;
00024 
00058 template <typename Type> struct FbxLessCompare
00059 {
00060     inline int operator()(const Type& pLeft, const Type& pRight) const
00061     {
00062         return (pLeft < pRight) ? -1 : ((pRight < pLeft) ? 1 : 0);
00063     }
00064 };
00065 
00068 template <typename Key, typename Type, typename Compare=FbxLessCompare<Key>, typename Allocator=FbxBaseAllocator> class FbxMap
00069 {
00070 protected:
00072         class KeyValuePair : private FbxPair<const Key, Type>
00073         {
00074         /*****************************************************************************************************************************
00075         ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
00076         *****************************************************************************************************************************/
00077         #ifndef DOXYGEN_SHOULD_SKIP_THIS
00078         public:
00079                 typedef const Key       KeyType;
00080                 typedef const Key       ConstKeyType;
00081                 typedef Type            ValueType;
00082                 typedef const Type      ConstValueType;
00083 
00084                 KeyValuePair(const Key& pFirst, const Type& pSecond) : FbxPair<const Key, Type>(pFirst, pSecond){}
00085                 ConstKeyType& GetKey() const { return this->mFirst; }
00086                 KeyType& GetKey(){ return this->mFirst; }
00087                 ConstValueType& GetValue() const { return this->mSecond; }
00088                 ValueType& GetValue(){ return this->mSecond; }
00089         #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
00090         };
00091 
00093         typedef FbxRedBlackTree<KeyValuePair, Compare, Allocator> StorageType;
00094 
00095 public:
00096         typedef Type                                                                    ValueType;
00097         typedef Key                                                                             KeyType;
00098         typedef typename StorageType::RecordType                RecordType;
00099         typedef typename StorageType::IteratorType              Iterator;
00100         typedef typename StorageType::ConstIteratorType ConstIterator;
00101 
00104         inline void Reserve(unsigned int pRecordCount)
00105         {
00106                 mTree.Reserve(pRecordCount);
00107         }
00108 
00110         inline int GetSize() const
00111         {
00112                 return mTree.GetSize();
00113         }
00114 
00119         inline FbxPair<RecordType*, bool> Insert(const KeyType& pKey, const ValueType& pValue)
00120         {
00121                 return mTree.Insert(KeyValuePair(pKey, pValue));
00122         }
00123 
00127         inline int Remove(const KeyType& pKey)
00128         {
00129                 return mTree.Remove(pKey);
00130         }
00131 
00133         inline void Clear()
00134         {
00135                 mTree.Clear();
00136         }
00137 
00139         inline bool Empty() const
00140         {
00141                 return mTree.Empty();
00142         }
00143 
00145         Iterator Begin()
00146         {
00147                 return Iterator(Minimum());
00148         }
00149 
00151         Iterator End()
00152         {
00153                 return Iterator();
00154         }
00155 
00157         ConstIterator Begin() const
00158         {
00159                 return ConstIterator(Minimum());
00160         }
00161 
00163         ConstIterator End() const
00164         {
00165                 return ConstIterator();
00166         }
00167 
00171         inline const RecordType* Find(const KeyType& pKey) const
00172         {
00173                 return mTree.Find(pKey);
00174         }
00175 
00179         inline RecordType* Find(const KeyType& pKey)
00180         {
00181                 return mTree.Find(pKey);
00182         }
00183 
00187         inline const RecordType* UpperBound(const KeyType& pKey) const
00188         {
00189                 return mTree.UpperBound(pKey);
00190         }
00191 
00195         inline RecordType* UpperBound(const KeyType& pKey)
00196         {
00197                 return mTree.UpperBound(pKey);
00198         }
00199 
00204         inline ValueType& operator[](const KeyType& pKey)
00205         {
00206                 RecordType* lRecord = Find(pKey);
00207 
00208                 if( !lRecord )
00209                 {
00210                         lRecord = Insert(pKey, ValueType()).mFirst;
00211                 }
00212 
00213                 return lRecord->GetValue();
00214         }
00215 
00217         inline const RecordType* Minimum() const
00218         {
00219                 return mTree.Minimum();
00220         }
00221 
00223         inline RecordType* Minimum()
00224         {
00225                 return mTree.Minimum();
00226         }
00227 
00229         inline const RecordType* Maximum() const
00230         {
00231                 return mTree.Maximum();
00232         }
00233 
00235         inline RecordType* Maximum()
00236         {
00237                 return mTree.Maximum();
00238         }
00239 
00240 /*****************************************************************************************************************************
00241 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
00242 *****************************************************************************************************************************/
00243 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00244         inline FbxMap(){}
00245         inline FbxMap(const FbxMap& pMap) : mTree(pMap.mTree){}
00246         inline ~FbxMap(){ Clear(); }
00247 
00248 private:
00249         StorageType mTree;
00250 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
00251 };
00252 
00255 template <class Key, class Type, class Compare> class FBXSDK_DLL FbxSimpleMap
00256 {
00257 public:
00258     typedef typename FbxMap<Key, Type, Compare>::RecordType* Iterator;
00259 
00263         inline void Add(const Key& pKey, const Type& pValue)
00264         {
00265                 mMap.Insert(pKey, pValue);
00266         }
00267 
00271         inline Iterator Find(const Key& pKey) const
00272         {
00273                 return (Iterator)mMap.Find(pKey);
00274         }
00275 
00279         inline Iterator Find(const Type& pValue) const
00280         {
00281                 Iterator lIterator = GetFirst();
00282                 while( lIterator )
00283                 {
00284                         if( lIterator->GetValue() == pValue )
00285                         {
00286                                 return lIterator;
00287                         }
00288                         lIterator = GetNext(lIterator);
00289                 }
00290                 return 0;
00291         }
00292 
00295         inline void Remove(Iterator pIterator)
00296         {
00297                 if( pIterator ) mMap.Remove(pIterator->GetKey());
00298         }
00299 
00302         inline Iterator GetFirst() const
00303         {
00304                 return (Iterator)mMap.Minimum();
00305         }
00306 
00310         inline Iterator GetNext(Iterator pIterator) const
00311         {
00312                 return (Iterator)pIterator ? pIterator->Successor() : 0;
00313         }
00314 
00316         inline void Clear() 
00317         {
00318                 mMap.Clear();
00319         }
00320 
00323         inline void Reserve(int pSize)
00324         {
00325                 mMap.Reserve(pSize);
00326         }
00327 
00330         inline int GetCount() const
00331         {
00332                 return mMap.GetSize();
00333         }
00334 
00335 /*****************************************************************************************************************************
00336 ** WARNING! Anything beyond these lines is for internal use, may not be documented and is subject to change without notice! **
00337 *****************************************************************************************************************************/
00338 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00339         inline FbxSimpleMap(){}
00340 
00341 private:
00342     FbxMap<Key, Type, Compare> mMap;
00343 #endif /* !DOXYGEN_SHOULD_SKIP_THIS *****************************************************************************************/
00344 };
00345 
00348 template <class Type, class Compare> class FBXSDK_DLL FbxObjectMap : public FbxSimpleMap<Type, FbxObject*, Compare>
00349 {
00350 public:
00352     inline FbxObjectMap(){}
00353 
00358     inline FbxObject* Get(typename FbxSimpleMap<Type, FbxObject*, Compare>::Iterator pIterator)
00359     {
00360         return pIterator ? pIterator->GetValue() : 0;
00361     }
00362 };
00363 
00366 class FBXSDK_DLL FbxObjectStringMap : public FbxObjectMap<class FbxString, class FbxStringCompare>
00367 {
00368 public:
00370     inline FbxObjectStringMap(){}
00371 };
00372 
00373 #include <fbxsdk/fbxsdk_nsend.h>
00374 
00375 #endif /* _FBXSDK_CORE_BASE_MAP_H_ */