/usr/include/ptrList.h is in libsnl-dev 0.2.1.svn.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 | /*
* Copyright (C) 2000 Scott A.E. Lanham.
* -------------------------------------
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// *** Doubly linked List of Pointers Template ***
#ifndef PTRLIST_H
#define PTRLIST_H
template < class T > class ptrListItem
{
// Linked list of pointers to type T
public:
virtual ~ptrListItem();
ptrListItem();
ptrListItem ( const ptrListItem<T>& item );
ptrListItem ( T* ptr, ptrListItem<T>* chain, bool delObjPtdTo, bool insert = false );
// Operators
ptrListItem<T>& operator= ( const ptrListItem<T>& item );
// Linked list functions.
ptrListItem<T>* next();
ptrListItem<T>* prev();
ptrListItem<T>* first();
ptrListItem<T>* last();
ptrListItem<T>* atIndex ( int index );
void replace ( T* ptr, bool delObjPtdTo );
bool hasItem ( T* ptr, bool traverse );
virtual ptrListItem<T>* getItem ( T* ptr );
T* getPtr();
virtual unsigned count(); // Number of items in list.
void link ( ptrListItem<T>* linkTo, bool next ); // Link a node into chain after this one.
void unlink(); // Unlink this node from chain.
void unlinkPrev ( ptrListItem<T>* newPrev );
void unlinkNext ( ptrListItem<T>* newNext );
void cascadeDelete(); // Whole list is being deleted.
protected:
void setNext ( ptrListItem<T>* next );
void setPrevious ( ptrListItem<T>* prev );
T* pointer;
bool delObj; // Delete the object this item points to when item is deleted.
ptrListItem<T>* prevLnk; // Linked list.
ptrListItem<T>* nextLnk;
};
template < class T > class ptrList
{
// Container for ptrListItem<T>
public:
virtual ~ptrList();
ptrList();
ptrList ( const ptrList<T>& list );
ptrList<T>& operator= ( const ptrList<T>& list );
void copyFrom ( const ptrList<T>& list );
T* first();
T* last();
T* prev();
T* next();
T* current();
T* atIndex ( int index );
virtual void clear();
virtual void truncate();
virtual void append ( T* ptr, bool delObj );
virtual void insert ( T* ptr, int index, bool delObj );
virtual void insert ( T* ptr, T* before, bool delObj );
virtual void replace ( T* ptr, bool delObj );
virtual void move ( T* ptr, int toIndex );
virtual void move ( T* ptr, T* after );
virtual void remove ( T* ptr );
virtual void remove();
virtual bool hasItem ( T* ptr );
virtual int itemIndex ( T* ptr );
virtual unsigned count();
protected:
ptrListItem<T>* cItem; // Current ptrListItem.
ptrListItem<T>* fItem; // First item in list.
};
// *** Implementation - Appears here so any type can be used ***
template < class T > ptrListItem<T>::~ptrListItem()
{
// Delete object pointed to if required.
if ( delObj && pointer ) delete pointer;
// Unlink this item from chain.
unlink();
}
template < class T > ptrListItem<T>::ptrListItem()
{
pointer = 0;
prevLnk = 0;
nextLnk = 0;
}
template < class T > ptrListItem<T>::ptrListItem ( const ptrListItem<T>& item )
{
// Copy constructor.
// -----------------
// Notes: Copies pointer NOT the object being pointed to.
pointer = item.pointer;
delObj = false; // The pointer is being copied so don't mark it for deletion.
prevLnk = 0;
nextLnk = 0;
}
template < class T > ptrListItem<T>::ptrListItem ( T* ptr, ptrListItem<T>* chain, bool delObjPtdTo, bool insert )
{
// Constructor
// -----------
// ptr: Pointer to store.
// chain: Root (start) of ptrListItem linked list.
// delObjPtdTo: Delete object this item points to when item is deleted.
// insert: Insert into list instead of the default which is to append at the end.
pointer = ptr;
delObj = delObjPtdTo;
// Set next link.
if ( insert && chain )
{
nextLnk = chain;
}
else
{
// Put at end of chain.
nextLnk = 0;
}
// Set previous link.
if ( chain )
{
if ( insert )
{
if ( chain -> prev() )
( chain -> prev() ) -> setNext ( this );
prevLnk = chain -> prev();
chain -> setPrevious ( this );
}
else
{
prevLnk = chain -> last();
// Link into chain.
( chain -> last() ) -> setNext ( this );
}
}
else
prevLnk = 0;
}
template < class T > ptrListItem<T>& ptrListItem<T>::operator= ( const ptrListItem<T>& item )
{
// Assignment Operator.
// --------------------
if ( this != & item )
{
if ( delObj && pointer ) delete pointer;
// Assume control of the pointer but don't use items linkage.
pointer = item.pointer;
delObj = item.delObj;
item.delObj = false;
}
return *this;
}
template < class T > ptrListItem<T>* ptrListItem<T>::next()
{
return nextLnk;
}
template < class T > ptrListItem<T>* ptrListItem<T>::prev()
{
return prevLnk;
}
template < class T > ptrListItem<T>* ptrListItem<T>::first()
{
ptrListItem* cSelect;
ptrListItem* rSelect;
if ( prevLnk )
cSelect = prevLnk;
else
{
cSelect = 0;
rSelect = this;
}
while ( cSelect )
{
rSelect = cSelect;
cSelect = cSelect -> prev();
}
return rSelect;
}
template < class T > ptrListItem<T>* ptrListItem<T>::last()
{
ptrListItem* cSelect;
ptrListItem* rSelect;
if ( nextLnk )
cSelect = nextLnk;
else
{
cSelect = 0;
rSelect = this;
}
while ( cSelect )
{
rSelect = cSelect;
cSelect = cSelect -> next();
}
return rSelect;
}
template < class T > ptrListItem<T>* ptrListItem<T>::atIndex ( int index )
{
// Return item at index.
// ---------------------
ptrListItem<T>* itm = first();
int count = 0;
while ( itm && count < index )
{
count ++;
itm = itm -> next();
}
return itm;
}
template < class T > bool ptrListItem<T>::hasItem ( T* ptr, bool traverse )
{
// Does the chain of items hold given pointer
// ------------------------------------------
// ptr: Pointer to item.
// traverse: Move forward through primSelects linked by the "next" variable.
if ( ptr == pointer ) return true; // Don't process anymore already found match.
if ( ! traverse ) return false; // No point in continuing traverse is false.
bool found = false;
ptrListItem<T>* itm = next();
while ( itm && ( ! found ) )
{
found = itm -> hasItem ( ptr, false );
itm = itm -> next();
}
return found;
}
template < class T > ptrListItem<T>* ptrListItem<T>::getItem ( T* ptr )
{
// Get item if pointer is part of list
// -----------------------------------
// ptr: Pointer to search for.
//
// Notes: Only a forward traversal is performed.
if ( ptr == pointer ) return this; // Don't process anymore already found match.
bool found = false;
ptrListItem<T>* itemFound = 0;
ptrListItem<T>* itm = next();
while ( itm && ( ! found ) )
{
found = itm -> hasItem ( ptr, false );
if ( found ) itemFound = itm;
itm = itm -> next();
}
return itemFound;
}
template < class T > unsigned ptrListItem<T>::count()
{
// Return number of nodes in list
// ------------------------------
ptrListItem<T>* itm = first();
unsigned count = 0;
while ( itm )
{
count ++;
itm = itm -> next();
}
return count;
}
template < class T > void ptrListItem<T>::setNext ( ptrListItem<T>* next )
{
// Set the "next" node of this node.
// ---------------------------------
if ( next == nextLnk ) return; // Stops recursive endless loop.
ptrListItem<T>* oldNext = nextLnk;
nextLnk = next;
// If there is already a next node reset it's previous to the new node.
if ( oldNext ) oldNext -> setPrevious ( next );
}
template < class T > void ptrListItem<T>::setPrevious ( ptrListItem<T>* prev )
{
// Set the previous node to "prev"
// -------------------------------
if ( prev == prevLnk ) return;
ptrListItem<T>* oldPrev = prevLnk;
prevLnk = prev;
// If there is already a previous node then reset it's next to this node.
if ( oldPrev ) oldPrev -> setNext ( prev );
}
template < class T > void ptrListItem<T>::link ( ptrListItem<T>* linkTo, bool next )
{
// Link a node into chain after this one.
// --------------------------------------
// linkTo: Node to link to.
// next: If true then link node after this one otherwise before this one.
if ( next )
{
if ( nextLnk )
{
linkTo -> setNext ( nextLnk );
nextLnk -> setPrevious ( linkTo );
}
linkTo -> setPrevious ( this );
nextLnk = linkTo;
}
else
{
if ( prevLnk )
{
linkTo -> setPrevious ( prevLnk );
prevLnk -> setNext ( linkTo );
}
linkTo -> setNext ( this );
prevLnk = linkTo;
}
}
template < class T > void ptrListItem<T>::unlink()
{
// Unlink this object from chain
// -----------------------------
if ( prevLnk ) prevLnk -> unlinkNext ( nextLnk );
if ( nextLnk ) nextLnk -> unlinkPrev ( prevLnk );
nextLnk = 0;
prevLnk = 0;
}
template < class T > void ptrListItem<T>::unlinkNext ( ptrListItem<T>* newNext )
{
// Unlink the next object in chain
// -------------------------------
// newNext: The object which is now being linked as next.
nextLnk = newNext;
}
template < class T > void ptrListItem<T>::unlinkPrev ( ptrListItem<T>* newPrev )
{
// Unlink the prev object in chain
// -------------------------------
// newPrev: The object which is now being linked as prev.
prevLnk = newPrev;
}
template < class T > T* ptrListItem<T>::getPtr()
{
return pointer;
}
template < class T > void ptrListItem<T>::cascadeDelete()
{
// Cascade down the linked list to delete entire list.
// ---------------------------------------------------
if ( nextLnk )
{
nextLnk -> cascadeDelete();
delete nextLnk;
}
}
template < class T > void ptrListItem<T>::replace ( T* ptr, bool delObjPtdTo )
{
// Replace object pointed to with ptr.
// -----------------------------------
// ptr: New object to point to.
// delObjPtdTo: Owns pointer and can / should delete it.
if ( pointer && delObj )
delete pointer;
pointer = ptr;
delObj = delObjPtdTo;
}
// *** Pointer List Object ***
template < class T > ptrList<T>::~ptrList()
{
if ( ! cItem ) return;
// Delete all associated items.
ptrListItem<T>* delItem = cItem -> last();
ptrListItem<T>* prevItem;
while ( delItem )
{
prevItem = delItem -> prev();
delete delItem;
delItem = prevItem;
}
}
template < class T > ptrList<T>::ptrList()
{
cItem = 0;
fItem = 0;
}
template < class T > ptrList<T>::ptrList ( const ptrList<T>& list )
{
// Copy Constructor.
// -----------------
copyFrom ( list );
}
template < class T > ptrList<T>& ptrList<T>::operator= ( const ptrList<T>& list )
{
clear();
copyFrom ( list );
}
template < class T > void ptrList<T>::copyFrom ( const ptrList<T>& list )
{
// Copy contents of list into this.
// --------------------------------
cItem = 0;
fItem = 0;
ptrListItem<T>* itemToCopy = list.fItem;
ptrListItem<T>* newItem;
ptrListItem<T>* newCItem;
// Copy pointers across but they can't be automatically deleted.
while ( itemToCopy )
{
newItem = new ptrListItem<T> ( itemToCopy -> getPtr(), cItem, false, false );
if ( ! fItem ) fItem = newItem;
if ( itemToCopy == list.cItem ) newCItem = newItem;
itemToCopy = itemToCopy -> next();
}
// Point new list to same position.
cItem = newCItem;
}
template < class T > void ptrList<T>::clear()
{
// Delete all items at once
// ------------------------
if ( ! cItem ) return;
ptrListItem<T>* delItem = cItem -> last();
ptrListItem<T>* prevItem;
while ( delItem )
{
prevItem = delItem -> prev();
delete delItem;
delItem = prevItem;
}
cItem = 0;
fItem = 0;
}
template < class T > void ptrList<T>::truncate()
{
// Delete all items after current item.
if ( cItem )
cItem -> cascadeDelete();
}
template < class T > void ptrList<T>::append ( T* ptr, bool delObj )
{
// Append pointer to list
// ----------------------
// ptr: Pointer to append.
// delObj: Delete object ptr points to when list item is deleted.
ptrListItem<T>* item = new ptrListItem<T> ( ptr, cItem, delObj );
cItem = item;
if ( ! item -> prev() ) fItem = item;
}
template < class T > void ptrList<T>::insert ( T* ptr, int index, bool delObj )
{
// Insert pointer into list
// ------------------------
// ptr: Pointer to append.
// index: Index to insert new pointer into.
// delObj: Delete object ptr points to when list item is deleted.
ptrListItem<T>* item;
if ( this -> atIndex ( index ) )
item = new ptrListItem<T> ( ptr, cItem, delObj, true );
else
item = new ptrListItem<T> ( ptr, cItem, delObj, false );
cItem = item;
if ( ! item -> prev() ) fItem = item;
}
template < class T > void ptrList<T>::insert ( T* ptr, T* before, bool delObj )
{
// Insert pointer into list
// ------------------------
// ptr: Pointer to append.
// after: Insert new pointer after given pointer.
// delObj: Delete object ptr points to when list item is deleted.
if ( ! cItem )
{
this -> append ( ptr, delObj );
return;
}
ptrListItem<T>* insertAt = cItem -> getItem ( ptr );
ptrListItem<T>* item;
if ( insertAt )
item = new ptrListItem<T> ( ptr, insertAt, delObj, true );
else
item = new ptrListItem<T> ( ptr, cItem, delObj, false );
cItem = item;
if ( ! item -> prev() ) fItem = item;
}
template < class T > void ptrList<T>::replace ( T* ptr, bool delObj )
{
// Replace current items pointer with ptr.
// ---------------------------------------
// ptr: Pointer to now use.
// delObj: List owns object.
if ( cItem )
cItem -> replace ( ptr, delObj );
}
template < class T > void ptrList<T>::move ( T* ptr, int toIndex )
{
// Move pointer to different position in list.
// -------------------------------------------
// ptr: Pointer to move within list.
// toIndex: Index to move item to.
if ( ! cItem ) return;
ptrListItem<T>* item = cItem -> getItem ( ptr );
if ( item )
{
ptrListItem<T>* itemAtPosition = cItem -> atIndex ( toIndex );
if ( item != itemAtPosition )
{
item -> unlink();
itemAtPosition -> link ( item, false );
}
if ( ! item -> prev() ) fItem = item;
}
}
template < class T > void ptrList<T>::move ( T* ptr, T* toPositionOf )
{
// Move pointer to different position in list.
// -------------------------------------------
// ptr: Pointer to move within list.
// toPositionOf: Move into position in list that this pointer occupies.
if ( ! cItem ) return;
if ( ptr == toPositionOf ) return;
ptrListItem<T>* itemToMove = cItem -> getItem ( ptr );
ptrListItem<T>* itemAtPosition = cItem -> getItem ( toPositionOf );
if ( itemToMove && itemAtPosition )
{
itemToMove -> unlink();
itemAtPosition -> link ( itemToMove, false );
cItem = itemToMove;
if ( ! itemToMove -> prev() ) fItem = itemToMove;
}
}
template < class T > void ptrList<T>::remove ( T* ptr )
{
// Remove pointer from list
// ------------------------
// ptr: Pointer to remove.
if ( cItem )
{
ptrListItem<T>* item = fItem -> getItem ( ptr );
if ( item )
{
if ( cItem == item )
{
// About to delete the current item so get new cItem.
if ( item -> prev() )
cItem = item -> prev(); // Make current item previous in list.
else
cItem = item -> next(); // If no previous in list then make current item next in list.
}
delete item;
}
if ( cItem )
if ( ! cItem -> prev() ) fItem = cItem;
else
fItem = 0;
}
}
template < class T > void ptrList<T>::remove()
{
// Remove current item from list
// -----------------------------
if ( ! cItem ) return;
ptrListItem<T>* prevItem = cItem -> prev();
ptrListItem<T>* nextItem = cItem -> next();
delete cItem;
if ( nextItem )
{
cItem = nextItem;
if ( !prevItem ) fItem = nextItem;
}
else if ( prevItem )
cItem = prevItem;
else
{
cItem = 0;
fItem = 0;
}
}
template < class T > T* ptrList<T>::first()
{
if ( cItem )
{
cItem = fItem;
return ( cItem -> getPtr() );
}
return 0;
}
template < class T > T* ptrList<T>::last()
{
if ( cItem )
{
ptrListItem<T>* item = cItem -> last();
cItem = item;
return ( item -> getPtr() );
}
return 0;
}
template < class T > T* ptrList<T>::prev()
{
if ( cItem )
{
ptrListItem<T>* item = cItem -> prev();
if ( item )
{
cItem = item;
return ( item -> getPtr() );
}
else
return 0;
}
return 0;
}
template < class T > T* ptrList<T>::next()
{
if ( cItem )
{
ptrListItem<T>* item = cItem -> next();
if ( item )
{
cItem = item;
return ( item -> getPtr() );
}
else
return 0;
}
return 0;
}
template < class T > unsigned ptrList<T>::count()
{
if ( cItem )
return ( cItem -> count() );
return 0;
}
template < class T > T* ptrList<T>::current()
{
// Get current item
// ----------------
if ( cItem )
return ( cItem -> getPtr() );
return 0;
}
template < class T > T* ptrList<T>::atIndex ( int index )
{
// Make item at index current and return it.
// -----------------------------------------
// index: Index of pointer to find. Index 0 is first item in list.
if ( cItem )
{
ptrListItem<T>* item = cItem -> atIndex ( index );
if ( item )
{
cItem = item;
return ( item -> getPtr() );
}
else
return 0;
}
return 0;
}
template < class T > bool ptrList<T>::hasItem ( T* ptr )
{
// Return True if Item "ptr" is Part of List
// -----------------------------------------
if ( cItem )
{
return ( cItem -> first() ) -> hasItem ( ptr, true );
}
return false;
}
template < class T > int ptrList<T>::itemIndex ( T* ptr )
{
// Find Item's Index.
// ------------------
if ( ! cItem ) return -1;
ptrListItem<T>* item = cItem -> first();
int index = 0;
while ( item && ( item -> getPtr() != ptr ) )
{
index ++;
item = item -> next();
}
return index;
}
#endif
|