Debian Codegraph @

src/cpSpaceHash.c

raw symbols SCIP
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 "chipmunk/chipmunk_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/
23 #include "prime.h"cxx . . $ `<file>/src/prime.h`/
24
25 typedef struct cpSpaceHashBincxx . . $ cpSpaceHashBin# cpSpaceHashBincxx . . $ cpSpaceHashBin#;
26 typedef struct cpHandlecxx . . $ cpHandle# cpHandlecxx . . $ cpHandle#;
27
28 struct cpSpaceHashcxx . . $ cpSpaceHash# {
29 cpSpatialIndexcxx . . $ cpSpatialIndex# spatialIndexcxx . . $ cpSpaceHash#spatialIndex.;
30
31 int numcellscxx . . $ cpSpaceHash#numcells.;
32 cpFloatcxx . . $ cpFloat# celldimcxx . . $ cpSpaceHash#celldim.;
33
34 cpSpaceHashBincxx . . $ cpSpaceHashBin# **tablecxx . . $ cpSpaceHash#table.;
35 cpHashSetcxx . . $ cpHashSet# *handleSetcxx . . $ cpSpaceHash#handleSet.;
36
37 cpSpaceHashBincxx . . $ cpSpaceHashBin# *pooledBinscxx . . $ cpSpaceHash#pooledBins.;
38 cpArraycxx . . $ cpArray# *pooledHandlescxx . . $ cpSpaceHash#pooledHandles.;
39 cpArraycxx . . $ cpArray# *allocatedBufferscxx . . $ cpSpaceHash#allocatedBuffers.;
40
41 cpTimestampcxx . . $ cpTimestamp# stampcxx . . $ cpSpaceHash#stamp.;
42 };
43
44
45 //MARK: Handle Functions
46
47 struct cpHandlecxx . . $ cpHandle# {
48 void *objcxx . . $ cpHandle#obj.;
49 int retaincxx . . $ cpHandle#retain.;
50 cpTimestampcxx . . $ cpTimestamp# stampcxx . . $ cpHandle#stamp.;
51 };
52
53 static cpHandlecxx . . $ cpHandle#*
54 cpHandleInitcxx . . $ cpHandleInit(6703c6bc71abd8f4).(cpHandlecxx . . $ cpHandle# *hand, void *obj)
55 {
56 hand->objcxx . . $ cpHandle#obj. = obj;
57 hand->retaincxx . . $ cpHandle#retain. = 0;
58 hand->stampcxx . . $ cpHandle#stamp. = 0;
59
60 return hand;
61 }
62
63 static inline void cpHandleRetaincxx . . $ cpHandleRetain(efb8c211f3c32e85).(cpHandlecxx . . $ cpHandle# *hand){hand->retaincxx . . $ cpHandle#retain.++;}
64
65 static inline void
66 cpHandleReleasecxx . . $ cpHandleRelease(fa8050529d9b8b54).(cpHandlecxx . . $ cpHandle# *hand, cpArraycxx . . $ cpArray# *pooledHandles)
67 {
68 hand->retaincxx . . $ cpHandle#retain.--;
69 if(hand->retaincxx . . $ cpHandle#retain. == 0) cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(pooledHandles, hand);
70 }
71
72 static int handleSetEqlcxx . . $ handleSetEql(8cb07e82f2027837).(void *obj, cpHandlecxx . . $ cpHandle# *hand){return (obj == hand->objcxx . . $ cpHandle#obj.);}
73
74 static void *
75 handleSetTranscxx . . $ handleSetTrans(178610659382e48d).(void *obj, cpSpaceHashcxx . . $ cpSpaceHash# *hash)
76 {
77 if(hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles.->numcxx . . $ cpArray#num. == 0){
78 // handle pool is exhausted, make more
79 int count = CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!/sizeof(cpHandlecxx . . $ cpHandle#);
80 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(count, "Internal Error: Buffer size is too small.");
81
82 cpHandlecxx . . $ cpHandle# *buffer = (cpHandlecxx . . $ cpHandle# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!);
83 cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(hash->allocatedBufferscxx . . $ cpSpaceHash#allocatedBuffers., buffer);
84
85 for(int i=0; i<count; i++) cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles., buffer + i);
86 }
87
88 cpHandlecxx . . $ cpHandle# *hand = cpHandleInitcxx . . $ cpHandleInit(6703c6bc71abd8f4).((cpHandlecxx . . $ cpHandle# *)cpArrayPopcxx . . $ cpArrayPop(deec8af08e758c71).(hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles.), obj);
89 cpHandleRetaincxx . . $ cpHandleRetain(efb8c211f3c32e85).(hand);
90
91 return hand;
92 }
93
94 //MARK: Bin Functions
95
96 struct cpSpaceHashBincxx . . $ cpSpaceHashBin# {
97 cpHandlecxx . . $ cpHandle# *handlecxx . . $ cpSpaceHashBin#handle.;
98 cpSpaceHashBincxx . . $ cpSpaceHashBin# *nextcxx . . $ cpSpaceHashBin#next.;
99 };
100
101 static inline void
102 recycleBincxx . . $ recycleBin(74ef15c3f8f7e1ae).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin)
103 {
104 bin->nextcxx . . $ cpSpaceHashBin#next. = hash->pooledBinscxx . . $ cpSpaceHash#pooledBins.;
105 hash->pooledBinscxx . . $ cpSpaceHash#pooledBins. = bin;
106 }
107
108 static inline void
109 clearTableCellcxx . . $ clearTableCell(e3be52993ed9bf6a).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, int idx)
110 {
111 cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin = hash->tablecxx . . $ cpSpaceHash#table.[idx];
112 while(bin){
113 cpSpaceHashBincxx . . $ cpSpaceHashBin# *next = bin->nextcxx . . $ cpSpaceHashBin#next.;
114
115 cpHandleReleasecxx . . $ cpHandleRelease(fa8050529d9b8b54).(bin->handlecxx . . $ cpSpaceHashBin#handle., hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles.);
116 recycleBincxx . . $ recycleBin(74ef15c3f8f7e1ae).(hash, bin);
117
118 bin = next;
119 }
120
121 hash->tablecxx . . $ cpSpaceHash#table.[idx] = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
122 }
123
124 static void
125 clearTablecxx . . $ clearTable(3c54e905acb17611).(cpSpaceHashcxx . . $ cpSpaceHash# *hash)
126 {
127 for(int i=0; i<hash->numcellscxx . . $ cpSpaceHash#numcells.; i++) clearTableCellcxx . . $ clearTableCell(e3be52993ed9bf6a).(hash, i);
128 }
129
130 // Get a recycled or new bin.
131 static inline cpSpaceHashBincxx . . $ cpSpaceHashBin# *
132 getEmptyBincxx . . $ getEmptyBin(f6398fbbc1890240).(cpSpaceHashcxx . . $ cpSpaceHash# *hash)
133 {
134 cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin = hash->pooledBinscxx . . $ cpSpaceHash#pooledBins.;
135
136 if(bin){
137 hash->pooledBinscxx . . $ cpSpaceHash#pooledBins. = bin->nextcxx . . $ cpSpaceHashBin#next.;
138 return bin;
139 } else {
140 // Pool is exhausted, make more
141 int count = CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!/sizeof(cpSpaceHashBincxx . . $ cpSpaceHashBin#);
142 cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(count, "Internal Error: Buffer size is too small.");
143
144 cpSpaceHashBincxx . . $ cpSpaceHashBin# *buffer = (cpSpaceHashBincxx . . $ cpSpaceHashBin# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!);
145 cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(hash->allocatedBufferscxx . . $ cpSpaceHash#allocatedBuffers., buffer);
146
147 // push all but the first one, return the first instead
148 for(int i=1; i<count; i++) recycleBincxx . . $ recycleBin(74ef15c3f8f7e1ae).(hash, buffer + i);
149 return buffer;
150 }
151 }
152
153 //MARK: Memory Management Functions
154
155 cpSpaceHashcxx . . $ cpSpaceHash# *
156 cpSpaceHashAlloccxx . . $ cpSpaceHashAlloc(b386c6a499353588).(void)
157 {
158 return (cpSpaceHashcxx . . $ cpSpaceHash# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpSpaceHashcxx . . $ cpSpaceHash#));
159 }
160
161 // Frees the old table, and allocate a new one.
162 static void
163 cpSpaceHashAllocTablecxx . . $ cpSpaceHashAllocTable(e3be52993ed9bf6a).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, int numcells)
164 {
165 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(hash->tablecxx . . $ cpSpaceHash#table.);
166
167 hash->numcellscxx . . $ cpSpaceHash#numcells. = numcells;
168 hash->tablecxx . . $ cpSpaceHash#table. = (cpSpaceHashBincxx . . $ cpSpaceHashBin# **)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(numcells, sizeof(cpSpaceHashBincxx . . $ cpSpaceHashBin# *));
169 }
170
171 static inline cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# *Klasscxx . . $ Klass(292ec58cde6c1067).();
172
173 cpSpatialIndexcxx . . $ cpSpatialIndex# *
174 cpSpaceHashInitcxx . . $ cpSpaceHashInit(344c6fa934f4189d).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpFloatcxx . . $ cpFloat# celldim, int numcells, cpSpatialIndexBBFunccxx . . $ cpSpatialIndexBBFunc# bbfunc, cpSpatialIndexcxx . . $ cpSpatialIndex# *staticIndex)
175 {
176 cpSpatialIndexInitcxx . . $ cpSpatialIndexInit(57a924cefa208dd7).((cpSpatialIndexcxx . . $ cpSpatialIndex# *)hash, Klasscxx . . $ Klass(292ec58cde6c1067).(), bbfunc, staticIndex);
177
178 cpSpaceHashAllocTablecxx . . $ cpSpaceHashAllocTable(e3be52993ed9bf6a).(hash, next_primecxx . . $ next_prime(7864480464b09eea).(numcells));
179 hash->celldimcxx . . $ cpSpaceHash#celldim. = celldim;
180
181 hash->handleSetcxx . . $ cpSpaceHash#handleSet. = cpHashSetNewcxx . . $ cpHashSetNew(1aa2f270b6b730e1).(0, (cpHashSetEqlFunccxx . . $ cpHashSetEqlFunc#)handleSetEqlcxx . . $ handleSetEql(8cb07e82f2027837).);
182
183 hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles. = cpArrayNewcxx . . $ cpArrayNew(f90d83c90697c3ae).(0);
184
185 hash->pooledBinscxx . . $ cpSpaceHash#pooledBins. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
186 hash->allocatedBufferscxx . . $ cpSpaceHash#allocatedBuffers. = cpArrayNewcxx . . $ cpArrayNew(f90d83c90697c3ae).(0);
187
188 hash->stampcxx . . $ cpSpaceHash#stamp. = 1;
189
190 return (cpSpatialIndexcxx . . $ cpSpatialIndex# *)hash;
191 }
192
193 cpSpatialIndexcxx . . $ cpSpatialIndex# *
194 cpSpaceHashNewcxx . . $ cpSpaceHashNew(6523dc62925bd740).(cpFloatcxx . . $ cpFloat# celldim, int cells, cpSpatialIndexBBFunccxx . . $ cpSpatialIndexBBFunc# bbfunc, cpSpatialIndexcxx . . $ cpSpatialIndex# *staticIndex)
195 {
196 return cpSpaceHashInitcxx . . $ cpSpaceHashInit(344c6fa934f4189d).(cpSpaceHashAlloccxx . . $ cpSpaceHashAlloc(b386c6a499353588).(), celldim, cells, bbfunc, staticIndex);
197 }
198
199 static void
200 cpSpaceHashDestroycxx . . $ cpSpaceHashDestroy(3c54e905acb17611).(cpSpaceHashcxx . . $ cpSpaceHash# *hash)
201 {
202 if(hash->tablecxx . . $ cpSpaceHash#table.) clearTablecxx . . $ clearTable(3c54e905acb17611).(hash);
203 cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(hash->tablecxx . . $ cpSpaceHash#table.);
204
205 cpHashSetFreecxx . . $ cpHashSetFree(a5aef454cd7046d4).(hash->handleSetcxx . . $ cpSpaceHash#handleSet.);
206
207 cpArrayFreeEachcxx . . $ cpArrayFreeEach(d583da02e3252d4f).(hash->allocatedBufferscxx . . $ cpSpaceHash#allocatedBuffers., cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!);
208 cpArrayFreecxx . . $ cpArrayFree(9f115453462bfa05).(hash->allocatedBufferscxx . . $ cpSpaceHash#allocatedBuffers.);
209 cpArrayFreecxx . . $ cpArrayFree(9f115453462bfa05).(hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles.);
210 }
211
212 //MARK: Helper Functions
213
214 static inline cpBoolcxx . . $ cpBool#
215 containsHandlecxx . . $ containsHandle(9b02c731a31a69a).(cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin, cpHandlecxx . . $ cpHandle# *hand)
216 {
217 while(bin){
218 if(bin->handlecxx . . $ cpSpaceHashBin#handle. == hand) return cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!;
219 bin = bin->nextcxx . . $ cpSpaceHashBin#next.;
220 }
221
222 return cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!;
223 }
224
225 // The hash function itself.
226 static inline cpHashValuecxx . . $ cpHashValue#
227 hash_funccxx . . $ hash_func(315873a0897e1da0).(cpHashValuecxx . . $ cpHashValue# x, cpHashValuecxx . . $ cpHashValue# y, cpHashValuecxx . . $ cpHashValue# n)
228 {
229 return (x*1640531513ul ^ y*2654435789ul) % n;
230 }
231
232 // Much faster than (int)floor(f)
233 // Profiling showed floor() to be a sizable performance hog
234 static inline int
235 floor_intcxx . . $ floor_int(620b9ac44d2573a6).(cpFloatcxx . . $ cpFloat# f)
236 {
237 int i = (int)f;
238 return (f < 0.0f && f != i ? i - 1 : i);
239 }
240
241 static inline void
242 hashHandlecxx . . $ hashHandle(4b813bc500ca7039).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpHandlecxx . . $ cpHandle# *hand, cpBBcxx . . $ cpBB# bb)
243 {
244 // Find the dimensions in cell coordinates.
245 cpFloatcxx . . $ cpFloat# dim = hash->celldimcxx . . $ cpSpaceHash#celldim.;
246 int l = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.lcxx . . $ cpBB#l./dim); // Fix by ShiftZ
247 int r = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.rcxx . . $ cpBB#r./dim);
248 int b = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.bcxx . . $ cpBB#b./dim);
249 int t = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.tcxx . . $ cpBB#t./dim);
250
251 int n = hash->numcellscxx . . $ cpSpaceHash#numcells.;
252 for(int i=l; i<=r; i++){
253 for(int j=b; j<=t; j++){
254 cpHashValuecxx . . $ cpHashValue# idx = hash_funccxx . . $ hash_func(315873a0897e1da0).(i,j,n);
255 cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin = hash->tablecxx . . $ cpSpaceHash#table.[idx];
256
257 // Don't add an object twice to the same cell.
258 if(containsHandlecxx . . $ containsHandle(9b02c731a31a69a).(bin, hand)) continue;
259
260 cpHandleRetaincxx . . $ cpHandleRetain(efb8c211f3c32e85).(hand);
261 // Insert a new bin for the handle in this cell.
262 cpSpaceHashBincxx . . $ cpSpaceHashBin# *newBin = getEmptyBincxx . . $ getEmptyBin(f6398fbbc1890240).(hash);
263 newBin->handlecxx . . $ cpSpaceHashBin#handle. = hand;
264 newBin->nextcxx . . $ cpSpaceHashBin#next. = bin;
265 hash->tablecxx . . $ cpSpaceHash#table.[idx] = newBin;
266 }
267 }
268 }
269
270 //MARK: Basic Operations
271
272 static void
273 cpSpaceHashInsertcxx . . $ cpSpaceHashInsert(762a591b4939c1e8).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
274 {
275 cpHandlecxx . . $ cpHandle# *hand = (cpHandlecxx . . $ cpHandle# *)cpHashSetInsertcxx . . $ cpHashSetInsert(65a72e7022b62d4d).(hash->handleSetcxx . . $ cpSpaceHash#handleSet., hashid, obj, (cpHashSetTransFunccxx . . $ cpHashSetTransFunc#)handleSetTranscxx . . $ handleSetTrans(178610659382e48d)., hash);
276 hashHandlecxx . . $ hashHandle(4b813bc500ca7039).(hash, hand, hash->spatialIndexcxx . . $ cpSpaceHash#spatialIndex..bbfunccxx . . $ cpSpatialIndex#bbfunc.(obj));
277 }
278
279 static void
280 cpSpaceHashRehashObjectcxx . . $ cpSpaceHashRehashObject(762a591b4939c1e8).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
281 {
282 cpHandlecxx . . $ cpHandle# *hand = (cpHandlecxx . . $ cpHandle# *)cpHashSetRemovecxx . . $ cpHashSetRemove(82fb1457cc8dfa6c).(hash->handleSetcxx . . $ cpSpaceHash#handleSet., hashid, obj);
283
284 if(hand){
285 hand->objcxx . . $ cpHandle#obj. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
286 cpHandleReleasecxx . . $ cpHandleRelease(fa8050529d9b8b54).(hand, hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles.);
287
288 cpSpaceHashInsertcxx . . $ cpSpaceHashInsert(762a591b4939c1e8).(hash, obj, hashid);
289 }
290 }
291
292 static void
293 rehash_helpercxx . . $ rehash_helper(40f2d7a8c2bf9d3f).(cpHandlecxx . . $ cpHandle# *hand, cpSpaceHashcxx . . $ cpSpaceHash# *hash)
294 {
295 hashHandlecxx . . $ hashHandle(4b813bc500ca7039).(hash, hand, hash->spatialIndexcxx . . $ cpSpaceHash#spatialIndex..bbfunccxx . . $ cpSpatialIndex#bbfunc.(hand->objcxx . . $ cpHandle#obj.));
296 }
297
298 static void
299 cpSpaceHashRehashcxx . . $ cpSpaceHashRehash(3c54e905acb17611).(cpSpaceHashcxx . . $ cpSpaceHash# *hash)
300 {
301 clearTablecxx . . $ clearTable(3c54e905acb17611).(hash);
302 cpHashSetEachcxx . . $ cpHashSetEach(3ff8996736d46a4d).(hash->handleSetcxx . . $ cpSpaceHash#handleSet., (cpHashSetIteratorFunccxx . . $ cpHashSetIteratorFunc#)rehash_helpercxx . . $ rehash_helper(40f2d7a8c2bf9d3f)., hash);
303 }
304
305 static void
306 cpSpaceHashRemovecxx . . $ cpSpaceHashRemove(762a591b4939c1e8).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
307 {
308 cpHandlecxx . . $ cpHandle# *hand = (cpHandlecxx . . $ cpHandle# *)cpHashSetRemovecxx . . $ cpHashSetRemove(82fb1457cc8dfa6c).(hash->handleSetcxx . . $ cpSpaceHash#handleSet., hashid, obj);
309
310 if(hand){
311 hand->objcxx . . $ cpHandle#obj. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
312 cpHandleReleasecxx . . $ cpHandleRelease(fa8050529d9b8b54).(hand, hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles.);
313 }
314 }
315
316 typedef struct eachContextcxx . . $ eachContext# {
317 cpSpatialIndexIteratorFunccxx . . $ cpSpatialIndexIteratorFunc# funccxx . . $ eachContext#func.;
318 void *datacxx . . $ eachContext#data.;
319 } eachContextcxx . . $ eachContext#;
320
321 static void eachHelpercxx . . $ eachHelper(1e0a10ff0ec28131).(cpHandlecxx . . $ cpHandle# *hand, eachContextcxx . . $ eachContext# *context){context->funccxx . . $ eachContext#func.(hand->objcxx . . $ cpHandle#obj., context->datacxx . . $ eachContext#data.);}
322
323 static void
324 cpSpaceHashEachcxx . . $ cpSpaceHashEach(d00296f8b45daa1e).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpSpatialIndexIteratorFunccxx . . $ cpSpatialIndexIteratorFunc# func, void *data)
325 {
326 eachContextcxx . . $ eachContext# context = {func, data};
327 cpHashSetEachcxx . . $ cpHashSetEach(3ff8996736d46a4d).(hash->handleSetcxx . . $ cpSpaceHash#handleSet., (cpHashSetIteratorFunccxx . . $ cpHashSetIteratorFunc#)eachHelpercxx . . $ eachHelper(1e0a10ff0ec28131)., &context);
328 }
329
330 static void
331 remove_orphaned_handlescxx . . $ remove_orphaned_handles(805216bf7c0e4133).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpSpaceHashBincxx . . $ cpSpaceHashBin# **bin_ptr)
332 {
333 cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin = *bin_ptr;
334 while(bin){
335 cpHandlecxx . . $ cpHandle# *hand = bin->handlecxx . . $ cpSpaceHashBin#handle.;
336 cpSpaceHashBincxx . . $ cpSpaceHashBin# *next = bin->nextcxx . . $ cpSpaceHashBin#next.;
337
338 if(!hand->objcxx . . $ cpHandle#obj.){
339 // orphaned handle, unlink and recycle the bin
340 (*bin_ptr) = bin->nextcxx . . $ cpSpaceHashBin#next.;
341 recycleBincxx . . $ recycleBin(74ef15c3f8f7e1ae).(hash, bin);
342
343 cpHandleReleasecxx . . $ cpHandleRelease(fa8050529d9b8b54).(hand, hash->pooledHandlescxx . . $ cpSpaceHash#pooledHandles.);
344 } else {
345 bin_ptr = &bin->nextcxx . . $ cpSpaceHashBin#next.;
346 }
347
348 bin = next;
349 }
350 }
351
352 //MARK: Query Functions
353
354 static inline void
355 query_helpercxx . . $ query_helper(23c7c3d7880d7e84).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpSpaceHashBincxx . . $ cpSpaceHashBin# **bin_ptr, void *obj, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data)
356 {
357 restart:
358 for(cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin = *bin_ptr; bin; bin = bin->nextcxx . . $ cpSpaceHashBin#next.){
359 cpHandlecxx . . $ cpHandle# *hand = bin->handlecxx . . $ cpSpaceHashBin#handle.;
360 void *other = hand->objcxx . . $ cpHandle#obj.;
361
362 if(hand->stampcxx . . $ cpHandle#stamp. == hash->stampcxx . . $ cpSpaceHash#stamp. || obj == other){
363 continue;
364 } else if(other){
365 func(obj, other, 0, data);
366 hand->stampcxx . . $ cpHandle#stamp. = hash->stampcxx . . $ cpSpaceHash#stamp.;
367 } else {
368 // The object for this handle has been removed
369 // cleanup this cell and restart the query
370 remove_orphaned_handlescxx . . $ remove_orphaned_handles(805216bf7c0e4133).(hash, bin_ptr);
371 goto restart; // GCC not smart enough/able to tail call an inlined function.
372 }
373 }
374 }
375
376 static void
377 cpSpaceHashQuerycxx . . $ cpSpaceHashQuery(395a8b410dda54da).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, void *obj, cpBBcxx . . $ cpBB# bb, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data)
378 {
379 // Get the dimensions in cell coordinates.
380 cpFloatcxx . . $ cpFloat# dim = hash->celldimcxx . . $ cpSpaceHash#celldim.;
381 int l = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.lcxx . . $ cpBB#l./dim); // Fix by ShiftZ
382 int r = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.rcxx . . $ cpBB#r./dim);
383 int b = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.bcxx . . $ cpBB#b./dim);
384 int t = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.tcxx . . $ cpBB#t./dim);
385
386 int n = hash->numcellscxx . . $ cpSpaceHash#numcells.;
387 cpSpaceHashBincxx . . $ cpSpaceHashBin# **table = hash->tablecxx . . $ cpSpaceHash#table.;
388
389 // Iterate over the cells and query them.
390 for(int i=l; i<=r; i++){
391 for(int j=b; j<=t; j++){
392 query_helpercxx . . $ query_helper(23c7c3d7880d7e84).(hash, &table[hash_funccxx . . $ hash_func(315873a0897e1da0).(i,j,n)], obj, func, data);
393 }
394 }
395
396 hash->stampcxx . . $ cpSpaceHash#stamp.++;
397 }
398
399 // Similar to struct eachPair above.
400 typedef struct queryRehashContextcxx . . $ queryRehashContext# {
401 cpSpaceHashcxx . . $ cpSpaceHash# *hashcxx . . $ queryRehashContext#hash.;
402 cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# funccxx . . $ queryRehashContext#func.;
403 void *datacxx . . $ queryRehashContext#data.;
404 } queryRehashContextcxx . . $ queryRehashContext#;
405
406 // Hashset iterator func used with cpSpaceHashQueryRehash().
407 static void
408 queryRehash_helpercxx . . $ queryRehash_helper(655b182be8ae71a4).(cpHandlecxx . . $ cpHandle# *hand, queryRehashContextcxx . . $ queryRehashContext# *context)
409 {
410 cpSpaceHashcxx . . $ cpSpaceHash# *hash = context->hashcxx . . $ queryRehashContext#hash.;
411 cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func = context->funccxx . . $ queryRehashContext#func.;
412 void *data = context->datacxx . . $ queryRehashContext#data.;
413
414 cpFloatcxx . . $ cpFloat# dim = hash->celldimcxx . . $ cpSpaceHash#celldim.;
415 int n = hash->numcellscxx . . $ cpSpaceHash#numcells.;
416
417 void *obj = hand->objcxx . . $ cpHandle#obj.;
418 cpBBcxx . . $ cpBB# bb = hash->spatialIndexcxx . . $ cpSpaceHash#spatialIndex..bbfunccxx . . $ cpSpatialIndex#bbfunc.(obj);
419
420 int l = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.lcxx . . $ cpBB#l./dim);
421 int r = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.rcxx . . $ cpBB#r./dim);
422 int b = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.bcxx . . $ cpBB#b./dim);
423 int t = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(bb.tcxx . . $ cpBB#t./dim);
424
425 cpSpaceHashBincxx . . $ cpSpaceHashBin# **table = hash->tablecxx . . $ cpSpaceHash#table.;
426
427 for(int i=l; i<=r; i++){
428 for(int j=b; j<=t; j++){
429 cpHashValuecxx . . $ cpHashValue# idx = hash_funccxx . . $ hash_func(315873a0897e1da0).(i,j,n);
430 cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin = table[idx];
431
432 if(containsHandlecxx . . $ containsHandle(9b02c731a31a69a).(bin, hand)) continue;
433
434 cpHandleRetaincxx . . $ cpHandleRetain(efb8c211f3c32e85).(hand); // this MUST be done first in case the object is removed in func()
435 query_helpercxx . . $ query_helper(23c7c3d7880d7e84).(hash, &bin, obj, func, data);
436
437 cpSpaceHashBincxx . . $ cpSpaceHashBin# *newBin = getEmptyBincxx . . $ getEmptyBin(f6398fbbc1890240).(hash);
438 newBin->handlecxx . . $ cpSpaceHashBin#handle. = hand;
439 newBin->nextcxx . . $ cpSpaceHashBin#next. = bin;
440 table[idx] = newBin;
441 }
442 }
443
444 // Increment the stamp for each object hashed.
445 hash->stampcxx . . $ cpSpaceHash#stamp.++;
446 }
447
448 static void
449 cpSpaceHashReindexQuerycxx . . $ cpSpaceHashReindexQuery(f0ed88b68b35e2c9).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpSpatialIndexQueryFunccxx . . $ cpSpatialIndexQueryFunc# func, void *data)
450 {
451 clearTablecxx . . $ clearTable(3c54e905acb17611).(hash);
452
453 queryRehashContextcxx . . $ queryRehashContext# context = {hash, func, data};
454 cpHashSetEachcxx . . $ cpHashSetEach(3ff8996736d46a4d).(hash->handleSetcxx . . $ cpSpaceHash#handleSet., (cpHashSetIteratorFunccxx . . $ cpHashSetIteratorFunc#)queryRehash_helpercxx . . $ queryRehash_helper(655b182be8ae71a4)., &context);
455
456 cpSpatialIndexCollideStaticcxx . . $ cpSpatialIndexCollideStatic(3c6f473dc08bdfa1).((cpSpatialIndexcxx . . $ cpSpatialIndex# *)hash, hash->spatialIndexcxx . . $ cpSpaceHash#spatialIndex..staticIndexcxx . . $ cpSpatialIndex#staticIndex., func, data);
457 }
458
459 static inline cpFloatcxx . . $ cpFloat#
460 segmentQuery_helpercxx . . $ segmentQuery_helper(a026334444794ea6).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpSpaceHashBincxx . . $ cpSpaceHashBin# **bin_ptr, void *obj, cpSpatialIndexSegmentQueryFunccxx . . $ cpSpatialIndexSegmentQueryFunc# func, void *data)
461 {
462 cpFloatcxx . . $ cpFloat# t = 1.0f;
463
464 restart:
465 for(cpSpaceHashBincxx . . $ cpSpaceHashBin# *bin = *bin_ptr; bin; bin = bin->nextcxx . . $ cpSpaceHashBin#next.){
466 cpHandlecxx . . $ cpHandle# *hand = bin->handlecxx . . $ cpSpaceHashBin#handle.;
467 void *other = hand->objcxx . . $ cpHandle#obj.;
468
469 // Skip over certain conditions
470 if(hand->stampcxx . . $ cpHandle#stamp. == hash->stampcxx . . $ cpSpaceHash#stamp.){
471 continue;
472 } else if(other){
473 t = cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(t, func(obj, other, data));
474 hand->stampcxx . . $ cpHandle#stamp. = hash->stampcxx . . $ cpSpaceHash#stamp.;
475 } else {
476 // The object for this handle has been removed
477 // cleanup this cell and restart the query
478 remove_orphaned_handlescxx . . $ remove_orphaned_handles(805216bf7c0e4133).(hash, bin_ptr);
479 goto restart; // GCC not smart enough/able to tail call an inlined function.
480 }
481 }
482
483 return t;
484 }
485
486 // modified from http://playtechs.blogspot.com/2007/03/raytracing-on-grid.html
487 static void
488 cpSpaceHashSegmentQuerycxx . . $ cpSpaceHashSegmentQuery(7f04a6bc1ba6f4db).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, void *obj, cpVectcxx . . $ cpVect# a, cpVectcxx . . $ cpVect# b, cpFloatcxx . . $ cpFloat# t_exit, cpSpatialIndexSegmentQueryFunccxx . . $ cpSpatialIndexSegmentQueryFunc# func, void *data)
489 {
490 a = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(a, 1.0f/hash->celldimcxx . . $ cpSpaceHash#celldim.);
491 b = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(b, 1.0f/hash->celldimcxx . . $ cpSpaceHash#celldim.);
492
493 int cell_x = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(a.xcxx . . $ cpVect#x.), cell_y = floor_intcxx . . $ floor_int(620b9ac44d2573a6).(a.ycxx . . $ cpVect#y.);
494
495 cpFloatcxx . . $ cpFloat# t = 0;
496
497 int x_inc, y_inc;
498 cpFloatcxx . . $ cpFloat# temp_v, temp_h;
499
500 if (b.xcxx . . $ cpVect#x. > a.xcxx . . $ cpVect#x.){
501 x_inc = 1;
502 temp_h = (cpffloorcxx . . $ `include/chipmunk/chipmunk_types.h:76:10`!(a.xcxx . . $ cpVect#x. + 1.0f) - a.xcxx . . $ cpVect#x.);
503 } else {
504 x_inc = -1;
505 temp_h = (a.xcxx . . $ cpVect#x. - cpffloorcxx . . $ `include/chipmunk/chipmunk_types.h:76:10`!(a.xcxx . . $ cpVect#x.));
506 }
507
508 if (b.ycxx . . $ cpVect#y. > a.ycxx . . $ cpVect#y.){
509 y_inc = 1;
510 temp_v = (cpffloorcxx . . $ `include/chipmunk/chipmunk_types.h:76:10`!(a.ycxx . . $ cpVect#y. + 1.0f) - a.ycxx . . $ cpVect#y.);
511 } else {
512 y_inc = -1;
513 temp_v = (a.ycxx . . $ cpVect#y. - cpffloorcxx . . $ `include/chipmunk/chipmunk_types.h:76:10`!(a.ycxx . . $ cpVect#y.));
514 }
515
516 // Division by zero is *very* slow on ARM
517 cpFloatcxx . . $ cpFloat# dx = cpfabscxx . . $ cpfabs(447c67c6676f75c3).(b.xcxx . . $ cpVect#x. - a.xcxx . . $ cpVect#x.), dy = cpfabscxx . . $ cpfabs(447c67c6676f75c3).(b.ycxx . . $ cpVect#y. - a.ycxx . . $ cpVect#y.);
518 cpFloatcxx . . $ cpFloat# dt_dx = (dx ? 1.0f/dx : INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!), dt_dy = (dy ? 1.0f/dy : INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!);
519
520 // fix NANs in horizontal directions
521 cpFloatcxx . . $ cpFloat# next_h = (temp_h ? temp_h*dt_dx : dt_dx);
522 cpFloatcxx . . $ cpFloat# next_v = (temp_v ? temp_v*dt_dy : dt_dy);
523
524 int n = hash->numcellscxx . . $ cpSpaceHash#numcells.;
525 cpSpaceHashBincxx . . $ cpSpaceHashBin# **table = hash->tablecxx . . $ cpSpaceHash#table.;
526
527 while(t < t_exit){
528 cpHashValuecxx . . $ cpHashValue# idx = hash_funccxx . . $ hash_func(315873a0897e1da0).(cell_x, cell_y, n);
529 t_exit = cpfmincxx . . $ cpfmin(d8e7e0733fa41c87).(t_exit, segmentQuery_helpercxx . . $ segmentQuery_helper(a026334444794ea6).(hash, &table[idx], obj, func, data));
530
531 if (next_v < next_h){
532 cell_y += y_inc;
533 t = next_v;
534 next_v += dt_dy;
535 } else {
536 cell_x += x_inc;
537 t = next_h;
538 next_h += dt_dx;
539 }
540 }
541
542 hash->stampcxx . . $ cpSpaceHash#stamp.++;
543 }
544
545 //MARK: Misc
546
547 void
548 cpSpaceHashResizecxx . . $ cpSpaceHashResize(1cab1dc1b618c7d1).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, cpFloatcxx . . $ cpFloat# celldim, int numcells)
549 {
550 if(hash->spatialIndexcxx . . $ cpSpaceHash#spatialIndex..klasscxx . . $ cpSpatialIndex#klass. != Klasscxx . . $ Klass(292ec58cde6c1067).()){
551 cpAssertWarncxx . . $ `include/chipmunk/chipmunk.h:50:10`!(cpFalse, "Ignoring cpSpaceHashResize() call to non-cpSpaceHash spatial index.");
552 return;
553 }
554
555 clearTablecxx . . $ clearTable(3c54e905acb17611).(hash);
556
557 hash->celldimcxx . . $ cpSpaceHash#celldim. = celldim;
558 cpSpaceHashAllocTablecxx . . $ cpSpaceHashAllocTable(e3be52993ed9bf6a).(hash, next_primecxx . . $ next_prime(7864480464b09eea).(numcells));
559 }
560
561 static int
562 cpSpaceHashCountcxx . . $ cpSpaceHashCount(8f9aced210b7d8f1).(cpSpaceHashcxx . . $ cpSpaceHash# *hash)
563 {
564 return cpHashSetCountcxx . . $ cpHashSetCount(e246982534155f1a).(hash->handleSetcxx . . $ cpSpaceHash#handleSet.);
565 }
566
567 static int
568 cpSpaceHashContainscxx . . $ cpSpaceHashContains(9c97ad716340614a).(cpSpaceHashcxx . . $ cpSpaceHash# *hash, void *obj, cpHashValuecxx . . $ cpHashValue# hashid)
569 {
570 return cpHashSetFindcxx . . $ cpHashSetFind(82fb1457cc8dfa6c).(hash->handleSetcxx . . $ cpSpaceHash#handleSet., hashid, obj) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!;
571 }
572
573 static cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# klasscxx . . $ klass. = {
574 (cpSpatialIndexDestroyImplcxx . . $ cpSpatialIndexDestroyImpl#)cpSpaceHashDestroycxx . . $ cpSpaceHashDestroy(3c54e905acb17611).,
575
576 (cpSpatialIndexCountImplcxx . . $ cpSpatialIndexCountImpl#)cpSpaceHashCountcxx . . $ cpSpaceHashCount(8f9aced210b7d8f1).,
577 (cpSpatialIndexEachImplcxx . . $ cpSpatialIndexEachImpl#)cpSpaceHashEachcxx . . $ cpSpaceHashEach(d00296f8b45daa1e).,
578 (cpSpatialIndexContainsImplcxx . . $ cpSpatialIndexContainsImpl#)cpSpaceHashContainscxx . . $ cpSpaceHashContains(9c97ad716340614a).,
579
580 (cpSpatialIndexInsertImplcxx . . $ cpSpatialIndexInsertImpl#)cpSpaceHashInsertcxx . . $ cpSpaceHashInsert(762a591b4939c1e8).,
581 (cpSpatialIndexRemoveImplcxx . . $ cpSpatialIndexRemoveImpl#)cpSpaceHashRemovecxx . . $ cpSpaceHashRemove(762a591b4939c1e8).,
582
583 (cpSpatialIndexReindexImplcxx . . $ cpSpatialIndexReindexImpl#)cpSpaceHashRehashcxx . . $ cpSpaceHashRehash(3c54e905acb17611).,
584 (cpSpatialIndexReindexObjectImplcxx . . $ cpSpatialIndexReindexObjectImpl#)cpSpaceHashRehashObjectcxx . . $ cpSpaceHashRehashObject(762a591b4939c1e8).,
585 (cpSpatialIndexReindexQueryImplcxx . . $ cpSpatialIndexReindexQueryImpl#)cpSpaceHashReindexQuerycxx . . $ cpSpaceHashReindexQuery(f0ed88b68b35e2c9).,
586
587 (cpSpatialIndexQueryImplcxx . . $ cpSpatialIndexQueryImpl#)cpSpaceHashQuerycxx . . $ cpSpaceHashQuery(395a8b410dda54da).,
588 (cpSpatialIndexSegmentQueryImplcxx . . $ cpSpatialIndexSegmentQueryImpl#)cpSpaceHashSegmentQuerycxx . . $ cpSpaceHashSegmentQuery(7f04a6bc1ba6f4db).,
589 };
590
591 static inline cpSpatialIndexClasscxx . . $ cpSpatialIndexClass# *Klasscxx . . $ Klass(292ec58cde6c1067).(){return &klasscxx . . $ klass.;}
592
593 //MARK: Debug Drawing
594
595 //#define CP_BBTREE_DEBUG_DRAW
596 #ifdef CP_BBTREE_DEBUG_DRAW
597 #include "OpenGL/gl.h"
598 #include "OpenGL/glu.h"
599 #include <GLUT/glut.h>
600
601 void
602 cpSpaceHashRenderDebug(cpSpatialIndex *index)
603 {
604 if(index->klass != &klass){
605 cpAssertWarn(cpFalse, "Ignoring cpSpaceHashRenderDebug() call to non-spatial hash spatial index.");
606 return;
607 }
608
609 cpSpaceHash *hash = (cpSpaceHash *)index;
610 cpBB bb = cpBBNew(-320, -240, 320, 240);
611
612 cpFloat dim = hash->celldim;
613 int n = hash->numcells;
614
615 int l = (int)floor(bb.l/dim);
616 int r = (int)floor(bb.r/dim);
617 int b = (int)floor(bb.b/dim);
618 int t = (int)floor(bb.t/dim);
619
620 for(int i=l; i<=r; i++){
621 for(int j=b; j<=t; j++){
622 int cell_count = 0;
623
624 int index = hash_func(i,j,n);
625 for(cpSpaceHashBin *bin = hash->table[index]; bin; bin = bin->next)
626 cell_count++;
627
628 GLfloat v = 1.0f - (GLfloat)cell_count/10.0f;
629 glColor3f(v,v,v);
630 glRectf(i*dim, j*dim, (i + 1)*dim, (j + 1)*dim);
631 }
632 }
633 }
634 #endif