Changeset 642


Ignore:
Timestamp:
12/31/16 20:32:03 (8 years ago)
Author:
Maciej Komosinski
Message:

Implemented insert() in FramScript? Vector class

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

Legend:

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

    r545 r642  
    1515ParamEntry vector_paramtab[]=
    1616{
    17 {"Vector",1,14,"Vector","Vector is a 1-dimensional array indexed by an integer value (starting from 0). "
     17{"Vector",1,15,"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"
     
    3333{"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)"},
    3434{"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),},
    3536{"add",0,PARAM_NOSTATIC,"Append at the end","p(x value)",PROCEDURE(p_add),},
    3637{"find",0,PARAM_NOSTATIC,"Find","p d(x value)",PROCEDURE(p_find),"returns the element index or -1 if not found"},
     
    9596        :readonly(0),owndata(1)
    9697{
    97 set(0,ExtValue(pt.x));
    98 set(1,ExtValue(pt.y));
    99 set(2,ExtValue(pt.z));
     98set_or_insert(0,ExtValue(pt.x),false);
     99set_or_insert(1,ExtValue(pt.y),false);
     100set_or_insert(2,ExtValue(pt.z),false);
    100101}
    101102
     
    121122}
    122123
    123 void VectorObject::set(int i,const ExtValue& val)
    124 {
     124void VectorObject::set_or_insert(int i,const ExtValue& val,bool insert)
     125{
     126if (i<0) return;
    125127int 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--;
     128if (i>oldsize)
     129        {
     130        data.setSize(i);
     131        while(i>oldsize)
     132                data.set(oldsize++,0);
     133        }
     134if (insert)
     135        data.insert(i,new ExtValue(val));
     136else
     137        {
     138        ExtValue *v=(ExtValue*)data.get(i);
     139        if (v) delete v;
     140        data.set(i,new ExtValue(val));
    135141        }
    136142}
  • cpp/frams/vm/classes/collectionobj.h

    r490 r642  
    1919void clear();
    2020ExtValue *get(int i) {return (ExtValue*)data.get(i);}
    21 void set(int i,const ExtValue& val);
     21void set_or_insert(int i,const ExtValue& val,bool insert);
    2222
    2323static Param par;
     
    3535PARAMPROCDEF(p_get);
    3636PARAMPROCDEF(p_find);
    37 PARAMPROCDEF(p_set) {if (!readonly) set(arg1[1].getInt(),arg1[0]);}
     37PARAMPROCDEF(p_set) {if (!readonly) set_or_insert(arg1[1].getInt(),arg1[0],false);}
    3838PARAMPROCDEF(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);}
     39PARAMPROCDEF(p_insert) {if (!readonly) set_or_insert(arg1[1].getInt(),arg1[0],true);}
    3940PARAMGETDEF(toString);
    4041PARAMPROCDEF(p_sort);
Note: See TracChangeset for help on using the changeset viewer.