Optimize dynamic storage

This commit is contained in:
Eduardo Bart 2012-08-06 10:25:17 -03:00
parent dcde317904
commit 5232501ecf
1 changed files with 9 additions and 10 deletions

View File

@ -32,20 +32,19 @@ namespace stdext {
template<typename Key> template<typename Key>
class dynamic_storage { class dynamic_storage {
public: public:
template<typename T> void set(const Key& k, const T& value) { m_map[k] = value; } template<typename T> void set(const Key& k, const T& value) {
template<typename T> T get(const Key& k) const { if(m_data.size() <= k)
auto it = m_map.find(k); m_data.resize(k+1);
if(it != m_map.end()) m_data[k] = value;
return any_cast<T>(it->second);
return T();
} }
bool has(const Key& k) const { return m_map.find(k) != m_map.end(); } template<typename T> T get(const Key& k) const { return has(k) ? any_cast<T>(m_data[k]) : T(); }
bool has(const Key& k) const { return k < m_data.size() && !m_data[k].empty(); }
std::size_t size() const { return m_map.size(); } std::size_t size() const { return m_data.size(); }
void clear() { m_map.clear(); } void clear() { m_data.clear(); }
private: private:
std::unordered_map<Key, any> m_map; std::vector<any> m_data;
}; };
} }