11#include <util/containers/Node.h>
12#include <util/containers/ListIterator.h>
20 template <
typename Data>
class ListIterator;
31 template <
typename Data>
183 friend class ::ListTest;
191 template <
typename Data>
205 template <
typename Data>
209 capacity_ = capacity;
216 template <
typename Data>
223 template <
typename Data>
225 {
return capacity_; }
230 template <
typename Data>
250 template <
typename Data>
272 template <
typename Data>
276 UTIL_THROW(
"Attempted popBack from empty List");
281 if (back_ == front_) {
285 back_ = oldBack.
prev();
287 contractBounds(oldBack);
297 template <
typename Data>
301 UTIL_THROW(
"Attempted popFront from empty List");
306 if (front_ == back_) {
310 front_ = oldFront.
next();
312 contractBounds(oldFront);
322 template <
typename Data>
325 if (&previous == back_) {
329 previous.
next()->attachPrev(newNode);
332 expandBounds(newNode);
339 template <
typename Data>
342 if (&next == front_ ) {
346 next.
prev()->attachNext(newNode);
349 expandBounds(newNode);
356 template <
typename Data>
359 assert( &node.
list() ==
this );
360 if (&node == back_) {
361 if (back_ == front_) {
370 if (&node == front_) {
371 front_ = node.
next();
374 node.
prev()->setNext( node.
next() );
375 node.
next()->setPrev( node.
prev() );
377 contractBounds(node);
385 template <
typename Data>
393 assert( &node.
list() !=
this);
407 if (&node > upper_) {
408 target = upper_->
next();
409 upper_->attachNext(node);
412 if (&node < lower_) {
413 target = lower_->
prev();
427 assert(current > lower_);
429 }
while (¤t->
list() !=
this);
430 target = current->
next();
448 template <
typename Data>
458 template <
typename Data>
464 UTIL_THROW(
"List<Data>::isValid: front_ == 0 and size_ != 0.");
469 UTIL_THROW(
"List<Data>::isValid: front_ == 0 and back_ != 0.");
473 if (lower_ != nodes_ + capacity_) {
474 UTIL_THROW(
"front_ == 0 and lower_ != nodes_+capacity.");
478 if (upper_ != nodes_ -1) {
479 UTIL_THROW(
"List<Data>::isValid: front_ == 0 and upper_ != nodes_-1.");
488 while (node->
next() != 0 ) {
489 if (node->
prev() != prev) {
490 UTIL_THROW(
"List<Data>::isValid: node->prev() != prev.");
493 if (&node->
list() !=
this) {
494 UTIL_THROW(
"List<Data>::isValid: &node->list() != this.");
498 UTIL_THROW(
"List<Data>::isValid: node > upper_.");
502 UTIL_THROW(
"List<Data>::isValid: node < lower_.");
511 UTIL_THROW(
"List<Data>::isValid: # elements < size_.");
515 UTIL_THROW(
"List<Data>::isValid: # elements > size_.");
519 UTIL_THROW(
"List<Data>::isValid: Last node != back_.");
522 if (node->
next() != 0) {
523 UTIL_THROW(
"List<Data>::isValid: Last node, node->next() != 0.");
537 template <
typename Data>
546 lower_ = nodes_ + capacity_ ;
554 template <
typename Data>
555 void List<Data>::expandBounds(Node<Data>& node)
557 if (&node > upper_) {
560 if (&node < lower_) {
568 template <
typename Data>
569 void List<Data>::contractBounds(Node<Data>& node)
571 if (&node == upper_) {
574 }
while ( &(upper_->list()) !=
this );
576 if (&node == lower_) {
579 }
while ( &(lower_->list()) !=
this );
Bidirectional iterator for a List.
void setCurrent(Node< Data > *nodePtr)
Point the iterator at a Node.
Linked list class template.
Node< Data > & popBack()
Remove a node from the back of the list.
void begin(ListIterator< Data > &iterator) const
Set an iterator to the front of this List.
Node< Data > & popFront()
Remove a node from the front of the list.
void insertNext(Node< Data > &node, Node< Data > &newNode)
Insert newNode into list after node.
bool isValid() const
Check validity of linked list.
void remove(Node< Data > &node)
Remove node from list.
void pushFront(Node< Data > &node)
Push a node onto the the front of the List.
int size() const
Get the number of elements.
List()
Default constructor.
void pushBack(Node< Data > &node)
Push a node onto the the back of the List.
virtual ~List()
Destructor (does nothing).
void insert(Node< Data > &node)
Insert node into list in sequential order.
void initialize(Node< Data > *nodes, int capacity)
Provide an array of Node<Data> objects for this List.
void insertPrev(Node< Data > &node, Node< Data > &newNode)
Insert newNode into list before node.
int capacity() const
Get capacity of the array.
Linked List Node, class template.
List< Data > & list() const
Get a reference to the List.
void setPrev(Node< Data > *prev)
Set pointer to the previous Node.
void clear()
Nullify previous and next pointers, and nullify the list pointer.
Node< Data > * next() const
Get the next pointer.
void attachPrev(Node< Data > &other)
Set pointers connecting the other node before this node.
void setList(List< Data > &list)
Set the list.
void attachNext(Node< Data > &other)
Set pointers connecting the other node after this node.
void setNext(Node< Data > *next)
Set pointer to the next Node.
Node< Data > * prev() const
Get the previous pointer.
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.