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