1 /*
2  * Copyright (c) 2007-2013 Scott Lembcke and Howling Moon Software
3  *
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 deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 module dchip.cpArray;
23 
24 import dchip.chipmunk;
25 import dchip.chipmunk_private;
26 import dchip.chipmunk_types;
27 
28 cpArray* cpArrayNew(int size)
29 {
30     cpArray* arr = cast(cpArray*)cpcalloc(1, cpArray.sizeof);
31 
32     arr.num = 0;
33     arr.max = (size ? size : 4);
34     arr.arr = cast(void**)cpcalloc(arr.max, (void**).sizeof);
35 
36     return arr;
37 }
38 
39 void cpArrayFree(cpArray* arr)
40 {
41     if (arr)
42     {
43         cpfree(arr.arr);
44         arr.arr = null;
45 
46         cpfree(arr);
47     }
48 }
49 
50 void cpArrayPush(cpArray* arr, void* obj)
51 {
52     if (arr.num == arr.max)
53     {
54         arr.max *= 2;
55         arr.arr  = cast(void**)cprealloc(arr.arr, arr.max * (void**).sizeof);
56     }
57 
58     arr.arr[arr.num] = obj;
59     arr.num++;
60 }
61 
62 void* cpArrayPop(cpArray* arr)
63 {
64     arr.num--;
65 
66     void* value = arr.arr[arr.num];
67     arr.arr[arr.num] = null;
68 
69     return value;
70 }
71 
72 //static void
73 //cpArrayDeleteIndex(cpArray *arr, int idx)
74 //{
75 //	arr.num--;
76 //
77 //	arr.arr[idx] = arr.arr[arr.num];
78 //	arr.arr[arr.num] = null;
79 //}
80 
81 void cpArrayDeleteObj(cpArray* arr, void* obj)
82 {
83     for (int i = 0; i < arr.num; i++)
84     {
85         if (arr.arr[i] == obj)
86         {
87             arr.num--;
88 
89             arr.arr[i]       = arr.arr[arr.num];
90             arr.arr[arr.num] = null;
91 
92             return;
93         }
94     }
95 }
96 
97 //void
98 //cpArrayAppend(cpArray *arr, cpArray *other)
99 //{
100 //	void *tail = &arr.arr[arr.num];
101 //
102 //	arr.num += other.num;
103 //	if(arr.num >= arr.max){
104 //		arr.max = arr.num;
105 //		arr.arr = (void **)cprealloc(arr.arr, arr.max*sizeof(void**));
106 //	}
107 //
108 //	memcpy(tail, other.arr, other.num*sizeof(void**));
109 //}
110 
111 alias extern(C) void function(void*) FreeFunc;
112 
113 void cpArrayFreeEach(cpArray* arr, FreeFunc freeFunc)
114 {
115     for (int i = 0; i < arr.num; i++)
116         freeFunc(arr.arr[i]);
117 }
118 
119 cpBool cpArrayContains(cpArray* arr, void* ptr)
120 {
121     for (int i = 0; i < arr.num; i++)
122         if (arr.arr[i] == ptr)
123             return cpTrue;
124 
125     return cpFalse;
126 }
127