| 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 cpHashSetBincxx . . $ cpHashSetBin# { |
| 26 | void *eltcxx . . $ cpHashSetBin#elt.; |
| 27 | cpHashValuecxx . . $ cpHashValue# hashcxx . . $ cpHashSetBin#hash.; |
| 28 | struct cpHashSetBincxx . . $ cpHashSetBin# *nextcxx . . $ cpHashSetBin#next.; |
| 29 | } cpHashSetBincxx . . $ cpHashSetBin#; |
| 30 | |
| 31 | struct cpHashSetcxx . . $ cpHashSet# { |
| 32 | unsigned int entriescxx . . $ cpHashSet#entries., sizecxx . . $ cpHashSet#size.; |
| 33 | |
| 34 | cpHashSetEqlFunccxx . . $ cpHashSetEqlFunc# eqlcxx . . $ cpHashSet#eql.; |
| 35 | void *default_valuecxx . . $ cpHashSet#default_value.; |
| 36 | |
| 37 | cpHashSetBincxx . . $ cpHashSetBin# **tablecxx . . $ cpHashSet#table.; |
| 38 | cpHashSetBincxx . . $ cpHashSetBin# *pooledBinscxx . . $ cpHashSet#pooledBins.; |
| 39 | |
| 40 | cpArraycxx . . $ cpArray# *allocatedBufferscxx . . $ cpHashSet#allocatedBuffers.; |
| 41 | }; |
| 42 | |
| 43 | void |
| 44 | cpHashSetFreecxx . . $ cpHashSetFree(a5aef454cd7046d4).(cpHashSetcxx . . $ cpHashSet# *set) |
| 45 | { |
| 46 | if(set){ |
| 47 | cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(set->tablecxx . . $ cpHashSet#table.); |
| 48 | |
| 49 | cpArrayFreeEachcxx . . $ cpArrayFreeEach(d583da02e3252d4f).(set->allocatedBufferscxx . . $ cpHashSet#allocatedBuffers., cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!); |
| 50 | cpArrayFreecxx . . $ cpArrayFree(9f115453462bfa05).(set->allocatedBufferscxx . . $ cpHashSet#allocatedBuffers.); |
| 51 | |
| 52 | cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(set); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | cpHashSetcxx . . $ cpHashSet# * |
| 57 | cpHashSetNewcxx . . $ cpHashSetNew(1aa2f270b6b730e1).(int size, cpHashSetEqlFunccxx . . $ cpHashSetEqlFunc# eqlFunc) |
| 58 | { |
| 59 | cpHashSetcxx . . $ cpHashSet# *set = (cpHashSetcxx . . $ cpHashSet# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpHashSetcxx . . $ cpHashSet#)); |
| 60 | |
| 61 | set->sizecxx . . $ cpHashSet#size. = next_primecxx . . $ next_prime(7864480464b09eea).(size); |
| 62 | set->entriescxx . . $ cpHashSet#entries. = 0; |
| 63 | |
| 64 | set->eqlcxx . . $ cpHashSet#eql. = eqlFunc; |
| 65 | set->default_valuecxx . . $ cpHashSet#default_value. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 66 | |
| 67 | set->tablecxx . . $ cpHashSet#table. = (cpHashSetBincxx . . $ cpHashSetBin# **)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(set->sizecxx . . $ cpHashSet#size., sizeof(cpHashSetBincxx . . $ cpHashSetBin# *)); |
| 68 | set->pooledBinscxx . . $ cpHashSet#pooledBins. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 69 | |
| 70 | set->allocatedBufferscxx . . $ cpHashSet#allocatedBuffers. = cpArrayNewcxx . . $ cpArrayNew(f90d83c90697c3ae).(0); |
| 71 | |
| 72 | return set; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | cpHashSetSetDefaultValuecxx . . $ cpHashSetSetDefaultValue(187792fade294a49).(cpHashSetcxx . . $ cpHashSet# *set, void *default_value) |
| 77 | { |
| 78 | set->default_valuecxx . . $ cpHashSet#default_value. = default_value; |
| 79 | } |
| 80 | |
| 81 | static int |
| 82 | setIsFullcxx . . $ setIsFull(e246982534155f1a).(cpHashSetcxx . . $ cpHashSet# *set) |
| 83 | { |
| 84 | return (set->entriescxx . . $ cpHashSet#entries. >= set->sizecxx . . $ cpHashSet#size.); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | cpHashSetResizecxx . . $ cpHashSetResize(a5aef454cd7046d4).(cpHashSetcxx . . $ cpHashSet# *set) |
| 89 | { |
| 90 | // Get the next approximate doubled prime. |
| 91 | unsigned int newSize = next_primecxx . . $ next_prime(7864480464b09eea).(set->sizecxx . . $ cpHashSet#size. + 1); |
| 92 | // Allocate a new table. |
| 93 | cpHashSetBincxx . . $ cpHashSetBin# **newTable = (cpHashSetBincxx . . $ cpHashSetBin# **)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(newSize, sizeof(cpHashSetBincxx . . $ cpHashSetBin# *)); |
| 94 | |
| 95 | // Iterate over the chains. |
| 96 | for(unsigned int i=0; i<set->sizecxx . . $ cpHashSet#size.; i++){ |
| 97 | // Rehash the bins into the new table. |
| 98 | cpHashSetBincxx . . $ cpHashSetBin# *bin = set->tablecxx . . $ cpHashSet#table.[i]; |
| 99 | while(bin){ |
| 100 | cpHashSetBincxx . . $ cpHashSetBin# *next = bin->nextcxx . . $ cpHashSetBin#next.; |
| 101 | |
| 102 | cpHashValuecxx . . $ cpHashValue# idx = bin->hashcxx . . $ cpHashSetBin#hash.%newSize; |
| 103 | bin->nextcxx . . $ cpHashSetBin#next. = newTable[idx]; |
| 104 | newTable[idx] = bin; |
| 105 | |
| 106 | bin = next; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | cpfreecxx . . $ `include/chipmunk/chipmunk.h:82:10`!(set->tablecxx . . $ cpHashSet#table.); |
| 111 | |
| 112 | set->tablecxx . . $ cpHashSet#table. = newTable; |
| 113 | set->sizecxx . . $ cpHashSet#size. = newSize; |
| 114 | } |
| 115 | |
| 116 | static inline void |
| 117 | recycleBincxx . . $ recycleBin(a0f06dc8cd51f337).(cpHashSetcxx . . $ cpHashSet# *set, cpHashSetBincxx . . $ cpHashSetBin# *bin) |
| 118 | { |
| 119 | bin->nextcxx . . $ cpHashSetBin#next. = set->pooledBinscxx . . $ cpHashSet#pooledBins.; |
| 120 | set->pooledBinscxx . . $ cpHashSet#pooledBins. = bin; |
| 121 | bin->eltcxx . . $ cpHashSetBin#elt. = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 122 | } |
| 123 | |
| 124 | static cpHashSetBincxx . . $ cpHashSetBin# * |
| 125 | getUnusedBincxx . . $ getUnusedBin(aa80070640ad130c).(cpHashSetcxx . . $ cpHashSet# *set) |
| 126 | { |
| 127 | cpHashSetBincxx . . $ cpHashSetBin# *bin = set->pooledBinscxx . . $ cpHashSet#pooledBins.; |
| 128 | |
| 129 | if(bin){ |
| 130 | set->pooledBinscxx . . $ cpHashSet#pooledBins. = bin->nextcxx . . $ cpHashSetBin#next.; |
| 131 | return bin; |
| 132 | } else { |
| 133 | // Pool is exhausted, make more |
| 134 | int count = CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!/sizeof(cpHashSetBincxx . . $ cpHashSetBin#); |
| 135 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(count, "Internal Error: Buffer size is too small."); |
| 136 | |
| 137 | cpHashSetBincxx . . $ cpHashSetBin# *buffer = (cpHashSetBincxx . . $ cpHashSetBin# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, CP_BUFFER_BYTEScxx . . $ `include/chipmunk/chipmunk.h:67:10`!); |
| 138 | cpArrayPushcxx . . $ cpArrayPush(5027a0abedaf1ddc).(set->allocatedBufferscxx . . $ cpHashSet#allocatedBuffers., buffer); |
| 139 | |
| 140 | // push all but the first one, return it instead |
| 141 | for(int i=1; i<count; i++) recycleBincxx . . $ recycleBin(a0f06dc8cd51f337).(set, buffer + i); |
| 142 | return buffer; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | int |
| 147 | cpHashSetCountcxx . . $ cpHashSetCount(e246982534155f1a).(cpHashSetcxx . . $ cpHashSet# *set) |
| 148 | { |
| 149 | return set->entriescxx . . $ cpHashSet#entries.; |
| 150 | } |
| 151 | |
| 152 | const void * |
| 153 | cpHashSetInsertcxx . . $ cpHashSetInsert(65a72e7022b62d4d).(cpHashSetcxx . . $ cpHashSet# *set, cpHashValuecxx . . $ cpHashValue# hash, const void *ptr, cpHashSetTransFunccxx . . $ cpHashSetTransFunc# trans, void *data) |
| 154 | { |
| 155 | cpHashValuecxx . . $ cpHashValue# idx = hash%set->sizecxx . . $ cpHashSet#size.; |
| 156 | |
| 157 | // Find the bin with the matching element. |
| 158 | cpHashSetBincxx . . $ cpHashSetBin# *bin = set->tablecxx . . $ cpHashSet#table.[idx]; |
| 159 | while(bin && !set->eqlcxx . . $ cpHashSet#eql.(ptr, bin->eltcxx . . $ cpHashSetBin#elt.)) |
| 160 | bin = bin->nextcxx . . $ cpHashSetBin#next.; |
| 161 | |
| 162 | // Create it if necessary. |
| 163 | if(!bin){ |
| 164 | bin = getUnusedBincxx . . $ getUnusedBin(aa80070640ad130c).(set); |
| 165 | bin->hashcxx . . $ cpHashSetBin#hash. = hash; |
| 166 | bin->eltcxx . . $ cpHashSetBin#elt. = (trans ? trans(ptr, data) : data); |
| 167 | |
| 168 | bin->nextcxx . . $ cpHashSetBin#next. = set->tablecxx . . $ cpHashSet#table.[idx]; |
| 169 | set->tablecxx . . $ cpHashSet#table.[idx] = bin; |
| 170 | |
| 171 | set->entriescxx . . $ cpHashSet#entries.++; |
| 172 | if(setIsFullcxx . . $ setIsFull(e246982534155f1a).(set)) cpHashSetResizecxx . . $ cpHashSetResize(a5aef454cd7046d4).(set); |
| 173 | } |
| 174 | |
| 175 | return bin->eltcxx . . $ cpHashSetBin#elt.; |
| 176 | } |
| 177 | |
| 178 | const void * |
| 179 | cpHashSetRemovecxx . . $ cpHashSetRemove(82fb1457cc8dfa6c).(cpHashSetcxx . . $ cpHashSet# *set, cpHashValuecxx . . $ cpHashValue# hash, const void *ptr) |
| 180 | { |
| 181 | cpHashValuecxx . . $ cpHashValue# idx = hash%set->sizecxx . . $ cpHashSet#size.; |
| 182 | |
| 183 | cpHashSetBincxx . . $ cpHashSetBin# **prev_ptr = &set->tablecxx . . $ cpHashSet#table.[idx]; |
| 184 | cpHashSetBincxx . . $ cpHashSetBin# *bin = set->tablecxx . . $ cpHashSet#table.[idx]; |
| 185 | |
| 186 | // Find the bin |
| 187 | while(bin && !set->eqlcxx . . $ cpHashSet#eql.(ptr, bin->eltcxx . . $ cpHashSetBin#elt.)){ |
| 188 | prev_ptr = &bin->nextcxx . . $ cpHashSetBin#next.; |
| 189 | bin = bin->nextcxx . . $ cpHashSetBin#next.; |
| 190 | } |
| 191 | |
| 192 | // Remove it if it exists. |
| 193 | if(bin){ |
| 194 | // Update the previous linked list pointer |
| 195 | (*prev_ptr) = bin->nextcxx . . $ cpHashSetBin#next.; |
| 196 | set->entriescxx . . $ cpHashSet#entries.--; |
| 197 | |
| 198 | const void *elt = bin->eltcxx . . $ cpHashSetBin#elt.; |
| 199 | recycleBincxx . . $ recycleBin(a0f06dc8cd51f337).(set, bin); |
| 200 | |
| 201 | return elt; |
| 202 | } |
| 203 | |
| 204 | return NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 205 | } |
| 206 | |
| 207 | const void * |
| 208 | cpHashSetFindcxx . . $ cpHashSetFind(82fb1457cc8dfa6c).(cpHashSetcxx . . $ cpHashSet# *set, cpHashValuecxx . . $ cpHashValue# hash, const void *ptr) |
| 209 | { |
| 210 | cpHashValuecxx . . $ cpHashValue# idx = hash%set->sizecxx . . $ cpHashSet#size.; |
| 211 | cpHashSetBincxx . . $ cpHashSetBin# *bin = set->tablecxx . . $ cpHashSet#table.[idx]; |
| 212 | while(bin && !set->eqlcxx . . $ cpHashSet#eql.(ptr, bin->eltcxx . . $ cpHashSetBin#elt.)) |
| 213 | bin = bin->nextcxx . . $ cpHashSetBin#next.; |
| 214 | |
| 215 | return (bin ? bin->eltcxx . . $ cpHashSetBin#elt. : set->default_valuecxx . . $ cpHashSet#default_value.); |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | cpHashSetEachcxx . . $ cpHashSetEach(3ff8996736d46a4d).(cpHashSetcxx . . $ cpHashSet# *set, cpHashSetIteratorFunccxx . . $ cpHashSetIteratorFunc# func, void *data) |
| 220 | { |
| 221 | for(unsigned int i=0; i<set->sizecxx . . $ cpHashSet#size.; i++){ |
| 222 | cpHashSetBincxx . . $ cpHashSetBin# *bin = set->tablecxx . . $ cpHashSet#table.[i]; |
| 223 | while(bin){ |
| 224 | cpHashSetBincxx . . $ cpHashSetBin# *next = bin->nextcxx . . $ cpHashSetBin#next.; |
| 225 | func(bin->eltcxx . . $ cpHashSetBin#elt., data); |
| 226 | bin = next; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | cpHashSetFiltercxx . . $ cpHashSetFilter(ded8e246e9dd8428).(cpHashSetcxx . . $ cpHashSet# *set, cpHashSetFilterFunccxx . . $ cpHashSetFilterFunc# func, void *data) |
| 233 | { |
| 234 | for(unsigned int i=0; i<set->sizecxx . . $ cpHashSet#size.; i++){ |
| 235 | // The rest works similarly to cpHashSetRemove() above. |
| 236 | cpHashSetBincxx . . $ cpHashSetBin# **prev_ptr = &set->tablecxx . . $ cpHashSet#table.[i]; |
| 237 | cpHashSetBincxx . . $ cpHashSetBin# *bin = set->tablecxx . . $ cpHashSet#table.[i]; |
| 238 | while(bin){ |
| 239 | cpHashSetBincxx . . $ cpHashSetBin# *next = bin->nextcxx . . $ cpHashSetBin#next.; |
| 240 | |
| 241 | if(func(bin->eltcxx . . $ cpHashSetBin#elt., data)){ |
| 242 | prev_ptr = &bin->nextcxx . . $ cpHashSetBin#next.; |
| 243 | } else { |
| 244 | (*prev_ptr) = next; |
| 245 | |
| 246 | set->entriescxx . . $ cpHashSet#entries.--; |
| 247 | recycleBincxx . . $ recycleBin(a0f06dc8cd51f337).(set, bin); |
| 248 | } |
| 249 | |
| 250 | bin = next; |
| 251 | } |
| 252 | } |
| 253 | } |