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