Branch: Tag:

1998-02-24

1998-02-24 14:51:16 by Henrik Grubbström (Grubba) <grubba@grubba.org>

Optimized diff somewhat.

Rev: src/builtin_functions.c:1.73

4:   ||| See the files COPYING and DISCLAIMER for more information.   \*/   #include "global.h" - RCSID("$Id: builtin_functions.c,v 1.72 1998/02/15 01:22:27 mirar Exp $"); + RCSID("$Id: builtin_functions.c,v 1.73 1998/02/24 14:51:16 grubba Exp $");   #include "interpret.h"   #include "svalue.h"   #include "pike_macros.h"
1851:    return a;   }    - static struct array* diff_longest_sequence(struct array *cmptbl) + static struct array* diff_longest_sequence(struct array *cmptbl, int blen)   {    int i,j,top=0,lsize=0;    struct array *a;    struct diff_magic_link_pool *pools=NULL;    struct diff_magic_link *dml;    struct diff_magic_link **stack; -  +  char *marks;       stack=malloc(sizeof(struct diff_magic_link*)*cmptbl->size);       if (!stack) error("out of memory\n");    -  +  marks = calloc(blen,1); +  +  if (!marks) { +  free(stack); +  error("Out of memory\n"); +  } +     for (i=0; i<cmptbl->size; i++)    {    struct svalue *inner=cmptbl->item[i].u.array->item;
1870:    for (j=cmptbl->item[i].u.array->size; j--;)    {    int x=inner[j].u.integer; +  +  if (!marks[x]) {    int pos;    -  if (top && x<=stack[top-1]->x) +  if (top && x<=stack[top-1]->x) {    pos=diff_ponder_stack(x,stack,top); -  else +  if (pos != top) { +  marks[stack[pos]->x] = 0; +  } +  } else    pos=top;       if (pos && j)    { -  if (stack[pos-1]->x+1 < inner[j].u.integer) +  if (!marks[inner[j-1].u.integer])    {    j=diff_ponder_array(stack[pos-1]->x+1,inner,j);    x=inner[j].u.integer;
1890:    j=0;    x=inner->u.integer;    } +  marks[x] = 1;    if (pos==top)    {    if (! (dml=dml_new(&pools)) )
1942:    }    }    } +  }    -  +  /* No need for marks anymore. */ +  +  free(marks); +     /* FIXME(?) memory unfreed upon error here */    a=low_allocate_array(top,0);    if (top)
2062:       cmptbl=diff_compare_table(sp[-args].u.array,sp[1-args].u.array);    push_array(cmptbl); -  seq=diff_longest_sequence(cmptbl); +  seq=diff_longest_sequence(cmptbl, sp[1-args].u.array->size);    push_array(seq);       diff=diff_build(sp[-2-args].u.array,sp[1-2-args].u.array,seq);
2103:       cmptbl=diff_compare_table(sp[-args].u.array,sp[1-args].u.array);    push_array(cmptbl); -  seq=diff_longest_sequence(cmptbl); +  seq=diff_longest_sequence(cmptbl, sp[1-args].u.array->size);    pop_n_elems(args+1);    push_array(seq);   }