| 1 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
// vi: set et ts=4 sw=2 sts=2: |
| 3 |
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 4 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 5 |
/****************************************************************************/ |
| 6 |
/* */ |
| 7 |
/* File: ooppcc.h */ |
| 8 |
/* */ |
| 9 |
/* Purpose: macros and templates for oopp-container classes. */ |
| 10 |
/* (object-oriented pre-processing container classes). */ |
| 11 |
/* */ |
| 12 |
/* Author: Klaus Birken */ |
| 13 |
/* Institut fuer Computeranwendungen III */ |
| 14 |
/* Universitaet Stuttgart */ |
| 15 |
/* Pfaffenwaldring 27 */ |
| 16 |
/* 70569 Stuttgart */ |
| 17 |
/* email: [email protected] */ |
| 18 |
/* phone: 0049-(0)711-685-7007 */ |
| 19 |
/* fax : 0049-(0)711-685-7000 */ |
| 20 |
/* */ |
| 21 |
/* History: 970902 kb begin */ |
| 22 |
/* 971015 kb added ArrayOf, BTreeOf */ |
| 23 |
/* 971020 kb added SetOf */ |
| 24 |
/* */ |
| 25 |
/* Remarks: */ |
| 26 |
/* */ |
| 27 |
/****************************************************************************/ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/****************************************************************************/ |
| 33 |
#ifdef PtrOf |
| 34 |
/****************************************************************************/ |
| 35 |
|
| 36 |
#define Ptr(T) CN(CCAT(T,Ptr)) |
| 37 |
|
| 38 |
/* class name for ptr */ |
| 39 |
#define CPtr CCAT(PtrOf,Ptr) |
| 40 |
|
| 41 |
/* an object's pointer class is simply a C pointer to the object */ |
| 42 |
typedef PtrOf * CPtr; |
| 43 |
|
| 44 |
|
| 45 |
/****************************************************************************/ |
| 46 |
#undef CPtr |
| 47 |
#undef PtrOf |
| 48 |
#endif |
| 49 |
/****************************************************************************/ |
| 50 |
|
| 51 |
/****************************************************************************/ |
| 52 |
#ifdef SegmListOf |
| 53 |
/****************************************************************************/ |
| 54 |
|
| 55 |
#define SegmList(T) CN(CCAT(T,SegmList)) |
| 56 |
|
| 57 |
/* class names for segmlist and segment */ |
| 58 |
#define CSegm CCAT(SegmListOf,Segm) |
| 59 |
#define CSegmList CCAT(SegmListOf,SegmList) |
| 60 |
|
| 61 |
|
| 62 |
#ifndef SegmSize |
| 63 |
/* define default size of each segment */ |
| 64 |
#define SegmSize 256 |
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
/*** one segment ***/ |
| 72 |
|
| 73 |
#define ClassName CSegm |
| 74 |
Class_Data_Begin |
| 75 |
CN(SegmListOf) data[SegmSize]; |
| 76 |
int nItems; |
| 77 |
|
| 78 |
ClassPtr next; |
| 79 |
Class_Data_End |
| 80 |
Method_New_ (_NEWPARAMS_OR_VOID); |
| 81 |
void Method(Free) (DefThis); |
| 82 |
void Method(Print) (DefThis _PRINTPARAMS); |
| 83 |
|
| 84 |
|
| 85 |
#ifdef ContainerImplementation |
| 86 |
|
| 87 |
Method_New_ (_NEWPARAMS_OR_VOID) |
| 88 |
{ |
| 89 |
Construct(This, _CHECKALLOC(This)); |
| 90 |
This->nItems = 0; |
| 91 |
return(This); |
| 92 |
} |
| 93 |
|
| 94 |
void Method(Free) (ParamThis) |
| 95 |
{ |
| 96 |
Destruct(This); |
| 97 |
} |
| 98 |
|
| 99 |
#endif |
| 100 |
|
| 101 |
#undef ClassName |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
/*** the segm-list ***/ |
| 108 |
|
| 109 |
#define ClassName CSegmList |
| 110 |
Class_Data_Begin |
| 111 |
CSegm *first; |
| 112 |
int nItems; |
| 113 |
int nSegms; |
| 114 |
|
| 115 |
int nDiscarded; |
| 116 |
Class_Data_End |
| 117 |
Method_New_ (_NEWPARAMS_OR_VOID); |
| 118 |
void Method(Free) (DefThis); |
| 119 |
CN(SegmListOf) *Method(NewItem) (DefThis); |
| 120 |
void Method(DiscardItem) (DefThis); |
| 121 |
void Method(Reset) (DefThis); |
| 122 |
int Method(GetNItems) (DefThis); |
| 123 |
int Method(GetNDiscarded) (DefThis); |
| 124 |
void Method(GetResources) (DefThis, int *, int *, size_t *, size_t *); |
| 125 |
|
| 126 |
|
| 127 |
#ifdef ContainerImplementation |
| 128 |
|
| 129 |
Method_New_ (_NEWPARAMS_OR_VOID) |
| 130 |
{ |
| 131 |
Construct(This, _CHECKALLOC(This)); |
| 132 |
This->first = NULL; |
| 133 |
This->nItems = 0; |
| 134 |
This->nSegms = 0; |
| 135 |
This->nDiscarded = 0; |
| 136 |
return(This); |
| 137 |
} |
| 138 |
|
| 139 |
void Method(Free) (ParamThis) |
| 140 |
{ |
| 141 |
CALL(CSegmList,Reset) (This); |
| 142 |
Destruct(This); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
CN(SegmListOf) *Method(NewItem) (ParamThis) |
| 147 |
{ |
| 148 |
CN(SegmListOf) *item; |
| 149 |
CSegm *segm = This->first; |
| 150 |
|
| 151 |
if (segm==NULL || segm->nItems==SegmSize) |
| 152 |
{ |
| 153 |
segm = CALL(New,CSegm) (); |
| 154 |
if (segm==NULL) |
| 155 |
return(NULL); |
| 156 |
segm->next = This->first; |
| 157 |
This->first = segm; |
| 158 |
This->nSegms++; |
| 159 |
} |
| 160 |
|
| 161 |
/* get item address in current segment */ |
| 162 |
item = &(segm->data[segm->nItems++]); |
| 163 |
|
| 164 |
/* count item */ |
| 165 |
This->nItems++; |
| 166 |
|
| 167 |
return(item); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
int Method(GetNItems) (ParamThis) |
| 172 |
{ |
| 173 |
return(This->nItems); |
| 174 |
} |
| 175 |
|
| 176 |
int Method(GetNDiscarded) (ParamThis) |
| 177 |
{ |
| 178 |
return(This->nDiscarded); |
| 179 |
} |
| 180 |
|
| 181 |
void Method(DiscardItem) (ParamThis) |
| 182 |
{ |
| 183 |
assert(This!=NULL); |
| 184 |
assert(This->first!=NULL); |
| 185 |
assert(This->first->nItems > 0); |
| 186 |
|
| 187 |
This->first->nItems--; |
| 188 |
This->nItems--; |
| 189 |
|
| 190 |
This->nDiscarded++; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
void Method(Reset) (ParamThis) |
| 195 |
{ |
| 196 |
CSegm *segm, *next; |
| 197 |
|
| 198 |
segm = This->first; |
| 199 |
next = NULL; |
| 200 |
|
| 201 |
while (segm!=NULL) |
| 202 |
{ |
| 203 |
next = segm->next; |
| 204 |
CALL(CSegm,Free) (segm); |
| 205 |
|
| 206 |
segm = next; |
| 207 |
} |
| 208 |
|
| 209 |
This->first = NULL; |
| 210 |
This->nItems = 0; |
| 211 |
This->nSegms = 0; |
| 212 |
This->nDiscarded = 0; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
void Method(GetResources) (ParamThis, |
| 217 |
int *nSegms, int *nItems, size_t *alloc_mem, size_t *used_mem) |
| 218 |
{ |
| 219 |
size_t allocated=0, used=0; |
| 220 |
CSegm *segm; |
| 221 |
|
| 222 |
for (segm=This->first; segm!=NULL; segm=segm->next) |
| 223 |
{ |
| 224 |
/* compute memory usage */ |
| 225 |
allocated += sizeof(CSegm); |
| 226 |
used += (sizeof(CSegm) - |
| 227 |
(sizeof(CN(SegmListOf)) * (SegmSize-segm->nItems))); |
| 228 |
} |
| 229 |
|
| 230 |
*nSegms = This->nSegms; |
| 231 |
*nItems = This->nItems; |
| 232 |
*alloc_mem = allocated; |
| 233 |
*used_mem = used; |
| 234 |
} |
| 235 |
|
| 236 |
#endif |
| 237 |
|
| 238 |
#undef ClassName |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
/****************************************************************************/ |
| 243 |
#undef CSegm |
| 244 |
#undef CSegmList |
| 245 |
#undef SegmSize |
| 246 |
#undef SegmListOf |
| 247 |
#endif |
| 248 |
/****************************************************************************/ |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
/****************************************************************************/ |
| 253 |
#ifdef ListOf |
| 254 |
/****************************************************************************/ |
| 255 |
|
| 256 |
#define List(T) CN(CCAT(T,List)) |
| 257 |
|
| 258 |
/* class names for list and list item */ |
| 259 |
#define CListItem CCAT(ListOf,ListItem) |
| 260 |
#define CList CCAT(ListOf,List) |
| 261 |
|
| 262 |
|
| 263 |
/* method pointers */ |
| 264 |
#define Find_Method CCAT(ListOf,FindMethod) |
| 265 |
typedef int (*Find_Method)(CN(ListOf) *); |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
/*** Item of list class ***/ |
| 270 |
|
| 271 |
#define ClassName CListItem |
| 272 |
Class_Data_Begin |
| 273 |
CN(ListOf) *data; |
| 274 |
ClassPtr next; |
| 275 |
Class_Data_End |
| 276 |
Method_New_ (CN(ListOf) * _NEWPARAMS); |
| 277 |
void Method(Print) (DefThis _PRINTPARAMS); |
| 278 |
|
| 279 |
|
| 280 |
#ifdef ContainerImplementation |
| 281 |
Method_New_ (CN(ListOf) *data _NEWPARAMS) |
| 282 |
{ |
| 283 |
Construct(This, _CHECKALLOC(This)); |
| 284 |
This->data = data; |
| 285 |
This->next = NULL; |
| 286 |
return(This); |
| 287 |
} |
| 288 |
|
| 289 |
void Method(Print) (ParamThis _PRINTPARAMS) |
| 290 |
{ |
| 291 |
CALL(ListOf,Print) (This->data _PRINTNEXT); |
| 292 |
} |
| 293 |
#endif |
| 294 |
#undef ClassName |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
/*** List class ***/ |
| 300 |
|
| 301 |
#define ClassName CList |
| 302 |
Class_Data_Begin |
| 303 |
CN(CListItem) *first; |
| 304 |
CN(CListItem) *last; |
| 305 |
Class_Data_End |
| 306 |
Method_New_ (_NEWPARAMS_OR_VOID); |
| 307 |
void Method(Print) (DefThis _PRINTPARAMS); |
| 308 |
ClassRef Method(Append) (DefThis, CN(ListOf) *); |
| 309 |
CN(ListOf) *Method(Find) (DefThis, Find_Method); |
| 310 |
|
| 311 |
|
| 312 |
#ifdef ContainerImplementation |
| 313 |
Method_New_ (_NEWPARAMS_OR_VOID) |
| 314 |
{ |
| 315 |
Construct(This, _CHECKALLOC(This)); |
| 316 |
This->first = NULL; |
| 317 |
This->last = NULL; |
| 318 |
return(This); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
void Method(Print) (ParamThis _PRINTPARAMS) |
| 323 |
{ |
| 324 |
CN(CListItem) *item; |
| 325 |
|
| 326 |
for(item=This->first; item!=NULL; item=item->next) |
| 327 |
CALL(CListItem,Print) (item _PRINTSAME); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
ClassRef Method(Append) (ParamThis, CN(ListOf) *data) |
| 332 |
{ |
| 333 |
CN(CListItem) *item; |
| 334 |
|
| 335 |
if (data==NULL) return(This); |
| 336 |
|
| 337 |
/* create item and link it into list */ |
| 338 |
item = CALL(New,CListItem) (data); |
| 339 |
assert(item!=NULL); |
| 340 |
if (This->first==NULL) |
| 341 |
{ |
| 342 |
This->first = item; |
| 343 |
This->last = item; |
| 344 |
} |
| 345 |
else |
| 346 |
{ |
| 347 |
This->last->next = item; |
| 348 |
This->last = item; |
| 349 |
} |
| 350 |
|
| 351 |
return(This); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
CN(ListOf) * Method(Find) (ParamThis, Find_Method find_data) |
| 356 |
{ |
| 357 |
CN(CListItem) *item; |
| 358 |
|
| 359 |
for (item=This->first; item!=NULL; item=item->next) |
| 360 |
{ |
| 361 |
if ((*find_data)(item->data)) |
| 362 |
return(item->data); |
| 363 |
} |
| 364 |
return(NULL); |
| 365 |
} |
| 366 |
|
| 367 |
#endif |
| 368 |
#undef ClassName |
| 369 |
|
| 370 |
|
| 371 |
/*** List iterator class ***/ |
| 372 |
|
| 373 |
/* iterator is simply a pointer to a CListItem */ |
| 374 |
#define ListIter(T) CN(CCAT(T,ListIter)) |
| 375 |
typedef CN (CListItem) * ListIter (ListOf); |
| 376 |
|
| 377 |
#ifndef __ListIterator__ |
| 378 |
#define __ListIterator__ |
| 379 |
|
| 380 |
/* forward iterator */ |
| 381 |
#define ListFirst(list) ((list)->first) |
| 382 |
#define ListNext(iter) ((iter)->next) |
| 383 |
#define ListIsDone(iter) ((iter)==NULL) |
| 384 |
#define ListCurrent(iter) ((iter)->data) |
| 385 |
#endif |
| 386 |
|
| 387 |
|
| 388 |
/****************************************************************************/ |
| 389 |
#undef CListItem |
| 390 |
#undef CList |
| 391 |
#undef Find_Method |
| 392 |
#undef ListOf |
| 393 |
#endif |
| 394 |
/****************************************************************************/ |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
/****************************************************************************/ |
| 400 |
#ifdef BTreeOf |
| 401 |
/****************************************************************************/ |
| 402 |
|
| 403 |
|
| 404 |
/* class names for btree and btree node */ |
| 405 |
#define CBTreeNode CCAT(BTreeOf,BTreeNode) |
| 406 |
#define CBTree CCAT(BTreeOf,BTree) |
| 407 |
|
| 408 |
/* method pointers */ |
| 409 |
#define Iterate_Method CCAT(BTreeOf,IterateMethod) |
| 410 |
typedef void (*Iterate_Method)(CN(BTreeOf) *); |
| 411 |
/* not used |
| 412 |
#define Compare_Method CCAT(BTreeOf,CompareMethod) |
| 413 |
typedef int (*Compare_Method) (CN(BTreeOf) *, CN(BTreeOf) *); |
| 414 |
*/ |
| 415 |
|
| 416 |
|
| 417 |
#ifndef BTreeOrder |
| 418 |
/* define default order of BTree */ |
| 419 |
#define BTreeOrder 8 |
| 420 |
#endif |
| 421 |
|
| 422 |
|
| 423 |
/* now the definitions for _all_ BTrees */ |
| 424 |
#ifndef BTreeGeneral |
| 425 |
#define BTreeGeneral |
| 426 |
|
| 427 |
enum BTreeConstant { |
| 428 |
BTREE_ERROR, BTREE_INSERTED, BTREE_FOUND, BTREE_SPLIT_ME |
| 429 |
}; |
| 430 |
|
| 431 |
#endif |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
/*** node of BTree class ***/ |
| 438 |
|
| 439 |
#define ClassName CBTreeNode |
| 440 |
Class_Data_Begin |
| 441 |
int nSons; |
| 442 |
ClassPtr sons[BTreeOrder+1]; |
| 443 |
|
| 444 |
CN(BTreeOf) *data[BTreeOrder]; |
| 445 |
Class_Data_End |
| 446 |
/* no method declaration here, all methods are static */ |
| 447 |
|
| 448 |
|
| 449 |
#ifdef ContainerImplementation |
| 450 |
|
| 451 |
static Method_New_ (CN(BTreeOf) *item, |
| 452 |
ClassPtr son_l, ClassPtr son_r _NEWPARAMS) |
| 453 |
{ |
| 454 |
Construct(This, _CHECKALLOC(This)); |
| 455 |
This->nSons = 2; |
| 456 |
This->sons[0] = son_l; |
| 457 |
This->sons[1] = son_r; |
| 458 |
This->data[0] = item; |
| 459 |
return(This); |
| 460 |
} |
| 461 |
|
| 462 |
static void Method(Print) (ParamThis _PRINTPARAMS) |
| 463 |
{ |
| 464 |
int i; |
| 465 |
|
| 466 |
if (This==NULL) return; |
| 467 |
|
| 468 |
_INDENT; fprintf(fp, "NODE nSons=%d\n", This->nSons); |
| 469 |
|
| 470 |
for(i=0; i<This->nSons-1; i++) |
| 471 |
{ |
| 472 |
if (This->sons[i]!=NULL) |
| 473 |
Method(Print) (This->sons[i] _PRINTNEXT); |
| 474 |
|
| 475 |
_INDENT1; CALL(BTreeOf,Print) (This->data[i] _PRINTNEXT); |
| 476 |
} |
| 477 |
if (This->sons[i]!=NULL) |
| 478 |
Method(Print) (This->sons[i] _PRINTNEXT); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
static void Method(Free) (ParamThis) |
| 483 |
{ |
| 484 |
int i; |
| 485 |
|
| 486 |
/* free all son nodes */ |
| 487 |
for(i=0; i<This->nSons; i++) |
| 488 |
{ |
| 489 |
if (This->sons[i]!=NULL) |
| 490 |
{ |
| 491 |
Method(Free) (This->sons[i]); |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
/* destruct this node */ |
| 496 |
Destruct(This); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
static ClassPtr Method(Split) (ParamThis, |
| 501 |
CN(BTreeOf) **split_item) |
| 502 |
{ |
| 503 |
int split, l, r; |
| 504 |
Construct(rnode, _CHECKALLOC(rnode)); |
| 505 |
|
| 506 |
/* compute split index, left part is at most one item smaller */ |
| 507 |
split = ((BTreeOrder+1)>>1)-1; |
| 508 |
/*printf("\t\tsplit_index = %d\n", split);*/ |
| 509 |
|
| 510 |
for(l=split+1, r=0; l<This->nSons-1; l++, r++) |
| 511 |
{ |
| 512 |
rnode->sons[r] = This->sons[l]; |
| 513 |
rnode->data[r] = This->data[l]; |
| 514 |
} |
| 515 |
rnode->sons[r] = This->sons[l]; |
| 516 |
|
| 517 |
rnode->nSons = This->nSons-split-1; |
| 518 |
This->nSons = split+1; |
| 519 |
/*BTreeNodePrint(lnode,9);*/ |
| 520 |
/*BTreeNodePrint(rnode,9);*/ |
| 521 |
|
| 522 |
/* set return values */ |
| 523 |
*split_item = This->data[split]; |
| 524 |
return(rnode); |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
static void Method(FreeIndex) (ParamThis, int idx) |
| 529 |
{ |
| 530 |
int j, n=This->nSons; |
| 531 |
|
| 532 |
This->sons[n] = This->sons[n-1]; |
| 533 |
for(j=n-1; j>idx; j--) |
| 534 |
{ |
| 535 |
This->sons[j] = This->sons[j-1]; |
| 536 |
This->data[j] = This->data[j-1]; |
| 537 |
} |
| 538 |
|
| 539 |
This->nSons++; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
static enum BTreeConstant Method(Insert) (ParamThis, |
| 544 |
/*Compare_Method cmp_func,*/ |
| 545 |
CN(BTreeOf) *item, |
| 546 |
const DDD::DDDContext* context) |
| 547 |
{ |
| 548 |
int i, nData = This->nSons-1; |
| 549 |
enum BTreeConstant ret; |
| 550 |
|
| 551 |
/* find position in node */ |
| 552 |
if (nData < 4) |
| 553 |
{ |
| 554 |
/* only a few entries -> use linear search */ |
| 555 |
bool found=false; |
| 556 |
|
| 557 |
for(i=0; i<nData && !found; i++) |
| 558 |
{ |
| 559 |
/* the func-ptr version is not used here. */ |
| 560 |
/* cmp = (*cmp_func) (This->data[i], item);*/ |
| 561 |
|
| 562 |
/* NOTE: the order of arguments for Compare is crucial! |
| 563 |
first comes the existing key/item, 2nd the new one. */ |
| 564 |
int cmp = CALL(CN(BTreeOf), Compare) (This->data[i], item, context); |
| 565 |
if (cmp==0) |
| 566 |
{ |
| 567 |
/* already inserted, return and report FOUND */ |
| 568 |
return(BTREE_FOUND); |
| 569 |
} |
| 570 |
|
| 571 |
if (cmp>0) found = true; |
| 572 |
} |
| 573 |
if (found) i--; |
| 574 |
} |
| 575 |
else |
| 576 |
{ |
| 577 |
/* many entries -> use binary search */ |
| 578 |
int l=0, r=nData-1; |
| 579 |
|
| 580 |
do { |
| 581 |
int m = (l+r)>>1; |
| 582 |
|
| 583 |
/* NOTE: the order of arguments for Compare is crucial! |
| 584 |
first comes the existing key/item, 2nd the new one. */ |
| 585 |
int cmp = CALL(CN(BTreeOf), Compare) (This->data[m], item, context); |
| 586 |
if (cmp==0) |
| 587 |
{ |
| 588 |
/* already inserted, return and report FOUND */ |
| 589 |
return(BTREE_FOUND); |
| 590 |
} |
| 591 |
|
| 592 |
if (cmp<0) |
| 593 |
l = m+1; |
| 594 |
else |
| 595 |
r = m-1; |
| 596 |
|
| 597 |
} while (l<=r); |
| 598 |
/* the correct position is between keys r and l (in that order!) */ |
| 599 |
|
| 600 |
/* i will be set to the index at the right of the correct position */ |
| 601 |
i = l; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
if (This->sons[i]!=NULL) |
| 606 |
{ |
| 607 |
/* case A: key must be in subtree i, recurse into it */ |
| 608 |
|
| 609 |
ret = Method(Insert) (This->sons[i], /*cmp_func,*/ item, context); |
| 610 |
|
| 611 |
if (ret==BTREE_SPLIT_ME) |
| 612 |
{ |
| 613 |
ClassPtr new_r; |
| 614 |
CN(BTreeOf) *split_item; |
| 615 |
new_r = Method(Split) (This->sons[i], &split_item); |
| 616 |
assert(new_r!=NULL); |
| 617 |
|
| 618 |
if (i<nData) |
| 619 |
{ |
| 620 |
Method(FreeIndex) (This, i); |
| 621 |
This->data[i] = split_item; |
| 622 |
This->sons[i+1] = new_r; |
| 623 |
} |
| 624 |
else |
| 625 |
{ |
| 626 |
This->data[i] = split_item; |
| 627 |
This->sons[i+1] = new_r; |
| 628 |
This->nSons++; |
| 629 |
} |
| 630 |
|
| 631 |
if (This->nSons<BTreeOrder+1) |
| 632 |
ret = BTREE_INSERTED; |
| 633 |
} |
| 634 |
} |
| 635 |
else |
| 636 |
{ |
| 637 |
/* case B: there is no subtree for key here, we insert it locally */ |
| 638 |
|
| 639 |
if (i<nData) |
| 640 |
{ |
| 641 |
Method(FreeIndex) (This, i); |
| 642 |
This->data[i] = item; |
| 643 |
} |
| 644 |
else |
| 645 |
{ |
| 646 |
This->data[i] = item; |
| 647 |
This->sons[i+1] = NULL; |
| 648 |
This->nSons++; |
| 649 |
} |
| 650 |
ret = (This->nSons<BTreeOrder+1) ? |
| 651 |
BTREE_INSERTED : BTREE_SPLIT_ME; |
| 652 |
} |
| 653 |
|
| 654 |
return(ret); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
static void Method(Iterate) (ParamThis, Iterate_Method iter_method) |
| 660 |
{ |
| 661 |
int i; |
| 662 |
|
| 663 |
/* find position in node */ |
| 664 |
for(i=0; i<This->nSons-1; i++) |
| 665 |
{ |
| 666 |
if (This->sons[i]!=NULL) |
| 667 |
{ |
| 668 |
Method(Iterate) (This->sons[i], iter_method); |
| 669 |
} |
| 670 |
|
| 671 |
/* call iterator method */ |
| 672 |
(*iter_method)(This->data[i]); |
| 673 |
} |
| 674 |
if (This->sons[i]!=NULL) |
| 675 |
{ |
| 676 |
Method(Iterate) (This->sons[i], iter_method); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
static CN(BTreeOf) **Method(Linearize) (ParamThis, CN(BTreeOf) **ptr) |
| 682 |
{ |
| 683 |
int i; |
| 684 |
|
| 685 |
for(i=0; i<This->nSons-1; i++) |
| 686 |
{ |
| 687 |
if (This->sons[i]!=NULL) |
| 688 |
{ |
| 689 |
ptr = Method(Linearize) (This->sons[i], ptr); |
| 690 |
} |
| 691 |
|
| 692 |
*ptr++ = This->data[i]; |
| 693 |
} |
| 694 |
if (This->sons[i]!=NULL) |
| 695 |
{ |
| 696 |
ptr = Method(Linearize) (This->sons[i], ptr); |
| 697 |
} |
| 698 |
|
| 699 |
return(ptr); |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
static void Method(GetResources) (ParamThis, |
| 704 |
int *nNodes, size_t *alloc_mem, size_t *used_mem) |
| 705 |
{ |
| 706 |
size_t allocated=0, used=0; |
| 707 |
int i, nodes=0; |
| 708 |
|
| 709 |
for(i=0; i<This->nSons; i++) |
| 710 |
{ |
| 711 |
if (This->sons[i]!=NULL) |
| 712 |
{ |
| 713 |
int i1; size_t s1, s2; |
| 714 |
Method(GetResources) (This->sons[i], &i1, &s1, &s2); |
| 715 |
nodes += i1; |
| 716 |
allocated += s1; |
| 717 |
used += s2; |
| 718 |
} |
| 719 |
} |
| 720 |
|
| 721 |
*nNodes = nodes + 1; |
| 722 |
*alloc_mem = allocated + sizeof(CBTreeNode); |
| 723 |
*used_mem = used + ((1 + 2*This->nSons) * (sizeof(void *))); |
| 724 |
} |
| 725 |
|
| 726 |
#endif |
| 727 |
|
| 728 |
#undef ClassName |
| 729 |
|
| 730 |
|
| 731 |
/*** BTree class ***/ |
| 732 |
|
| 733 |
#define ClassName CBTree |
| 734 |
Class_Data_Begin |
| 735 |
CN(CBTreeNode) *root; |
| 736 |
int nItems; |
| 737 |
const DDD::DDDContext* context; |
| 738 |
|
| 739 |
/*Compare_Method compare_func;*/ |
| 740 |
Class_Data_End |
| 741 |
Method_New_ (/*Compare_Method*/ _NEWPARAMS_OR_VOID); |
| 742 |
void Method(Free) (DefThis); |
| 743 |
void Method(Print) (DefThis _PRINTPARAMS); |
| 744 |
void Method(Reset) (DefThis); |
| 745 |
int Method(Insert) (DefThis, CN(BTreeOf) *); |
| 746 |
void Method(Iterate) (DefThis, Iterate_Method); |
| 747 |
std::vector<BTreeOf*> Method(GetArray) (DefThis); |
| 748 |
void Method(GetResources) (DefThis, int *, int *, size_t *, size_t *); |
| 749 |
|
| 750 |
|
| 751 |
#ifdef ContainerImplementation |
| 752 |
Method_New_ (/*Compare_Method compare_func*/ _NEWPARAMS_OR_VOID) |
| 753 |
{ |
| 754 |
Construct(This, _CHECKALLOC(This)); |
| 755 |
This->root = NULL; |
| 756 |
This->nItems = 0; |
| 757 |
This->context = nullptr; |
| 758 |
/*This->compare_func = compare_func;*/ |
| 759 |
return(This); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
void Method(Free) (ParamThis) |
| 764 |
{ |
| 765 |
CALL(CBTree, Reset)(This); |
| 766 |
Destruct(This); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
void Method(Print) (ParamThis _PRINTPARAMS) |
| 771 |
{ |
| 772 |
CALL(CBTreeNode,Print) (This->root _PRINTSAME); |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
void Method(Reset) (ParamThis) |
| 777 |
{ |
| 778 |
if (This->root!=NULL) |
| 779 |
{ |
| 780 |
CALL(CBTreeNode,Free) (This->root); |
| 781 |
} |
| 782 |
|
| 783 |
This->root = NULL; |
| 784 |
This->nItems = 0; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
int Method(Insert) (ParamThis, CN(BTreeOf) *item) |
| 789 |
{ |
| 790 |
enum BTreeConstant ret; |
| 791 |
|
| 792 |
if (This->root==NULL) |
| 793 |
{ |
| 794 |
This->root = CALL(New,CBTreeNode) (item,NULL,NULL); |
| 795 |
assert(This->root!=NULL); |
| 796 |
This->nItems++; |
| 797 |
return(true); |
| 798 |
} |
| 799 |
|
| 800 |
ret = CALL(CBTreeNode,Insert) (This->root, /*This->compare_func,*/ item, This->context); |
| 801 |
|
| 802 |
if (ret==BTREE_SPLIT_ME) |
| 803 |
{ |
| 804 |
CBTreeNode *new_l, *new_r; |
| 805 |
CN(BTreeOf) *split_item; |
| 806 |
|
| 807 |
new_l = This->root; |
| 808 |
new_r = CALL(CBTreeNode,Split) (new_l, &split_item); |
| 809 |
assert(new_r!=NULL); |
| 810 |
This->root = CALL(New,CBTreeNode) (split_item, new_l, new_r); |
| 811 |
assert(This->root!=NULL); |
| 812 |
} |
| 813 |
|
| 814 |
if (ret!=BTREE_FOUND) |
| 815 |
This->nItems++; |
| 816 |
|
| 817 |
/* return true if item was really inserted */ |
| 818 |
return(ret!=BTREE_FOUND); |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
void Method(Iterate) (ParamThis, Iterate_Method iter_method) |
| 823 |
{ |
| 824 |
if (This->root!=NULL) |
| 825 |
CALL(CBTreeNode,Iterate) (This->root, iter_method); |
| 826 |
} |
| 827 |
|
| 828 |
|
| 829 |
std::vector<BTreeOf*> Method(GetArray) (ParamThis) |
| 830 |
{ |
| 831 |
std::vector<BTreeOf*> array(This->nItems); |
| 832 |
|
| 833 |
if (This->nItems != 0) { |
| 834 |
BTreeOf** ptr = array.data(); |
| 835 |
CALL(CBTreeNode,Linearize) (This->root, ptr); |
| 836 |
} |
| 837 |
|
| 838 |
return array; |
| 839 |
} |
| 840 |
|
| 841 |
|
| 842 |
void Method(GetResources) (ParamThis, |
| 843 |
int *nNodes, int *nItems, size_t *alloc_mem, size_t *used_mem) |
| 844 |
{ |
| 845 |
size_t allocated=0, used=0; |
| 846 |
int nodes=0; |
| 847 |
|
| 848 |
if (This->root!=NULL) |
| 849 |
{ |
| 850 |
CALL(CBTreeNode,GetResources) (This->root, &nodes, &allocated, &used); |
| 851 |
} |
| 852 |
|
| 853 |
*nNodes = nodes; |
| 854 |
*nItems = This->nItems; |
| 855 |
*alloc_mem = allocated + sizeof(CBTree); |
| 856 |
*used_mem = used + sizeof(CBTree); |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
#endif |
| 861 |
|
| 862 |
#undef ClassName |
| 863 |
|
| 864 |
|
| 865 |
/****************************************************************************/ |
| 866 |
#undef CBTreeNode |
| 867 |
#undef CBTree |
| 868 |
/*#undef Compare_Method*/ |
| 869 |
#undef BTreeOrder |
| 870 |
#undef BTreeOf |
| 871 |
#endif |
| 872 |
/****************************************************************************/ |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
/****************************************************************************/ |
| 877 |
#ifdef SetOf |
| 878 |
#ifndef CSet |
| 879 |
/****************************************************************************/ |
| 880 |
|
| 881 |
/* |
| 882 |
this class is implemented by using a SegmList for storing the actual |
| 883 |
data and a BTree for fast access, sorting and uniqueness. |
| 884 |
*/ |
| 885 |
|
| 886 |
/* class name for set */ |
| 887 |
#define CSet CCAT(SetOf,Set) |
| 888 |
|
| 889 |
|
| 890 |
/* set default values for parameters */ |
| 891 |
#ifndef Set_SegmSize |
| 892 |
#define Set_SegmSize 256 /* default size of each segment in SegmList */ |
| 893 |
#endif |
| 894 |
#ifndef Set_BTreeOrder |
| 895 |
#define Set_BTreeOrder 32 /* default order of BTree */ |
| 896 |
#endif |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
/* instantiate all template types we will need lateron */ |
| 901 |
#define PtrOf SetOf |
| 902 |
#define SegmListOf SetOf |
| 903 |
#define SegmSize Set_SegmSize |
| 904 |
#define BTreeOf SetOf |
| 905 |
#define BTreeOrder Set_BTreeOrder |
| 906 |
|
| 907 |
#include "ooppcc.h" |
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
/****************************************************************************/ |
| 912 |
|
| 913 |
#define Set(T) CN(CCAT(T,Set)) |
| 914 |
|
| 915 |
/* class names for set, segmlist and btree */ |
| 916 |
#define CSet CCAT(SetOf,Set) |
| 917 |
#define CSegmList CCAT(SetOf,SegmList) |
| 918 |
#define CBTree CCAT(SetOf,BTree) |
| 919 |
|
| 920 |
|
| 921 |
/*** Set class ***/ |
| 922 |
|
| 923 |
#define ClassName CSet |
| 924 |
Class_Data_Begin |
| 925 |
CSegmList *list; |
| 926 |
CBTree *tree; |
| 927 |
|
| 928 |
CN(SetOf) *last_item; /* temp storage for last call to NewItem() */ |
| 929 |
Class_Data_End |
| 930 |
Method_New_ (_NEWPARAMS_OR_VOID); |
| 931 |
void Method(Free) (DefThis); |
| 932 |
void Method(Print) (DefThis _PRINTPARAMS); |
| 933 |
void Method(Reset) (DefThis); |
| 934 |
CN(SetOf) *Method(NewItem) (DefThis); |
| 935 |
int Method(ItemOK) (DefThis); |
| 936 |
int Method(GetNItems) (DefThis); |
| 937 |
int Method(GetNDiscarded) (DefThis); |
| 938 |
std::vector<SetOf*> Method(GetArray) (DefThis); |
| 939 |
void Method(GetResources) (DefThis, int *, int *, int *, size_t *, size_t *); |
| 940 |
|
| 941 |
|
| 942 |
#ifdef ContainerImplementation |
| 943 |
|
| 944 |
Method_New_ (_NEWPARAMS_OR_VOID) |
| 945 |
{ |
| 946 |
Construct(This, _CHECKALLOC(This)); |
| 947 |
|
| 948 |
This->list = CALL(New, CSegmList) (); |
| 949 |
assert(This->list!=NULL); |
| 950 |
|
| 951 |
This->tree = CALL(New, CBTree) (); |
| 952 |
assert(This->tree!=NULL); |
| 953 |
|
| 954 |
This->last_item = NULL; |
| 955 |
return(This); |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
void Method(Free) (ParamThis) |
| 960 |
{ |
| 961 |
CALL(CSegmList,Free) (This->list); |
| 962 |
CALL(CBTree,Free) (This->tree); |
| 963 |
Destruct(This); |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
void Method(Print) (ParamThis _PRINTPARAMS) |
| 968 |
{ |
| 969 |
/* CALL(CSegmList,Print) (This->list _PRINTNEXT); */ |
| 970 |
CALL(CBTree,Print) (This->tree _PRINTNEXT); |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
void Method(Reset) (ParamThis) |
| 975 |
{ |
| 976 |
CALL(CSegmList,Reset) (This->list); |
| 977 |
CALL(CBTree,Reset) (This->tree); |
| 978 |
} |
| 979 |
|
| 980 |
|
| 981 |
CN(SetOf) *Method(NewItem) (ParamThis) |
| 982 |
{ |
| 983 |
This->last_item = CALL(CSegmList,NewItem) (This->list); |
| 984 |
return(This->last_item); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
int Method(ItemOK) (ParamThis) |
| 989 |
{ |
| 990 |
if (CALL(CBTree,Insert) (This->tree, This->last_item)) |
| 991 |
{ |
| 992 |
/* item could be inserted in btree, hence it was a new one. */ |
| 993 |
return(true); |
| 994 |
} |
| 995 |
|
| 996 |
/* item was in tree already, it was an old one. */ |
| 997 |
CALL(CSegmList,DiscardItem) (This->list); |
| 998 |
return(false); |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
int Method(GetNItems) (ParamThis) |
| 1003 |
{ |
| 1004 |
return(CALL(CSegmList,GetNItems) (This->list)); |
| 1005 |
} |
| 1006 |
|
| 1007 |
int Method(GetNDiscarded) (ParamThis) |
| 1008 |
{ |
| 1009 |
return(CALL(CSegmList,GetNDiscarded) (This->list)); |
| 1010 |
} |
| 1011 |
|
| 1012 |
|
| 1013 |
std::vector<SetOf*> Method(GetArray) (ParamThis) |
| 1014 |
{ |
| 1015 |
return(CALL(CBTree,GetArray) (This->tree)); |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
void Method(GetResources) (ParamThis, |
| 1020 |
int *nSegms, int *nItems, int *nNodes, size_t *alloc_mem, size_t *used_mem) |
| 1021 |
{ |
| 1022 |
size_t allocated=0, used=0; |
| 1023 |
|
| 1024 |
CALL(CSegmList,GetResources) (This->list, nSegms, nItems, &allocated, &used); |
| 1025 |
*alloc_mem = allocated; |
| 1026 |
*used_mem = used; |
| 1027 |
|
| 1028 |
CALL(CBTree,GetResources) (This->tree, nNodes, nItems, &allocated, &used); |
| 1029 |
*alloc_mem += allocated; |
| 1030 |
*used_mem += used; |
| 1031 |
|
| 1032 |
*alloc_mem += sizeof(CSet); |
| 1033 |
*used_mem += sizeof(CSet); |
| 1034 |
} |
| 1035 |
|
| 1036 |
|
| 1037 |
#endif |
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
#undef ClassName |
| 1042 |
|
| 1043 |
|
| 1044 |
/****************************************************************************/ |
| 1045 |
#undef CSegmList |
| 1046 |
#undef CBTree |
| 1047 |
#undef CSet |
| 1048 |
#undef SetOf |
| 1049 |
#endif |
| 1050 |
#endif |
| 1051 |
/****************************************************************************/ |
| 1052 |
|
| 1053 |
|
| 1054 |
/****************************************************************************/ |