[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / chipmunk2d / src / cpArray.c
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2  * 
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:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
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
19  * SOFTWARE.
20  */
21
22 #include <string.h>
23
24 #include "chipmunk/chipmunk_private.h"
25
26
27 cpArray *
28 cpArrayNew(int size)
29 {
30         cpArray *arr = (cpArray *)cpcalloc(1, sizeof(cpArray));
31         
32         arr->num = 0;
33         arr->max = (size ? size : 4);
34         arr->arr = (void **)cpcalloc(arr->max, sizeof(void*));
35         
36         return arr;
37 }
38
39 void
40 cpArrayFree(cpArray *arr)
41 {
42         if(arr){
43                 cpfree(arr->arr);
44                 arr->arr = NULL;
45                 
46                 cpfree(arr);
47         }
48 }
49
50 void
51 cpArrayPush(cpArray *arr, void *object)
52 {
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*));
56         }
57         
58         arr->arr[arr->num] = object;
59         arr->num++;
60 }
61
62 void *
63 cpArrayPop(cpArray *arr)
64 {
65         arr->num--;
66         
67         void *value = arr->arr[arr->num];
68         arr->arr[arr->num] = NULL;
69         
70         return value;
71 }
72
73 void
74 cpArrayDeleteObj(cpArray *arr, void *obj)
75 {
76         for(int i=0; i<arr->num; i++){
77                 if(arr->arr[i] == obj){
78                         arr->num--;
79                         
80                         arr->arr[i] = arr->arr[arr->num];
81                         arr->arr[arr->num] = NULL;
82                         
83                         return;
84                 }
85         }
86 }
87
88 void
89 cpArrayFreeEach(cpArray *arr, void (freeFunc)(void*))
90 {
91         for(int i=0; i<arr->num; i++) freeFunc(arr->arr[i]);
92 }
93
94 cpBool
95 cpArrayContains(cpArray *arr, void *ptr)
96 {
97         for(int i=0; i<arr->num; i++)
98                 if(arr->arr[i] == ptr) return cpTrue;
99         
100         return cpFalse;
101 }