#include "MapValue.h" #include #include "IntegerValue.h" #include "Constants.h" namespace Databoard { namespace Value { MapValue::MapValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard) { } MapValue::~MapValue() { for(MKVMap::iterator it = values.begin(); it != values.end(); ++it) { delete it->first; delete it->second; } values.clear(); } bool MapValue::put(Value* key, Value* value) { if(containsKey(key) == false) { values[key] = value; return true; } else { MKVMap::iterator it = values.find(key); delete it->first; delete it->second; values.erase(it); values[key] = value; return false; } } int MapValue::count() { return (int)values.size(); } Value* MapValue::get(Value* key) { if(containsKey(key) == true) { return values[key]; } else { return false; } } bool MapValue::containsKey(Value* key) { return (values.find(key) != values.end()); } Value* MapValue::remove(Value* key) { if(containsKey(key) == true) { Value* oldValue = values[key]; values.erase(key); return oldValue; } else { return NULL; } } void MapValue::removeRange(Value* startKey, Value* endKey) { if(containsKey(startKey) && containsKey(endKey)) { MKVMap::iterator iBegin = values.find(startKey); MKVMap::iterator iEnd = values.find(endKey); int indexBegin = -1; int indexEnd = -1; int i = 0; for(MKVMap::iterator it = values.begin(); it != values.end(); ++i, ++it) { if(it == iBegin) { indexBegin = i; } if(it == iEnd) { indexEnd = i; } } if(indexBegin == -1 || indexEnd == -1 || (indexBegin > indexEnd)) { return; } MKVMap::iterator it = values.begin(); for(int j = 0; j < indexBegin; ++j) { ++it; } for(i = indexBegin; i < indexEnd; ++i) { delete it->first; delete it->second; ++it; } values.erase(iBegin, iEnd); } } void MapValue::clear() { values.clear(); } std::string MapValue::writeOut(int /*indent*/) { std::string s; s += "map { "; // counter is inside the block for(MKVMap::iterator it = values.begin(); it != values.end(); ) { s += it->first->writeOut(); s += " = "; s += it->second->writeOut(); if(++it != values.end()) { s += ", "; } } s += " }"; return s; } bool MapValue::equals(const Value* other) { MapValue* o = (MapValue*)other; return true;//(this->value < o->value); } std::string MapValue::isValid() { for(MKVMap::iterator it = values.begin(); it != values.end(); ++it) { std::string s; s = it->first->isValid(); if(s.length() > 0) { return s; } s = it->second->isValid(); if(s.length() > 0) { return s; } } return STR_EMPTY; } bool ValueComparison::operator()(Value* const &a, Value* const &b) const { return a->equals(b); } } }