summaryrefslogtreecommitdiffstats
path: root/src/ptrvector.h
blob: 727c29e029f27f73084249f5447086508368a8cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#ifndef TELLICO_PTRVECTOR_H
#define TELLICO_PTRVECTOR_H

#include <kdebug.h>
#include <ksharedptr.h>

#include <tqvaluevector.h>

namespace Tellico {

template <class T> class Vector;
template <class T> class VectorIterator;
template <class T> class VectorConstIterator;
template <class T> bool operator==(const VectorIterator<T>& left, const VectorIterator<T>& right);
template <class T> bool operator!=(const VectorIterator<T>& left, const VectorIterator<T>& right);
template <class T> bool operator==(const VectorConstIterator<T>& left, const VectorConstIterator<T>& right);
template <class T> bool operator!=(const VectorConstIterator<T>& left, const VectorConstIterator<T>& right);

template <class T>
class VectorIterator {
public:
  VectorIterator() : m_vector(0), m_index(0) {}
  VectorIterator(Vector<T>* vector, size_t index) : m_vector(vector), m_index(index) {}
  VectorIterator(const VectorIterator<T>& other) : m_vector(other.m_vector), m_index(other.m_index) {}

//  operator T*() { return m_vector->at(m_index).data(); }
  operator KSharedPtr<T>() { return m_vector->at(m_index); }
  T* operator->() { return m_vector->at(m_index).data(); }
  T& operator*() { return *m_vector->at(m_index); }
  T* data() { return m_vector->at(m_index).data(); }

  VectorIterator& operator++() { ++m_index; return *this; }
  VectorIterator& operator--() { --m_index; return *this; }

  friend bool operator==(const VectorIterator<T>& left, const VectorIterator<T>& right)
    { return left.m_vector == right.m_vector && left.m_index == right.m_index; }
  friend bool operator!=(const VectorIterator<T>& left, const VectorIterator<T>& right)
    { return left.m_vector != right.m_vector || left.m_index != right.m_index; }

  bool nextEnd() const { return m_index == m_vector->count()-1; }

private:
  friend class Vector<T>;
  Vector<T>* m_vector;
  size_t m_index;
};

template <class T>
class VectorConstIterator {
public:
  VectorConstIterator() : m_vector(0), m_index(0) {}
  VectorConstIterator(const Vector<T>* vector, size_t index) : m_vector(vector), m_index(index) {}
  VectorConstIterator(const VectorIterator<T>& other) : m_vector(other.m_vector), m_index(other.m_index) {}

//  operator const T*() { return m_vector->at(m_index).data(); }
  operator KSharedPtr<const T>() { return m_vector->at(m_index); }
  const T* operator->() const { return m_vector->at(m_index).data(); }
  const T& operator*() const { return *m_vector->at(m_index); }
  const T* data() const { return m_vector->at(m_index).data(); }

  VectorConstIterator& operator++() { ++m_index; return *this; }
  VectorConstIterator& operator--() { --m_index; return *this; }

  friend bool operator==(const VectorConstIterator<T>& left, const VectorConstIterator<T>& right)
    { return left.m_vector == right.m_vector && left.m_index == right.m_index; }
  friend bool operator!=(const VectorConstIterator<T>& left, const VectorConstIterator<T>& right)
    { return left.m_vector != right.m_vector || left.m_index != right.m_index; }

  bool nextEnd() const { return m_index == m_vector->count()-1; }

private:
  friend class Vector<T>;
  const Vector<T>* m_vector;
  size_t m_index;
};

template <class T>
class Vector {
public:
  typedef KSharedPtr<T> Ptr;
  typedef VectorIterator<T> Iterator;
  typedef VectorConstIterator<T> ConstIterator;

  Vector() {}
  Vector(Ptr t) { append(t); }
  Vector(const Vector<T>& v) : m_baseVector(v.m_baseVector) {}
  Vector& operator=(const Vector<T>& other) {
    if(this != &other) {
      m_baseVector = other.m_baseVector;
    }
    return *this;
  }

  bool operator== (const Vector<T>& x) const { return x.m_baseVector == m_baseVector; }
  bool isEmpty() const { return m_baseVector.empty(); }
  size_t count() const { return m_baseVector.size(); }

