1 /* **************************************************************************
2 * Copyright (c) 2007, David Waite
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice
12 * this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * The name(s) of contributors may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ************************************************************************* */
30 #include "ptr_array.h"
36 #define array_good_size(max_size) ((max_size + 7L) & ~7L)
37 ptr_array
ptr_array_new(size_t max_size
)
39 size_t good_size
= array_good_size(max_size
);
40 assert(good_size
>= max_size
);
42 ptr_array retval
= (ptr_array
)malloc(sizeof(struct ptr_array_t
));
47 retval
->size
= good_size
;
50 retval
->array
= malloc(sizeof(void*)*good_size
);
51 if (retval
->array
== NULL
)
64 void* ptr_array_remove_ordered(ptr_array self
, const void *obj
)
67 assert (self
!= NULL
);
69 for (i
= 0; i
< self
->length
; ++i
)
70 if (self
->array
[i
] == obj
)
71 return ptr_array_remove_index_ordered(self
, i
);
76 void *ptr_array_remove_fast(ptr_array self
, const void *obj
)
79 assert (self
!= NULL
);
81 for (i
= 0; i
< self
->length
; ++i
)
82 if (self
->array
[i
] == obj
)
83 return ptr_array_remove_index_fast(self
, i
);
88 void * ptr_array_get_index(const ptr_array self
, size_t index
)
90 assert (self
!= NULL
);
91 assert (index
< self
->length
);
92 return (void*)self
->array
[index
];
95 void * ptr_array_set_index(ptr_array self
, size_t index
, const void *value
)
98 assert( self
!= NULL
);
99 assert(index
< self
->length
);
101 oldval
= self
->array
[index
];
102 self
->array
[index
] = value
;
103 return (void*)oldval
;
106 void* ptr_array_remove_index_ordered(ptr_array self
, size_t index
)
110 assert (self
!= NULL
);
111 assert (index
< self
->length
);
113 retval
= self
->array
[index
];
114 for (i
= index
+ 1; i
< self
->length
; ++i
)
115 self
->array
[i
- 1] = self
->array
[i
];
118 #ifdef CLEANUP_REFERENCES
119 self
->array
[self
->length
] = NULL
;
121 return (void*)retval
;
124 void* ptr_array_remove_index_fast(ptr_array self
, size_t index
)
127 assert (self
!= NULL
);
128 assert (index
< self
->length
);
130 retval
= self
->array
[index
];
132 self
->array
[index
] = self
->array
[self
->length
];
134 #ifdef CLEANUP_REFERENCES
135 self
->array
[self
->length
] = NULL
;
137 return (void*)retval
;
140 int _ptr_array_growing_append(ptr_array self
, const void *value
)
142 size_t good_size
= array_good_size(self
->size
+ 1);
143 void * new_array
= realloc(self
->array
, good_size
*sizeof(void*));
144 if (new_array
== NULL
)
147 self
->size
= good_size
;
148 self
->array
= new_array
;
149 self
->array
[self
->length
] = value
;
154 void ptr_array_free(ptr_array self
)
163 int ptr_array_contains(const ptr_array self
, const void *value
)
166 assert (self
!= NULL
);
167 for (i
= 0; i
< self
->length
; ++i
)
168 if (self
->array
[i
] == value
)
174 void ptr_array_clear(ptr_array self
)
176 #ifdef CLEANUP_REFERENCES
177 memset(self
->array
, 0, self
->size
* sizeof(void*));