oclist.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include "config.h"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "oclist.h"
  8. static ocelem ocDATANULL = (ocelem)0;
  9. /*static int ocinitialized=0;*/
  10. int oclistnull(ocelem e) {return e == ocDATANULL;}
  11. #ifndef TRUE
  12. #define TRUE 1
  13. #endif
  14. #ifndef FALSE
  15. #define FALSE 0
  16. #endif
  17. #define DEFAULTALLOC 16
  18. #define ALLOCINCR 16
  19. OClist* oclistnewn(int prealloc)
  20. {
  21. OClist* l;
  22. /*
  23. if(!ocinitialized) {
  24. memset((void*)&ocDATANULL,0,sizeof(ocelem));
  25. ocinitialized = 1;
  26. }
  27. */
  28. if(prealloc < 0) prealloc = 0;
  29. l = (OClist*)malloc(sizeof(OClist));
  30. if(l) {
  31. l->alloc=prealloc;
  32. l->length=prealloc;
  33. l->content=(prealloc==0?NULL:(ocelem*)calloc(prealloc,sizeof(ocelem)));
  34. if(l == NULL) {free(l);return 0;}
  35. }
  36. return l;
  37. }
  38. int
  39. oclistfree(OClist* l)
  40. {
  41. if(l) {
  42. l->alloc = 0;
  43. if(l->content != NULL) {free(l->content); l->content = NULL;}
  44. free(l);
  45. }
  46. return TRUE;
  47. }
  48. int
  49. oclistsetalloc(OClist* l, unsigned int sz)
  50. {
  51. ocelem* newcontent;
  52. if(l == NULL) return FALSE;
  53. if(sz <= 0) {sz = (l->length?2*l->length:DEFAULTALLOC);}
  54. if(l->alloc >= sz) {return TRUE;}
  55. newcontent=(ocelem*)calloc(sz,sizeof(ocelem));
  56. if(l->alloc > 0 && l->length > 0 && l->content != NULL) {
  57. memcpy((void*)newcontent,(void*)l->content,sizeof(ocelem)*l->length);
  58. free(l->content);
  59. }
  60. l->content=newcontent;
  61. l->alloc=sz;
  62. return TRUE;
  63. }
  64. int
  65. oclistsetlength(OClist* l, unsigned int sz)
  66. {
  67. if(l == NULL) return FALSE;
  68. if(sz > l->alloc && !oclistsetalloc(l,sz)) return FALSE;
  69. l->length = sz;
  70. return TRUE;
  71. }
  72. ocelem
  73. oclistget(OClist* l, unsigned int index)
  74. {
  75. if(l == NULL || l->length == 0) return ocDATANULL;
  76. if(index >= l->length) return ocDATANULL;
  77. return l->content[index];
  78. }
  79. int
  80. oclistset(OClist* l, unsigned int index, ocelem elem)
  81. {
  82. if(l == NULL) return FALSE;
  83. if(index >= l->length) return FALSE;
  84. l->content[index] = elem;
  85. return TRUE;
  86. }
  87. /* Insert at position i of l; will push up elements i..|seq|. */
  88. int
  89. oclistinsert(OClist* l, unsigned int index, ocelem elem)
  90. {
  91. unsigned int i;
  92. if(l == NULL) return FALSE;
  93. if(index > l->length) return FALSE;
  94. oclistsetalloc(l,0);
  95. for(i=l->length;i>index;i--) l->content[i] = l->content[i-1];
  96. l->content[index] = elem;
  97. l->length++;
  98. return TRUE;
  99. }
  100. int
  101. oclistpush(OClist* l, ocelem elem)
  102. {
  103. if(l == NULL) return FALSE;
  104. if(l->length >= l->alloc) oclistsetalloc(l,0);
  105. l->content[l->length] = elem;
  106. l->length++;
  107. return TRUE;
  108. }
  109. ocelem
  110. oclistpop(OClist* l)
  111. {
  112. if(l == NULL || l->length == 0) return ocDATANULL;
  113. l->length--;
  114. return l->content[l->length];
  115. }
  116. ocelem
  117. oclisttop(OClist* l)
  118. {
  119. if(l == NULL || l->length == 0) return ocDATANULL;
  120. return l->content[l->length - 1];
  121. }
  122. ocelem
  123. oclistremove(OClist* l, unsigned int i)
  124. {
  125. unsigned int len;
  126. ocelem elem;
  127. if(l == NULL || (len=l->length) == 0) return ocDATANULL;
  128. if(i >= len) return ocDATANULL;
  129. elem = l->content[i];
  130. for(i++;i<len;i++) l->content[i-1] = l->content[i];
  131. l->length--;
  132. return elem;
  133. }
  134. /* Duplicate and return the content (null terminate) */
  135. ocelem*
  136. oclistdup(OClist* l)
  137. {
  138. ocelem* result = (ocelem*)malloc(sizeof(ocelem)*(l->length+1));
  139. memcpy((void*)result,(void*)l->content,sizeof(ocelem)*l->length);
  140. result[l->length] = (ocelem)0;
  141. return result;
  142. }
  143. int
  144. oclistcontains(OClist* list, ocelem elem)
  145. {
  146. unsigned int i;
  147. for(i=0;i<oclistlength(list);i++) {
  148. if(elem == oclistget(list,i)) return 1;
  149. }
  150. return 0;
  151. }