1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include "chipmunk/chipmunk_private.h"
30 cpArray *arr = (cpArray *)cpcalloc(1, sizeof(cpArray));
33 arr->max = (size ? size : 4);
34 arr->arr = (void **)cpcalloc(arr->max, sizeof(void*));
40 cpArrayFree(cpArray *arr)
51 cpArrayPush(cpArray *arr, void *object)
53 if(arr->num == arr->max){
54 arr->max = 3*(arr->max + 1)/2;
55 arr->arr = (void **)cprealloc(arr->arr, arr->max*sizeof(void*));
58 arr->arr[arr->num] = object;
63 cpArrayPop(cpArray *arr)
67 void *value = arr->arr[arr->num];
68 arr->arr[arr->num] = NULL;
74 cpArrayDeleteObj(cpArray *arr, void *obj)
76 for(int i=0; i<arr->num; i++){
77 if(arr->arr[i] == obj){
80 arr->arr[i] = arr->arr[arr->num];
81 arr->arr[arr->num] = NULL;
89 cpArrayFreeEach(cpArray *arr, void (freeFunc)(void*))
91 for(int i=0; i<arr->num; i++) freeFunc(arr->arr[i]);
95 cpArrayContains(cpArray *arr, void *ptr)
97 for(int i=0; i<arr->num; i++)
98 if(arr->arr[i] == ptr) return cpTrue;