source: cpp/frams/vm/classes/collectionobj.cpp @ 852

Last change on this file since 852 was 852, checked in by Maciej Komosinski, 7 months ago

Dictionary.find() now also works for null values stored in a dictionary (now that they can be officially stored)

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