./configure --prefix support for perl
[adesklets.git] / src / vector.c
blobc409fb2c23f27cec1de5a5fc0c77b8c50237e924
1 /*--- vector.c -----------------------------------------------------------------
2 Copyright (C) 2004, 2005, 2006 Sylvain Fourmanoit <syfou@users.sourceforge.net>
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to
6 deal in the Software without restriction, including without limitation the
7 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies of the Software and its documentation and acknowledgment shall be
13 given in the documentation and software packages that this Software was
14 used.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 ------------------------------------------------------------------------------*/
23 /* This implements a minimalistic vector API with error checking,
24 useful for storing dynamic collections of structures.
26 Have a look at vector.h for a more detailed description. */
28 /*----------------------------------------------------------------------------*/
29 #include "vector.h" /* Vector header */
31 /*----------------------------------------------------------------------------*/
32 void *
33 default_free(void* item)
35 TRY_RESET; TRY(free(item));
36 return (TRY_SUCCESS)?NULL:item;
39 /*----------------------------------------------------------------------------*/
40 int
41 vector_resize(vector* vec, uint newsize) {
42 uint i;
43 if (!vec) return 0L;
44 if((newsize>vec->size)||((newsize<vec->size)&&(newsize>vec->pos))) {
45 if(vec->size==0)
46 TRY_CATCH(vec->content=malloc(sizeof(void*)*newsize));
47 else
48 TRY_CATCH(vec->content=realloc(vec->content,sizeof(void*)*newsize));
49 if(newsize>vec->size)
50 for(i=vec->size;i<newsize;++i) vec->content[i]=NULL;
51 vec->size=newsize;
53 return 1;
56 /*----------------------------------------------------------------------------*/
57 vector *
58 vector_init(void)
60 vector * vec;
61 TRY_CATCH(vec = (vector *)malloc(sizeof(vector)));
62 vec->pos=vec->size=0;
63 vec->vector_free_item=default_free;
64 if (!vector_resize(vec,VECTOR_CHUNK)) {
65 free(vec);
66 vec=NULL;
68 return vec;
71 /*----------------------------------------------------------------------------*/
72 int
73 vector_push(vector * vec, void * elem)
75 int result = 1;
76 if (vec!=NULL && elem!=NULL) {
77 if(vec->pos==vec->size)
78 result=vector_resize(vec,vec->size+VECTOR_CHUNK);
79 if (result) vec->content[vec->pos++]=elem;
80 } else result=0;
81 return result;
84 /*----------------------------------------------------------------------------*/
85 int
86 vector_pop(vector * vec)
88 int result = 1;
89 if(!vec) return 0L;
90 if (vec->pos>0) {
91 --vec->pos;
92 if(vec->pos%VECTOR_CHUNK==VECTOR_CHUNK-1 && vec->pos!=0)
93 result=vector_resize(vec,vec->pos+1);
94 if (result)
95 if(vec->content[vec->pos])
96 result=(vec->content[vec->pos]=
97 vec->vector_free_item(vec->content[vec->pos]))==NULL;
98 } else result=0;
99 return result;
102 /*----------------------------------------------------------------------------*/
104 vector_insert(vector * vec, uint pos, void * elem)
106 uint i;
107 int result = 1;
108 if (!vec) return 0L;
109 if (pos==vec->pos)
110 result=vector_push(vec,elem);
111 else
112 if (pos<vec->pos) {
113 if(vec->pos==vec->size)
114 result=vector_resize(vec,vec->size+VECTOR_CHUNK);
115 if (result) {
116 if(vec->pos)
117 for(i=vec->pos;i>pos;--i)
118 vec->content[i]=vec->content[i-1];
119 vec->content[pos]=elem;
120 ++vec->pos;
122 } else result=0;
123 return result;
126 /*----------------------------------------------------------------------------*/
127 int
128 vector_delete(vector* vec, uint pos)
130 uint i;
131 int result = 1;
132 if (!vec) return 0L;
133 if(pos<vec->pos) {
134 if (!(vec->content[pos]=vec->vector_free_item(vec->content[pos]))) {
135 for(i=pos;i<vec->pos-1;++i)
136 vec->content[i]=vec->content[i+1];
137 --vec->pos;
138 if(vec->pos%VECTOR_CHUNK==VECTOR_CHUNK-1 && vec->pos!=0)
139 result=vector_resize(vec,vec->pos+1);
140 } else result=0;
141 } else result=0;
142 return result;
145 /*----------------------------------------------------------------------------*/
146 void *
147 vector_find(vector * vec , uint * i,
148 vector_search_func search_func, void *callback)
150 if (!vec || !i) return 0L;
151 for(*i=0;*i<vec->pos;++*i)
152 if (search_func(vec->content[*i],callback))
153 return vec->content[*i];
154 return NULL;
157 /*----------------------------------------------------------------------------*/
158 vector *
159 vector_free(vector * vec)
161 if (!vec) return NULL;
162 while(vector_pop(vec)); /* This is certainly not the quickest way
163 to empty a vector, but it is a simple
164 method to ensure its coherence when it fails.
166 if (!vec->pos) {
167 free(vec->content);
168 free(vec);
169 return NULL;
170 } else return vec;