Add remove function to stdext storages

This commit is contained in:
Eduardo Bart 2012-08-17 18:19:51 -03:00
parent e41c8d5805
commit affe641a1f
2 changed files with 32 additions and 1 deletions

View File

@ -37,10 +37,25 @@ public:
m_data.resize(k+1);
m_data[k] = value;
}
bool remove(const Key& k) {
if(m_data.size() < k)
return false;
if(m_data[k].empty())
return false;
m_data[k] = any();
return true;
}
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_data.size(); }
std::size_t size() const {
std::size_t count = 0;
for(std::size_t i=0;i<m_data.size();++i)
if(!m_data[i].empty())
count++;
return count;
}
void clear() { m_data.clear(); }
private:

View File

@ -60,6 +60,22 @@ public:
m_values[m_size++] = { id, packed_any(value) };
}
bool remove(Key id) {
auto begin = m_values;
auto end = m_values + m_size;
auto it = std::find_if(begin, end, [=](const value_pair& pair) -> bool { return pair.id == id; } );
if(it == end)
return false;
int pos = it - begin;
auto tmp = new value_pair[m_size-1];
std::copy(begin, begin + pos, tmp);
std::copy(begin + pos + 1, end, tmp + pos);
delete[] m_values;
m_values = tmp;
m_size--;
return true;
}
template<typename T>
T get(Key id) const {
for(SizeType i=0;i<m_size;++i)