/usr/include/dune/common/sllist.hh is in libdune-common-dev 2.5.0-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 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_SLLIST_HH
#define DUNE_SLLIST_HH
#include <memory>
#include <cassert>
#include "iteratorfacades.hh"
#include <ostream>
namespace Dune
{
/**
* @addtogroup Common
*
* @{
*/
/**
* @file
* \brief Implements a singly linked list together with
* the necessary iterators.
* @author Markus Blatt
*/
template<typename T, class A>
class SLListIterator;
template<typename T, class A>
class SLListConstIterator;
template<typename T, class A>
class SLListModifyIterator;
/**
* @brief A single linked list.
*
* The list is capable of insertions at the front and at
* the end and of removing elements at the front. Those
* operations require constant time.
*/
template<typename T, class A=std::allocator<T> >
class SLList
{
struct Element;
friend class SLListIterator<T,A>;
friend class SLListConstIterator<T,A>;
public:
/**
* @brief The size type.
*/
typedef typename A::size_type size_type;
/**
* @brief The type we store.
*/
typedef T MemberType;
/**
* @brief The allocator to use.
*/
typedef typename A::template rebind<Element>::other Allocator;
/**
* @brief The mutable iterator of the list.
*/
typedef SLListIterator<T,A> iterator;
/**
* @brief The constant iterator of the list.
*/
typedef SLListConstIterator<T,A> const_iterator;
/**
* @brief Constructor.
*/
SLList();
/**
* @brief Copy constructor with type conversion.
*/
template<typename T1, typename A1>
SLList(const SLList<T1,A1>& other);
/**
* @brief Copy constructor.
*/
SLList(const SLList<T,A>& other);
/**
* @brief Destructor.
*
* Deallocates all elements in the list.
*/
~SLList();
/**
* @brief The type of the iterator capable of deletion
* and insertion.
*/
typedef SLListModifyIterator<T,A> ModifyIterator;
/**
* @brief Assignment operator.
*/
SLList<T,A>& operator=(const SLList<T,A>& other);
/**
* @brief Add a new entry to the end of the list.
* @param item The item to add.
*/
inline void push_back(const MemberType& item);
/**
* @brief Add a new entry to the beginning of the list.
* @param item The item to add.
*/
inline void push_front(const MemberType& item);
/**
* @brief Remove the first item in the list.
*/
inline void pop_front();
/** @brief Remove all elements from the list. */
inline void clear();
/**
* @brief Get an iterator pointing to the first
* element in the list.
*
* @return An iterator pointing to the first
* element or the end if the list is empty.
*/
inline iterator begin();
/**
* @brief Get an iterator pointing to the first
* element in the list.
*
* @return An iterator pointing to the first
* element or the end if the list is empty.
*/
inline const_iterator begin() const;
/**
* @brief Get an iterator capable of deleting and
* inserting elements.
*
* @return Modifying iterator positioned at the beginning
* of the list.
*/
inline ModifyIterator beginModify();
/**
* @brief Get an iterator capable of deleting and
* inserting elements.
*
* @return Modifying iterator positioned after the end
* of the list.
*/
inline ModifyIterator endModify();
/**
* @brief Get an iterator pointing to the
* end of the list.
*
* @return An iterator pointing to the end.
*/
inline iterator end();
/**
* @brief Get an iterator pointing to the
* end of the list.
*
* @return An iterator pointing to the end.
*/
inline const_iterator end() const;
/**
* @brief Check whether the list is empty.
*
* @return True if the list is empty;
*/
inline bool empty() const;
/**
* @brief Get the number of elements the list
* contains.
*/
inline int size() const;
bool operator==(const SLList& sl) const;
bool operator!=(const SLList& sl) const;
private:
/** \todo Please doc me! */
struct Element
{
/**
* @brief The next element in the list.
*/
Element* next_;
/**
* @brief The element we hold.
*/
MemberType item_;
Element(const MemberType& item, Element* next_=0);
Element();
~Element();
};
/**
* @brief Delete the next element in the list.
* @param current Element whose next element should be deleted.
*/
void deleteNext(Element* current);
/**
* @brief Copy the elements from another list.
* @param other The other list.
*/
void copyElements(const SLList<T,A>& other);
/**
* @brief Delete the next element in the list.
*
* If the template parameter watchForTail is true, it is checked whether
* the deleted element is the tail and therefore the tail must be updated.
* @param current Element whose next element should be deleted.
*/
template<bool watchForTail>
void deleteNext(Element* current);
/**
* @brief Insert an element after another one in the list.
* @param current The element after which we insert.
* @param item The item to insert.
*/
void insertAfter(Element* current, const T& item);
/** @brief Pseudo element before the first entry. */
Element beforeHead_;
/**
* @brief Pointer to he last element in the list.
*
* If list is empty this will point to beforeHead_
*/
Element* tail_;
/** @brief The allocator we use. */
Allocator allocator_;
/** brief The number of elements the list holds. */
int size_;
};
/**
* @brief A mutable iterator for the SLList.
*/
template<typename T, class A>
class SLListIterator : public Dune::ForwardIteratorFacade<SLListIterator<T,A>, T, T&, std::size_t>
{
friend class SLListConstIterator<T,A>;
friend class SLListModifyIterator<T,A>;
friend class SLList<T,A>;
public:
inline SLListIterator(typename SLList<T,A>::Element* item,
SLList<T,A>* sllist)
: current_(item), list_(sllist)
{}
inline SLListIterator()
: current_(0), list_(0)
{}
inline SLListIterator(const SLListModifyIterator<T,A>& other)
: current_(other.iterator_.current_), list_(other.iterator_.list_)
{}
/**
* @brief Dereferencing function for the iterator facade.
* @return A reference to the element at the current position.
*/
inline T& dereference() const
{
return current_->item_;
}
/**
* @brief Equality test for the iterator facade.
* @param other The other iterator to check.
* @return true If the other iterator is at the same position.
*/
inline bool equals(const SLListConstIterator<T,A>& other) const
{
return current_==other.current_;
}
/**
* @brief Equality test for the iterator facade.
* @param other The other iterator to check.
* @return true If the other iterator is at the same position.
*/
inline bool equals(const SLListIterator<T,A>& other) const
{
return current_==other.current_;
}
/**
* @brief Equality test for the iterator facade.
* @param other The other iterator to check.
* @return true If the other iterator is at the same position.
*/
inline bool equals(const SLListModifyIterator<T,A>& other) const
{
return current_==other.iterator_.current_;
}
/**
* @brief Increment function for the iterator facade.
*/
inline void increment()
{
current_ = current_->next_;
}
/**
* @brief Insert an element in the underlying list after
* the current position.
* @param v The value to insert.
*/
inline void insertAfter(const T& v) const
{
assert(list_ );
list_->insertAfter(current_, v);
}
/**
* @brief Delete the entry after the current position.
*
* @warning This will invalidate all iterators positioned at the delete position! Use with care!
*/
inline void deleteNext() const
{
assert(list_);
list_->deleteNext(current_);
}
private:
/** @brief The current element. */
typename SLList<T,A>::Element* current_;
/** @brief The list we iterate over. */
SLList<T,A>* list_;
};
/**
* @brief A constant iterator for the SLList.
*/
template<class T, class A>
class SLListConstIterator : public Dune::ForwardIteratorFacade<SLListConstIterator<T,A>, const T, const T&, std::size_t>
{
friend class SLListIterator<T,A>;
friend class SLList<T,A>;
public:
inline SLListConstIterator()
: current_(0)
{}
inline SLListConstIterator(typename SLList<T,A>::Element* item)
: current_(item)
{}
inline SLListConstIterator(const SLListIterator<T,A>& other)
: current_(other.current_)
{}
inline SLListConstIterator(const SLListConstIterator<T,A>& other)
: current_(other.current_)
{}
inline SLListConstIterator(const SLListModifyIterator<T,A>& other)
: current_(other.iterator_.current_)
{}
/**
* @brief Dereferencing function for the facade.
* @return A reference to the element at the current position.
*/
inline const T& dereference() const
{
return current_->item_;
}
/**
* @brief Equality test for the iterator facade.
* @param other The other iterator to check.
* @return true If the other iterator is at the same position.
*/
inline bool equals(const SLListConstIterator<T,A>& other) const
{
return current_==other.current_;
}
/**
* @brief Increment function for the iterator facade.
*/
inline void increment()
{
current_ = current_->next_;
}
private:
/** @brief The current element. */
typename SLList<T,A>::Element* current_;
};
/**
* @brief A mutable iterator for the SLList.
*/
template<typename T, class A>
class SLListModifyIterator : public Dune::ForwardIteratorFacade<SLListModifyIterator<T,A>, T, T&, std::size_t>
{
friend class SLListConstIterator<T,A>;
friend class SLListIterator<T,A>;
public:
inline SLListModifyIterator(SLListIterator<T,A> beforeIterator,
SLListIterator<T,A> _iterator)
: beforeIterator_(beforeIterator), iterator_(_iterator)
{}
inline SLListModifyIterator(const SLListModifyIterator<T,A>& other)
: beforeIterator_(other.beforeIterator_), iterator_(other.iterator_)
{}
inline SLListModifyIterator()
: beforeIterator_(), iterator_()
{}
/**
* @brief Dereferencing function for the iterator facade.
* @return A reference to the element at the current position.
*/
inline T& dereference() const
{
return *iterator_;
}
/**
* @brief Test whether another iterator is equal.
* @return true if the other iterator is at the same position as
* this one.
*/
inline bool equals(const SLListConstIterator<T,A>& other) const
{
return iterator_== other;
}
/**
* @brief Test whether another iterator is equal.
* @return true if the other iterator is at the same position as
* this one.
*/
inline bool equals(const SLListIterator<T,A>& other) const
{
return iterator_== other;
}
/**
* @brief Test whether another iterator is equal.
* @return true if the other iterator is at the same position as
* this one.
*/
inline bool equals(const SLListModifyIterator<T,A>& other) const
{
return iterator_== other.iterator_;
}
/**
* @brief Increment function for the iterator facade.
*/
inline void increment()
{
++iterator_;
++beforeIterator_;
}
/**
* @brief Insert an element at the current position.
*
* Starting from the element at the current position all
* elements will be shifted by one position to the back.
* The iterator will point to the same element as before
* after the insertion, i.e the number of increments to
* reach the same position from a begin iterator increases
* by one.
* This means the inserted element is the one before the one
* the iterator points to.
* @param v The value to insert.
*/
inline void insert(const T& v)
{
beforeIterator_.insertAfter(v);
++beforeIterator_;
}
/**
* @brief Delete the entry at the current position.
*
* The iterator will be positioned at the next position after the
* deletion
* @warning This will invalidate all iterators positioned at the delete position! Use with care!
*/
inline void remove()
{
++iterator_;
beforeIterator_.deleteNext();
}
private:
/** @brief Iterator positioned at the position before the current. */
SLListIterator<T,A> beforeIterator_;
/** @brief Iterator positioned at the current position. */
SLListIterator<T,A> iterator_;
};
} // namespace Dune
namespace std
{
template<typename T, typename A>
ostream& operator<<(ostream& os, const Dune::SLList<T,A> sllist)
{
typedef typename Dune::SLList<T,A>::const_iterator Iterator;
Iterator end = sllist.end();
Iterator current= sllist.begin();
os << "{ ";
if(current!=end) {
os<<*current<<" ("<<static_cast<const void*>(&(*current))<<")";
++current;
for(; current != end; ++current)
os<<", "<<*current<<" ("<<static_cast<const void*>(&(*current))<<")";
}
os<<"} ";
return os;
}
} //namespace std
namespace Dune
{
template<typename T, class A>
SLList<T,A>::Element::Element(const MemberType& item, Element* next)
: next_(next), item_(item)
{}
template<typename T, class A>
SLList<T,A>::Element::Element()
: next_(0), item_()
{}
template<typename T, class A>
SLList<T,A>::Element::~Element()
{
next_=0;
}
template<typename T, class A>
SLList<T,A>::SLList()
: beforeHead_(), tail_(&beforeHead_), allocator_(), size_(0)
{
beforeHead_.next_=0;
assert(&beforeHead_==tail_);
assert(tail_->next_==0);
}
template<typename T, class A>
SLList<T,A>::SLList(const SLList<T,A>& other)
: beforeHead_(), tail_(&beforeHead_), allocator_(), size_(0)
{
copyElements(other);
}
template<typename T, class A>
template<typename T1, class A1>
SLList<T,A>::SLList(const SLList<T1,A1>& other)
: beforeHead_(), tail_(&beforeHead_), allocator_(), size_(0)
{
copyElements(other);
}
template<typename T, typename A>
void SLList<T,A>::copyElements(const SLList<T,A>& other)
{
assert(tail_==&beforeHead_);
assert(size_==0);
typedef typename SLList<T,A>::const_iterator Iterator;
Iterator iend = other.end();
for(Iterator element=other.begin(); element != iend; ++element)
push_back(*element);
assert(other.size()==size());
}
template<typename T, class A>
SLList<T,A>::~SLList()
{
clear();
}
template<typename T, class A>
bool SLList<T,A>::operator==(const SLList& other) const
{
if(size()!=other.size())
return false;
for(const_iterator iter=begin(), oiter=other.begin();
iter != end(); ++iter, ++oiter)
if(*iter!=*oiter)
return false;
return true;
}
template<typename T, class A>
bool SLList<T,A>::operator!=(const SLList& other) const
{
if(size()==other.size()) {
for(const_iterator iter=begin(), oiter=other.begin();
iter != end(); ++iter, ++oiter)
if(*iter!=*oiter)
return true;
return false;
}else
return true;
}
template<typename T, class A>
SLList<T,A>& SLList<T,A>::operator=(const SLList<T,A>& other)
{
clear();
copyElements(other);
return *this;
}
template<typename T, class A>
inline void SLList<T,A>::push_back(const MemberType& item)
{
assert(size_>0 || tail_==&beforeHead_);
tail_->next_ = allocator_.allocate(1, 0);
assert(size_>0 || tail_==&beforeHead_);
tail_ = tail_->next_;
::new (static_cast<void*>(&(tail_->item_)))T(item);
tail_->next_=0;
assert(tail_->next_==0);
++size_;
}
template<typename T, class A>
inline void SLList<T,A>::insertAfter(Element* current, const T& item)
{
assert(current);
#ifndef NDEBUG
bool changeTail = (current == tail_);
#endif
// Save old next element
Element* tmp = current->next_;
assert(!changeTail || !tmp);
// Allocate space
current->next_ = allocator_.allocate(1, 0);
// Use copy constructor to initialize memory
allocator_.construct(current->next_, Element(item,tmp));
//::new(static_cast<void*>(&(current->next_->item_))) T(item);
if(!current->next_->next_) {
// Update tail
assert(changeTail);
tail_ = current->next_;
}
++size_;
assert(!tail_->next_);
}
template<typename T, class A>
inline void SLList<T,A>::push_front(const MemberType& item)
{
if(tail_ == &beforeHead_) {
// list was empty
beforeHead_.next_ = tail_ = allocator_.allocate(1, 0);
::new(static_cast<void*>(&beforeHead_.next_->item_))T(item);
beforeHead_.next_->next_=0;
}else{
Element* added = allocator_.allocate(1, 0);
::new(static_cast<void*>(&added->item_))T(item);
added->next_=beforeHead_.next_;
beforeHead_.next_=added;
}
assert(tail_->next_==0);
++size_;
}
template<typename T, class A>
inline void SLList<T,A>::deleteNext(Element* current)
{
this->template deleteNext<true>(current);
}
template<typename T, class A>
template<bool watchForTail>
inline void SLList<T,A>::deleteNext(Element* current)
{
assert(current->next_);
Element* next = current->next_;
if(watchForTail)
if(next == tail_) {
// deleting last element changes tail!
tail_ = current;
}
current->next_ = next->next_;
allocator_.destroy(next);
allocator_.deallocate(next, 1);
--size_;
assert(!watchForTail || &beforeHead_ != tail_ || size_==0);
}
template<typename T, class A>
inline void SLList<T,A>::pop_front()
{
deleteNext(&beforeHead_);
}
template<typename T, class A>
inline void SLList<T,A>::clear()
{
while(beforeHead_.next_ ) {
this->template deleteNext<false>(&beforeHead_);
}
assert(size_==0);
// update the tail!
tail_ = &beforeHead_;
}
template<typename T, class A>
inline bool SLList<T,A>::empty() const
{
return (&beforeHead_ == tail_);
}
template<typename T, class A>
inline int SLList<T,A>::size() const
{
return size_;
}
template<typename T, class A>
inline SLListIterator<T,A> SLList<T,A>::begin()
{
return iterator(beforeHead_.next_, this);
}
template<typename T, class A>
inline SLListConstIterator<T,A> SLList<T,A>::begin() const
{
return const_iterator(beforeHead_.next_);
}
template<typename T, class A>
inline SLListIterator<T,A> SLList<T,A>::end()
{
return iterator();
}
template<typename T, class A>
inline SLListModifyIterator<T,A> SLList<T,A>::endModify()
{
return SLListModifyIterator<T,A>(iterator(tail_, this),iterator());
}
template<typename T, class A>
inline SLListModifyIterator<T,A> SLList<T,A>::beginModify()
{
return SLListModifyIterator<T,A>(iterator(&beforeHead_, this),
iterator(beforeHead_.next_, this));
}
template<typename T, class A>
inline SLListConstIterator<T,A> SLList<T,A>::end() const
{
return const_iterator();
}
/** }@ */
}
#endif
|