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