| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include "bltInt.h" |
| 28 |
#include "bltChain.h" |
| 29 |
|
| 30 |
#ifndef ALIGN |
| 31 |
#define ALIGN(a) \ |
| 32 |
(((size_t)a + (sizeof(double) - 1)) & (~(sizeof(double) - 1))) |
| 33 |
#endif |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
Blt_Chain * |
| 49 |
Blt_ChainCreate() |
| 50 |
{ |
| 51 |
Blt_Chain *chainPtr; |
| 52 |
|
| 53 |
chainPtr = Blt_Malloc(sizeof(Blt_Chain)); |
| 54 |
if (chainPtr != NULL) { |
| 55 |
Blt_ChainInit(chainPtr); |
| 56 |
} |
| 57 |
return chainPtr; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
Blt_ChainLink * |
| 74 |
Blt_ChainAllocLink(extraSize) |
| 75 |
unsigned int extraSize; |
| 76 |
{ |
| 77 |
Blt_ChainLink *linkPtr; |
| 78 |
unsigned int linkSize; |
| 79 |
|
| 80 |
linkSize = ALIGN(sizeof(Blt_ChainLink)); |
| 81 |
linkPtr = Blt_Calloc(1, linkSize + extraSize); |
| 82 |
assert(linkPtr); |
| 83 |
if (extraSize > 0) { |
| 84 |
|
| 85 |
linkPtr->clientData = (ClientData)((char *)linkPtr + linkSize); |
| 86 |
} |
| 87 |
return linkPtr; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
Blt_ChainLink * |
| 103 |
Blt_ChainNewLink() |
| 104 |
{ |
| 105 |
Blt_ChainLink *linkPtr; |
| 106 |
|
| 107 |
linkPtr = Blt_Malloc(sizeof(Blt_ChainLink)); |
| 108 |
assert(linkPtr); |
| 109 |
linkPtr->clientData = NULL; |
| 110 |
linkPtr->nextPtr = linkPtr->prevPtr = NULL; |
| 111 |
return linkPtr; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
void |
| 129 |
Blt_ChainReset(chainPtr) |
| 130 |
Blt_Chain *chainPtr; |
| 131 |
{ |
| 132 |
if (chainPtr != NULL) { |
| 133 |
Blt_ChainLink *oldPtr; |
| 134 |
Blt_ChainLink *linkPtr = chainPtr->headPtr; |
| 135 |
|
| 136 |
while (linkPtr != NULL) { |
| 137 |
oldPtr = linkPtr; |
| 138 |
linkPtr = linkPtr->nextPtr; |
| 139 |
Blt_Free(oldPtr); |
| 140 |
} |
| 141 |
Blt_ChainInit(chainPtr); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
void |
| 160 |
Blt_ChainDestroy(chainPtr) |
| 161 |
Blt_Chain *chainPtr; |
| 162 |
{ |
| 163 |
if (chainPtr != NULL) { |
| 164 |
Blt_ChainReset(chainPtr); |
| 165 |
Blt_Free(chainPtr); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
void |
| 182 |
Blt_ChainInit(chainPtr) |
| 183 |
Blt_Chain *chainPtr; |
| 184 |
{ |
| 185 |
chainPtr->nLinks = 0; |
| 186 |
chainPtr->headPtr = chainPtr->tailPtr = NULL; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
void |
| 202 |
Blt_ChainLinkAfter(chainPtr, linkPtr, afterPtr) |
| 203 |
Blt_Chain *chainPtr; |
| 204 |
Blt_ChainLink *linkPtr, *afterPtr; |
| 205 |
{ |
| 206 |
if (chainPtr->headPtr == NULL) { |
| 207 |
chainPtr->tailPtr = chainPtr->headPtr = linkPtr; |
| 208 |
} else { |
| 209 |
if (afterPtr == NULL) { |
| 210 |
|
| 211 |
linkPtr->nextPtr = chainPtr->headPtr; |
| 212 |
linkPtr->prevPtr = NULL; |
| 213 |
chainPtr->headPtr->prevPtr = linkPtr; |
| 214 |
chainPtr->headPtr = linkPtr; |
| 215 |
} else { |
| 216 |
linkPtr->nextPtr = afterPtr->nextPtr; |
| 217 |
linkPtr->prevPtr = afterPtr; |
| 218 |
if (afterPtr == chainPtr->tailPtr) { |
| 219 |
chainPtr->tailPtr = linkPtr; |
| 220 |
} else { |
| 221 |
afterPtr->nextPtr->prevPtr = linkPtr; |
| 222 |
} |
| 223 |
afterPtr->nextPtr = linkPtr; |
| 224 |
} |
| 225 |
} |
| 226 |
chainPtr->nLinks++; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
void |
| 242 |
Blt_ChainLinkBefore(chainPtr, linkPtr, beforePtr) |
| 243 |
Blt_Chain *chainPtr; |
| 244 |
Blt_ChainLink *linkPtr; |
| 245 |
Blt_ChainLink *beforePtr; |
| 246 |
{ |
| 247 |
if (chainPtr->headPtr == NULL) { |
| 248 |
chainPtr->tailPtr = chainPtr->headPtr = linkPtr; |
| 249 |
} else { |
| 250 |
if (beforePtr == NULL) { |
| 251 |
|
| 252 |
linkPtr->nextPtr = NULL; |
| 253 |
linkPtr->prevPtr = chainPtr->tailPtr; |
| 254 |
chainPtr->tailPtr->nextPtr = linkPtr; |
| 255 |
chainPtr->tailPtr = linkPtr; |
| 256 |
} else { |
| 257 |
linkPtr->prevPtr = beforePtr->prevPtr; |
| 258 |
linkPtr->nextPtr = beforePtr; |
| 259 |
if (beforePtr == chainPtr->headPtr) { |
| 260 |
chainPtr->headPtr = linkPtr; |
| 261 |
} else { |
| 262 |
beforePtr->prevPtr->nextPtr = linkPtr; |
| 263 |
} |
| 264 |
beforePtr->prevPtr = linkPtr; |
| 265 |
} |
| 266 |
} |
| 267 |
chainPtr->nLinks++; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
void |
| 284 |
Blt_ChainUnlinkLink(chainPtr, linkPtr) |
| 285 |
Blt_Chain *chainPtr; |
| 286 |
Blt_ChainLink *linkPtr; |
| 287 |
{ |
| 288 |
int unlinked; |
| 289 |
|
| 290 |
|
| 291 |
unlinked = FALSE; |
| 292 |
if (chainPtr->headPtr == linkPtr) { |
| 293 |
chainPtr->headPtr = linkPtr->nextPtr; |
| 294 |
unlinked = TRUE; |
| 295 |
} |
| 296 |
if (chainPtr->tailPtr == linkPtr) { |
| 297 |
chainPtr->tailPtr = linkPtr->prevPtr; |
| 298 |
unlinked = TRUE; |
| 299 |
} |
| 300 |
if (linkPtr->nextPtr != NULL) { |
| 301 |
linkPtr->nextPtr->prevPtr = linkPtr->prevPtr; |
| 302 |
unlinked = TRUE; |
| 303 |
} |
| 304 |
if (linkPtr->prevPtr != NULL) { |
| 305 |
linkPtr->prevPtr->nextPtr = linkPtr->nextPtr; |
| 306 |
unlinked = TRUE; |
| 307 |
} |
| 308 |
if (unlinked) { |
| 309 |
chainPtr->nLinks--; |
| 310 |
} |
| 311 |
linkPtr->prevPtr = linkPtr->nextPtr = NULL; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
void |
| 327 |
Blt_ChainDeleteLink(chainPtr, linkPtr) |
| 328 |
Blt_Chain *chainPtr; |
| 329 |
Blt_ChainLink *linkPtr; |
| 330 |
{ |
| 331 |
Blt_ChainUnlinkLink(chainPtr, linkPtr); |
| 332 |
Blt_Free(linkPtr); |
| 333 |
} |
| 334 |
|
| 335 |
Blt_ChainLink * |
| 336 |
Blt_ChainAppend(chainPtr, clientData) |
| 337 |
Blt_Chain *chainPtr; |
| 338 |
ClientData clientData; |
| 339 |
{ |
| 340 |
Blt_ChainLink *linkPtr; |
| 341 |
|
| 342 |
linkPtr = Blt_ChainNewLink(); |
| 343 |
Blt_ChainLinkBefore(chainPtr, linkPtr, (Blt_ChainLink *)NULL); |
| 344 |
Blt_ChainSetValue(linkPtr, clientData); |
| 345 |
return linkPtr; |
| 346 |
} |
| 347 |
|
| 348 |
Blt_ChainLink * |
| 349 |
Blt_ChainPrepend(chainPtr, clientData) |
| 350 |
Blt_Chain *chainPtr; |
| 351 |
ClientData clientData; |
| 352 |
{ |
| 353 |
Blt_ChainLink *linkPtr; |
| 354 |
|
| 355 |
linkPtr = Blt_ChainNewLink(); |
| 356 |
Blt_ChainLinkAfter(chainPtr, linkPtr, (Blt_ChainLink *)NULL); |
| 357 |
Blt_ChainSetValue(linkPtr, clientData); |
| 358 |
return linkPtr; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
Blt_ChainLink * |
| 375 |
Blt_ChainGetNthLink(chainPtr, position) |
| 376 |
Blt_Chain *chainPtr; |
| 377 |
int position; |
| 378 |
|
| 379 |
{ |
| 380 |
Blt_ChainLink *linkPtr; |
| 381 |
|
| 382 |
if (chainPtr != NULL) { |
| 383 |
for (linkPtr = chainPtr->headPtr; linkPtr != NULL; |
| 384 |
linkPtr = linkPtr->nextPtr) { |
| 385 |
if (position == 0) { |
| 386 |
return linkPtr; |
| 387 |
} |
| 388 |
position--; |
| 389 |
} |
| 390 |
} |
| 391 |
return NULL; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
void |
| 410 |
Blt_ChainSort(chainPtr, proc) |
| 411 |
Blt_Chain *chainPtr; |
| 412 |
Blt_ChainCompareProc *proc; |
| 413 |
{ |
| 414 |
Blt_ChainLink **linkArr; |
| 415 |
register Blt_ChainLink *linkPtr; |
| 416 |
register int i; |
| 417 |
|
| 418 |
if (chainPtr->nLinks < 2) { |
| 419 |
return; |
| 420 |
} |
| 421 |
linkArr = Blt_Malloc(sizeof(Blt_ChainLink *) * (chainPtr->nLinks + 1)); |
| 422 |
if (linkArr == NULL) { |
| 423 |
return; |
| 424 |
} |
| 425 |
i = 0; |
| 426 |
for (linkPtr = chainPtr->headPtr; linkPtr != NULL; |
| 427 |
linkPtr = linkPtr->nextPtr) { |
| 428 |
linkArr[i++] = linkPtr; |
| 429 |
} |
| 430 |
qsort((char *)linkArr, chainPtr->nLinks, sizeof(Blt_ChainLink *), |
| 431 |
(QSortCompareProc *)proc); |
| 432 |
|
| 433 |
|
| 434 |
linkPtr = linkArr[0]; |
| 435 |
chainPtr->headPtr = linkPtr; |
| 436 |
linkPtr->prevPtr = NULL; |
| 437 |
for (i = 1; i < chainPtr->nLinks; i++) { |
| 438 |
linkPtr->nextPtr = linkArr[i]; |
| 439 |
linkPtr->nextPtr->prevPtr = linkPtr; |
| 440 |
linkPtr = linkPtr->nextPtr; |
| 441 |
} |
| 442 |
chainPtr->tailPtr = linkPtr; |
| 443 |
linkPtr->nextPtr = NULL; |
| 444 |
Blt_Free(linkArr); |
| 445 |
} |