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