pike.git / src / builtin_functions.c

version» Context lines:

pike.git/src/builtin_functions.c:6213:   static struct array *longest_ordered_sequence(struct array *a)   {    int *stack;    int *links;    int i, top=0, ltop=-1;    struct array *res;    ONERROR tmp;    ONERROR tmp2;       if(!a->size) -  return allocate_array(0); +  return &empty_array;    -  stack = malloc(sizeof(int)*a->size); -  links = malloc(sizeof(int)*a->size); +  stack = calloc(sizeof(int), a->size); +  links = calloc(sizeof(int), a->size);       if (!stack || !links)    {    if (stack) free(stack);    if (links) free(links);    return 0;    }       /* is_gt(), is_lt() and low_allocate_array() can generate errors. */   
pike.git/src/builtin_functions.c:6246:    top++;    ltop = i;    }    if (pos != 0)    links[i] = stack[pos-1];    else    links[i] = -1;    stack[pos] = i;    }    -  /* FIXME(?) memory unfreed upon error here */ +     res = low_allocate_array(top, 0);    while (ltop != -1)    {    ITEM(res)[--top].u.integer = ltop;    ltop = links[ltop];    }    res->type_field = BIT_INT;       UNSET_ONERROR(tmp2);    UNSET_ONERROR(tmp);
pike.git/src/builtin_functions.c:6291:       aa = longest_ordered_sequence(a);       /* THREADS_DISALLOW(); */       if (!aa) {    SIMPLE_OUT_OF_MEMORY_ERROR("longest_ordered_sequence",    (int)sizeof(int *)*a->size*2);    }    -  pop_n_elems(args); +     push_array(aa);   }      /**** diff ************************************************************/      static struct array* diff_compare_table(struct array *a,struct array *b,int *u)   {    struct array *res;    struct mapping *map;    struct svalue *pval;
pike.git/src/builtin_functions.c:6982:    *!    *! @seealso    *! @[shuffle()]    */   PMOD_EXPORT void f_permute( INT32 args )   {    INT_TYPE q, i=0, n;    struct array *a;    struct svalue *it;    -  if( args != 2 ) -  SIMPLE_TOO_FEW_ARGS_ERROR("permute", 2); -  if( TYPEOF(Pike_sp[ -2 ]) != T_ARRAY ) -  SIMPLE_BAD_ARG_ERROR("permute", 1, "array"); -  if (TYPEOF(Pike_sp[ -1 ]) != T_INT) -  SIMPLE_BAD_ARG_ERROR("permute", 2, "int"); +  get_all_args("permute", args, "%a%+", &a, &n);    -  n = Pike_sp[ -1 ].u.integer; -  if( n<0 ) Pike_error("Only positive permutations are allowed.\n"); -  a = copy_array( Pike_sp[ -2 ].u.array ); -  pop_n_elems( args ); +  if( a->refs>1 ) +  { +  a = copy_array( a ); +  push_array( a ); +  } +  else +  pop_n_elems(args-1); +     q = a->size;    it = a->item;    while( n && q )    {    int x = n % q;    n /= q;    q--;    if( x )    {    struct svalue tmp;    tmp = it[i];    it[i] = it[i+x];    it[i+x] = tmp;    }    i++;    } -  push_array( a ); +    }      /*! @decl array(array(array)) diff(array a, array b)    *!    *! Calculates which parts of the arrays that are common to both, and    *! which parts that are not.    *!    *! @returns    *! Returns an array with two elements, the first is an array of parts in    *! array @[a], and the second is an array of parts in array @[b].
pike.git/src/builtin_functions.c:7049:    ref_push_array(b);    f_aggregate(2);    } else {    /* The arrays are equal or one of them is empty. */    ref_push_array(a);    f_aggregate(1);    ref_push_array(b);    f_aggregate(1);    f_aggregate(2);    } -  stack_pop_n_elems_keep_top(args); +     return;    }       cmptbl = diff_compare_table(a, b, &uniq); -  +     push_array(cmptbl); -  +    #ifdef ENABLE_DYN_DIFF    if (uniq * 100 > cmptbl->size) {   #endif /* ENABLE_DYN_DIFF */   #ifdef DIFF_DEBUG    fprintf(stderr, "diff: Using G-M algorithm, u:%d, s:%d\n",    uniq, cmptbl->size);   #endif /* DIFF_DEBUG */    seq = diff_longest_sequence(cmptbl, b->size);   #ifdef ENABLE_DYN_DIFF    } else {   #ifdef DIFF_DEBUG    fprintf(stderr, "diff: Using dyn algorithm, u:%d, s:%d\n",    uniq, cmptbl->size);   #endif /* DIFF_DEBUG */    seq = diff_dyn_longest_sequence(cmptbl, b->size);    }   #endif /* ENABLE_DYN_DIFF */ -  push_array(seq); +     -  diff=diff_build(a,b,seq); -  -  pop_n_elems(2+args); -  push_array(diff); +  push_array(seq); +  push_array(diff_build(a,b,seq));   }      /*! @decl array(array(int)) diff_compare_table(array a, array b)    *!    *! Returns an array which maps from index in @[a] to corresponding    *! indices in @[b].    *!    *! @pre{    *! > Array.diff_compare_table( ({ "a","b","c" }), ({ "b", "b", "c", "d", "b" }));    *! Result: ({
pike.git/src/builtin_functions.c:7108:    *! })    *! @}    *!    *! @seealso    *! @[diff()], @[diff_longest_sequence()], @[String.fuzzymatch()]    */   PMOD_EXPORT void f_diff_compare_table(INT32 args)   {    struct array *a;    struct array *b; -  struct array *cmptbl; +        get_all_args("diff_compare_table", args, "%a%a", &a, &b);    -  cmptbl = diff_compare_table(a, b, NULL); -  -  pop_n_elems(args); -  push_array(cmptbl); +  push_array(diff_compare_table(a, b, NULL));   }      /*! @decl array(int) diff_longest_sequence(array a, array b)    *!    *! Gives the longest sequence of indices in @[b] that have corresponding    *! values in the same order in @[a].    *!    *! @seealso    *! @[diff()], @[diff_compare_table()], @[String.fuzzymatch()]    */   PMOD_EXPORT void f_diff_longest_sequence(INT32 args)   {    struct array *a;    struct array *b; -  struct array *seq; +     struct array *cmptbl;       get_all_args("diff_longest_sequence", args, "%a%a", &a, &b);       cmptbl = diff_compare_table(a, b, NULL); -  +     push_array(cmptbl);    -  seq = diff_longest_sequence(cmptbl, b->size); -  -  pop_n_elems(args+1); -  push_array(seq); +  push_array(diff_longest_sequence(cmptbl, b->size));   }      /*! @decl array(int) diff_dyn_longest_sequence(array a, array b)    *!    *! Gives the longest sequence of indices in @[b] that have corresponding    *! values in the same order in @[a].    *!    *! This function performs the same operation as @[diff_longest_sequence()],    *! but uses a different algorithm, which in some rare cases might be faster    *! (usually it's slower though).    *!    *! @seealso    *! @[diff_longest_sequence()], @[diff()], @[diff_compare_table()],    *! @[String.fuzzymatch()]    */   PMOD_EXPORT void f_diff_dyn_longest_sequence(INT32 args)   {    struct array *a;    struct array *b; -  struct array *seq; +     struct array *cmptbl;       get_all_args("diff_dyn_longest_sequence", args, "%a%a", &a, &b);       cmptbl=diff_compare_table(a, b, NULL); -  +     push_array(cmptbl);    -  seq = diff_dyn_longest_sequence(cmptbl, b->size); -  -  pop_n_elems(args+1); -  push_array(seq); +  push_array(diff_dyn_longest_sequence(cmptbl, b->size));   }      /*! @endmodule    */      /**********************************************************************/      static struct callback_list memory_usage_callback;      struct callback *add_memory_usage_callback(callback_func call,
pike.git/src/builtin_functions.c:7849:    *! @[lfun::__hash] for further details if the array contains    *! objects).    */   PMOD_EXPORT void f_uniq_array(INT32 args)   {    struct array *a, *b;    struct mapping *m;    int i, j=0,size=0;       get_all_args("uniq", args, "%a", &a); +  if( !a->size ) +  { +  push_empty_array(); +  return; +  } +     push_mapping(m = allocate_mapping(a->size));    b = allocate_array(a->size);       for(i =0; i< a->size; i++)    {    mapping_insert(m, ITEM(a)+i, &svalue_int_one);    if(m_sizeof(m) != size)    {    size=m_sizeof(m);    assign_svalue_no_free(ITEM(b)+ j++, ITEM(a)+i);    }    }       b->type_field = a->type_field; -  b=array_shrink(b, j); -  push_array(b); +  push_array(array_shrink(b, j));   }      /*! @decl array(mixed) splice(array(mixed) arr1, array(mixed) arr2, @    *! array(mixed) ... more_arrays)    *!    *! Splice two or more arrays.    *!    *! This means that the returned array has the first element in the    *! first given array, then the first argument in next array and so    *! on for all arrays. Then the second elements are added, etc.
pike.git/src/builtin_functions.c:8832:    if( TYPEOF(Pike_sp[1-args]) != PIKE_T_STRING )    Pike_error("Illegal argument 2 to defined(program,string)\n");    else    ident = Pike_sp[-args+1].u.string;       if( (offset = find_shared_string_identifier( ident, p )) == -1 )    {    INT_TYPE line;    struct pike_string *tmp = low_get_program_line(p, &line);    -  pop_n_elems(args); -  +     if (tmp)    {    push_string(tmp);    if(line >= 1)    {    push_text(":");    push_int(line);    f_add(3);    }    }
pike.git/src/builtin_functions.c:8865:    else if (IDENTIFIER_IS_CONSTANT (id->identifier_flags) &&    id->func.const_info.offset >= 0 &&    (p2 = program_from_svalue (&id_prog->constants[id->func.const_info.offset].sval)))    file = low_get_program_line (p2, &line);    else    /* The program line is better than nothing for C functions. */    file = low_get_program_line (p, &line);       if (file)    { -  pop_n_elems(args); +     if (line) {    push_string(file);    push_text(":");    push_int(line);    f_add(3);    }    else    push_string (file);    return;    } -  pop_n_elems(args); +     push_int(0);   }      /*! @decl array(program) inherit_list(program p)    *!    *! Returns an array with the programs that @[p] has inherited.    */   PMOD_EXPORT void f_inherit_list(INT32 args)   {    struct program *p;
pike.git/src/builtin_functions.c:9013:    else if (IDENTIFIER_IS_CONSTANT (id->identifier_flags) &&    id->func.const_info.offset >= 0 &&    (p2 = program_from_svalue (&id_prog->constants[id->func.const_info.offset].sval)))    file = low_get_program_line (p2, &line);    else    /* The program line is better than nothing for C functions. */    file = low_get_program_line (p, &line);       if (file)    { -  pop_n_elems(args); +     if (line) {    push_string(file);    push_text(":");    push_int(line);    f_add(3);    }    else    push_string (file);    return;    }    }    -  pop_n_elems(args); +     push_int(0);   }      /*! @endmodule Function    */      /* FIXME: Document catch and gauge. */      void init_builtin_efuns(void)   {