BVB Source Codes

CRYENGINE Show BiDirMap.h Source code

Return Download CRYENGINE: download BiDirMap.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. #ifndef BIDIRECTIONAL_MAP_H
  4. #define BIDIRECTIONAL_MAP_H
  5.  
  6. #include <set>
  7. #include <vector>
  8.  
  9. /// Small utility to compare pointers by the values they point to.
  10. template<typename T>
  11. class PointerValueCompare
  12. {
  13. public:
  14.         bool operator()(const T* lhs, const T* rhs) const { return *lhs < *rhs; }
  15. };
  16.  
  17. /**
  18.  * @brief Allows going from values to keys as well.
  19.  *
  20.  * NOTE Jul 10, 2007: <pvl> not truly generic but could be made if needed.
  21.  * Uses two std::sets to index pairs of values.  Supports a subset of STL-like
  22.  * operations, however everything's capitalized (e.g. 'Iterator' instead of
  23.  * 'iterator'), partly to indicate that no specific effort has been made
  24.  * to make it really STL-conformant.
  25.  */
  26. template<typename T0, typename T1>
  27. class BidirectionalMap
  28. {
  29.         typedef std::set<const T0*, PointerValueCompare<T0>> FwdMap;
  30.         typedef std::set<const T1*, PointerValueCompare<T1>> BwdMap;
  31.  
  32.         FwdMap mFwd;
  33.         BwdMap mBwd;
  34.  
  35.         void Swap(BidirectionalMap&);
  36. public:
  37.         typedef std::pair<T0, T1> DataRecord;
  38.  
  39.         BidirectionalMap();
  40.         BidirectionalMap(const BidirectionalMap&);
  41.         ~BidirectionalMap();
  42.  
  43.         BidirectionalMap& operator=(const BidirectionalMap&);
  44.  
  45.         void              Insert(const T1&, const T0&);
  46.         void              Insert(const T0&, const T1&);
  47.  
  48.         void              Erase(const T0&);
  49.         void              Erase(const T1&);
  50.  
  51.         void              Clear();
  52.  
  53.         class Iterator;
  54.         Iterator  Find(const T0&) const;
  55.         Iterator  Find(const T1&) const;
  56.  
  57.         const T1& operator[](const T0&) const;
  58.         const T0& operator[](const T1&) const;
  59.  
  60.         int       Size() const;
  61.         bool      Empty() const;
  62.  
  63.         // TODO Jun 28, 2007: make the GetKeys function a template is possible:
  64.         // bimap.GetKeys<float> ();
  65.         // Using terms "primary" and "secondary" implies asymetry between the two
  66.         // sets of values where there's essentially none (except for fairly minor
  67.         // implementation one).
  68.         //      template <typename T>
  69.         //      std::vector <T> GetKeys () const;
  70.  
  71.         // use the swap idiom:
  72.         // bimap.GetKeys().swap (your_key_vec);
  73.         std::vector<T0> GetPrimaryKeys() const;
  74.         std::vector<T1> GetSecondaryKeys() const;
  75.  
  76.         class Iterator
  77.         {
  78.                 typename FwdMap::const_iterator mPrimKeysIter;
  79.         public:
  80.                 Iterator(const typename FwdMap::const_iterator& prim_keys_iter);
  81.  
  82.                 Iterator&         operator++();
  83.                 const DataRecord* operator*() const;
  84.                 bool              operator==(const Iterator&) const;
  85.                 bool              operator!=(const Iterator&) const;
  86.         };
  87.  
  88.         Iterator Begin() const;
  89.         Iterator End() const;
  90. };
  91.  
  92. template<typename T0, typename T1>
  93. inline BidirectionalMap<T0, T1>::BidirectionalMap()
  94. {
  95. }
  96.  
  97. template<typename T0, typename T1>
  98. inline BidirectionalMap<T0, T1>::BidirectionalMap(const BidirectionalMap& rhs)
  99. {
  100.         typename FwdMap::const_iterator it = rhs.mFwd.begin();
  101.         typename FwdMap::const_iterator end = rhs.mFwd.end();
  102.         for (; it != end; ++it)
  103.         {
  104.                 const DataRecord* data = reinterpret_cast<const DataRecord*>(*it);
  105.                 Insert(data->first, data->second);
  106.         }
  107. }
  108.  
  109. template<typename T0, typename T1>
  110. inline BidirectionalMap<T0, T1>::~BidirectionalMap()
  111. {
  112.         Clear();
  113. }
  114.  
  115. template<typename T0, typename T1>
  116. inline BidirectionalMap<T0, T1>& BidirectionalMap<T0, T1 >::operator=(const BidirectionalMap& rhs)
  117. {
  118.         BidirectionalMap tmp(rhs);
  119.         Swap(tmp);
  120.         return *this;
  121. }
  122.  
  123. template<typename T0, typename T1>
  124. inline void BidirectionalMap<T0, T1 >::Swap(BidirectionalMap& rhs)
  125. {
  126.         mFwd.swap(rhs.mFwd);
  127.         mBwd.swap(rhs.mBwd);
  128. }
  129.  
  130. template<typename T0, typename T1>
  131. inline void BidirectionalMap<T0, T1 >::Insert(const T1& f, const T0& i)
  132. {
  133.         DataRecord* new_val = new DataRecord(i, f);
  134.         std::pair<typename FwdMap::iterator, bool> fwd_result = mFwd.insert(&new_val->first);
  135.         assert(fwd_result.second);
  136.         std::pair<typename BwdMap::iterator, bool> bwd_result = mBwd.insert(&new_val->second);
  137.         assert(bwd_result.second);
  138. }
  139.  
  140. template<typename T0, typename T1>
  141. inline void BidirectionalMap<T0, T1 >::Insert(const T0& i, const T1& f)
  142. {
  143.         Insert(f, i);
  144. }
  145.  
  146. template<typename T0, typename T1>
  147. inline void BidirectionalMap<T0, T1 >::Erase(const T0& i)
  148. {
  149.         typename FwdMap::iterator fwd_it = mFwd.find(&i);
  150.         assert(fwd_it != mFwd.end());
  151.         const DataRecord* to_be_deleted = reinterpret_cast<const DataRecord*>(*fwd_it);
  152.  
  153.         typename BwdMap::iterator bwd_it = mBwd.find(&to_be_deleted->second);
  154.         assert(bwd_it != mBwd.end());
  155.  
  156.         mFwd.erase(fwd_it);
  157.         mBwd.erase(bwd_it);
  158.  
  159.         delete to_be_deleted;
  160. }
  161.  
  162. template<typename T0, typename T1>
  163. inline void BidirectionalMap<T0, T1 >::Erase(const T1& f)
  164. {
  165.         typename BwdMap::const_iterator bwd_it = mBwd.find(&f);
  166.         assert(bwd_it != mBwd.end());
  167.         const DataRecord* to_be_deleted = reinterpret_cast<const DataRecord*>((char*)*bwd_it - offsetof(DataRecord, second));
  168.  
  169.         typename FwdMap::const_iterator fwd_it = mFwd.find(&to_be_deleted->first);
  170.         assert(fwd_it != mFwd.end());
  171.  
  172.         mFwd.erase(fwd_it);
  173.         mBwd.erase(bwd_it);
  174.  
  175.         delete to_be_deleted;
  176. }
  177.  
  178. template<typename T0, typename T1>
  179. inline void BidirectionalMap<T0, T1 >::Clear()
  180. {
  181.         typename FwdMap::iterator fwd_it = mFwd.begin();
  182.         typename FwdMap::iterator fwd_end = mFwd.end();
  183.         for (; fwd_it != fwd_end; ++fwd_it)
  184.         {
  185.                 delete reinterpret_cast<const DataRecord*>(*fwd_it);
  186.         }
  187.         mFwd.clear();
  188.         mBwd.clear();
  189. }
  190.  
  191. template<typename T0, typename T1>
  192. inline typename BidirectionalMap<T0, T1>::Iterator
  193. BidirectionalMap<T0, T1 >::Find(const T0& i) const
  194. {
  195.         return Iterator(mFwd.find(&i));
  196. }
  197.  
  198. template<typename T0, typename T1>
  199. inline typename BidirectionalMap<T0, T1>::Iterator
  200. BidirectionalMap<T0, T1 >::Find(const T1& f) const
  201. {
  202.         typename BwdMap::const_iterator it = mBwd.find(&f);
  203.         if (it == mBwd.end())
  204.                 return mFwd.end();
  205.         const DataRecord* value = reinterpret_cast<const std::pair<T0, T1>*>((char*)*it - offsetof(DataRecord, second));
  206.         return Iterator(mFwd.find(&value->first));
  207. }
  208.  
  209. template<typename T0, typename T1>
  210. inline const T1& BidirectionalMap<T0, T1 >::operator[](const T0& i) const
  211. {
  212.         return (*Find(i))->second;
  213. }
  214.  
  215. template<typename T0, typename T1>
  216. inline const T0& BidirectionalMap<T0, T1 >::operator[](const T1& f) const
  217. {
  218.         return (*Find(f))->first;
  219. }
  220.  
  221. template<typename T0, typename T1>
  222. inline int BidirectionalMap<T0, T1 >::Size() const
  223. {
  224.         assert(mFwd.size() == mBwd.size());
  225.         return mFwd.size();
  226. }
  227.  
  228. template<typename T0, typename T1>
  229. inline bool BidirectionalMap<T0, T1 >::Empty() const
  230. {
  231.         assert(mFwd.size() == mBwd.size());
  232.         return mFwd.empty();
  233. }
  234.  
  235. #if 0
  236. template<typename T0, typename T1>
  237. template<typename T>
  238. inline std::vector<T> BidirectionalMap<T0, T1 >::GetKeys() const
  239. {
  240.         std::vector<T> result;
  241.         typename FwdMap::const_iterator it = mFwd.begin();
  242.         typename FwdMap::const_iterator end = mFwd.end();
  243.         for (; it != end; ++it)
  244.                 result.push_back(**it);
  245.         return result;
  246. }
  247. #endif
  248.  
  249. template<typename T0, typename T1>
  250. inline std::vector<T0> BidirectionalMap<T0, T1 >::GetPrimaryKeys() const
  251. {
  252.         std::vector<T0> result;
  253.         result.reserve(mFwd.size());
  254.  
  255.         typename FwdMap::const_iterator it = mFwd.begin();
  256.         typename FwdMap::const_iterator end = mFwd.end();
  257.         for (; it != end; ++it)
  258.                 result.push_back(**it);
  259.         return result;
  260. }
  261.  
  262. template<typename T0, typename T1>
  263. inline std::vector<T1> BidirectionalMap<T0, T1 >::GetSecondaryKeys() const
  264. {
  265.         std::vector<T1> result;
  266.         result.reserve(mBwd.size());
  267.  
  268. #if 0
  269.         typename BwdMap::const_iterator it = mBwd.begin();
  270.         typename BwdMap::const_iterator end = mBwd.end();
  271.         for (; it != end; ++it)
  272.                 result.push_back(**it);
  273. #endif
  274.         Iterator it = Begin();
  275.         Iterator end = End();
  276.         for (; it != end; ++it)
  277.                 result.push_back((*it)->second);
  278.         return result;
  279. }
  280.  
  281. template<typename T0, typename T1>
  282. inline BidirectionalMap<T0, T1>::Iterator::Iterator(const typename FwdMap::const_iterator& prim_keys_iter) :
  283.         mPrimKeysIter(prim_keys_iter)
  284. {
  285. }
  286.  
  287. template<typename T0, typename T1>
  288. inline typename BidirectionalMap<T0, T1>::Iterator&
  289. BidirectionalMap<T0, T1 >::Iterator::operator++()
  290. {
  291.         ++mPrimKeysIter;
  292.         return *this;
  293. }
  294.  
  295. template<typename T0, typename T1>
  296. inline const typename BidirectionalMap<T0, T1>::DataRecord*
  297. BidirectionalMap<T0, T1 >::Iterator::operator*() const
  298. {
  299.         return reinterpret_cast<const DataRecord*>(*mPrimKeysIter);
  300. }
  301.  
  302. template<typename T0, typename T1>
  303. inline bool BidirectionalMap<T0, T1 >::Iterator::operator==(const Iterator& rhs) const
  304. {
  305.         return mPrimKeysIter == rhs.mPrimKeysIter;
  306. }
  307.  
  308. template<typename T0, typename T1>
  309. inline bool BidirectionalMap<T0, T1 >::Iterator::operator!=(const Iterator& rhs) const
  310. {
  311.         return !(mPrimKeysIter == rhs.mPrimKeysIter);
  312. }
  313.  
  314. template<typename T0, typename T1>
  315. inline typename BidirectionalMap<T0, T1>::Iterator
  316. BidirectionalMap<T0, T1 >::Begin() const
  317. {
  318.         return Iterator(mFwd.begin());
  319. }
  320.  
  321. template<typename T0, typename T1>
  322. inline typename BidirectionalMap<T0, T1>::Iterator
  323. BidirectionalMap<T0, T1 >::End() const
  324. {
  325.         return Iterator(mFwd.end());
  326. }
  327.  
  328. #endif
  329.  
downloadBiDirMap.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top