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