  Ptr& operator[](size_t i) { return m_baseVector[i]; }
  const Ptr& operator[](size_t i) const { return m_baseVector[i]; }

  Ptr& at(size_t i, bool* ok = 0) { return m_baseVector.at(i, ok); }
  const Ptr& at(size_t i, bool* ok = 0) const { return m_baseVector.at(i, ok); }

  Iterator begin() { return Iterator(this, 0); }
  ConstIterator begin() const { return ConstIterator(this, 0); }
  ConstIterator constBegin() const { return ConstIterator(this, 0); }
  Iterator end() { return Iterator(this, count()); }
  ConstIterator end() const { return ConstIterator(this, count()); }
  ConstIterator constEnd() const { return ConstIterator(this, count()); }

  Ptr& front() { return at(0); }
  const Ptr& front() const { return at(0); }
  Ptr& back() { return at(count()-1); }
  const Ptr& back() const { return at(count()-1); }

  void clear() { m_baseVector.clear(); }
  void append(Ptr t) { m_baseVector.append(t); }
  void append(Vector<T> v);

  void insert(Iterator pos, Ptr t) {
    m_baseVector.insert(&m_baseVector[pos.m_index], t);
  }

  Iterator find(Ptr t) {
    for(size_t i = 0; i < count(); ++i) {
      if(m_baseVector[i].data() == t) {
        return Iterator(this, i);
      }
    }
    return end();
  }

  bool contains(Ptr t) const { return tqFind(m_baseVector.begin(), m_baseVector.end(), Ptr(t)) != m_baseVector.end(); }
  bool remove(const Ptr& t) {
    Ptr* it = tqFind(m_baseVector.begin(), m_baseVector.end(), t);
    if(it == m_baseVector.end()) return false;
    m_baseVector.erase(it);
    return true;
  }

private:
  TQValueVector<Ptr> m_baseVector;
};

template <class T> class PtrVector;
template <class T> class PtrVectorIterator;
template <class T> class PtrVectorConstIterator;
template <class T> bool operator==(const PtrVectorIterator<T>& left, const PtrVectorIterator<T>& right);
template <class T> bool operator!=(const PtrVectorIterator<T>& left, const PtrVectorIterator<T>& right);
template <class T> bool operator==(const PtrVectorConstIterator<T>& left, const PtrVectorConstIterator<T>& right);
template <class T> bool operator!=(const PtrVectorConstIterator<T>& left, const PtrVectorConstIterator<T>& right);

template <class T>
class PtrVectorIterator {
public:
  PtrVectorIterator() : m_vector(0), m_index(0) {}
  PtrVectorIterator(const PtrVector<T>* vector, size_t index) : m_vector(vector), m_index(index) {}
  PtrVectorIterator(const PtrVectorIterator<T>& other) : m_vector(other.m_vector), m_index(other.m_index) {}

  T* operator->() { return &m_vector->at(m_index); }
  T& operator*() { return m_vector->at(m_index); }

  T* ptr() { return &m_vector->at(m_index); }

  PtrVectorIterator& operator++() { ++m_index; return *this; }
  PtrVectorIterator& operator--() { --m_index; return *this; }

  friend bool operator==(const PtrVectorIterator<T>& left, const PtrVectorIterator<T>& right)
    { return left.m_vector == right.m_vector && left.m_index == right.m_index; }
  friend bool operator!=(const PtrVectorIterator<T>& left, const PtrVectorIterator<T>& right)
    { return left.m_vector != right.m_vector || left.m_index != right.m_index; }

private:
  const PtrVector<T>* m_vector;
  size_t m_index;
};

template <class T>
class PtrVectorConstIterator {
public:
  PtrVectorConstIterator() : m_vector(0), m_index(0) {}
  PtrVectorConstIterator(const PtrVector<T>* vector, size_t index) : m_vector(vector), m_index(index) {}
  PtrVectorConstIterator(const PtrVectorConstIterator<T>& other) : m_vector(other.m_vector), m_index(other.m_index) {}

  const T* operator->() const { return &m_vector->at(m_index); }
  const T& operator*() const { return m_vector->at(m_index); }

  const T* ptr() const { return &m_vector->at(m_index); }

