list.c 858 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "sam.h"
  2. /*
  3. * Check that list has room for one more element.
  4. */
  5. void
  6. growlist(List *l)
  7. {
  8. if(l->listptr==0 || l->nalloc==0){
  9. l->nalloc = INCR;
  10. l->listptr = emalloc(INCR*sizeof(long));
  11. l->nused = 0;
  12. }else if(l->nused == l->nalloc){
  13. l->listptr = erealloc(l->listptr, (l->nalloc+INCR)*sizeof(long));
  14. memset((void*)(l->longptr+l->nalloc), 0, INCR*sizeof(long));
  15. l->nalloc += INCR;
  16. }
  17. }
  18. /*
  19. * Remove the ith element from the list
  20. */
  21. void
  22. dellist(List *l, int i)
  23. {
  24. memmove(&l->longptr[i], &l->longptr[i+1], (l->nused-(i+1))*sizeof(long));
  25. l->nused--;
  26. }
  27. /*
  28. * Add a new element, whose position is i, to the list
  29. */
  30. void
  31. inslist(List *l, int i, long val)
  32. {
  33. growlist(l);
  34. memmove(&l->longptr[i+1], &l->longptr[i], (l->nused-i)*sizeof(long));
  35. l->longptr[i] = val;
  36. l->nused++;
  37. }
  38. void
  39. listfree(List *l)
  40. {
  41. free(l->listptr);
  42. free(l);
  43. }