]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/MapValue.cpp
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / DataBoard / MapValue.cpp
1 #include "MapValue.h"
2
3 #include <iostream>
4
5 #include "IntegerValue.h"
6 #include "Constants.h"
7
8 namespace Databoard {
9         namespace Value {
10
11                 MapValue::MapValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard)
12                 {
13                 }
14
15                 MapValue::~MapValue()
16                 {
17                         for(MKVMap::iterator it = values.begin(); it != values.end(); ++it)
18                         {
19                                 delete it->first;
20                                 delete it->second;
21                         }
22
23                         values.clear();
24                 }
25
26                 bool MapValue::put(Value* key, Value* value)
27                 {
28                         if(containsKey(key) == false)
29                         {
30                                 values[key] = value;
31
32                                 return true;
33                         }
34                         else
35                         {
36                                 MKVMap::iterator it = values.find(key);
37
38                                 delete it->first;
39                                 delete it->second;
40
41                                 values.erase(it);
42
43                                 values[key] = value;
44
45                                 return false;
46                         }
47                 }
48
49                 int MapValue::count()
50                 {
51                         return (int)values.size();
52                 }
53
54                 Value* MapValue::get(Value* key)
55                 {
56                         if(containsKey(key) == true)
57                         {
58                                 return values[key];
59                         }
60                         else
61                         {
62                                 return false;
63                         }
64                 }
65
66                 bool MapValue::containsKey(Value* key)
67                 {
68                         return (values.find(key) != values.end());
69                 }
70
71                 Value* MapValue::remove(Value* key)
72                 {
73                         if(containsKey(key) == true)
74                         {
75                                 Value* oldValue = values[key];
76
77                                 values.erase(key);
78
79                                 return oldValue;
80                         }
81                         else
82                         {
83                                 return NULL;
84                         }
85                 }
86
87                 void MapValue::removeRange(Value* startKey, Value* endKey)
88                 {
89                         if(containsKey(startKey) && containsKey(endKey))
90                         {
91                                 MKVMap::iterator iBegin = values.find(startKey);
92                                 MKVMap::iterator iEnd = values.find(endKey);
93
94                                 int indexBegin = -1;
95                                 int indexEnd = -1;
96
97                                 int i = 0;
98
99                                 for(MKVMap::iterator it = values.begin(); it != values.end(); ++i, ++it)
100                                 {
101                                         if(it == iBegin)
102                                         {
103                                                 indexBegin = i;
104                                         }
105
106                                         if(it == iEnd)
107                                         {
108                                                 indexEnd = i;
109                                         }
110                                 }
111
112                                 if(indexBegin == -1 || indexEnd == -1 ||
113                                         (indexBegin > indexEnd))
114                                 {
115                                         return;
116                                 }
117
118                                 MKVMap::iterator it = values.begin();
119
120                                 for(int j = 0; j < indexBegin; ++j)
121                                 {
122                                         ++it;
123                                 }
124
125                                 for(i = indexBegin; i < indexEnd; ++i)
126                                 {
127                                         delete it->first;
128                                         delete it->second;
129
130                                         ++it;
131                                 }
132
133                                 values.erase(iBegin, iEnd);
134                         }
135                 }
136
137                 void MapValue::clear()
138                 {
139                         values.clear();
140                 }
141
142                 std::string MapValue::writeOut(int /*indent*/)
143                 {
144                         std::string s;
145
146                         s += "map { ";
147
148                         // counter is inside the block
149                         for(MKVMap::iterator it = values.begin(); it != values.end(); )
150                         {
151                                 s += it->first->writeOut();
152                                 s += " = ";
153                                 s += it->second->writeOut();
154
155                                 if(++it != values.end())
156                                 {
157                                         s += ", ";
158                                 }
159                         }
160
161                         s += " }";
162
163                         return s;
164                 }
165
166                 bool MapValue::equals(const Value* other)
167                 {
168                         MapValue* o = (MapValue*)other;
169
170                         return true;//(this->value < o->value);
171
172                 }
173
174                 std::string MapValue::isValid()
175                 {
176                         for(MKVMap::iterator it = values.begin(); it != values.end(); ++it)
177                         {
178                                 std::string s;
179                                         
180                                 s = it->first->isValid();
181
182                                 if(s.length() > 0)
183                                 {
184                                         return s;
185                                 }
186
187                                 s = it->second->isValid();
188
189                                 if(s.length() > 0)
190                                 {
191                                         return s;
192                                 }
193                         }
194
195                         return STR_EMPTY;
196                 }
197
198                 bool ValueComparison::operator()(Value* const &a, Value* const &b) const
199                 {
200                         return a->equals(b);
201                 }
202         }
203 }