1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 2002-2014 Maciej Komosinski and Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #include "collectionobj.h" |
---|
6 | #include <common/nonstd_math.h> //sqrt in borland |
---|
7 | #include <frams/errmgr/stderrors.h> |
---|
8 | #include <common/nonstd_stl.h> |
---|
9 | #include <frams/util/sstringutils.h> |
---|
10 | #ifndef NO_VMACHINE |
---|
11 | #include <frams/vm/vmachine.h> |
---|
12 | #endif |
---|
13 | |
---|
14 | #define FIELDSTRUCT VectorObject |
---|
15 | ParamEntry vector_paramtab[]= |
---|
16 | { |
---|
17 | {"Vector",1,13,"Vector","Vector is 1-dimensional array, indexed by integer value (starting from 0). " |
---|
18 | "Multidimensional arrays can be simulated by putting other Vector objects into the Vector.\n" |
---|
19 | "Example:\nvar v=Vector.new();\nv.add(123); v.add(\"string\");",}, |
---|
20 | {"clear",0,PARAM_NOSTATIC,"clear data","p()",PROCEDURE(p_clear),}, |
---|
21 | {"size",0,PARAM_READONLY+PARAM_NOSTATIC,"element count","d",GETONLY(size),}, |
---|
22 | {"remove",0,PARAM_NOSTATIC,"remove at position","p(d position)",PROCEDURE(p_remove),}, |
---|
23 | {"get",0,PARAM_NOSTATIC,"get value at position","p x(d position)",PROCEDURE(p_get),}, |
---|
24 | {"set",0,PARAM_NOSTATIC,"set value at position","p(d position,x value)",PROCEDURE(p_set),}, |
---|
25 | {"add",0,PARAM_NOSTATIC,"append at the end","p(x value)",PROCEDURE(p_add),}, |
---|
26 | {"find",0,PARAM_NOSTATIC,"find","p d(x value)",PROCEDURE(p_find),"returns the element index or -1 if not found"}, |
---|
27 | {"avg",0,PARAM_READONLY+PARAM_NOSTATIC,"average","f",GETONLY(avg)}, |
---|
28 | {"stdev",0,PARAM_READONLY+PARAM_NOSTATIC,"standard deviation","f",GETONLY(stdev),"=sqrt(sum((element[i]-avg)^2)/(size-1)) which is estimated population std.dev. from sample std.dev."}, |
---|
29 | {"toString",0,PARAM_READONLY+PARAM_NOSTATIC,"textual form","s",GETONLY(toString),}, |
---|
30 | {"new",0,0,"create new Vector","p oVector()",STATICPROCEDURE(p_new),}, |
---|
31 | {"sort",0,PARAM_NOSTATIC,"sort elements (in place)","p(o comparator)",PROCEDURE(p_sort),"comparator can be null, giving the \"natural\" sorting order (depending on element type), otherwise it must be a function reference obtained by the \"function FUNCTIONNAME\" operator.\n\nExample:\nfunction compareLastDigit(a,b) {return (a%10)<(b%10);}\nvar v=[16,23,35,42,54,61];\nv.sort(function compareLastDigit);"}, |
---|
32 | {"iterator",0,PARAM_NOSTATIC,"iterator","o",GETONLY(iterator),}, |
---|
33 | {0,0,0,}, |
---|
34 | }; |
---|
35 | #undef FIELDSTRUCT |
---|
36 | |
---|
37 | #define FIELDSTRUCT DictionaryObject |
---|
38 | ParamEntry dictionary_paramtab[]= |
---|
39 | { |
---|
40 | {"Dictionary",1,9,"Dictionary","Dictionary associates stored values with string keys " |
---|
41 | "(\"key\" is the first argument in get/set/remove functions). Integer \"key\" can be " |
---|
42 | "used to enumerate all elements (the sequence of elements is not preserved).\n" |
---|
43 | "Example:\nvar d=Dictionary.new(); d.set(\"name\",\"John\"); d.set(\"age\",44);\n" |
---|
44 | "var i;\nfor(i=0;i<d.size;i++) Simulator.print(d.getKey(i)+\" is \"+d.get(i));",}, |
---|
45 | {"clear",0,PARAM_NOSTATIC,"clear data","p()",PROCEDURE(p_clear),}, |
---|
46 | {"size",0,PARAM_NOSTATIC,"element count","d",GETONLY(size),}, |
---|
47 | {"remove",0,PARAM_NOSTATIC,"remove named or indexed element","p(x key)",PROCEDURE(p_remove),}, |
---|
48 | {"get",0,PARAM_NOSTATIC,"get named or indexed element","p x(x key)",PROCEDURE(p_get),}, |
---|
49 | {"getKey",0,PARAM_NOSTATIC,"get a key of the indexed element","p s(d index)",PROCEDURE(p_getKey),}, |
---|
50 | {"set",0,PARAM_NOSTATIC,"set named or indexed element","p(x key,x value)",PROCEDURE(p_set),}, |
---|
51 | {"find",0,PARAM_NOSTATIC,"find","p s(x value)",PROCEDURE(p_find),"returns the element key or null if not found"}, |
---|
52 | {"new",0,0,"create new Dictionary","p oDictionary()",STATICPROCEDURE(p_new),}, |
---|
53 | {"toString",0,PARAM_READONLY+PARAM_NOSTATIC,"textual form","s",GETONLY(toString),}, |
---|
54 | {0,0,0,}, |
---|
55 | }; |
---|
56 | #undef FIELDSTRUCT |
---|
57 | |
---|
58 | Param VectorObject::par(vector_paramtab); |
---|
59 | Param DictionaryObject::par(dictionary_paramtab); |
---|
60 | |
---|
61 | ///////////////////////////////////////// |
---|
62 | |
---|
63 | VectorObject::VectorObject(Pt3D &pt) |
---|
64 | :readonly(0),owndata(1) |
---|
65 | { |
---|
66 | set(0,ExtValue(pt.x)); |
---|
67 | set(1,ExtValue(pt.y)); |
---|
68 | set(2,ExtValue(pt.z)); |
---|
69 | } |
---|
70 | |
---|
71 | void VectorObject::clear() |
---|
72 | { |
---|
73 | if (owndata) |
---|
74 | for(int i=data.size()-1;i>=0;i--) |
---|
75 | { |
---|
76 | ExtValue *v=(ExtValue*)data.get(i); |
---|
77 | if (v) delete v; |
---|
78 | } |
---|
79 | data.clear(); |
---|
80 | } |
---|
81 | |
---|
82 | void VectorObject::p_remove(PARAMPROCARGS) |
---|
83 | { |
---|
84 | if (readonly) return; |
---|
85 | int i=args->getInt(); |
---|
86 | if ((i<0)||(i>=data.size())) return; |
---|
87 | ExtValue *v=(ExtValue*)data.get(i); |
---|
88 | if (v) delete v; |
---|
89 | data-=i; |
---|
90 | } |
---|
91 | |
---|
92 | void VectorObject::set(int i,const ExtValue& val) |
---|
93 | { |
---|
94 | int oldsize=data.size(); |
---|
95 | if (i<0) return; |
---|
96 | ExtValue *v=(ExtValue*)data.get(i); |
---|
97 | if (v) delete v; |
---|
98 | data.set(i,new ExtValue(val)); |
---|
99 | i--; |
---|
100 | while(i>=oldsize) |
---|
101 | { |
---|
102 | data.set(i,0); |
---|
103 | i--; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | void VectorObject::p_get(PARAMPROCARGS) |
---|
108 | { |
---|
109 | int i=args->getInt(); |
---|
110 | if (listIndexCheck(&data,i,"VectorObject","get")) |
---|
111 | { |
---|
112 | ExtValue *v=get(i); |
---|
113 | if (v) |
---|
114 | { |
---|
115 | *ret=*v; |
---|
116 | return; |
---|
117 | } |
---|
118 | } |
---|
119 | *ret=ExtValue(); |
---|
120 | } |
---|
121 | |
---|
122 | void VectorObject::get_avg(ExtValue* ret) |
---|
123 | { |
---|
124 | if (!data.size()) {ret->setEmpty(); return;} |
---|
125 | double s=0.0; |
---|
126 | for(int i=data.size()-1;i>=0;i--) |
---|
127 | s+=((ExtValue*)data.get(i))->getDouble(); |
---|
128 | s/=data.size(); |
---|
129 | ret->setDouble(s); |
---|
130 | } |
---|
131 | |
---|
132 | SString VectorObject::serialize() const |
---|
133 | { |
---|
134 | SString out="["; |
---|
135 | { |
---|
136 | for(int i=0;i<data.size();i++) |
---|
137 | { |
---|
138 | ExtValue* v=(ExtValue*)data.get(i); |
---|
139 | if (i) out+=","; |
---|
140 | if (v) |
---|
141 | out+=v->serialize(); |
---|
142 | else |
---|
143 | out+="null"; |
---|
144 | } |
---|
145 | } |
---|
146 | out+="]"; |
---|
147 | //sprintf(out.directAppend(20),"<Vector@%p>",this);out.endAppend(); |
---|
148 | return out; |
---|
149 | } |
---|
150 | |
---|
151 | static THREAD_LOCAL_DEF(SList,tostring_trace); |
---|
152 | |
---|
153 | void VectorObject::get_toString(ExtValue* ret) |
---|
154 | { |
---|
155 | SString out="["; |
---|
156 | //static SListTempl<VectorObject*> trace; |
---|
157 | if (tlsGetRef(tostring_trace).find(this)>=0) |
---|
158 | out+="..."; |
---|
159 | else |
---|
160 | { |
---|
161 | tlsGetRef(tostring_trace)+=this; |
---|
162 | for(int i=0;i<data.size();i++) |
---|
163 | { |
---|
164 | ExtValue* v=(ExtValue*)data.get(i); |
---|
165 | if (i) out+=","; |
---|
166 | if (v) |
---|
167 | out+=v->getString(); |
---|
168 | else |
---|
169 | out+="null"; |
---|
170 | } |
---|
171 | tlsGetRef(tostring_trace)-=this; |
---|
172 | } |
---|
173 | out+="]"; |
---|
174 | ret->setString(out); |
---|
175 | } |
---|
176 | |
---|
177 | void VectorObject::get_stdev(ExtValue* ret) |
---|
178 | { |
---|
179 | if (!data.size()) {ret->setEmpty(); return;} |
---|
180 | get_avg(ret); |
---|
181 | double a=ret->getDouble(); |
---|
182 | double s=0.0; |
---|
183 | for(int i=data.size()-1;i>=0;i--) |
---|
184 | { |
---|
185 | double d=a-((ExtValue*)data.get(i))->getDouble(); |
---|
186 | s+=d*d; |
---|
187 | } |
---|
188 | ret->setDouble(sqrt(s/max(1,data.size()-1))); |
---|
189 | } |
---|
190 | |
---|
191 | void VectorObject::p_find(PARAMPROCARGS) |
---|
192 | { |
---|
193 | short i; |
---|
194 | for(i=0;i<data.size();i++) |
---|
195 | { |
---|
196 | if ((*args)==(*get(i))) |
---|
197 | {ret->setInt(i);return;} |
---|
198 | } |
---|
199 | ret->setInt(-1); |
---|
200 | } |
---|
201 | |
---|
202 | class VEComparator |
---|
203 | { |
---|
204 | public: |
---|
205 | bool operator()(const ExtValue *a,const ExtValue *b) {return a->compare(*b)<0;} |
---|
206 | }; |
---|
207 | |
---|
208 | #ifndef NO_VMACHINE |
---|
209 | class VMVEComparator |
---|
210 | { |
---|
211 | public: |
---|
212 | VMachine::JumpTargetObject *jto; |
---|
213 | VMachine *vm; |
---|
214 | VMVEComparator(VMachine::JumpTargetObject *_jto):jto(_jto),vm(jto->vm) {} |
---|
215 | bool operator()(const ExtValue *a,const ExtValue *b); |
---|
216 | }; |
---|
217 | |
---|
218 | bool VMVEComparator::operator()(const ExtValue *a,const ExtValue *b) |
---|
219 | { |
---|
220 | if (!VMCode::prepareDynamicJumpTarget(jto->pc,jto->code)) |
---|
221 | return false; |
---|
222 | |
---|
223 | vm->push(*a); |
---|
224 | vm->push(*b); |
---|
225 | vm->pushNewCallState(); |
---|
226 | vm->jumpDynamicJumpTarget(jto->pc); |
---|
227 | vm->run(); |
---|
228 | vm->popCallState(); |
---|
229 | bool ret; |
---|
230 | ExtValue& retval=vm->getValue(); |
---|
231 | if (retval.type==TInvalid) |
---|
232 | { |
---|
233 | ret=false; |
---|
234 | FMprintf("VectorElementComparator","",FMLV_ERROR,"Comparison function returned no value"); |
---|
235 | } |
---|
236 | else |
---|
237 | ret=(retval.getInt()!=0); |
---|
238 | vm->drop(2); |
---|
239 | return ret; |
---|
240 | } |
---|
241 | #endif |
---|
242 | |
---|
243 | void VectorObject::p_sort(PARAMPROCARGS) |
---|
244 | { |
---|
245 | #ifndef NO_VMACHINE |
---|
246 | VMachine::JumpTargetObject *jto=VMachine::JumpTargetObject::fromObject(args->getObject()); |
---|
247 | if (jto) |
---|
248 | { |
---|
249 | VMVEComparator cmp(jto); |
---|
250 | ExtValue **first=(ExtValue**)&data.getref(); |
---|
251 | std::sort(first,first+data.size(),cmp); |
---|
252 | } |
---|
253 | else |
---|
254 | #endif |
---|
255 | { |
---|
256 | VEComparator cmp; |
---|
257 | ExtValue **first=(ExtValue**)&data.getref(); |
---|
258 | std::sort(first,first+data.size(),cmp); |
---|
259 | } |
---|
260 | ret->setEmpty(); |
---|
261 | } |
---|
262 | |
---|
263 | void VectorObject::get_iterator(ExtValue* ret) |
---|
264 | { |
---|
265 | ret->setObject(VectorIterator::makeFrom(this)); |
---|
266 | } |
---|
267 | |
---|
268 | VectorObject* VectorObject::fromObject(const ExtObject& o) |
---|
269 | { |
---|
270 | return (VectorObject*)o.getTarget(par.getName()); |
---|
271 | } |
---|
272 | |
---|
273 | ///////////////////////////// |
---|
274 | |
---|
275 | void DictionaryObject::clear() |
---|
276 | { |
---|
277 | for(HashEntryIterator it(hash);it.isValid();) |
---|
278 | { |
---|
279 | ExtValue *v=(ExtValue*)hash.remove(it); |
---|
280 | if (v) delete v; |
---|
281 | } |
---|
282 | hash.clear(); |
---|
283 | hash.init(); |
---|
284 | } |
---|
285 | |
---|
286 | void DictionaryObject::p_find(PARAMPROCARGS) |
---|
287 | { |
---|
288 | for(HashEntryIterator it(hash);it.isValid();it++) |
---|
289 | { |
---|
290 | if ((*args)==(*((ExtValue*)it->value))) |
---|
291 | { |
---|
292 | ret->setString(it->key); |
---|
293 | return; |
---|
294 | } |
---|
295 | } |
---|
296 | ret->setEmpty(); |
---|
297 | } |
---|
298 | |
---|
299 | HashEntryIterator* DictionaryObject::getIndexIterator(int i) |
---|
300 | { |
---|
301 | if (i<0) return 0; |
---|
302 | if (i>=hash.getSize()) return 0; |
---|
303 | |
---|
304 | if ((!it.isValid())||(it_index>i)) |
---|
305 | { |
---|
306 | it=HashEntryIterator(hash); |
---|
307 | it_index=0; |
---|
308 | } |
---|
309 | while(it.isValid()) |
---|
310 | { |
---|
311 | if (it_index==i) |
---|
312 | return ⁢ |
---|
313 | it_index++; |
---|
314 | it++; |
---|
315 | } |
---|
316 | return 0; |
---|
317 | } |
---|
318 | |
---|
319 | void DictionaryObject::p_remove(PARAMPROCARGS) |
---|
320 | { |
---|
321 | if ((args->type==TInt)||(args->type==TDouble)) |
---|
322 | { |
---|
323 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
324 | if (iter) |
---|
325 | { |
---|
326 | ExtValue *oldval=(ExtValue*)hash.remove(*iter); |
---|
327 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
328 | } |
---|
329 | } |
---|
330 | else |
---|
331 | { |
---|
332 | ExtValue *oldval=(ExtValue*)hash.remove(args[0].getString()); |
---|
333 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
334 | } |
---|
335 | } |
---|
336 | |
---|
337 | void DictionaryObject::p_get(PARAMPROCARGS) |
---|
338 | { |
---|
339 | if ((args->type==TInt)||(args->type==TDouble)) |
---|
340 | { |
---|
341 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
342 | if (iter && (*iter)->value) |
---|
343 | { |
---|
344 | *ret=*((ExtValue*)(*iter)->value); |
---|
345 | return; |
---|
346 | } |
---|
347 | } |
---|
348 | else |
---|
349 | { |
---|
350 | ExtValue *val=(ExtValue*)hash.get(args[0].getString()); |
---|
351 | if (val) |
---|
352 | { |
---|
353 | *ret=*val; |
---|
354 | return; |
---|
355 | } |
---|
356 | } |
---|
357 | *ret=ExtValue(); |
---|
358 | } |
---|
359 | |
---|
360 | void DictionaryObject::p_getKey(PARAMPROCARGS) |
---|
361 | { |
---|
362 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
363 | if (iter) |
---|
364 | { |
---|
365 | *ret=(*iter)->key; |
---|
366 | return; |
---|
367 | } |
---|
368 | *ret=ExtValue(); |
---|
369 | } |
---|
370 | |
---|
371 | void DictionaryObject::p_set(PARAMPROCARGS) |
---|
372 | { |
---|
373 | ExtValue *newval=(args[0].getType()==TUnknown)?0:new ExtValue(args[0]); |
---|
374 | ExtValue *oldval=(ExtValue*)hash.put(args[1].getString(),newval); |
---|
375 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
376 | } |
---|
377 | |
---|
378 | SString DictionaryObject::serialize() const |
---|
379 | { |
---|
380 | SString out="{"; |
---|
381 | { |
---|
382 | for(HashEntryIterator it(hash);it.isValid();) |
---|
383 | { |
---|
384 | out+="\""; |
---|
385 | SString q=it->key; sstringQuote(q); |
---|
386 | out+=q; |
---|
387 | out+="\":"; |
---|
388 | out+=((ExtValue*)it->value)->serialize(); |
---|
389 | it++; |
---|
390 | if (it.isValid()) out+=","; |
---|
391 | } |
---|
392 | } |
---|
393 | out+="}"; |
---|
394 | return out; |
---|
395 | } |
---|
396 | |
---|
397 | void DictionaryObject::get_toString(ExtValue* ret) |
---|
398 | { |
---|
399 | SString out="{"; |
---|
400 | //static SListTempl<DictionaryObject*> trace; |
---|
401 | if (tlsGetRef(tostring_trace).find(this)>=0) |
---|
402 | out+="..."; |
---|
403 | else |
---|
404 | { |
---|
405 | tlsGetRef(tostring_trace)+=this; |
---|
406 | for(HashEntryIterator it(hash);it.isValid();) |
---|
407 | { |
---|
408 | out+=it->key; |
---|
409 | out+=":"; |
---|
410 | out+=((ExtValue*)it->value)->getString(); |
---|
411 | it++; |
---|
412 | if (it.isValid()) out+=","; |
---|
413 | } |
---|
414 | tlsGetRef(tostring_trace)-=this; |
---|
415 | } |
---|
416 | out+="}"; |
---|
417 | ret->setString(out); |
---|
418 | } |
---|
419 | |
---|
420 | DictionaryObject* DictionaryObject::fromObject(const ExtObject& o) |
---|
421 | { |
---|
422 | return (DictionaryObject*)o.getTarget(par.getName()); |
---|
423 | } |
---|
424 | |
---|
425 | //////////////// |
---|
426 | |
---|
427 | VectorIterator::VectorIterator(VectorObject* v) |
---|
428 | { |
---|
429 | vec=v; |
---|
430 | vec->incref(); |
---|
431 | pos=-1; |
---|
432 | } |
---|
433 | |
---|
434 | #define FIELDSTRUCT VectorIterator |
---|
435 | ParamEntry vectoriterator_paramtab[]= |
---|
436 | { |
---|
437 | {"VectorIterator",1,2,"VectorIterator","VectorIterator"}, |
---|
438 | {"next",0,PARAM_READONLY+PARAM_NOSTATIC,"next","d 0 1",GETONLY(next),}, |
---|
439 | {"value",0,PARAM_READONLY+PARAM_NOSTATIC,"value","x",GETONLY(value),}, |
---|
440 | {0,0,0,}, |
---|
441 | }; |
---|
442 | #undef FIELDSTRUCT |
---|
443 | |
---|
444 | ExtObject VectorIterator::makeFrom(VectorObject *v) |
---|
445 | { |
---|
446 | static Param par(vectoriterator_paramtab); |
---|
447 | return ExtObject(&par,new VectorIterator(v)); |
---|
448 | } |
---|
449 | |
---|
450 | VectorIterator::~VectorIterator() |
---|
451 | { |
---|
452 | vec->decref(); |
---|
453 | } |
---|
454 | |
---|
455 | void VectorIterator::get_next(ExtValue* ret) |
---|
456 | { |
---|
457 | pos++; |
---|
458 | ret->setInt((pos < vec->data.size()) ? 1 : 0); |
---|
459 | } |
---|
460 | |
---|
461 | void VectorIterator::get_value(ExtValue* ret) |
---|
462 | { |
---|
463 | ExtValue *v=(ExtValue*) (((pos>=0)&&(pos<vec->data.size())) ? vec->data(pos) : NULL ); |
---|
464 | if (v) |
---|
465 | *ret=*v; |
---|
466 | else |
---|
467 | ret->setEmpty(); |
---|
468 | } |
---|