HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_LinkList.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: Utility Library (C++)
7  *
8  * COMMENTS:
9  * This class implements a doubly linked list that maintains
10  * a replacement list of nodes.
11  *
12  */
13 
14 #ifndef __UT_LinkList_h__
15 #define __UT_LinkList_h__
16 
17 #include "UT_API.h"
18 #include "UT_Swap.h"
19 #include <iosfwd>
20 
22 public:
23  // Constructor and destructor:
24  UT_LinkNode() { predNode = succNode = 0; }
25  virtual ~UT_LinkNode();
26 
27  UT_LinkNode(const UT_LinkNode &) = delete;
28  UT_LinkNode &operator=(const UT_LinkNode &) = delete;
29 
30  // Query/set methods:
31  UT_LinkNode *&prev() { return predNode; }
32  UT_LinkNode *&next() { return succNode; }
33  const UT_LinkNode *prev() const { return predNode; }
34  const UT_LinkNode *next() const { return succNode; }
35 
36  // I/O friends:
37  friend std::ostream &operator<<(std::ostream &os, const UT_LinkNode &n)
38  {
39  n.outTo(os);
40  return os;
41  }
42 
43 protected:
44  // Query the name of this class:
45  virtual const char *className() const;
46 
47  // Our own I/O methods, so we can redefine them in derived classes:
48  virtual void outTo (std::ostream &os) const;
49 
50 private:
51  // Pointers to predecessor and successor respectively:
52  UT_LinkNode *predNode;
53  UT_LinkNode *succNode;
54 };
55 
56 
58 public:
59  // Trivial class constructor and destructor. The destructor
60  // deallocates every node in the active and replacement lists.
61  UT_LinkList() { first=last=stashHead=0; count=0; }
62  virtual ~UT_LinkList();
63 
64  void swap( UT_LinkList &other );
65 
66  // Reverse the order of the active nodes:
67  virtual void reverse();
68 
69  // Add nodes to the list, or delete them:
70  // append() appends a node, insert() inserts it at index 'where';
71  // remove() deletes the node from the list and leaves it intact;
72  // destroy() deletes the node from the list and deallocates it.
73  // If the node is not in the list, or if nodeIdx is invalid,
74  // both remove() and destroy() return 0. If the node is 0,
75  // remove() and destroy() will act upon the last element in the
76  // list. None of the functions below checks whether the node
77  // is or is not in the list at the time it is deleted or added.
78  // To check for existence, see inList().
79  void append (UT_LinkNode *node);
80  void insert (UT_LinkNode *node, int where);
81  // node is the node to insert, before and after are the node already
82  // in the list that you wish to insert before/after.
83  void insertBefore(UT_LinkNode *node, UT_LinkNode *before);
84  void insertAfter(UT_LinkNode *node, UT_LinkNode *after);
85 
86  UT_LinkNode *remove (UT_LinkNode *node = 0);
87  UT_LinkNode *remove (int nodeIdx);
88  void destroy(UT_LinkNode *node = 0);
89  void destroy(int nodeIdx);
90 
91  // Take a node from the active list and stash it into the
92  // replacement list. recall() performs the inverse procedure.
93  // If the node parameter is 0, stash and recall will act upon
94  // the last elemenent added to the respective list.
95  UT_LinkNode *stash (UT_LinkNode *node = 0);
96  UT_LinkNode *stash (int nodeIdx);
97  UT_LinkNode *recall(UT_LinkNode *node = 0);
98  UT_LinkNode *recall(UT_LinkNode *node, int nodeIdx);
99 
100  // Return the index of the node in the list or vice-versa.
101  // The first method returns -1 if not found; the 2nd method
102  // returns 0 if unsuccessful.
103  int find(UT_LinkNode *node) const;
104  UT_LinkNode *find(int nodeIdx) const;
105 
106  // Check if the node is in the active list: return 1 if true, o if
107  // false.
108  int inList(UT_LinkNode *node) const;
109 
110 
111  // Return the predecessor or the successor of a node, or the
112  // first or last node in the list respectively.
113  UT_LinkNode *prev(UT_LinkNode *node) const { return node->prev(); }
114  UT_LinkNode *next(UT_LinkNode *node) const { return node->next(); }
115  UT_LinkNode *head() const { return first; }
116  UT_LinkNode *tail() const { return last; }
117 
118  // Some iteration functions, in case you are not using the separate
119  // iterator class:
120  UT_LinkNode *iterateInit() const { return first; }
122  {
123  return curr ? curr->next() : first;
124  }
126  {
127  return curr ? curr->prev() : last;
128  }
129  UT_LinkNode *iterateFastNext(UT_LinkNode *curr) const {return curr->next();}
130  UT_LinkNode *iterateFastPrev(UT_LinkNode *curr) const {return curr->prev();}
131 
132  // Clear the linked list: the list of stashed nodes, the active
133  // list, or both.
134  void clearStashed();
135  void clearActive();
136  void clear() { clearActive(); clearStashed(); }
137 
138  // Return the length of the list or just find out whether it's empty.
139  int length() const { return count; }
140  int isEmpty() const { return count == 0; }
141 
142  // Apply a user-defined function to each element of the linked
143  // list, as long as the function returns zero. If applyFct returns
144  // 0, apply() stops traversing the list and returns the current
145  // node; otherwise, apply() returns 0.
146  UT_LinkNode *apply(int (*applyFct)(UT_LinkNode *n, void *d), void *d);
147 
148  // Prohibit copy construction as we've never implemented one.
149  UT_LinkList(const UT_LinkList &) = delete;
150 
151  // Copy the other list's data into us. The source gets cleared.
152  UT_LinkList &operator=(UT_LinkList &src);
153 
154  // Append the other list's data to ours. The source gets cleared.
156 
157  // I/O friends:
158  friend std::ostream &operator<<(std::ostream &os, const UT_LinkList &v)
159  {
160  v.outTo(os);
161  return os;
162  }
163 
164 protected:
165  // Current node of iteration, in case you are not using the iterator class.
166  // Remove a node from the replacement list. Remove the last
167  // one inserted if 0.
168  UT_LinkNode *unStash(UT_LinkNode *node = 0);
169 
170  // Query the stash head:
171  UT_LinkNode *firstStash() const { return stashHead; }
172 
173  // Query the name of this class:
174  virtual const char *className() const;
175 
176  // Our own I/O methods, so we can redefine them in derived classes:
177  virtual void outTo (std::ostream &os) const;
178 
179  // Head of the stash list.
181 
182  // Just reset the state of the link list
183  // Not, this will cause memory leaks unless used with caution,
184  // ie. list sorting
185  void reset() { first = last = 0; count = 0; }
186 
187 
188 private:
189 
190  // DATA
191 
192  // First and last nodes in the active list.
194  UT_LinkNode *last;
195 
196  // Number of nodes in the active list.
197  int count;
198 };
199 
201 
202 #endif
GLint first
Definition: glcorearb.h:405
const GLdouble * v
Definition: glcorearb.h:837
void reverse(I begin, I end)
Definition: pugixml.cpp:7190
void swap(T &lhs, T &rhs)
Definition: pugixml.cpp:7172
#define UT_API
Definition: UT_API.h:14
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3436
GLdouble n
Definition: glcorearb.h:2008
OIIO_FORCEINLINE const vint4 & operator+=(vint4 &a, const vint4 &b)
Definition: simd.h:4369
#define UT_SWAPPER_CLASS(T)
Definition: UT_Swap.h:60
GLint GLsizei count
Definition: glcorearb.h:405
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
GLenum src
Definition: glcorearb.h:1793