Merge pull request #5128 from dumganhar/iss2789-perf-container

closed #2789: Adds a macro ‘’USE_STD_UNORDERED_MAP” for switching the implementation of Map<K, V> between using std::unordered_map and std::map.
This commit is contained in:
James Chen 2014-01-19 20:03:23 -08:00
commit a0c0954063
1 changed files with 25 additions and 0 deletions

View File

@ -25,10 +25,17 @@
#ifndef __CCMAP_H__ #ifndef __CCMAP_H__
#define __CCMAP_H__ #define __CCMAP_H__
#define USE_STD_UNORDERED_MAP 1
#include "ccMacros.h" #include "ccMacros.h"
#include "CCObject.h" #include "CCObject.h"
#include <vector> #include <vector>
#if USE_STD_UNORDERED_MAP
#include <unordered_map> #include <unordered_map>
#else
#include <map>
#endif
NS_CC_BEGIN NS_CC_BEGIN
@ -44,7 +51,11 @@ public:
// ------------------------------------------ // ------------------------------------------
// Iterators // Iterators
// ------------------------------------------ // ------------------------------------------
#if USE_STD_UNORDERED_MAP
typedef std::unordered_map<K, V> RefMap; typedef std::unordered_map<K, V> RefMap;
#else
typedef std::map<K, V> RefMap;
#endif
typedef typename RefMap::iterator iterator; typedef typename RefMap::iterator iterator;
typedef typename RefMap::const_iterator const_iterator; typedef typename RefMap::const_iterator const_iterator;
@ -104,25 +115,39 @@ public:
/** Sets capacity of the map */ /** Sets capacity of the map */
void reserve(ssize_t capacity) void reserve(ssize_t capacity)
{ {
#if USE_STD_UNORDERED_MAP
_data.reserve(capacity); _data.reserve(capacity);
#endif
} }
/** Returns the number of buckets in the Map container. */ /** Returns the number of buckets in the Map container. */
ssize_t bucketCount() const ssize_t bucketCount() const
{ {
#if USE_STD_UNORDERED_MAP
return _data.bucket_count(); return _data.bucket_count();
#else
return 0;
#endif
} }
/** Returns the number of elements in bucket n. */ /** Returns the number of elements in bucket n. */
ssize_t bucketSize(ssize_t n) const ssize_t bucketSize(ssize_t n) const
{ {
#if USE_STD_UNORDERED_MAP
return _data.bucket_size(n); return _data.bucket_size(n);
#else
return 0;
#endif
} }
/** Returns the bucket number where the element with key k is located. */ /** Returns the bucket number where the element with key k is located. */
ssize_t bucket(const K& k) const ssize_t bucket(const K& k) const
{ {
#if USE_STD_UNORDERED_MAP
return _data.bucket(k); return _data.bucket(k);
#else
return 0;
#endif
} }
/** The number of elements in the map. */ /** The number of elements in the map. */