CFx SDK Documentation  2020SP3
OdUnidirectionalList.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 #ifndef _ODA_UNIDIRECTIONALLISTT_H_
25 #define _ODA_UNIDIRECTIONALLISTT_H_
26 
27 #include "OdArrayMemAlloc.h"
28 
29 #include "TD_PackPush.h"
30 
31 template<class T, class A = OdObjectsAllocator<T>, class Mm = OdrxMemoryManager> class OdUnidirectionalList;
32 
38 template<class T, class A, class Mm>
40 {
41  struct SNode
42  {
43  SNode()
44  : m_pNext(NULL)
45  {
46  }
47 
48  public:
49  SNode* m_pNext;
50  T m_value;
51  };
52 
53  class CmpValues
54  {
55  public:
56  CmpValues(const T& value)
57  : m_value(value)
58  {
59  }
60 
61  bool operator()(const T& value) const
62  {
63  return (m_value == value);
64  }
65 
66  protected:
67  const T& m_value;
68  };
69 
70 public:
72  {
73  public:
74  ConstIterator(SNode* pNode = NULL)
75  : m_pNode(pNode)
76  {
77  }
78 
79  const T& operator*() const
80  {
82 
83  return m_pNode->m_value;
84  }
85 
86  const T* operator->() const
87  {
89 
90  return &(m_pNode->m_value);
91  }
92 
94  {
96 
97  m_pNode = m_pNode->m_pNext;
98 
99  return *this;
100  }
101 
103  {
104  ODA_ASSERT(m_pNode != NULL);
105 
106  return ConstIterator(m_pNode->m_pNext);
107  }
108 
109  bool operator==(const ConstIterator& it) const
110  {
111  return (m_pNode == it.m_pNode);
112  }
113 
114  bool operator!=(const ConstIterator& it) const
115  {
116  return (m_pNode != it.m_pNode);
117  }
118 
119  SNode* node()
120  {
121  return m_pNode;
122  }
123 
124  void setNode(SNode* pNode)
125  {
126  m_pNode = pNode;
127  }
128 
129  private:
130  ConstIterator& operator--()
131  {
132  return *this;
133  }
134 
135  ConstIterator operator--(int)
136  {
137  return ConstIterator();
138  }
139 
140  protected:
141  SNode* m_pNode;
142  };
143 
144  class Iterator : public ConstIterator
145  {
146  public:
147  Iterator(SNode* pNode = NULL)
148  : ConstIterator(pNode)
149  {
150  }
151 
153  {
155 
156  return ConstIterator::m_pNode->m_value;
157  }
158 
160  {
162 
163  return &(ConstIterator::m_pNode->m_value);
164  }
165 
167  {
169 
171 
172  return *this;
173  }
174 
176  {
178 
179  return Iterator(ConstIterator::m_pNode->m_pNext);
180  }
181 
182  private:
183  Iterator& operator--()
184  {
185  return *this;
186  }
187 
188  Iterator operator--(int)
189  {
190  return Iterator();
191  }
192  };
193 
194  typedef typename A::size_type size_type;
197 
198 public:
201 
203 
205 
206  iterator begin();
207  const_iterator begin() const;
208  iterator end();
209  const_iterator end() const;
210 
211  size_type size() const;
212 
213  bool isEmpty() const;
214 
215  iterator insertAfter(const_iterator it, const T& value = T());
216  iterator insertAtBegin(const T& value = T());
217 
219 
220  void clear();
221 
222  bool find(iterator& it, iterator& prevIt, const T& value
223  , const_iterator prevStartIt = const_iterator()) const;
224  bool find(iterator& it, const T& value) const;
225 
226  template<class P>
227  bool findIf(iterator& it, iterator& prevIt, P pred
228  , const_iterator prevStartIt = const_iterator()) const;
229  template<class P>
230  bool findIf(iterator& it, P pred) const;
231 
232  bool operator==(const OdUnidirectionalList& list) const;
233 
234 protected:
235  static SNode* createNode();
236  static SNode* createNode(const T& value);
237 
238  static void destroyNode(SNode* pNode);
239 
240  static SNode* copyList(SNode* pNode);
241 
242 private:
243  SNode* m_pHead;
244  size_type m_size;
245 };
246 
247 
248 template<class T, class A, class Mm>
250 : m_pHead(NULL), m_size(0)
251 {
252 }
253 
254 template<class T, class A, class Mm>
256 : m_pHead(copyList(list.m_pHead)), m_size(list.m_size)
257 {
258 }
259 
260 template<class T, class A, class Mm>
262 {
263  clear();
264 }
265 
266 template<class T, class A, class Mm>
268 {
269  if(this != &list)
270  {
271  clear();
272 
273  m_pHead = copyList(list.m_pHead);
274  m_size = list.m_size;
275  }
276 
277  return *this;
278 }
279 
280 template<class T, class A, class Mm>
282 {
283  SNode* pNewNode = reinterpret_cast<SNode*>(Mm::Alloc(sizeof(SNode)));
284 
285  if(pNewNode == NULL)
286  throw OdError(eOutOfMemory);
287 
288  A::construct(&(pNewNode->m_value));
289 
290  return pNewNode;
291 }
292 
293 template<class T, class A, class Mm>
295 {
296  SNode* pNewNode = reinterpret_cast<SNode*>(Mm::Alloc(sizeof(SNode)));
297 
298  if(pNewNode == NULL)
299  throw OdError(eOutOfMemory);
300 
301  A::construct(&(pNewNode->m_value), value);
302 
303  return pNewNode;
304 }
305 
306 template<class T, class A, class Mm>
308 {
309  ODA_ASSERT(pNode != NULL);
310 
311  A::destroy(&(pNode->m_value));
312 
313  Mm::Free(pNode);
314 }
315 
316 template<class T, class A, class Mm>
318 {
319  SNode* pHead = NULL;
320 
321  if(pNode != NULL)
322  {
323  SNode* pNewNode = pHead = createNode(pNode->m_value);
324 
325  pNewNode->m_pNext = NULL;
326 
327  for(pNode = pNode->m_pNext; pNode != NULL; pNode = pNode->m_pNext)
328  {
329  pNewNode->m_pNext = createNode(pNode->m_value);
330 
331  pNewNode->m_pNext->m_pNext = NULL;
332 
333  pNewNode = pNewNode->m_pNext;
334  }
335  }
336 
337  return pHead;
338 }
339 
340 template<class T, class A, class Mm>
342 {
343  return iterator(m_pHead);
344 }
345 
346 template<class T, class A, class Mm>
348 {
349  return const_iterator(m_pHead);
350 }
351 
352 template<class T, class A, class Mm>
354 {
355  return iterator();
356 }
357 
358 template<class T, class A, class Mm>
360 {
361  return const_iterator();
362 }
363 
364 template<class T, class A, class Mm>
366 {
367  return m_size;
368 }
369 
370 template<class T, class A, class Mm>
372 {
373  return (m_size == 0);
374 }
375 
376 template<class T, class A, class Mm>
378  , const T& value)
379 {
380  SNode* pNode = createNode(value);
381 
382  if(it.node() != NULL)
383  {
384  pNode->m_pNext = it.node()->m_pNext;
385  it.node()->m_pNext = pNode;
386 
387  }
388  else
389  {
390  pNode->m_pNext = m_pHead;
391  m_pHead = pNode;
392  }
393 
394  ++m_size;
395 
396  return iterator(pNode);
397 }
398 
399 template<class T, class A, class Mm>
401 {
402  SNode* pNode = createNode(value);
403 
404  pNode->m_pNext = m_pHead;
405  m_pHead = pNode;
406 
407  ++m_size;
408 
409  return iterator(pNode);
410 }
411 
412 template<class T, class A, class Mm>
414 {
415  if(it.node() != NULL)
416  {
417  SNode* pNode = it.node()->m_pNext;
418 
419  if(pNode != NULL)
420  {
421  it.node()->m_pNext = pNode->m_pNext;
422 
423  destroyNode(pNode);
424 
425  --m_size;
426  }
427 
428  return iterator(it.node()->m_pNext);
429  }
430 
431  SNode* pNode = m_pHead;
432 
433  m_pHead = m_pHead->m_pNext;
434 
435  destroyNode(pNode);
436 
437  --m_size;
438 
439  return iterator(m_pHead);
440 }
441 
442 template<class T, class A, class Mm>
444 {
445  SNode* pNode;
446  SNode* pNextNode;
447 
448  for(pNode = m_pHead; pNode != NULL; pNode = pNextNode)
449  {
450  pNextNode = pNode->m_pNext;
451 
452  destroyNode(pNode);
453  }
454 
455  m_pHead = NULL;
456  m_size = 0;
457 }
458 
459 template<class T, class A, class Mm>
461  , const T& value, const_iterator prevStartIt) const
462 {
463  return find(it, prevIt, CmpValues(value), prevStartIt);
464 }
465 
466 template<class T, class A, class Mm>
467 inline bool OdUnidirectionalList<T, A, Mm>::find(iterator& it, const T& value) const
468 {
469  return find(it, CmpValues(value));
470 }
471 
472 template<class T, class A, class Mm>
473 template<class P>
475  , P pred, const_iterator prevStartIt) const
476 {
477  if(!isEmpty())
478  {
479  ODA_ASSERT(m_pHead != NULL);
480 
481  SNode* pPrevNode = prevStartIt.node();
482  SNode* pNode = ((pPrevNode != NULL) ? pPrevNode->m_pNext : m_pHead);
483 
484  for(; pNode != NULL; pPrevNode = pNode, pNode = pNode->m_pNext)
485  {
486  if(pred(pNode->m_value))
487  {
488  it.setNode(pNode);
489  prevIt.setNode(pPrevNode);
490 
491  return true;
492  }
493  }
494  }
495 
496  return false;
497 }
498 
499 template<class T, class A, class Mm>
500 template<class P>
501 inline bool OdUnidirectionalList<T, A, Mm>::findIf(iterator& it, P pred) const
502 {
503  if(!isEmpty())
504  {
505  ODA_ASSERT(m_pHead != NULL);
506 
507  for(SNode* pNode = m_pHead; pNode != NULL; pNode = pNode->m_pNext)
508  {
509  if(pred(pNode->m_value))
510  {
511  it.setNode(pNode);
512 
513  return true;
514  }
515  }
516  }
517 
518  return false;
519 }
520 
521 template<class T, class A, class Mm>
523 {
524  if(m_size == list.m_size)
525  {
526  SNode* pNode1 = m_pHead;
527  SNode* pNode2 = list.m_pHead;
528 
529  for(; pNode1 != NULL; pNode1 = pNode1->m_pNext, pNode2 = pNode2->m_pNext)
530  {
531  ODA_ASSERT(pNode2 != NULL);
532 
533  if(pNode1->m_value != pNode2->m_value)
534  return false;
535  }
536 
537  return true;
538  }
539 
540  return false;
541 }
542 
543 #include "TD_PackPop.h"
544 
545 #endif // _ODA_UNIDIRECTIONALLISTT_H_
OdUnidirectionalList::ConstIterator::ConstIterator
ConstIterator(SNode *pNode=NULL)
Definition: OdUnidirectionalList.h:74
OdUnidirectionalList::~OdUnidirectionalList
~OdUnidirectionalList()
Definition: OdUnidirectionalList.h:261
NULL
#define NULL
Definition: GsProperties.h:177
OdUnidirectionalList::insertAfter
iterator insertAfter(const_iterator it, const T &value=T())
Definition: OdUnidirectionalList.h:377
OdUnidirectionalList::clear
void clear()
Definition: OdUnidirectionalList.h:443
OdUnidirectionalList::Iterator::operator++
Iterator & operator++()
Definition: OdUnidirectionalList.h:166
OdUnidirectionalList::Iterator
Definition: OdUnidirectionalList.h:145
OdUnidirectionalList::findIf
bool findIf(iterator &it, iterator &prevIt, P pred, const_iterator prevStartIt=const_iterator()) const
Definition: OdUnidirectionalList.h:474
OdArrayMemAlloc.h
TD_PackPop.h
OdUnidirectionalList::removeAfter
iterator removeAfter(const_iterator it)
Definition: OdUnidirectionalList.h:413
OdUnidirectionalList::const_iterator
ConstIterator const_iterator
Definition: OdUnidirectionalList.h:196
OdUnidirectionalList::size
size_type size() const
Definition: OdUnidirectionalList.h:365
OdUnidirectionalList::Iterator::operator++
Iterator operator++(int)
Definition: OdUnidirectionalList.h:175
OdUnidirectionalList::ConstIterator::operator!=
bool operator!=(const ConstIterator &it) const
Definition: OdUnidirectionalList.h:114
OdUnidirectionalList::ConstIterator::m_pNode
SNode * m_pNode
Definition: OdUnidirectionalList.h:141
OdUnidirectionalList::operator=
OdUnidirectionalList & operator=(const OdUnidirectionalList &list)
Definition: OdUnidirectionalList.h:267
OdUnidirectionalList::ConstIterator::operator==
bool operator==(const ConstIterator &it) const
Definition: OdUnidirectionalList.h:109
OdUnidirectionalList::ConstIterator
Definition: OdUnidirectionalList.h:72
OdUnidirectionalList::find
bool find(iterator &it, iterator &prevIt, const T &value, const_iterator prevStartIt=const_iterator()) const
Definition: OdUnidirectionalList.h:460
OdUnidirectionalList::Iterator::operator*
T & operator*()
Definition: OdUnidirectionalList.h:152
TD_PackPush.h
OdUnidirectionalList::isEmpty
bool isEmpty() const
Definition: OdUnidirectionalList.h:371
OdUnidirectionalList
Definition: OdUnidirectionalList.h:40
OdUnidirectionalList::copyList
static SNode * copyList(SNode *pNode)
Definition: OdUnidirectionalList.h:317
OdUnidirectionalList::destroyNode
static void destroyNode(SNode *pNode)
Definition: OdUnidirectionalList.h:307
OdUnidirectionalList::insertAtBegin
iterator insertAtBegin(const T &value=T())
Definition: OdUnidirectionalList.h:400
OdUnidirectionalList::ConstIterator::operator++
ConstIterator operator++(int)
Definition: OdUnidirectionalList.h:102
ODA_ASSERT
#define ODA_ASSERT(exp)
Definition: DebugStuff.h:49
OdUnidirectionalList::end
iterator end()
Definition: OdUnidirectionalList.h:353
OdUnidirectionalList::size_type
A::size_type size_type
Definition: OdUnidirectionalList.h:194
OdUnidirectionalList::begin
iterator begin()
Definition: OdUnidirectionalList.h:341
OdError
Definition: OdError.h:43
value
GLsizei const GLfloat * value
Definition: gles2_ext.h:302
OdUnidirectionalList::ConstIterator::operator++
ConstIterator & operator++()
Definition: OdUnidirectionalList.h:93
OdUnidirectionalList::ConstIterator::node
SNode * node()
Definition: OdUnidirectionalList.h:119
OdUnidirectionalList::ConstIterator::operator*
const T & operator*() const
Definition: OdUnidirectionalList.h:79
OdUnidirectionalList::iterator
Iterator iterator
Definition: OdUnidirectionalList.h:195
OdUnidirectionalList::ConstIterator::operator->
const T * operator->() const
Definition: OdUnidirectionalList.h:86
OdUnidirectionalList::operator==
bool operator==(const OdUnidirectionalList &list) const
Definition: OdUnidirectionalList.h:522
OdUnidirectionalList::Iterator::operator->
T * operator->()
Definition: OdUnidirectionalList.h:159
OdUnidirectionalList::createNode
static SNode * createNode()
Definition: OdUnidirectionalList.h:281
OdUnidirectionalList::Iterator::Iterator
Iterator(SNode *pNode=NULL)
Definition: OdUnidirectionalList.h:147
OdUnidirectionalList::ConstIterator::setNode
void setNode(SNode *pNode)
Definition: OdUnidirectionalList.h:124
OdUnidirectionalList::OdUnidirectionalList
OdUnidirectionalList()
Definition: OdUnidirectionalList.h:249