X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FMapValue.cpp;fp=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FMapValue.cpp;h=8be9eb2c54891bb269f97edebe67a72752565ab1;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/MapValue.cpp b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/MapValue.cpp new file mode 100644 index 000000000..8be9eb2c5 --- /dev/null +++ b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/MapValue.cpp @@ -0,0 +1,203 @@ +#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); + } + } +}