示例#1
0
int main(void)
{
  struct iarray ia;
  struct iarray ib;
  struct iarray_node *in_a;
  struct iarray_node *in_b;
  unsigned long ind;

  test_assert(iarray_init(&ia, 2));
  for (ind = 0; ind < ITERS; ++ind)
    test_assert(iarray_cat(&ia, &ind, sizeof(ind)));

  /* remove some nodes to show effect of rebuild */
  iarray_remove(&ia, 0);
  iarray_remove(&ia, ITERS - 1);
  iarray_remove(&ia, ITERS / 5);
  iarray_remove(&ia, ITERS / 4);
  iarray_remove(&ia, ITERS / 2);

  iarray_zero(&ib);
  test_assert(iarray_copy(&ib, &ia));

  test_assert(ib.u == ia.u);
  test_assert(iarray_integrity(&ib));

  ind = 0;
  in_a = ia.head;
  in_b = ib.head;
  for (;;) {
    printf("[%lu] ", ind);

    if (in_a->data)
      printf("%lu ", * (unsigned long *) in_a->data);
    else
      printf("X ");

    if (in_b->data)
      printf("%lu ", * (unsigned long *) in_b->data);
    else
      printf("X ");
 
    printf("\n");
    if (in_a->next) in_a = in_a->next; else break;
    if (in_b->next) in_b = in_b->next; else break;

    ++ind;
  }

  iarray_free(&ia);
  iarray_free(&ib);
  return 0;
}
示例#2
0
/* reverse numeric odd -> numeric odd */
int test2()
{
  printf("--\n");

  test_assert(iarray_init(&ia, 10));
  for (ind = 10; ind; --ind)
    test_assert(iarray_cat(&ia, &ind, sizeof(unsigned long)));

  for (ind = 10; ind; --ind)
    if (ind % 2 == 0)
      iarray_remove(&ia, ind);
 
  iarray_dump(&ia, IARRAY_DUMP_INT);
  iarray_sort(&ia, sort_numeric);
  iarray_dump(&ia, IARRAY_DUMP_INT);
  test_assert(iarray_integrity(&ia));

  nptr = ia.head;
  for (ind = 0; ind < 5; ++ind) {
    data = * (unsigned long *) nptr->data;
    test_assert(data == ind * 2 + 1);
    nptr = nptr->next;
    if (!nptr) break;
  }

  iarray_free(&ia);
  return 1;
}
示例#3
0
文件: iarray.c 项目: pcercuei/dcplaya
int iarray_set(iarray_t *a, int idx, void *elt, unsigned int eltsize)
{
  void * addr = 0;
  iarray_elt_t * e;

  if (!elt) {
    /* 	printf("iarray_remove(%d)\n",idx); */
    /* Seting a null elt : remove */
    return iarray_remove(a, idx);
  }

  /*   printf("iarray_set(%d) %p %d\n",idx, elt, eltsize); */
  /*   printf("iarray [n=%d max=%d]\n", a->n, a->max); */

  iarray_lock(a);
  if ((unsigned int)idx > a->n) {
    goto error;
  }
  if (idx == a->max) {
    /* 	printf("iarray_resize\n"); */
    resize(a, a->max ? a->max * 2 : 256);
    /* 	printf("iarray_resized\n"); */
  }
  if ((unsigned int)idx >= a->max) {
    goto error;
  }

  e = a->elt + idx;
  /* Case to alloc a new entry : */
  if (idx == a->n                    /* Append                            */
      || !e->addr                    /* No data in target element         */
      || e->size < eltsize           /* Data buffer too small for new elt */
      || e->size - eltsize > 1024) { /* Data buffer too big for new elt   */
    /* Alloc a new entry , if this one is too small or bigger from 1Kb    */
    /* 	printf("realloc\n"); */
    addr = a->alloc(eltsize, a->cookie);
    if (!addr) {
      goto error;
    }
    /* 	printf("realloc completed : %p\n", addr); */

    if (idx == a->n) {
      /* 	  printf("Insert an element at %d\n", idx); */
      ++a->n;
    } else {
      /* 	  printf("free old : %p\n", e->addr); */
      a->free(e->addr, a->cookie);
    }
    e->addr = addr;
  }
  e->size = eltsize;
  /*   printf("copy %d bytes from %p ->%p\n", eltsize, e, e->addr); */
  memcpy(e->addr, elt, eltsize);
  /*   printf("copied\n"); */
  iarray_unlock(a);
  return idx;

 error:
  if (addr) {
    a->free(addr, a->cookie);
  }
  iarray_unlock(a);
  return -1;
}