1 /*--- vector.c -----------------------------------------------------------------
2 Copyright (C) 2004, 2005 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
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 /*----------------------------------------------------------------------------*/
33 default_free(void* item
)
35 TRY_RESET
; TRY(free(item
));
36 return (TRY_SUCCESS
)?NULL
:item
;
39 /*----------------------------------------------------------------------------*/
41 vector_resize(vector
* vec
, uint newsize
) {
44 if((newsize
>vec
->size
)||((newsize
<vec
->size
)&&(newsize
>vec
->pos
))) {
46 TRY_CATCH(vec
->content
=malloc(sizeof(void*)*newsize
));
48 TRY_CATCH(vec
->content
=realloc(vec
->content
,sizeof(void*)*newsize
));
50 for(i
=vec
->size
;i
<newsize
;++i
) vec
->content
[i
]=NULL
;
56 /*----------------------------------------------------------------------------*/
61 TRY_CATCH(vec
= (vector
*)malloc(sizeof(vector
)));
63 vec
->vector_free_item
=default_free
;
64 if (!vector_resize(vec
,VECTOR_CHUNK
)) {
71 /*----------------------------------------------------------------------------*/
73 vector_push(vector
* vec
, void * elem
)
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
;
84 /*----------------------------------------------------------------------------*/
86 vector_pop(vector
* vec
)
92 if(vec
->pos
%VECTOR_CHUNK
==VECTOR_CHUNK
-1 && vec
->pos
!=0)
93 result
=vector_resize(vec
,vec
->pos
+1);
95 if(vec
->content
[vec
->pos
])
96 result
=(vec
->content
[vec
->pos
]=
97 vec
->vector_free_item(vec
->content
[vec
->pos
]))==NULL
;
102 /*----------------------------------------------------------------------------*/
104 vector_insert(vector
* vec
, uint pos
, void * elem
)
110 result
=vector_push(vec
,elem
);
113 if(vec
->pos
==vec
->size
)
114 result
=vector_resize(vec
,vec
->size
+VECTOR_CHUNK
);
117 for(i
=vec
->pos
;i
>pos
;--i
)
118 vec
->content
[i
]=vec
->content
[i
-1];
119 vec
->content
[pos
]=elem
;
126 /*----------------------------------------------------------------------------*/
128 vector_delete(vector
* vec
, uint 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];
138 if(vec
->pos
%VECTOR_CHUNK
==VECTOR_CHUNK
-1 && vec
->pos
!=0)
139 result
=vector_resize(vec
,vec
->pos
+1);
145 /*----------------------------------------------------------------------------*/
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
];
157 /*----------------------------------------------------------------------------*/
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.