File: DataElement.cpp

package info (click to toggle)
basic256 2.0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,076 kB
  • sloc: cpp: 16,791; yacc: 3,979; lex: 1,446; makefile: 25
file content (311 lines) | stat: -rwxr-xr-x 6,647 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "DataElement.h"
#include "Settings.h"

#include <string>

int DataElement::e = ERROR_NONE;

void DataElement::init() {
	// initialize dataelement common stuff
	e=0;
	arr = NULL;
	map = NULL;
}

DataElement::DataElement() {
	// create an empty dataelement
	init();
	type = T_UNASSIGNED;
}

DataElement::DataElement(QString s) {
	init();
	type = T_STRING;
	stringval = s;
}

DataElement::DataElement(int i) {
	init();
    type = T_INT;
    intval = i;
}

DataElement::DataElement(long l) {
	init();
	type = T_INT;
	intval = l;
}

DataElement::DataElement(double d) {
	init();
	type = T_FLOAT;
	floatval = d;
}

DataElement::~DataElement() {
	stringval.clear();
	if (arr){
		delete(arr);
		arr=NULL;
	}
}




void DataElement::copy(DataElement *source) {
	// fill an existing from as a copy of another
	// it is as fast as it can be
	// unused values of strings can be found as garbage but this is the cost of speed
	// source->stringval.clear() is too expensive to be used for each assignment/copy
	switch (source->type){
		case T_UNASSIGNED:
			break;
		case T_FLOAT:
			floatval = source->floatval;
			break;
		case T_STRING:
			stringval = source->stringval;
			break;
		case T_ARRAY:
			{
				// copy array elements from one old array to a new one
				if (!arr)
					arr = new DataElementArray;
				int i = source->arr->xdim * source->arr->ydim;
				arr->xdim = source->arr->xdim;
				arr->ydim = source->arr->ydim;
				arr->data.resize(i);
				while(i-- > 0) {
					arr->data[i].copy(&source->arr->data[i]);
				}
			}
			break;
		case T_MAP:
			{
#ifdef DEBUG
fprintf(stderr,"de copy map source len %d\n",source->map->data.size());
#endif
				if (type==T_MAP) {
					map->data.clear();
				} else {
					map = new DataElementMap;
				}
				map->data.insert(source->map->data.begin(), source->map->data.end());
			}
			break;
		case T_REF:
			intval = source->intval;
			level = source->level;
			break;
		case T_INT:
			intval = source->intval;
	}
	type = source->type;
}

void DataElement::clear() {
	type = T_UNASSIGNED;
	intval = 0;
	stringval.clear();
	level = 0;
	if (arr){
		delete(arr);
		arr = NULL;
	}
	if (map) {
		delete(map);
		map = NULL;
	}
}


QString DataElement::debug() {
	// return a string representing the DataElement contents
	if(type==T_INT) return("int(" + QString::number(intval) + ") ");
	if(type==T_REF) return("varref(" + QString::number(intval) + ", level: " + QString::number(level) + ") ");
	if(type==T_FLOAT) return("float(" + QString::number(floatval) + ") ");
	if(type==T_STRING) return("string(" + stringval + ") ");
	if(type==T_ARRAY) return("array(" + QString::number(intval) + ")");
	if(type==T_MAP) return("MAP()");
	if(type==T_UNASSIGNED) return("unused ");
	return("badtype ");
}

void DataElement::arrayDim(const int xdim, const int ydim, const bool redim) {
	const int size = xdim * ydim;
	
	if (size <= MAXARRAYSIZE) {
		if (size >= 1) {
			if (type != T_ARRAY || !redim) {
				// if array data is dim or redim without a dim then create a new one (clear the old)
				if (!arr){
					arr = new DataElementArray;
				}else{
					arr->data.clear();
				}
				arr->data.resize(size);
				//int i = size;
				//while(i-- > 0) {
				//	arr->data[i].intval = 0;
				//}
				type = T_ARRAY;
			}else{
				// redim - resize the vector
				//const int oldsize = arr->xdim * arr->ydim;
				arr->data.resize(size);
				//for(int i=oldsize;i<size;i++) {
				//	arr->data[i].intval = 0;
				//}
			}

			arr->xdim = xdim;
			arr->ydim = ydim;
		} else {
			e = ERROR_ARRAYSIZESMALL;
		}
	} else {
		e = ERROR_ARRAYSIZELARGE;
	}
}