  PtrVectorConstIterator& operator++() { ++m_index; return *this; }
  PtrVectorConstIterator& operator--() { --m_index; return *this; }

  friend bool operator==(const PtrVectorConstIterator<T>& left, const PtrVectorConstIterator<T>& right)
    { return left.m_vector == right.m_vector && left.m_index == right.m_index; }
  friend bool operator!=(const PtrVectorConstIterator<T>& left, const PtrVectorConstIterator<T>& right)
    { return left.m_vector != right.m_vector || left.m_index != right.m_index; }

private:
  const PtrVector<T>* m_vector;
  size_t m_index;
};

/**
 * @author Robby Stephenson
 */
template <class T>
class PtrVector {

public:
  typedef Tellico::PtrVectorIterator<T> Iterator;
  typedef Tellico::PtrVectorConstIterator<T> ConstIterator;

  PtrVector() : m_autoDelete(false) {}
  PtrVector(const PtrVector<T>& other) : m_baseVector(other.m_baseVector), m_autoDelete(false) {}
  PtrVector& operator=(const PtrVector<T>& other) {
    if(this != &other) {
      m_baseVector = other.m_baseVector;
      m_autoDelete = false;
    }
    return *this;
  }
  ~PtrVector() { if(m_autoDelete) clear(); }

  size_t count() const { return m_baseVector.size(); }
  bool isEmpty() const { return m_baseVector.empty(); }
  bool autoDelete() const { return m_autoDelete; }
  void setAutoDelete(bool b) { m_autoDelete = b; }

  T& operator[](size_t n) const { check(n); return *m_baseVector[n]; }
  T& at(size_t n) const { check(n); return *m_baseVector.at(n); }

  Iterator begin() { return Iterator(this, 0); }
  ConstIterator begin() const { return ConstIterator(this, 0); }
  ConstIterator constBegin() const { return ConstIterator(this, 0); }
  Iterator end() { return Iterator(this, count()); }
  ConstIterator end() const { return ConstIterator(this, count()); }
  ConstIterator constEnd() const { return ConstIterator(this, count()); }

  T* front() { return count() > 0 ? m_baseVector.at(0) : 0; }
  const T* front() const { return count() > 0 ? m_baseVector.at(0) : 0; }
  T* back() { return count() > 0 ? m_baseVector.at(count()-1) : 0; }
  const T* back() const { return count() > 0 ? m_baseVector.at(count()-1) : 0; }

  void clear() { while(remove(begin())) { ; } }

  void push_back(T* ptr) { m_baseVector.push_back(ptr); }
  bool remove(T* ptr);
  bool remove(Iterator it);
  bool contains(const T* ptr) const;

private:
#ifndef NDEBUG
  void check(size_t n) const { if(n >= count()) kdDebug() << "PtrVector() - bad index" << endl; }
#else
  void check(size_t) const {}
#endif

  TQValueVector<T*> m_baseVector;
  bool m_autoDelete : 1;
};

}

template <class T>
void Tellico::Vector<T>::append(Tellico::Vector<T> v) {
  typename Tellico::Vector<T>::Iterator it;
  for(it = v.begin(); it != v.end(); ++it) {
    append(it.data());
  }
}

template <class T>
bool Tellico::PtrVector<T>::remove(T* t) {
  if(!t) {
    return false;
  }
  T** ptr = tqFind(m_baseVector.begin(), m_baseVector.end(), t);
  if(ptr == m_baseVector.end()) {
    return false;
  }
  if(m_autoDelete) {
    delete *ptr;
  }
  m_baseVector.erase(ptr);
  // in case the pointer is in the vector multiple times
  while(remove(t)) {
    kdDebug() << "PtrVector::remove() - pointer was duplicated in vector" << endl;
  }
  return true;
}

template <class T>
bool Tellico::PtrVector<T>::remove(Iterator it) {
  if(it == end()) {
    return false;
  }
  return remove(it.ptr());
}

template <class T>
bool Tellico::PtrVector<T>::contains(const T* t) const {
  if(!t) {
    return false;
  }
  const T* const* ptr = tqFind(m_baseVector.begin(), m_baseVector.end(), t);
  return ptr != m_baseVector.end();
}

#endif