1 | // This file is a part of the Framsticks GDK library. |
---|
2 | // Copyright (C) 2002-2011 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 | //nie tak spektakularny jak rasowe bugi roku ale tez idiotyczny :> |
---|
80 | //wszystko przez to ze poczatkowo bylo tylko uzywane w destruktorze... |
---|
81 | data.clear(); |
---|
82 | } |
---|
83 | |
---|
84 | void VectorObject::p_remove(PARAMPROCARGS) |
---|
85 | { |
---|
86 | if (readonly) return; |
---|
87 | int i=args->getInt(); |
---|
88 | if ((i<0)||(i>=data.size())) return; |
---|
89 | ExtValue *v=(ExtValue*)data.get(i); |
---|
90 | if (v) delete v; |
---|
91 | data-=i; |
---|
92 | } |
---|
93 | |
---|
94 | void VectorObject::set(int i,const ExtValue& val) |
---|
95 | { |
---|
96 | int oldsize=data.size(); |
---|
97 | if (i<0) return; |
---|
98 | ExtValue *v=(ExtValue*)data.get(i); |
---|
99 | if (v) delete v; |
---|
100 | data.set(i,new ExtValue(val)); |
---|
101 | i--; |
---|
102 | while(i>=oldsize) |
---|
103 | { |
---|
104 | data.set(i,0); |
---|
105 | i--; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | void VectorObject::p_get(PARAMPROCARGS) |
---|
110 | { |
---|
111 | int i=args->getInt(); |
---|
112 | if (listIndexCheck(&data,i,"VectorObject","get")) |
---|
113 | { |
---|
114 | ExtValue *v=get(i); |
---|
115 | if (v) |
---|
116 | { |
---|
117 | *ret=*v; |
---|
118 | return; |
---|
119 | } |
---|
120 | } |
---|
121 | *ret=ExtValue(); |
---|
122 | } |
---|
123 | |
---|
124 | void VectorObject::get_avg(ExtValue* ret) |
---|
125 | { |
---|
126 | if (!data.size()) {ret->setEmpty(); return;} |
---|
127 | double s=0.0; |
---|
128 | for(int i=data.size()-1;i>=0;i--) |
---|
129 | s+=((ExtValue*)data.get(i))->getDouble(); |
---|
130 | s/=data.size(); |
---|
131 | ret->setDouble(s); |
---|
132 | } |
---|
133 | |
---|
134 | SString VectorObject::serialize() const |
---|
135 | { |
---|
136 | SString out="["; |
---|
137 | { |
---|
138 | for(int i=0;i<data.size();i++) |
---|
139 | { |
---|
140 | ExtValue* v=(ExtValue*)data.get(i); |
---|
141 | if (i) out+=","; |
---|
142 | if (v) |
---|
143 | out+=v->serialize(); |
---|
144 | else |
---|
145 | out+="null"; |
---|
146 | } |
---|
147 | } |
---|
148 | out+="]"; |
---|
149 | //sprintf(out.directAppend(20),"<Vector@%p>",this);out.endAppend(); |
---|
150 | return out; |
---|
151 | } |
---|
152 | |
---|
153 | static THREAD_LOCAL_DEF(SList,tostring_trace); |
---|
154 | |
---|
155 | void VectorObject::get_toString(ExtValue* ret) |
---|
156 | { |
---|
157 | SString out="["; |
---|
158 | //static SListTempl<VectorObject*> trace; |
---|
159 | if (tlsGetRef(tostring_trace).find(this)>=0) |
---|
160 | out+="..."; |
---|
161 | else |
---|
162 | { |
---|
163 | tlsGetRef(tostring_trace)+=this; |
---|
164 | for(int i=0;i<data.size();i++) |
---|
165 | { |
---|
166 | ExtValue* v=(ExtValue*)data.get(i); |
---|
167 | if (i) out+=","; |
---|
168 | if (v) |
---|
169 | out+=v->getString(); |
---|
170 | else |
---|
171 | out+="null"; |
---|
172 | } |
---|
173 | tlsGetRef(tostring_trace)-=this; |
---|
174 | } |
---|
175 | out+="]"; |
---|
176 | ret->setString(out); |
---|
177 | } |
---|
178 | |
---|
179 | void VectorObject::get_stdev(ExtValue* ret) |
---|
180 | { |
---|
181 | if (!data.size()) {ret->setEmpty(); return;} |
---|
182 | get_avg(ret); |
---|
183 | double a=ret->getDouble(); |
---|
184 | double s=0.0; |
---|
185 | for(int i=data.size()-1;i>=0;i--) |
---|
186 | { |
---|
187 | double d=a-((ExtValue*)data.get(i))->getDouble(); |
---|
188 | s+=d*d; |
---|
189 | } |
---|
190 | ret->setDouble(sqrt(s/max(1,data.size()-1))); |
---|
191 | } |
---|
192 | |
---|
193 | void VectorObject::p_find(PARAMPROCARGS) |
---|
194 | { |
---|
195 | short i; |
---|
196 | for(i=0;i<data.size();i++) |
---|
197 | { |
---|
198 | if ((*args)==(*get(i))) |
---|
199 | {ret->setInt(i);return;} |
---|
200 | } |
---|
201 | ret->setInt(-1); |
---|
202 | } |
---|
203 | |
---|
204 | class VEComparator |
---|
205 | { |
---|
206 | public: |
---|
207 | bool operator()(const ExtValue *a,const ExtValue *b) {return a->compare(*b)<0;} |
---|
208 | }; |
---|
209 | |
---|
210 | #ifndef NO_VMACHINE |
---|
211 | class VMVEComparator |
---|
212 | { |
---|
213 | public: |
---|
214 | VMachine::JumpTargetObject *jto; |
---|
215 | VMachine *vm; |
---|
216 | VMVEComparator(VMachine::JumpTargetObject *_jto):jto(_jto),vm(jto->vm) {} |
---|
217 | bool operator()(const ExtValue *a,const ExtValue *b); |
---|
218 | }; |
---|
219 | |
---|
220 | bool VMVEComparator::operator()(const ExtValue *a,const ExtValue *b) |
---|
221 | { |
---|
222 | if (!VMCode::prepareDynamicJumpTarget(jto->pc,jto->code)) |
---|
223 | return false; |
---|
224 | |
---|
225 | vm->push(*a); |
---|
226 | vm->push(*b); |
---|
227 | vm->pushNewCallState(); |
---|
228 | vm->jumpDynamicJumpTarget(jto->pc); |
---|
229 | vm->run(); |
---|
230 | vm->popCallState(); |
---|
231 | bool ret; |
---|
232 | ExtValue& retval=vm->getValue(); |
---|
233 | if (retval.type==TInvalid) |
---|
234 | { |
---|
235 | ret=false; |
---|
236 | FMprintf("VectorElementComparator","",FMLV_ERROR,"Comparison function returned no value"); |
---|
237 | } |
---|
238 | else |
---|
239 | ret=(retval.getInt()!=0); |
---|
240 | vm->drop(2); |
---|
241 | return ret; |
---|
242 | } |
---|
243 | #endif |
---|
244 | |
---|
245 | void VectorObject::p_sort(PARAMPROCARGS) |
---|
246 | { |
---|
247 | #ifndef NO_VMACHINE |
---|
248 | VMachine::JumpTargetObject *jto=VMachine::JumpTargetObject::fromObject(args->getObject()); |
---|
249 | if (jto) |
---|
250 | { |
---|
251 | VMVEComparator cmp(jto); |
---|
252 | ExtValue **first=(ExtValue**)&data.getref(); |
---|
253 | std::sort(first,first+data.size(),cmp); |
---|
254 | } |
---|
255 | else |
---|
256 | #endif |
---|
257 | { |
---|
258 | VEComparator cmp; |
---|
259 | ExtValue **first=(ExtValue**)&data.getref(); |
---|
260 | std::sort(first,first+data.size(),cmp); |
---|
261 | } |
---|
262 | ret->setEmpty(); |
---|
263 | } |
---|
264 | |
---|
265 | void VectorObject::get_iterator(ExtValue* ret) |
---|
266 | { |
---|
267 | ret->setObject(VectorIterator::makeFrom(this)); |
---|
268 | } |
---|
269 | |
---|
270 | VectorObject* VectorObject::fromObject(const ExtObject& o) |
---|
271 | { |
---|
272 | return (VectorObject*)o.getTarget(par.getName()); |
---|
273 | } |
---|
274 | |
---|
275 | ///////////////////////////// |
---|
276 | |
---|
277 | void DictionaryObject::clear() |
---|
278 | { |
---|
279 | for(HashEntryIterator it(hash);it.isValid();) |
---|
280 | { |
---|
281 | ExtValue *v=(ExtValue*)hash.remove(it); |
---|
282 | if (v) delete v; |
---|
283 | } |
---|
284 | hash.clear(); |
---|
285 | hash.init(); |
---|
286 | } |
---|
287 | |
---|
288 | void DictionaryObject::p_find(PARAMPROCARGS) |
---|
289 | { |
---|
290 | for(HashEntryIterator it(hash);it.isValid();it++) |
---|
291 | { |
---|
292 | if ((*args)==(*((ExtValue*)it->value))) |
---|
293 | { |
---|
294 | ret->setString(it->key); |
---|
295 | return; |
---|
296 | } |
---|
297 | } |
---|
298 | ret->setEmpty(); |
---|
299 | } |
---|
300 | |
---|
301 | HashEntryIterator* DictionaryObject::getIndexIterator(int i) |
---|
302 | { |
---|
303 | if (i<0) return 0; |
---|
304 | if (i>=hash.getSize()) return 0; |
---|
305 | |
---|
306 | if ((!it.isValid())||(it_index>i)) |
---|
307 | { |
---|
308 | it=HashEntryIterator(hash); |
---|
309 | it_index=0; |
---|
310 | } |
---|
311 | while(it.isValid()) |
---|
312 | { |
---|
313 | if (it_index==i) |
---|
314 | return ⁢ |
---|
315 | it_index++; |
---|
316 | it++; |
---|
317 | } |
---|
318 | return 0; |
---|
319 | } |
---|
320 | |
---|
321 | void DictionaryObject::p_remove(PARAMPROCARGS) |
---|
322 | { |
---|
323 | if ((args->type==TInt)||(args->type==TDouble)) |
---|
324 | { |
---|
325 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
326 | if (iter) |
---|
327 | { |
---|
328 | ExtValue *oldval=(ExtValue*)hash.remove(*iter); |
---|
329 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
330 | } |
---|
331 | } |
---|
332 | else |
---|
333 | { |
---|
334 | ExtValue *oldval=(ExtValue*)hash.remove(args[0].getString()); |
---|
335 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
336 | } |
---|
337 | } |
---|
338 | |
---|
339 | void DictionaryObject::p_get(PARAMPROCARGS) |
---|
340 | { |
---|
341 | if ((args->type==TInt)||(args->type==TDouble)) |
---|
342 | { |
---|
343 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
344 | if (iter && (*iter)->value) |
---|
345 | { |
---|
346 | *ret=*((ExtValue*)(*iter)->value); |
---|
347 | return; |
---|
348 | } |
---|
349 | } |
---|
350 | else |
---|
351 | { |
---|
352 | ExtValue *val=(ExtValue*)hash.get(args[0].getString()); |
---|
353 | if (val) |
---|
354 | { |
---|
355 | *ret=*val; |
---|
356 | return; |
---|
357 | } |
---|
358 | } |
---|
359 | *ret=ExtValue(); |
---|
360 | } |
---|
361 | |
---|
362 | void DictionaryObject::p_getKey(PARAMPROCARGS) |
---|
363 | { |
---|
364 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
365 | if (iter) |
---|
366 | { |
---|
367 | *ret=(*iter)->key; |
---|
368 | return; |
---|
369 | } |
---|
370 | *ret=ExtValue(); |
---|
371 | } |
---|
372 | |
---|
373 | void DictionaryObject::p_set(PARAMPROCARGS) |
---|
374 | { |
---|
375 | ExtValue *newval=(args[0].getType()==TUnknown)?0:new ExtValue(args[0]); |
---|
376 | ExtValue *oldval=(ExtValue*)hash.put(args[1].getString(),newval); |
---|
377 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
378 | } |
---|
379 | |
---|
380 | SString DictionaryObject::serialize() const |
---|
381 | { |
---|
382 | SString out="{"; |
---|
383 | { |
---|
384 | for(HashEntryIterator it(hash);it.isValid();) |
---|
385 | { |
---|
386 | out+="\""; |
---|
387 | SString q=it->key; sstringQuote(q); |
---|
388 | out+=q; |
---|
389 | out+="\":"; |
---|
390 | out+=((ExtValue*)it->value)->serialize(); |
---|
391 | it++; |
---|
392 | if (it.isValid()) out+=","; |
---|
393 | } |
---|
394 | } |
---|
395 | out+="}"; |
---|
396 | return out; |
---|
397 | } |
---|
398 | |
---|
399 | void DictionaryObject::get_toString(ExtValue* ret) |
---|
400 | { |
---|
401 | SString out="{"; |
---|
402 | //static SListTempl<DictionaryObject*> trace; |
---|
403 | if (tlsGetRef(tostring_trace).find(this)>=0) |
---|
404 | out+="..."; |
---|
405 | else |
---|
406 | { |
---|
407 | tlsGetRef(tostring_trace)+=this; |
---|
408 | for(HashEntryIterator it(hash);it.isValid();) |
---|
409 | { |
---|
410 | out+=it->key; |
---|
411 | out+=":"; |
---|
412 | out+=((ExtValue*)it->value)->getString(); |
---|
413 | it++; |
---|
414 | if (it.isValid()) out+=","; |
---|
415 | } |
---|
416 | tlsGetRef(tostring_trace)-=this; |
---|
417 | } |
---|
418 | out+="}"; |
---|
419 | ret->setString(out); |
---|
420 | } |
---|
421 | |
---|
422 | DictionaryObject* DictionaryObject::fromObject(const ExtObject& o) |
---|
423 | { |
---|
424 | return (DictionaryObject*)o.getTarget(par.getName()); |
---|
425 | } |
---|
426 | |
---|
427 | //////////////// |
---|
428 | |
---|
429 | VectorIterator::VectorIterator(VectorObject* v) |
---|
430 | { |
---|
431 | vec=v; |
---|
432 | vec->incref(); |
---|
433 | pos=-1; |
---|
434 | } |
---|
435 | |
---|
436 | #define FIELDSTRUCT VectorIterator |
---|
437 | ParamEntry vectoriterator_paramtab[]= |
---|
438 | { |
---|
439 | {"VectorIterator",1,2,"VectorIterator","VectorIterator"}, |
---|
440 | {"next",0,PARAM_READONLY+PARAM_NOSTATIC,"next","d 0 1",GETONLY(next),}, |
---|
441 | {"value",0,PARAM_READONLY+PARAM_NOSTATIC,"value","x",GETONLY(value),}, |
---|
442 | {0,0,0,}, |
---|
443 | }; |
---|
444 | #undef FIELDSTRUCT |
---|
445 | |
---|
446 | ExtObject VectorIterator::makeFrom(VectorObject *v) |
---|
447 | { |
---|
448 | static Param par(vectoriterator_paramtab); |
---|
449 | return ExtObject(&par,new VectorIterator(v)); |
---|
450 | } |
---|
451 | |
---|
452 | VectorIterator::~VectorIterator() |
---|
453 | { |
---|
454 | vec->decref(); |
---|
455 | } |
---|
456 | |
---|
457 | void VectorIterator::get_next(ExtValue* ret) |
---|
458 | { |
---|
459 | pos++; |
---|
460 | ret->setInt((pos < vec->data.size()) ? 1 : 0); |
---|
461 | } |
---|
462 | |
---|
463 | void VectorIterator::get_value(ExtValue* ret) |
---|
464 | { |
---|
465 | ExtValue *v=(ExtValue*) (((pos>=0)&&(pos<vec->data.size())) ? vec->data(pos) : NULL ); |
---|
466 | if (v) |
---|
467 | *ret=*v; |
---|
468 | else |
---|
469 | ret->setEmpty(); |
---|
470 | } |
---|