pike.git / src / post_modules / _ADT / circular_list.cmod

version» Context lines:

pike.git/src/post_modules/_ADT/circular_list.cmod:6:      #include "global.h"      #include "object.h"   #include "svalue.h"   #include "array.h"   #include "pike_error.h"   #include "interpret.h"   #include "stralloc.h"   #include "program.h" + #include "pike_types.h" + #include "pike_embed.h"      #include "program_id.h"      #include "module_support.h"   #include "sequence.h"   #include "circular_list.h"            
pike.git/src/post_modules/_ADT/circular_list.cmod:39:   PIKECLASS CircularList   {       CVAR int pos;    CVAR struct array *a;    CVAR int size;          //if there is more than one reference to this array copy it.    -  static inline void should_copy() +  static inline void should_copy(void)    {    if (THIS->a->refs > 1)    {    free_array(THIS->a);    THIS->a = copy_array(THIS->a);    }    }          //convert an index in the circular list to the index in the underlaying array
pike.git/src/post_modules/_ADT/circular_list.cmod:144:       for (i=0; i < args; i++) {    // FIXME: Ought to work on collection classes too    if (coll[i].u.object->prog == CircularList_program)    {    tmpList = OBJ2_CIRCULARLIST(coll[i].u.object);    sz += tmpList->size;    }    else    { -  SIMPLE_BAD_ARG_ERROR("`+",i+1,"ADT.CircularList"); +  SIMPLE_ARG_TYPE_ERROR("`+",i+1,"ADT.CircularList");    }    }       /* Allocate double the space, so that we can grow. */    push_array(a = allocate_array_no_init(sz*2, 0));       a->type_field = 0;       for (i = -1; i < args; i++) {    if (i >= 0) {
pike.git/src/post_modules/_ADT/circular_list.cmod:275:    */       PIKEFUN array _indices()    {    ptrdiff_t size = THIS->size;    struct array *a;       a=allocate_array_no_init(size,0);    while(--size>=0)    { -  ITEM(a)[size].u.integer = DO_NOT_WARN((INT_TYPE)size); +  ITEM(a)[size].u.integer = (INT_TYPE)size;    }    a->type_field = BIT_INT;    RETURN a;    }      /*! @decl void _insert_element(int index, mixed value)    *! Insert an element in the list at the position @[index], the value    *! at the position @[index] and all above will have their index increased    *! by one.    *!
pike.git/src/post_modules/_ADT/circular_list.cmod:362:    */       PIKEFUN int _search(mixed value, void|int start)    {    int s;    int retval;    if(start)    {    if (TYPEOF(*start) != PIKE_T_INT)    { -  SIMPLE_BAD_ARG_ERROR("_search",2,"int"); +  SIMPLE_ARG_TYPE_ERROR("_search",2,"int");    }    if(start->u.integer <0 || start->u.integer>=THIS->size) {    if (THIS->a->size) {    Pike_error("Start %"PRINTPIKEINT"d is out of array range 0 - %d.\n",    start->u.integer,    THIS->size-1);    } else {    Pike_error("Attempt to index the empty array with %"PRINTPIKEINT"d.\n",    start->u.integer);    }
pike.git/src/post_modules/_ADT/circular_list.cmod:464:    }    should_copy();    THIS->pos--;    if (THIS->pos < 0)    {    THIS->pos=THIS->a->size-1;    }    SET_SVAL(ind, T_INT, NUMBER_NUMBER, integer, THIS->pos);    simple_set_index(THIS->a, &ind, value);    THIS->size++; -  pop_n_elems(args); +     }       /*! @decl void allocate(int elements)    *! Increase the maxsize of the CircularlList.    *!    *! @param elements    *! Add this number of new elements to the list.    */       PIKEFUN void allocate(int elements)
pike.git/src/post_modules/_ADT/circular_list.cmod:487:    INT32 endpos = THIS->a->size - THIS->pos; //distanse of pos from the end    if (elements <= 0)    {    Pike_error("Allocate expects a value larger than zero.\n");    }    if(THIS->a->refs <= 1 && THIS->a->malloced_size >= size)    {    THIS->a->size = size;    if (THIS->size > 0)    { -  MEMMOVE((char *)(ITEM(THIS->a)+(size-endpos)), -  (char *)(ITEM(THIS->a)+(THIS->pos)), +  memmove(ITEM(THIS->a)+(size-endpos), +  ITEM(THIS->a)+(THIS->pos),    (endpos) * sizeof(struct svalue));    THIS->pos = size-endpos;    }    while(elements) {    SET_SVAL(ITEM(THIS->a)[THIS->pos - elements], T_INT, NUMBER_NUMBER,    integer, 0);    elements--;    }    THIS->a->type_field |= BIT_INT;    }
pike.git/src/post_modules/_ADT/circular_list.cmod:521:    //copy from start to size    assign_svalues_no_free(ITEM(a)+endpos,    ITEM(THIS->a),    THIS->size-endpos,    THIS->a->type_field);    }    free_array(THIS->a);    THIS->a=a;    THIS->pos=0;    } -  pop_n_elems(args); +     }    - /*! @decl mixed cast(string type) + /*! @decl array cast(string type)    *! Cast operator.    *!    *! @param type    *! Casts to this type.    *!    *! Casts to the following types are supported:    *! @string    *! @value "array"    *! Cast the content of this list to an array.    *! @endstring    *!    *! @returns    *! An array with the contents of this list.    */    -  PIKEFUN mixed cast(string type) +  PIKEFUN array cast(string type) +  flags ID_PROTECTED;    { -  struct pike_string *array_t; -  MAKE_CONST_STRING( array_t, "array" ); -  -  if (type == array_t) +  pop_n_elems(args); /* type as at least one more reference. */ +  if (type == literal_array_string)    {    struct array *a;    int end;    int tmp;       a=allocate_array_no_init(THIS->size, 0);    a->type_field = THIS->a->type_field;    end = circ2array(THIS->size);       if (end < THIS->pos)
pike.git/src/post_modules/_ADT/circular_list.cmod:577:    {    //just do it direct       assign_svalues_no_free(ITEM(a), ITEM(THIS->a) + THIS->pos,    THIS->size, THIS->a->type_field);    }    push_array(a);       }    else -  { -  Pike_error("Cannot cast to %S\n", type); +  push_undefined();    } -  } +        /*! @decl void clear()    *! Clear the contents of the list.    *!    */       PIKEFUN void clear()    {    int oldsize;    should_copy();
pike.git/src/post_modules/_ADT/circular_list.cmod:767:    }       SET_SVAL(ind, T_INT, NUMBER_NUMBER, integer, circ2array(THIS->size));    THIS->size++;    simple_set_index(THIS->a, &ind, value);    }          /*! @decl void push_front(mixed value)    *! -  *! Add a new value at the end of the list. +  *! Add a new value at the front of the list.    *!    *! @param value    *! The value to add.    */       PIKEFUN void push_front(mixed value)    {    struct svalue ind;    if (THIS->size == THIS->a->size)    {
pike.git/src/post_modules/_ADT/circular_list.cmod:789:    }    should_copy();    THIS->pos--;    if (THIS->pos < 0)    {    THIS->pos=THIS->a->size-1;    }    SET_SVAL(ind, T_INT, NUMBER_NUMBER, integer, THIS->pos);    simple_set_index(THIS->a, &ind, value);    THIS->size++; -  pop_n_elems(args); +     }          /*! @decl void create(array|int arg)    *! Creates a new @[CircularList] around the array arg or a new    *! @[CircularList] with the maximum size of arg.    */       PIKEFUN void create(array|int arg)    {    if (TYPEOF(*arg) == T_INT)    {    THIS->a =allocate_array_no_init(arg->u.integer, 0);    THIS->a->type_field = BIT_INT;    }    else if (TYPEOF(*arg) == T_ARRAY)    {    add_ref(THIS->a=arg->u.array);    THIS->size=THIS->a->size;    } -  -  pop_n_elems(args); +  pop_stack();    }       /*! @decl CircularListIterator _get_iterator(void|int ind)    *! Create and initiate a new CircularListIterator that could be used    *! to iterate over this list.    *!    *! @param ind    *! If an @[ind] value is supplied the iterator will be positioned at    *! that index.    *!
pike.git/src/post_modules/_ADT/circular_list.cmod:846:       /*! @decl CircularListIterator last()    *! Create and initiate a new CircularListIterator that could be used    *! to iterate over this list.    *!    *! @returns    *! An iterator positioned after the last element in the list.    */       + #ifdef PIKE_NULL_IS_SPECIAL    INIT    {    THIS->a=NULL;    THIS->pos=0;    THIS->size=0;    } -  + #endif       EXIT    gc_trivial;    {    if( THIS->a ) -  { +     free_array(THIS->a); -  THIS->a = NULL; +     } -  } +              /*! @class CircularListIterator    *! This is the iterator for the CircularList. It implements the    *! IndexIterator and the OutputIterator.    */       PIKECLASS CircularListIterator    {
pike.git/src/post_modules/_ADT/circular_list.cmod:888:    *! Creates a new iterator for the CircularList @[list]. If @[start] is    *! supplied it will try to position the iterator at @[start].    *!    */       PIKEFUN void create(object list, void|int start)    {       if (list->prog != CircularList_program)    { -  SIMPLE_BAD_ARG_ERROR("create",1,"ADT.CircularList"); -  +  SIMPLE_ARG_TYPE_ERROR("create",1,"ADT.CircularList");    }    else    {    THIS->list = OBJ2_CIRCULARLIST(list);    add_ref(THIS->obj = list);    if (start) // if there was an start index supplied    {    THIS->pos=start->u.integer;    if (THIS->list->a && ((THIS->pos > THIS->list->size)    || (THIS->pos < 0)))    {    Pike_error("Index %d is out of array range 0 - %d.\n",    THIS->pos,    THIS->list->size);    } -  +  pop_n_elems(2);    }    else    {    THIS->pos = 0; -  +  pop_stack();    }    } -  pop_n_elems(args); +     }       /*! @decl int index()    *!    *! @returns    *! The index at the current position.    */       PIKEFUN int index()    {
pike.git/src/post_modules/_ADT/circular_list.cmod:967:    /*! @decl CircularListIterator `+(int steps)    *! Move the iterator @[steps] steps forward (negative value on @[steps]    *! will cause the iterator to move backwards) and return the result    *! as a new iterator.    *! @returns    *! A new iterator positioned @[steps] steps forward.    */       PIKEFUN object `+(int steps)    { -  struct object *o=low_clone(CircularList_CircularListIterator_program); +  struct object *o=fast_clone_object(CircularList_CircularListIterator_program);    struct CircularList_CircularListIterator_struct *new;    new = OBJ2_CIRCULARLIST_CIRCULARLISTITERATOR(o);    new[0]=*THIS;    add_ref(THIS->obj);    new->pos+=steps;    if (new->pos < 0)    {    new->pos = 0;    }    else if (new->pos > new->list->size)
pike.git/src/post_modules/_ADT/circular_list.cmod:1020:    /*! @decl CircularListIterator `-(int steps)    *! Move the iterator @[steps] steps backwards (negative value on    *! @[steps] will cause the iterator to move forwards) and return    *! the result as a new iterator.    *! @returns    *! A new iterator positioned @[steps] steps backwards.    */       PIKEFUN object `-(int steps)    { -  struct object *o=low_clone(CircularList_CircularListIterator_program); +  struct object *o=fast_clone_object(CircularList_CircularListIterator_program);    struct CircularList_CircularListIterator_struct *new;    new = OBJ2_CIRCULARLIST_CIRCULARLISTITERATOR(o);    new[0]=*THIS;    add_ref(THIS->obj);    new->pos-=steps;    if (new->pos < 0)    {    new->pos = 0;    }    else if (new->pos > new->list->size)
pike.git/src/post_modules/_ADT/circular_list.cmod:1141:    {    if (TYPEOF(*iter) == T_OBJECT &&    iter->u.object->prog == CircularList_CircularListIterator_program)    {    struct CircularList_CircularListIterator_struct *i =    OBJ2_CIRCULARLIST_CIRCULARLISTITERATOR(iter->u.object);    RETURN (THIS->pos < i->pos);    }    else    { -  SIMPLE_BAD_ARG_ERROR("`<",1,"ADT.CircularList.CircularListIterator"); +  SIMPLE_ARG_TYPE_ERROR("`<",1,"ADT.CircularList.CircularListIterator");    }    }          /*! @decl int(0..1) `>(mixed iter)    *! Greater then operator    *!    *! @returns    *! Returns @tt{true@} if this iterator has a higher index    *! then @[iter].
pike.git/src/post_modules/_ADT/circular_list.cmod:1166:    {    if (TYPEOF(*iter) == T_OBJECT &&    iter->u.object->prog == CircularList_CircularListIterator_program)    {    struct CircularList_CircularListIterator_struct *i =    OBJ2_CIRCULARLIST_CIRCULARLISTITERATOR(iter->u.object);    RETURN (THIS->pos > i->pos);    }    else    { -  SIMPLE_BAD_ARG_ERROR("`>",1,"ADT.CircularList.CircularListIterator"); +  SIMPLE_ARG_TYPE_ERROR("`>",1,"ADT.CircularList.CircularListIterator");    }    }       /*! @decl int distance(object iter)    *!    *! @param iter    *! The iterator to measure the distance to.    *! @returns    *! Returns distance between this iterator and @[iter].    *! @throws
pike.git/src/post_modules/_ADT/circular_list.cmod:1191:    {    if (iter->prog == CircularList_CircularListIterator_program)    {       struct CircularList_CircularListIterator_struct *i =    OBJ2_CIRCULARLIST_CIRCULARLISTITERATOR(iter);    RETURN (i->pos - THIS->pos);    }    else    { -  SIMPLE_BAD_ARG_ERROR("`-",1,"ADT.CircularList.CircularListIterator"); +  SIMPLE_ARG_TYPE_ERROR("`-",1,"ADT.CircularList.CircularListIterator");    }    }       /*! @decl CircularList get_collection()    *!    *! @returns    *! Returns the CircularList this iterator currently iterates over.    */      
pike.git/src/post_modules/_ADT/circular_list.cmod:1246:    simple_array_index_no_free(&retval, THIS->list->a, &ind);    simple_set_index(THIS->list->a, &ind, val);    push_svalue(&retval);    }    else    {    push_undefined();    }    }    + #ifdef PIKE_NULL_IS_SPECIAL    INIT    {    THIS->list=0;    THIS->pos=0;    THIS->obj = NULL;    } -  + #endif       EXIT    gc_trivial;    {    if( THIS->obj ) -  { +     free_object(THIS->obj); -  THIS->obj = NULL; +     } -  } +        }       //End CircularListIterator    /*! @endclass    */       PIKEFUN object _get_iterator(void|int ind)    {    ref_push_object(Pike_fp->current_object);