[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 | { |
---|
[478] | 51 | {"Dictionary",1,11,"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));"}, |
---|
[478] | 78 | {"assign",0,PARAM_NOSTATIC,"Assign from another object","p(x)",PROCEDURE(p_assign),"Replaces current dictionary with dictionary contents from another object."}, |
---|
| 79 | |
---|
[109] | 80 | {0,0,0,}, |
---|
| 81 | }; |
---|
| 82 | #undef FIELDSTRUCT |
---|
| 83 | |
---|
| 84 | Param VectorObject::par(vector_paramtab); |
---|
| 85 | Param DictionaryObject::par(dictionary_paramtab); |
---|
| 86 | |
---|
| 87 | ///////////////////////////////////////// |
---|
| 88 | |
---|
| 89 | VectorObject::VectorObject(Pt3D &pt) |
---|
| 90 | :readonly(0),owndata(1) |
---|
| 91 | { |
---|
| 92 | set(0,ExtValue(pt.x)); |
---|
| 93 | set(1,ExtValue(pt.y)); |
---|
| 94 | set(2,ExtValue(pt.z)); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void VectorObject::clear() |
---|
| 98 | { |
---|
| 99 | if (owndata) |
---|
| 100 | for(int i=data.size()-1;i>=0;i--) |
---|
| 101 | { |
---|
| 102 | ExtValue *v=(ExtValue*)data.get(i); |
---|
| 103 | if (v) delete v; |
---|
| 104 | } |
---|
| 105 | data.clear(); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void VectorObject::p_remove(PARAMPROCARGS) |
---|
| 109 | { |
---|
| 110 | if (readonly) return; |
---|
| 111 | int i=args->getInt(); |
---|
| 112 | if ((i<0)||(i>=data.size())) return; |
---|
| 113 | ExtValue *v=(ExtValue*)data.get(i); |
---|
| 114 | if (v) delete v; |
---|
| 115 | data-=i; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | void VectorObject::set(int i,const ExtValue& val) |
---|
| 119 | { |
---|
| 120 | int oldsize=data.size(); |
---|
| 121 | if (i<0) return; |
---|
| 122 | ExtValue *v=(ExtValue*)data.get(i); |
---|
| 123 | if (v) delete v; |
---|
| 124 | data.set(i,new ExtValue(val)); |
---|
| 125 | i--; |
---|
| 126 | while(i>=oldsize) |
---|
| 127 | { |
---|
| 128 | data.set(i,0); |
---|
| 129 | i--; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void VectorObject::p_get(PARAMPROCARGS) |
---|
| 134 | { |
---|
| 135 | int i=args->getInt(); |
---|
| 136 | if (listIndexCheck(&data,i,"VectorObject","get")) |
---|
| 137 | { |
---|
| 138 | ExtValue *v=get(i); |
---|
| 139 | if (v) |
---|
| 140 | { |
---|
| 141 | *ret=*v; |
---|
| 142 | return; |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | *ret=ExtValue(); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void VectorObject::get_avg(ExtValue* ret) |
---|
| 149 | { |
---|
| 150 | if (!data.size()) {ret->setEmpty(); return;} |
---|
| 151 | double s=0.0; |
---|
| 152 | for(int i=data.size()-1;i>=0;i--) |
---|
| 153 | s+=((ExtValue*)data.get(i))->getDouble(); |
---|
| 154 | s/=data.size(); |
---|
| 155 | ret->setDouble(s); |
---|
| 156 | } |
---|
| 157 | |
---|
[464] | 158 | SString VectorObject::serialize(SerializationFormat format) const |
---|
[109] | 159 | { |
---|
| 160 | SString out="["; |
---|
| 161 | { |
---|
| 162 | for(int i=0;i<data.size();i++) |
---|
| 163 | { |
---|
| 164 | ExtValue* v=(ExtValue*)data.get(i); |
---|
| 165 | if (i) out+=","; |
---|
| 166 | if (v) |
---|
[464] | 167 | out+=v->serialize(format); |
---|
[109] | 168 | else |
---|
| 169 | out+="null"; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | out+="]"; |
---|
| 173 | //sprintf(out.directAppend(20),"<Vector@%p>",this);out.endAppend(); |
---|
| 174 | return out; |
---|
| 175 | } |
---|
| 176 | |
---|
[371] | 177 | static THREAD_LOCAL_DEF(SList,VectorObject_tostring_trace); |
---|
[109] | 178 | |
---|
| 179 | void VectorObject::get_toString(ExtValue* ret) |
---|
| 180 | { |
---|
| 181 | SString out="["; |
---|
| 182 | //static SListTempl<VectorObject*> trace; |
---|
[371] | 183 | if (tlsGetRef(VectorObject_tostring_trace).find(this)>=0) |
---|
[109] | 184 | out+="..."; |
---|
| 185 | else |
---|
| 186 | { |
---|
[371] | 187 | tlsGetRef(VectorObject_tostring_trace)+=this; |
---|
[109] | 188 | for(int i=0;i<data.size();i++) |
---|
| 189 | { |
---|
| 190 | ExtValue* v=(ExtValue*)data.get(i); |
---|
| 191 | if (i) out+=","; |
---|
| 192 | if (v) |
---|
| 193 | out+=v->getString(); |
---|
| 194 | else |
---|
| 195 | out+="null"; |
---|
| 196 | } |
---|
[371] | 197 | tlsGetRef(VectorObject_tostring_trace)-=this; |
---|
[109] | 198 | } |
---|
| 199 | out+="]"; |
---|
| 200 | ret->setString(out); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void VectorObject::get_stdev(ExtValue* ret) |
---|
| 204 | { |
---|
| 205 | if (!data.size()) {ret->setEmpty(); return;} |
---|
| 206 | get_avg(ret); |
---|
| 207 | double a=ret->getDouble(); |
---|
| 208 | double s=0.0; |
---|
| 209 | for(int i=data.size()-1;i>=0;i--) |
---|
| 210 | { |
---|
| 211 | double d=a-((ExtValue*)data.get(i))->getDouble(); |
---|
| 212 | s+=d*d; |
---|
| 213 | } |
---|
| 214 | ret->setDouble(sqrt(s/max(1,data.size()-1))); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | void VectorObject::p_find(PARAMPROCARGS) |
---|
| 218 | { |
---|
| 219 | short i; |
---|
| 220 | for(i=0;i<data.size();i++) |
---|
| 221 | { |
---|
| 222 | if ((*args)==(*get(i))) |
---|
| 223 | {ret->setInt(i);return;} |
---|
| 224 | } |
---|
| 225 | ret->setInt(-1); |
---|
| 226 | } |
---|
| 227 | |
---|
[453] | 228 | void VectorObject::p_clone(PARAMPROCARGS) |
---|
| 229 | { |
---|
| 230 | VectorObject *c=new VectorObject; |
---|
| 231 | c->data.setSize(data.size()); |
---|
| 232 | for(int i=0;i<data.size();i++) |
---|
| 233 | { |
---|
| 234 | ExtValue *v=(ExtValue*)get(i); |
---|
| 235 | if (v) |
---|
| 236 | c->data.set(i,new ExtValue(*v)); |
---|
| 237 | } |
---|
| 238 | ret->setObject(ExtObject(&par,c)); |
---|
| 239 | } |
---|
| 240 | |
---|
[109] | 241 | class VEComparator |
---|
| 242 | { |
---|
| 243 | public: |
---|
[333] | 244 | bool operator()(const ExtValue *a,const ExtValue *b) {return a->compare(*b)==ExtValue::ResultLower;} |
---|
[109] | 245 | }; |
---|
| 246 | |
---|
| 247 | #ifndef NO_VMACHINE |
---|
| 248 | class VMVEComparator |
---|
| 249 | { |
---|
| 250 | public: |
---|
| 251 | VMachine::JumpTargetObject *jto; |
---|
| 252 | VMachine *vm; |
---|
| 253 | VMVEComparator(VMachine::JumpTargetObject *_jto):jto(_jto),vm(jto->vm) {} |
---|
| 254 | bool operator()(const ExtValue *a,const ExtValue *b); |
---|
| 255 | }; |
---|
| 256 | |
---|
| 257 | bool VMVEComparator::operator()(const ExtValue *a,const ExtValue *b) |
---|
| 258 | { |
---|
| 259 | if (!VMCode::prepareDynamicJumpTarget(jto->pc,jto->code)) |
---|
| 260 | return false; |
---|
| 261 | |
---|
| 262 | vm->push(*a); |
---|
| 263 | vm->push(*b); |
---|
| 264 | vm->pushNewCallState(); |
---|
| 265 | vm->jumpDynamicJumpTarget(jto->pc); |
---|
| 266 | vm->run(); |
---|
| 267 | vm->popCallState(); |
---|
| 268 | bool ret; |
---|
| 269 | ExtValue& retval=vm->getValue(); |
---|
| 270 | if (retval.type==TInvalid) |
---|
| 271 | { |
---|
| 272 | ret=false; |
---|
[375] | 273 | logPrintf("VectorElementComparator","",LOG_ERROR,"Comparison function returned no value"); |
---|
[109] | 274 | } |
---|
| 275 | else |
---|
| 276 | ret=(retval.getInt()!=0); |
---|
| 277 | vm->drop(2); |
---|
| 278 | return ret; |
---|
| 279 | } |
---|
| 280 | #endif |
---|
| 281 | |
---|
| 282 | void VectorObject::p_sort(PARAMPROCARGS) |
---|
| 283 | { |
---|
| 284 | #ifndef NO_VMACHINE |
---|
[182] | 285 | VMachine::JumpTargetObject *jto=VMachine::JumpTargetObject::fromObject(args->getObject(),false); |
---|
[109] | 286 | if (jto) |
---|
| 287 | { |
---|
| 288 | VMVEComparator cmp(jto); |
---|
[153] | 289 | ExtValue **first=(ExtValue**)&data.getref(0); |
---|
[109] | 290 | std::sort(first,first+data.size(),cmp); |
---|
| 291 | } |
---|
| 292 | else |
---|
| 293 | #endif |
---|
| 294 | { |
---|
| 295 | VEComparator cmp; |
---|
[153] | 296 | ExtValue **first=(ExtValue**)&data.getref(0); |
---|
[109] | 297 | std::sort(first,first+data.size(),cmp); |
---|
| 298 | } |
---|
| 299 | ret->setEmpty(); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | void VectorObject::get_iterator(ExtValue* ret) |
---|
| 303 | { |
---|
| 304 | ret->setObject(VectorIterator::makeFrom(this)); |
---|
| 305 | } |
---|
| 306 | |
---|
[171] | 307 | VectorObject* VectorObject::fromObject(const ExtObject& o, bool warn) |
---|
[109] | 308 | { |
---|
[171] | 309 | return (VectorObject*)o.getTarget(par.getName(),true,warn); |
---|
[109] | 310 | } |
---|
| 311 | |
---|
| 312 | ///////////////////////////// |
---|
| 313 | |
---|
| 314 | void DictionaryObject::clear() |
---|
| 315 | { |
---|
| 316 | for(HashEntryIterator it(hash);it.isValid();) |
---|
| 317 | { |
---|
| 318 | ExtValue *v=(ExtValue*)hash.remove(it); |
---|
| 319 | if (v) delete v; |
---|
| 320 | } |
---|
| 321 | hash.clear(); |
---|
| 322 | hash.init(); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | void DictionaryObject::p_find(PARAMPROCARGS) |
---|
| 326 | { |
---|
| 327 | for(HashEntryIterator it(hash);it.isValid();it++) |
---|
| 328 | { |
---|
| 329 | if ((*args)==(*((ExtValue*)it->value))) |
---|
| 330 | { |
---|
| 331 | ret->setString(it->key); |
---|
| 332 | return; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | ret->setEmpty(); |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | HashEntryIterator* DictionaryObject::getIndexIterator(int i) |
---|
| 339 | { |
---|
| 340 | if (i<0) return 0; |
---|
| 341 | if (i>=hash.getSize()) return 0; |
---|
| 342 | |
---|
| 343 | if ((!it.isValid())||(it_index>i)) |
---|
| 344 | { |
---|
| 345 | it=HashEntryIterator(hash); |
---|
| 346 | it_index=0; |
---|
| 347 | } |
---|
| 348 | while(it.isValid()) |
---|
| 349 | { |
---|
| 350 | if (it_index==i) |
---|
| 351 | return ⁢ |
---|
| 352 | it_index++; |
---|
| 353 | it++; |
---|
| 354 | } |
---|
| 355 | return 0; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | void DictionaryObject::p_remove(PARAMPROCARGS) |
---|
| 359 | { |
---|
| 360 | if ((args->type==TInt)||(args->type==TDouble)) |
---|
| 361 | { |
---|
| 362 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
| 363 | if (iter) |
---|
| 364 | { |
---|
| 365 | ExtValue *oldval=(ExtValue*)hash.remove(*iter); |
---|
| 366 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | else |
---|
| 370 | { |
---|
| 371 | ExtValue *oldval=(ExtValue*)hash.remove(args[0].getString()); |
---|
| 372 | if (oldval) {*ret=*oldval; delete oldval;} else *ret=ExtValue(); |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | |
---|
[478] | 376 | ExtValue DictionaryObject::get(SString key) |
---|
| 377 | { |
---|
| 378 | ExtValue *val=(ExtValue*)hash.get(key); |
---|
| 379 | if (val) |
---|
| 380 | return *val; |
---|
| 381 | return ExtValue::empty(); |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | ExtValue DictionaryObject::get(int index) |
---|
| 385 | { |
---|
| 386 | HashEntryIterator* iter=getIndexIterator(index); |
---|
| 387 | if (iter && (*iter)->value) |
---|
| 388 | return *((ExtValue*)(*iter)->value); |
---|
| 389 | return ExtValue::empty(); |
---|
| 390 | } |
---|
| 391 | |
---|
[109] | 392 | void DictionaryObject::p_get(PARAMPROCARGS) |
---|
| 393 | { |
---|
| 394 | if ((args->type==TInt)||(args->type==TDouble)) |
---|
[478] | 395 | *ret=get(args->getInt()); |
---|
[109] | 396 | else |
---|
[478] | 397 | *ret=get(args[0].getString()); |
---|
[109] | 398 | } |
---|
| 399 | |
---|
| 400 | void DictionaryObject::p_getKey(PARAMPROCARGS) |
---|
| 401 | { |
---|
| 402 | HashEntryIterator* iter=getIndexIterator(args->getInt()); |
---|
| 403 | if (iter) |
---|
| 404 | { |
---|
| 405 | *ret=(*iter)->key; |
---|
| 406 | return; |
---|
| 407 | } |
---|
| 408 | *ret=ExtValue(); |
---|
| 409 | } |
---|
| 410 | |
---|
[478] | 411 | ExtValue DictionaryObject::set(SString key,ExtValue new_value) |
---|
| 412 | { |
---|
| 413 | ExtValue ret; |
---|
| 414 | ExtValue *new_ext=(new_value.getType()==TUnknown) ? NULL : new ExtValue(new_value); |
---|
| 415 | ExtValue *old_ext=(ExtValue*)hash.put(key,new_ext); |
---|
| 416 | if (old_ext) { ret=*old_ext; delete old_ext;} |
---|
| 417 | return ret; |
---|
| 418 | } |
---|
| 419 | |
---|
[109] | 420 | void DictionaryObject::p_set(PARAMPROCARGS) |
---|
| 421 | { |
---|
[478] | 422 | *ret=set(args[1].getString(),args[0]); |
---|
[109] | 423 | } |
---|
| 424 | |
---|
[464] | 425 | SString DictionaryObject::serialize(SerializationFormat format) const |
---|
[109] | 426 | { |
---|
| 427 | SString out="{"; |
---|
| 428 | { |
---|
| 429 | for(HashEntryIterator it(hash);it.isValid();) |
---|
| 430 | { |
---|
| 431 | out+="\""; |
---|
| 432 | SString q=it->key; sstringQuote(q); |
---|
| 433 | out+=q; |
---|
| 434 | out+="\":"; |
---|
[164] | 435 | if (it->value!=NULL) |
---|
[464] | 436 | out+=((ExtValue*)it->value)->serialize(format); |
---|
[164] | 437 | else |
---|
| 438 | out+="null"; |
---|
[109] | 439 | it++; |
---|
| 440 | if (it.isValid()) out+=","; |
---|
| 441 | } |
---|
| 442 | } |
---|
| 443 | out+="}"; |
---|
| 444 | return out; |
---|
| 445 | } |
---|
| 446 | |
---|
| 447 | void DictionaryObject::get_toString(ExtValue* ret) |
---|
| 448 | { |
---|
| 449 | SString out="{"; |
---|
| 450 | //static SListTempl<DictionaryObject*> trace; |
---|
[371] | 451 | if (tlsGetRef(VectorObject_tostring_trace).find(this)>=0) |
---|
[109] | 452 | out+="..."; |
---|
| 453 | else |
---|
| 454 | { |
---|
[371] | 455 | tlsGetRef(VectorObject_tostring_trace)+=this; |
---|
[109] | 456 | for(HashEntryIterator it(hash);it.isValid();) |
---|
| 457 | { |
---|
| 458 | out+=it->key; |
---|
| 459 | out+=":"; |
---|
[164] | 460 | if (it->value!=NULL) |
---|
| 461 | out+=((ExtValue*)it->value)->getString(); |
---|
| 462 | else |
---|
| 463 | out+="null"; |
---|
[109] | 464 | it++; |
---|
| 465 | if (it.isValid()) out+=","; |
---|
| 466 | } |
---|
[371] | 467 | tlsGetRef(VectorObject_tostring_trace)-=this; |
---|
[109] | 468 | } |
---|
| 469 | out+="}"; |
---|
| 470 | ret->setString(out); |
---|
| 471 | } |
---|
| 472 | |
---|
[478] | 473 | void DictionaryObject::copyFrom(DictionaryObject *other) |
---|
[453] | 474 | { |
---|
[478] | 475 | for(HashEntryIterator it(other->hash);it.isValid();it++) |
---|
[453] | 476 | { |
---|
| 477 | ExtValue *v=(ExtValue*)it->value; |
---|
[478] | 478 | hash.put(it->key,v?new ExtValue(*v):NULL); |
---|
[453] | 479 | } |
---|
[478] | 480 | } |
---|
| 481 | |
---|
| 482 | void DictionaryObject::p_clone(PARAMPROCARGS) |
---|
| 483 | { |
---|
| 484 | DictionaryObject *c=new DictionaryObject; |
---|
| 485 | c->copyFrom(this); |
---|
[453] | 486 | ret->setObject(ExtObject(&par,c)); |
---|
| 487 | } |
---|
| 488 | |
---|
[478] | 489 | void DictionaryObject::p_assign(PARAMPROCARGS) |
---|
| 490 | { |
---|
| 491 | clear(); |
---|
| 492 | DictionaryObject *other=DictionaryObject::fromObject(args[0].getObject(),false); |
---|
| 493 | if (other) |
---|
| 494 | copyFrom(other); |
---|
| 495 | ret->setEmpty(); |
---|
| 496 | } |
---|
| 497 | |
---|
[171] | 498 | DictionaryObject* DictionaryObject::fromObject(const ExtObject& o, bool warn) |
---|
[109] | 499 | { |
---|
[171] | 500 | return (DictionaryObject*)o.getTarget(par.getName(), true, warn); |
---|
[109] | 501 | } |
---|
| 502 | |
---|
| 503 | //////////////// |
---|
| 504 | |
---|
| 505 | VectorIterator::VectorIterator(VectorObject* v) |
---|
| 506 | { |
---|
| 507 | vec=v; |
---|
| 508 | vec->incref(); |
---|
| 509 | pos=-1; |
---|
| 510 | } |
---|
| 511 | |
---|
| 512 | #define FIELDSTRUCT VectorIterator |
---|
| 513 | ParamEntry vectoriterator_paramtab[]= |
---|
| 514 | { |
---|
| 515 | {"VectorIterator",1,2,"VectorIterator","VectorIterator"}, |
---|
[240] | 516 | {"next",0,PARAM_READONLY | PARAM_NOSTATIC,"next","d 0 1",GETONLY(next),}, |
---|
| 517 | {"value",0,PARAM_READONLY | PARAM_NOSTATIC,"value","x",GETONLY(value),}, |
---|
[109] | 518 | {0,0,0,}, |
---|
| 519 | }; |
---|
| 520 | #undef FIELDSTRUCT |
---|
| 521 | |
---|
| 522 | ExtObject VectorIterator::makeFrom(VectorObject *v) |
---|
| 523 | { |
---|
| 524 | static Param par(vectoriterator_paramtab); |
---|
| 525 | return ExtObject(&par,new VectorIterator(v)); |
---|
| 526 | } |
---|
| 527 | |
---|
| 528 | VectorIterator::~VectorIterator() |
---|
| 529 | { |
---|
| 530 | vec->decref(); |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | void VectorIterator::get_next(ExtValue* ret) |
---|
| 534 | { |
---|
| 535 | pos++; |
---|
| 536 | ret->setInt((pos < vec->data.size()) ? 1 : 0); |
---|
| 537 | } |
---|
| 538 | |
---|
| 539 | void VectorIterator::get_value(ExtValue* ret) |
---|
| 540 | { |
---|
| 541 | ExtValue *v=(ExtValue*) (((pos>=0)&&(pos<vec->data.size())) ? vec->data(pos) : NULL ); |
---|
| 542 | if (v) |
---|
| 543 | *ret=*v; |
---|
| 544 | else |
---|
| 545 | ret->setEmpty(); |
---|
| 546 | } |
---|