Experimental Code
Code Written To Experiment With Various Techniques And Otherwise Not Very Useful...
DoubleLinkedListImpl.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2013, Komodo Does Inc
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7 - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 - Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 
14 #ifndef DOUBLE_LINKED_LIST_IMPL_H
15 #define DOUBLE_LINKED_LIST_IMPL_H
16 
17 /** \file
18  * Implementation details of a DoubleLinkedList
19  */
20 
21 namespace Experiment {
22 
23  namespace DoubleLinkedListImpl {
24 
25  /** DoubleLinkedList internal Node base class.
26  *
27  * This is a common implementation with DataNode which adds the data value.
28  *
29  * DoubleLinkedList and/or Iterator is responsible for allocation, deallocation
30  * and management of a Node's next and previous nodes. Node simply provides the
31  * ability to set these pointers.
32  *
33  * \tparam T the type of the value this Node works on
34  *
35  * \note This class is not accessible to the users of DoubleLinkedList.
36  */
37  template<typename T>
38  class Node {
39  public:
40  /** The type of value this Node works on */
41  typedef T value_type;
42 
43  virtual ~Node();
44  Node(Node* previous = NULL, Node* next = NULL);
45 
46  private:
47  Node(const Node& rhs);
48  Node& operator=(const Node& rhs);
49 
50  public:
51  Node* getPrevious() const;
52  void setPrevious(Node* previous);
53 
54  Node* getNext() const;
55  void setNext(Node* next);
56 
57  void swapWith(Node* other);
58 
59  void moveBefore(Node* other);
60 
61  /** \return a reference to the value of this node
62  *
63  * \throw invalid_argument if data is not available such as marker nodes
64  */
65  virtual value_type& operator*() {
66  throw std::invalid_argument("Dereferencing at invalid position");
67  }
68 
69  /** \return a const reference to the value of this node
70  *
71  * \throw invalid_argument if data is not available such as marker nodes
72  */
73  virtual const value_type& operator*() const {
74  throw std::invalid_argument("Dereferencing at invalid position");
75  }
76 
77  /** @return true if this is a valid note for which operator*() may be called,
78  * otherwise false such as marker nodes.
79  */
80  virtual bool valid() const {
81  return false;
82  }
83 
84  private:
85  void swapWithNext();
86  void swapWithDisjoint(Node* other);
87 
88  private:
89  /** The Node before this such that this == thePrevious->theNext or NULL for none. */
90  Node* thePrevious;
91 
92  /** The Node after this such that this == theNext->thePrevious or NULL for none. */
93  Node* theNext;
94  };
95 
96  /** DoubleLinkedList internal Node holding Data.
97  *
98  * This provides a Node with a valid data type.
99  *
100  * This class will hold a copy of the value given to it. That value will be
101  * destructed, of course; however, DataNode is not responsible for deleting
102  * the values of pointers.
103  *
104  * \tparam T the type of the value this Node works on
105  *
106  * \note This class is not accessible to the users of DoubleLinkedList.
107  */
108  template<typename T>
109  class DataNode : public Node<T> {
110  public:
111  /** The type of value this Node works on */
112  typedef T value_type;
113 
114  virtual ~DataNode();
115 
116  DataNode(const value_type& t, Node<T>* previous = NULL, Node<T>* next = NULL);
117 
118  private:
119  DataNode(const DataNode& rhs);
120  DataNode& operator=(const DataNode& rhs);
121 
122  public:
123  /** \return a reference to the value of this none */
124  virtual value_type& operator*() {
125  return theT;
126  }
127 
128  /** \return a const reference to the value of this none */
129  virtual const value_type& operator*() const {
130  return theT;
131  }
132 
133  /** @return true as this is a valid data node */
134  virtual bool valid() const {
135  return true;
136  }
137 
138  private:
139  /** The value in this node */
140  value_type theT;
141  };
142 
143  } // namespace DoubleLinkedListImpl
144 
145 } // namespace Experiment
146 
147 #include "DoubleLinkedListImpl.cpp"
148 
149 #endif // DOUBLE_LINKED_LIST_IMPL_H
Implementations of longer template methods of the DoubleLinkedListImpl.h file.
DoubleLinkedList internal Node holding Data.
T value_type
The type of value this Node works on.
virtual const value_type & operator*() const
virtual ~DataNode()
Destructor for DataNode.
DataNode(const value_type &t, Node< T > *previous=NULL, Node< T > *next=NULL)
Create a DataNode with the given value t, previous and next pointers.
DoubleLinkedList internal Node base class.
virtual const value_type & operator*() const
void moveBefore(Node *other)
Move this node before other by appropriately updating previous and next pointers.
void setPrevious(Node *previous)
Set the Node sequentially before this Node, or NULL for none.
void setNext(Node *next)
Set the Node sequentially after this Node, or NULL for none.
Node(Node *previous=NULL, Node *next=NULL)
Create a Node with the given previous and next pointers.
void swapWith(Node *other)
Swap this node with other by appropriately updating previous and next pointers.
virtual ~Node()
Virtual destructor that does not delete its pointers.
T value_type
The type of value this Node works on.
This file is to be included at the end of Iterator.h.