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