Imported File#ftype spec from rubyspecs.
[rbx.git] / shotgun / external_libs / libmpa / ptr_array.c
blob9f54e42899c1a11507dd7534533feaa464c8c351
1 /* **************************************************************************
2 * Copyright (c) 2007, David Waite
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
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
16 * permission.
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"
32 #include <string.h>
33 #include <stdlib.h>
34 #include <assert.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));
43 if (retval == NULL)
44 return NULL;
46 retval->length = 0;
47 retval->size = good_size;
48 if (good_size != 0)
50 retval->array = malloc(sizeof(void*)*good_size);
51 if (retval->array == NULL)
53 free(retval);
54 return NULL;
57 else
59 retval->array = NULL;
61 return retval;
64 void* ptr_array_remove_ordered(ptr_array self, const void *obj)
66 size_t i;
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);
73 return NULL;
76 void *ptr_array_remove_fast(ptr_array self, const void *obj)
78 size_t i;
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);
85 return NULL;
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)
97 const void * oldval;
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)
108 const void *retval;
109 size_t i;
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];
117 self->length--;
118 #ifdef CLEANUP_REFERENCES
119 self->array[self->length] = NULL;
120 #endif
121 return (void*)retval;
124 void* ptr_array_remove_index_fast(ptr_array self, size_t index)
126 const void *retval;
127 assert (self != NULL);
128 assert (index < self->length);
130 retval = self->array[index];
131 self->length--;
132 self->array[index] = self->array[self->length];
134 #ifdef CLEANUP_REFERENCES
135 self->array[self->length] = NULL;
136 #endif
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)
145 return 0;
147 self->size = good_size;
148 self->array = new_array;
149 self->array[self->length] = value;
150 self->length++;
151 return 1;
154 void ptr_array_free(ptr_array self)
156 if (self != NULL)
158 free(self->array);
159 free(self);
163 int ptr_array_contains(const ptr_array self, const void *value)
165 int i;
166 assert (self != NULL);
167 for (i = 0; i < self->length; ++i)
168 if (self->array[i] == value)
169 return 1;
171 return 0;
174 void ptr_array_clear(ptr_array self)
176 #ifdef CLEANUP_REFERENCES
177 memset(self->array, 0, self->size * sizeof(void*));
178 #endif
179 self->length = 0;