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