int DataElement::arrayRows() {
	// return number of columns of array as if it was a two dimensional array - 0 = not an array
	if (type == T_ARRAY) {
		return(arr->xdim);
	} else if (type==T_UNASSIGNED){
		e = ERROR_VARNOTASSIGNED;
	} else {
		e = ERROR_NOTARRAY;
	}
	return(0);
}

int DataElement::arrayCols() {
	// return number of rows of array as if it was a two dimensional array - 0 = not an array
	if (type == T_ARRAY) {
		return(arr->ydim);
	} else if (type==T_UNASSIGNED){
		e = ERROR_VARNOTASSIGNED;
	} else {
		e = ERROR_NOTARRAY;
	}
	return(0);
}

DataElement* DataElement::arrayGetData(const int x, const int y) {
	// get data from array elements from map (using x, y)
	// if there is an error return an unassigned value
	if (type == T_ARRAY) {
		if (x >=0 && x < arr->xdim && y >=0 && y < arr->ydim) {
			const int i = x * arr->ydim + y;
			DataElement *d = &arr->data[i];
			return d;
		} else {
			e = ERROR_ARRAYINDEX;
		}
	} else {
		e = ERROR_NOTARRAY;
	}
	return this;
}

void DataElement::arraySetData(const int x, const int y, DataElement *d) {
	// DataElement's data is copied and it should be deleted
	// by whomever created it
	if (type == T_ARRAY) {
		if (x >=0 && x < arr->xdim && y >=0 && y < arr->ydim) {
			const int i = x * arr->ydim + y;
			arr->data[i].copy(d);
		} else {
			e = ERROR_ARRAYINDEX;
		}
	} else {
		e = ERROR_NOTARRAY;
	}
}

void DataElement::arrayUnassign(const int x, const int y) {
	if (type == T_ARRAY) {
		if (x >=0 && x < arr->xdim && y >=0 && y < arr->ydim) {
			const int i = x * arr->ydim + y;
			arr->data[i].type = T_UNASSIGNED;
		} else {
			e = ERROR_ARRAYINDEX;
		}
	} else {
		e = ERROR_NOTARRAY;
	}
}



void DataElement::mapDim(){
	if (type==T_MAP) {
		map->data.clear();
	} else {
		clear();
		map = new DataElementMap();
		type=T_MAP;
	}
}

DataElement* DataElement::mapGetData(QString key){
	DataElement *d;
	if (type==T_MAP) {
		if (map->data.count(key)) {
			d = &map->data[key];
		} else {
			e = ERROR_MAPKEY;
			d = new DataElement();
		}
	} else {
		e = ERROR_NOTMAP;
		d = new DataElement();
	}
	return d;
}

void DataElement::mapSetData(QString key, DataElement *d){
	if (type==T_MAP) {
		map->data[key] = *d;
	} else {
		e = ERROR_NOTMAP;
	}
}

void DataElement::mapUnassign(QString key){
	if (type==T_MAP) {
		if (map->data.count(key)) {
			map->data.erase(key);
		} else {
			e = ERROR_MAPKEY;
		}
	} else {
		e = ERROR_NOTMAP;
	}
}

int DataElement::mapLength(){
	int l = 0;
	if (type==T_MAP) {
		l = map->data.size();
	} else {
		e = ERROR_NOTMAP;
	}
	return l;
}

bool DataElement::mapKey(QString key){
	// is the key in the map
	if (type==T_MAP) {
		if (map->data.count(key)) {
			return true;
		} else {
			e = ERROR_MAPKEY;
		}
	} else {
		e = ERROR_NOTMAP;
	}
	return false;
}