nclist.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* Copyright 2009, UCAR/Unidata and OPeNDAP, Inc.
  2. See the COPYRIGHT file for more information. */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "nclist.h"
  7. static ncelem ncDATANULL = (ncelem)0;
  8. /*static int ncinitialized=0;*/
  9. int nclistnull(ncelem e) {return e == ncDATANULL;}
  10. #ifndef TRUE
  11. #define TRUE 1
  12. #endif
  13. #ifndef FALSE
  14. #define FALSE 0
  15. #endif
  16. #define DEFAULTALLOC 16
  17. #define ALLOCINCR 16
  18. NClist* nclistnew(void)
  19. {
  20. NClist* l;
  21. /*
  22. if(!ncinitialized) {
  23. memset((void*)&ncDATANULL,0,sizeof(ncelem));
  24. ncinitialized = 1;
  25. }
  26. */
  27. l = (NClist*)malloc(sizeof(NClist));
  28. if(l) {
  29. l->alloc=0;
  30. l->length=0;
  31. l->content=NULL;
  32. }
  33. return l;
  34. }
  35. int
  36. nclistfree(NClist* l)
  37. {
  38. if(l) {
  39. l->alloc = 0;
  40. if(l->content != NULL) {free(l->content); l->content = NULL;}
  41. free(l);
  42. }
  43. return TRUE;
  44. }
  45. int
  46. nclistsetalloc(NClist* l, unsigned int sz)
  47. {
  48. ncelem* newcontent;
  49. if(l == NULL) return FALSE;
  50. if(sz <= 0) {sz = (l->length?2*l->length:DEFAULTALLOC);}
  51. if(l->alloc >= sz) {return TRUE;}
  52. newcontent=(ncelem*)calloc(sz,sizeof(ncelem));
  53. if(l->alloc > 0 && l->length > 0 && l->content != NULL) {
  54. memcpy((void*)newcontent,(void*)l->content,sizeof(ncelem)*l->length);
  55. }
  56. if(l->content != NULL) free(l->content);
  57. l->content=newcontent;
  58. l->alloc=sz;
  59. return TRUE;
  60. }
  61. int
  62. nclistsetlength(NClist* l, unsigned int sz)
  63. {
  64. if(l == NULL) return FALSE;
  65. if(sz > l->alloc && !nclistsetalloc(l,sz)) return FALSE;
  66. l->length = sz;
  67. return TRUE;
  68. }
  69. ncelem
  70. nclistget(NClist* l, unsigned int index)
  71. {
  72. if(l == NULL || l->length == 0) return ncDATANULL;
  73. if(index >= l->length) return ncDATANULL;
  74. return l->content[index];
  75. }
  76. int
  77. nclistset(NClist* l, unsigned int index, ncelem elem)
  78. {
  79. if(l == NULL) return FALSE;
  80. if(index >= l->length) return FALSE;
  81. l->content[index] = elem;
  82. return TRUE;
  83. }
  84. /* Insert at position i of l; will push up elements i..|seq|. */
  85. int
  86. nclistinsert(NClist* l, unsigned int index, ncelem elem)
  87. {
  88. int i; /* do not make unsigned */
  89. if(l == NULL) return FALSE;
  90. if(index > l->length) return FALSE;
  91. nclistsetalloc(l,0);
  92. for(i=(int)l->length;i>index;i--) l->content[i] = l->content[i-1];
  93. l->content[index] = elem;
  94. l->length++;
  95. return TRUE;
  96. }
  97. int
  98. nclistpush(NClist* l, ncelem elem)
  99. {
  100. if(l == NULL) return FALSE;
  101. if(l->length >= l->alloc) nclistsetalloc(l,0);
  102. l->content[l->length] = elem;
  103. l->length++;
  104. return TRUE;
  105. }
  106. ncelem
  107. nclistpop(NClist* l)
  108. {
  109. if(l == NULL || l->length == 0) return ncDATANULL;
  110. l->length--;
  111. return l->content[l->length];
  112. }
  113. ncelem
  114. nclisttop(NClist* l)
  115. {
  116. if(l == NULL || l->length == 0) return ncDATANULL;
  117. return l->content[l->length - 1];
  118. }
  119. ncelem
  120. nclistremove(NClist* l, unsigned int i)
  121. {
  122. unsigned int len;
  123. ncelem elem;
  124. if(l == NULL || (len=l->length) == 0) return ncDATANULL;
  125. if(i >= len) return ncDATANULL;
  126. elem = l->content[i];
  127. for(i+=1;i<len;i++) l->content[i-1] = l->content[i];
  128. l->length--;
  129. return elem;
  130. }
  131. /* Duplicate and return the content (null terminate) */
  132. ncelem*
  133. nclistdup(NClist* l)
  134. {
  135. ncelem* result = (ncelem*)malloc(sizeof(ncelem)*(l->length+1));
  136. memcpy((void*)result,(void*)l->content,sizeof(ncelem)*l->length);
  137. result[l->length] = (ncelem)0;
  138. return result;
  139. }
  140. int
  141. nclistcontains(NClist* list, ncelem elem)
  142. {
  143. unsigned int i;
  144. for(i=0;i<nclistlength(list);i++) {
  145. if(elem == nclistget(list,i)) return 1;
  146. }
  147. return 0;
  148. }
  149. /* Remove element by value; only removes first encountered */
  150. int
  151. nclistelemremove(NClist* l, ncelem elem)
  152. {
  153. unsigned int len;
  154. unsigned int i;
  155. int found = 0;
  156. if(l == NULL || (len=l->length) == 0) return ncDATANULL;
  157. for(i=0;i<nclistlength(l);i++) {
  158. ncelem candidate = l->content[i];
  159. if(elem == candidate) {
  160. for(i+=1;i<len;i++) l->content[i-1] = l->content[i];
  161. l->length--;
  162. found = 1;
  163. break;
  164. }
  165. }
  166. return found;
  167. }
  168. /* Extends nclist to include a unique operator
  169. which remove duplicate values; NULL values removed
  170. return value is always 1.
  171. */
  172. int
  173. nclistunique(NClist* list)
  174. {
  175. unsigned int i,j,k,len;
  176. ncelem* content;
  177. if(list == NULL || list->length == 0) return 1;
  178. len = list->length;
  179. content = list->content;
  180. for(i=0;i<len;i++) {
  181. for(j=i+1;j<len;j++) {
  182. if(content[i] == content[j]) {
  183. /* compress out jth element */
  184. for(k=j+1;k<len;k++) content[k-1] = content[k];
  185. len--;
  186. }
  187. }
  188. }
  189. list->length = len;
  190. return 1;
  191. }
  192. NClist*
  193. nclistclone(NClist* list)
  194. {
  195. NClist* clone = nclistnew();
  196. *clone = *list;
  197. clone->content = nclistdup(list);
  198. return clone;
  199. }