Changeset 453 for cpp/frams


Ignore:
Timestamp:
11/29/15 22:20:55 (6 years ago)
Author:
Maciej Komosinski
Message:

Built-in shallow clone() for Vector and Dictionary

Location:
cpp/frams/vm/classes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/vm/classes/collectionobj.cpp

    r409 r453  
    1515ParamEntry vector_paramtab[]=
    1616{
    17 {"Vector",1,13,"Vector","Vector is a 1-dimensional array indexed by an integer value (starting from 0). "
     17{"Vector",1,14,"Vector","Vector is a 1-dimensional array indexed by an integer value (starting from 0). "
    1818 "Multidimensional arrays can be simulated by putting other Vector objects into a Vector.\n"
    1919"Examples:\n"
     
    3939{"toString",0,PARAM_READONLY | PARAM_NOSTATIC,"Textual form","s",GETONLY(toString),},
    4040{"new",0,0,"Create new Vector","p oVector()",STATICPROCEDURE(p_new),},
    41 {"sort",0,PARAM_NOSTATIC,"Sort elements (in place)","p(o 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 by the \"function FUNCTIONNAME\" operator.\n\nExample:\nfunction compareLastDigit(a,b) {return (a%10)<(b%10);}\nvar v=[16,23,35,42,54,61];\nv.sort(function compareLastDigit);"},
     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);"},
    4242{"iterator",0,PARAM_NOSTATIC | PARAM_READONLY,"Iterator","o",GETONLY(iterator),},
     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));"},
    4344{0,0,0,},
    4445};
     
    4849ParamEntry dictionary_paramtab[]=
    4950{
    50 {"Dictionary",1,9,"Dictionary","Dictionary associates stored values with string keys "
     51{"Dictionary",1,10,"Dictionary","Dictionary associates stored values with string keys "
    5152 "(\"key\" is the first argument in get/set/remove functions). Integer key can be "
    5253 "used to enumerate all elements (note that while iterating, the elements are returned in no particular order).\n"
     
    7475{"new",0,0,"Create a Dictionary","p oDictionary()",STATICPROCEDURE(p_new),"Empty directory can be also created using the {} expression."},
    7576{"toString",0,PARAM_READONLY | PARAM_NOSTATIC,"Textual form","s",GETONLY(toString),},
     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));"},
    7678{0,0,0,},
    7779};
     
    220222        }
    221223ret->setInt(-1);
     224}
     225
     226void VectorObject::p_clone(PARAMPROCARGS)
     227{
     228VectorObject *c=new VectorObject;
     229c->data.setSize(data.size());
     230for(int i=0;i<data.size();i++)
     231        {
     232        ExtValue *v=(ExtValue*)get(i);
     233        if (v)
     234                c->data.set(i,new ExtValue(*v));
     235        }
     236ret->setObject(ExtObject(&par,c));
    222237}
    223238
     
    446461}
    447462
     463void DictionaryObject::p_clone(PARAMPROCARGS)
     464{
     465DictionaryObject *c=new DictionaryObject;
     466for(HashEntryIterator it(hash);it.isValid();it++)
     467        {
     468        ExtValue *v=(ExtValue*)it->value;
     469        c->hash.put(it->key,v?new ExtValue(*v):NULL);
     470        }
     471ret->setObject(ExtObject(&par,c));
     472}
     473
    448474DictionaryObject* DictionaryObject::fromObject(const ExtObject& o, bool warn)
    449475{
  • cpp/frams/vm/classes/collectionobj.h

    r286 r453  
    3838PARAMGETDEF(toString);
    3939PARAMPROCDEF(p_sort);
     40PARAMPROCDEF(p_clone);
    4041#undef STATRICKCLASS
    4142static void p_new(void*,ExtValue*args,ExtValue*ret)
     
    7071PARAMPROCDEF(p_find);
    7172PARAMGETDEF(toString);
     73PARAMPROCDEF(p_clone);
    7274#undef STATRICKCLASS
    7375SString serialize() const;
Note: See TracChangeset for help on using the changeset viewer.