VR-Forces 4.1.1 Class Documentation
orderedKeyList.h
Go to the documentation of this file.
1 /*******************************************************************************
2 ** Copyright (c) 2004 MAK Technologies, Inc.
3 ** All rights reserved.
4 *******************************************************************************/
5 
8 
9 #pragma once
10 
14 
15 #include <QtCore/QList>
16 #include "ngutil/entityMapperKey.h"
17 
18 template <typename T>
19 class DtOrderedKeyList : public QList<T>
20 {
21 public:
24 
26 
27  virtual ~DtOrderedKeyList();
28 
29  void clear();
30  void copyList(const DtOrderedKeyList&);
31 
33  void addEntry(T, bool replace = false);
34  T removeEntry(const DtEntityMapperKey& key);
35  T find(const DtEntityMapperKey& key) const;
36 };
37 
38 template <typename T>
40 {
41 }
42 
43 template <typename T>
45 {
46  clear();
47  copyList(orig);
48 }
49 
50 template <typename T>
52 {
53  if (this != &rhs)
54  {
55  clear();
56  copyList(rhs);
57  }
58 
59  return *this;
60 }
61 
62 template <typename T>
64 {
65  clear();
66 }
67 
68 template <typename T>
70 {
71  typename QList<T>::iterator it = this->begin();
72 
73  while (it != this->end())
74  {
75  delete *it;
76  ++it;
77  }
78 
80 }
81 
82 template <typename T>
84 {
85  typename QList<T>::const_iterator it = rhs.begin();
86 
87  while (it != rhs.end())
88  {
89  addEntry((*it)->clone());
90  ++it;
91  }
92 }
93 
94 template <typename T>
95 void DtOrderedKeyList<T>::addEntry(T entry, bool replace)
96 {
97  if (this->count() == 0)
98  {
99  this->append(entry);
100 
101  return;
102  }
103 
105  T item = this->last();
106  DtEntityMapperKey sdKey = *entry;
107 
108  if (*item < sdKey)
109  {
110  this->append(entry);
111  return;
112  }
113 
114  item = this->first();
115 
116  if (*item > sdKey)
117  {
118  this->insert(0, entry);
119  return;
120  }
121 
122  QString strKey = entry->string().c_str();
123 
124  unsigned int iLower = 0;
125  unsigned int iUpper = this->count() - 1;
126 
127  while ((iUpper - iLower) != 1)
128  {
129  int iPos = (iUpper + iLower) / 2;
130 
131  if (sdKey < *this->at(iPos))
132  {
133  iUpper = iPos;
134  }
135  else if (sdKey > *this->at(iPos))
136  {
137  iLower = iPos;
138  }
139  else if ((sdKey == *this->at(iPos)) && replace)
140  {
141  *this->at(iPos) = *entry;
142 
143  return;
144  }
145  }
146 
147  this->insert(iLower + 1, entry);
148 }
149 
150 template <typename T>
152 {
153  typename QList<T>::const_iterator iter = this->begin();
154 
155  while (iter != this->end())
156  {
157  if (**iter == key)
158  {
159  return *iter;
160  }
161 
162  ++iter;
163  }
164 
165  return T();
166 }
167 
168 template <typename T>
170 {
171  typename QList<T>::iterator iter = this->begin();
172 
173  while (iter != this->end())
174  {
175  if (**iter == key)
176  {
177  T item = *iter;
178 
179  erase(iter);
180 
181  return item;
182  }
183 
184  ++iter;
185  }
186 
187  return NULL;
188 }
189 

Document ID: Generated on Mon Apr 8 19:24:01 EDT 2013 from SVN revision 125877
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)