VR-Forces 4.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 /*******************************************************************************
6 ** $RCSfile: orderedKeyList.h,v $ $Revision: 1.3 $ $State: Exp $
7 *******************************************************************************/
8 
11 
12 #ifndef ORDEREDKEYLIST_H_
13 #define ORDEREDKEYLIST_H_
14 
18 
19 #include <QtCore/QList>
20 #include "ngutil/entityMapperKey.h"
21 
22 template <typename T>
23 class DtOrderedKeyList : public QList<T>
24 {
25 public:
28 
30 
31  virtual ~DtOrderedKeyList();
32 
33  void clear();
34  void copyList(const DtOrderedKeyList&);
35 
37  void addEntry(T, bool replace = false);
38  T removeEntry(const DtEntityMapperKey& key);
39  T find(const DtEntityMapperKey& key) const;
40 };
41 
42 template <typename T>
44 {
45 }
46 
47 template <typename T>
49 {
50  clear();
51  copyList(orig);
52 }
53 
54 template <typename T>
56 {
57  if (this != &rhs)
58  {
59  clear();
60  copyList(rhs);
61  }
62 
63  return *this;
64 }
65 
66 template <typename T>
68 {
69  clear();
70 }
71 
72 template <typename T>
74 {
75  typename QList<T>::iterator it = this->begin();
76 
77  while (it != this->end())
78  {
79  delete *it;
80  ++it;
81  }
82 
84 }
85 
86 template <typename T>
88 {
89  typename QList<T>::const_iterator it = rhs.begin();
90 
91  while (it != rhs.end())
92  {
93  addEntry((*it)->clone());
94  ++it;
95  }
96 }
97 
98 template <typename T>
99 void DtOrderedKeyList<T>::addEntry(T entry, bool replace)
100 {
101  if (this->count() == 0)
102  {
103  append(entry);
104 
105  return;
106  }
107 
109  T item = this->last();
110  DtEntityMapperKey sdKey = *entry;
111 
112  if (*item < sdKey)
113  {
114  append(entry);
115  return;
116  }
117 
118  item = this->first();
119 
120  if (*item > sdKey)
121  {
122  insert(0, entry);
123  return;
124  }
125 
126  QString strKey = entry->string().c_str();
127 
128  unsigned int iLower = 0;
129  unsigned int iUpper = this->count() - 1;
130 
131  while ((iUpper - iLower) != 1)
132  {
133  int iPos = (iUpper + iLower) / 2;
134 
135  if (sdKey < *this->at(iPos))
136  {
137  iUpper = iPos;
138  }
139  else if (sdKey > *this->at(iPos))
140  {
141  iLower = iPos;
142  }
143  else if ((sdKey == *this->at(iPos)) && replace)
144  {
145  *this->at(iPos) = *entry;
146 
147  return;
148  }
149  }
150 
151  insert(iLower + 1, entry);
152 }
153 
154 template <typename T>
156 {
157  typename QList<T>::const_iterator iter = this->begin();
158 
159  while (iter != this->end())
160  {
161  if (**iter == key)
162  {
163  return *iter;
164  }
165 
166  ++iter;
167  }
168 
169  return T();
170 }
171 
172 template <typename T>
174 {
175  typename QList<T>::iterator iter = this->begin();
176 
177  while (iter != this->end())
178  {
179  if (**iter == key)
180  {
181  T item = *iter;
182 
183  erase(iter);
184 
185  return item;
186  }
187 
188  ++iter;
189  }
190 
191  return NULL;
192 }
193 
194 #endif //ORDEREDKEYLIST_H_

Document ID: Generated on Tue Jan 29 18:21:16 EST 2013 from SVN revision 123193
Copyright © 2005-2013 VT MÄK Inc. All Rights Reserved (www.mak.com)