-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPtrVector.hpp
771 lines (653 loc) · 21.8 KB
/
PtrVector.hpp
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
**/
#ifndef QUICKSTEP_UTILITY_PTR_VECTOR_HPP_
#define QUICKSTEP_UTILITY_PTR_VECTOR_HPP_
#include <iterator>
#include <vector>
#include "utility/Macros.hpp"
#include "glog/logging.h"
namespace quickstep {
/** \addtogroup Utility
* @{
*/
/**
* @brief A vector of pointers to objects, which are automatically deleted when
* the PtrVector goes out of scope.
**/
template <typename T, bool null_allowed = false>
class PtrVector {
public:
// TODO(chasseur): Attempt some template magic to get rid of the duplicated
// code between iterator classes.
/**
* @brief Iterator over the contents of a PtrVector.
* @warning If null_allowed is true, then always check that isNull() is false
* before attempting to dereference.
**/
class PtrVectorIterator : public std::iterator<std::random_access_iterator_tag, T> {
public:
typedef typename std::iterator<std::random_access_iterator_tag, T>::difference_type difference_type;
PtrVectorIterator() {
}
PtrVectorIterator(const PtrVectorIterator& other)
: internal_iterator_(other.internal_iterator_) {
}
PtrVectorIterator& operator=(const PtrVectorIterator& other) {
if (this != &other) {
internal_iterator_ = other.internal_iterator_;
}
return *this;
}
// Comparisons.
inline bool operator==(const PtrVectorIterator& other) const {
return internal_iterator_ == other.internal_iterator_;
}
inline bool operator!=(const PtrVectorIterator& other) const {
return internal_iterator_ != other.internal_iterator_;
}
inline bool operator<(const PtrVectorIterator& other) const {
return internal_iterator_ < other.internal_iterator_;
}
inline bool operator<=(const PtrVectorIterator& other) const {
return internal_iterator_ <= other.internal_iterator_;
}
inline bool operator>(const PtrVectorIterator& other) const {
return internal_iterator_ > other.internal_iterator_;
}
inline bool operator>=(const PtrVectorIterator& other) const {
return internal_iterator_ >= other.internal_iterator_;
}
// Increment/decrement.
inline PtrVectorIterator& operator++() {
++internal_iterator_;
return *this;
}
PtrVectorIterator operator++(int) {
PtrVectorIterator result(*this);
++(*this);
return result;
}
inline PtrVectorIterator& operator--() {
--internal_iterator_;
return *this;
}
PtrVectorIterator operator--(int) {
PtrVectorIterator result(*this);
--(*this);
return result;
}
// Compound assignment.
inline PtrVectorIterator& operator+=(difference_type n) {
internal_iterator_ += n;
return *this;
}
inline PtrVectorIterator& operator-=(difference_type n) {
internal_iterator_ -= n;
return *this;
}
// Note: + operator with difference_type on the left is defined out-of-line.
PtrVectorIterator operator+(difference_type n) const {
return PtrVectorIterator(internal_iterator_ + n);
}
PtrVectorIterator operator-(difference_type n) const {
return PtrVectorIterator(internal_iterator_ - n);
}
difference_type operator-(const PtrVectorIterator &other) const {
return PtrVectorIterator(internal_iterator_ - other.internal_iterator_);
}
// Dereference.
/**
* @brief Check whether the element at the current iterator position is
* NULL.
* @return Whether the element at the current iterator position is NULL.
**/
inline bool isNull() const {
return (null_allowed && (*internal_iterator_ == nullptr));
}
/**
* @brief Delete the element at the current iterator position and replace
* it with a new value.
*
* @param value_ptr The new value to place at the current position. The
* PtrVector takes ownership of the pointed-to value.
**/
void replaceValue(T *value_ptr) {
if (!null_allowed) {
DCHECK(value_ptr != nullptr);
}
if (!null_allowed || (*internal_iterator_ != nullptr)) {
delete *internal_iterator_;
}
*internal_iterator_ = value_ptr;
}
/**
* @brief Replace the element at the current iterator position, and
* release ownership and return the original value.
*
* @param value_ptr The new value to place at the current position. The
* PtrVector takes ownership of the pointed-to value.
* @return A pointer to the original value at the current position. The
* PtrVector no longer owns the pointed-to object, which should now
* be managed by the caller.
**/
T* releaseAndReplaceElement(T *value_ptr) {
if (!null_allowed) {
DCHECK(value_ptr != nullptr);
}
T *original_value = *internal_iterator_;
*internal_iterator_ = value_ptr;
return original_value;
}
inline T& operator*() const {
if (null_allowed) {
DCHECK(!isNull());
}
return **internal_iterator_;
}
inline T* operator->() const {
if (null_allowed) {
DCHECK(!isNull());
}
return *internal_iterator_;
}
// Offset dereference. Potentially unsafe if null_allowed.
T& operator[](difference_type n) const {
if (null_allowed) {
DCHECK(internal_iterator_[n] != nullptr);
}
return *(internal_iterator_[n]);
}
private:
explicit PtrVectorIterator(const typename std::vector<T*>::iterator &internal_iterator)
: internal_iterator_(internal_iterator) {
}
typename std::vector<T*>::iterator internal_iterator_;
friend class PtrVector;
friend class PtrVectorConstIterator;
};
/**
* @brief Const iterator over the contents of a PtrVector.
* @warning If null_allowed is true, then always check that isNull() is false
* before attempting to dereference.
**/
class PtrVectorConstIterator : public std::iterator<std::input_iterator_tag, const T> {
public:
typedef typename std::iterator<std::input_iterator_tag, T>::difference_type difference_type;
PtrVectorConstIterator() {
}
PtrVectorConstIterator(const PtrVectorConstIterator& other)
: internal_iterator_(other.internal_iterator_) {
}
PtrVectorConstIterator(const PtrVectorIterator& other) // NOLINT(runtime/explicit) - allow implicit conversion
: internal_iterator_(other.internal_iterator_) {
}
PtrVectorConstIterator& operator=(const PtrVectorConstIterator& other) {
if (this != &other) {
internal_iterator_ = other.internal_iterator_;
}
return *this;
}
PtrVectorConstIterator& operator=(const PtrVectorIterator& other) {
internal_iterator_ = other.internal_iterator_;
return *this;
}
// Comparisons.
inline bool operator==(const PtrVectorConstIterator& other) const {
return internal_iterator_ == other.internal_iterator_;
}
inline bool operator!=(const PtrVectorConstIterator& other) const {
return internal_iterator_ != other.internal_iterator_;
}
inline bool operator<(const PtrVectorConstIterator& other) const {
return internal_iterator_ < other.internal_iterator_;
}
inline bool operator<=(const PtrVectorConstIterator& other) const {
return internal_iterator_ <= other.internal_iterator_;
}
inline bool operator>(const PtrVectorConstIterator& other) const {
return internal_iterator_ > other.internal_iterator_;
}
inline bool operator>=(const PtrVectorConstIterator& other) const {
return internal_iterator_ >= other.internal_iterator_;
}
// Increment/decrement.
inline PtrVectorConstIterator& operator++() {
++internal_iterator_;
return *this;
}
PtrVectorConstIterator operator++(int) {
PtrVectorConstIterator result(*this);
++(*this);
return result;
}
inline PtrVectorConstIterator& operator--() {
--internal_iterator_;
return *this;
}
PtrVectorConstIterator operator--(int) {
PtrVectorConstIterator result(*this);
--(*this);
return result;
}
// Compound assignment.
inline PtrVectorConstIterator& operator+=(difference_type n) {
internal_iterator_ += n;
return *this;
}
inline PtrVectorConstIterator& operator-=(difference_type n) {
internal_iterator_ -= n;
return *this;
}
// Note: + operator with difference_type on the left is defined out-of-line.
PtrVectorConstIterator operator+(difference_type n) const {
return PtrVectorConstIterator(internal_iterator_ + n);
}
PtrVectorConstIterator operator-(difference_type n) const {
return PtrVectorConstIterator(internal_iterator_ - n);
}
difference_type operator-(const PtrVectorConstIterator &other) const {
return PtrVectorConstIterator(internal_iterator_ - other.internal_iterator_);
}
// Dereference.
/**
* @brief Check whether the element at the current iterator position is
* NULL.
* @return Whether the element at the current iterator position is NULL.
**/
inline bool isNull() const {
return (null_allowed && (*internal_iterator_ == nullptr));
}
inline const T& operator*() const {
if (null_allowed) {
DCHECK(!isNull());
}
return **internal_iterator_;
}
inline const T* operator->() const {
if (null_allowed) {
DCHECK(!isNull());
}
return *internal_iterator_;
}
// Offset dereference. Potentially unsafe if null_allowed.
const T& operator[](difference_type n) const {
if (null_allowed) {
DCHECK(internal_iterator_[n] != nullptr);
}
return *(internal_iterator_[n]);
}
private:
explicit PtrVectorConstIterator(const typename std::vector<T*>::const_iterator &internal_iterator)
: internal_iterator_(internal_iterator) {
}
typename std::vector<T*>::const_iterator internal_iterator_;
friend class PtrVector;
};
/**
* @brief Input iterator over the contents of a PtrVector which automatically
* skips over NULL entries.
**/
class PtrVectorConstSkipIterator : public std::iterator<std::input_iterator_tag, const T> {
public:
typedef typename std::iterator<std::input_iterator_tag, T>::difference_type difference_type;
PtrVectorConstSkipIterator()
: parent_vector_(nullptr) {
}
PtrVectorConstSkipIterator(const PtrVectorConstSkipIterator& other)
: internal_iterator_(other.internal_iterator_),
parent_vector_(other.parent_vector_) {
}
PtrVectorConstSkipIterator& operator=(const PtrVectorConstSkipIterator& other) {
if (this != &other) {
internal_iterator_ = other.internal_iterator_;
parent_vector_ = other.parent_vector_;
}
return *this;
}
// Comparisons.
inline bool operator==(const PtrVectorConstSkipIterator& other) const {
return internal_iterator_ == other.internal_iterator_;
}
inline bool operator!=(const PtrVectorConstSkipIterator& other) const {
return internal_iterator_ != other.internal_iterator_;
}
inline bool operator<(const PtrVectorConstSkipIterator& other) const {
return internal_iterator_ < other.internal_iterator_;
}
inline bool operator<=(const PtrVectorConstSkipIterator& other) const {
return internal_iterator_ <= other.internal_iterator_;
}
inline bool operator>(const PtrVectorConstSkipIterator& other) const {
return internal_iterator_ > other.internal_iterator_;
}
inline bool operator>=(const PtrVectorConstSkipIterator& other) const {
return internal_iterator_ >= other.internal_iterator_;
}
// Increment/decrement.
inline PtrVectorConstSkipIterator& operator++() {
++internal_iterator_;
DCHECK(parent_vector_ != nullptr);
while (internal_iterator_ != parent_vector_->end()) {
if (*internal_iterator_ != nullptr) {
break;
}
++internal_iterator_;
}
return *this;
}
PtrVectorConstSkipIterator operator++(int) {
PtrVectorConstSkipIterator result(*this);
++(*this);
return result;
}
inline const T& operator*() const {
return **internal_iterator_;
}
inline const T* operator->() const {
return *internal_iterator_;
}
private:
explicit PtrVectorConstSkipIterator(const typename std::vector<T*>::const_iterator &internal_iterator,
const std::vector<T*> *parent_vector)
: internal_iterator_(internal_iterator), parent_vector_(parent_vector) {
DCHECK(parent_vector_ != nullptr);
while ((internal_iterator_ != parent_vector_->end()) && (*internal_iterator_ == nullptr)) {
++internal_iterator_;
}
}
typename std::vector<T*>::const_iterator internal_iterator_;
const std::vector<T*> *parent_vector_;
friend class PtrVector;
};
typedef typename std::vector<T*>::size_type size_type;
typedef T value_type;
typedef PtrVectorIterator iterator;
typedef PtrVectorConstIterator const_iterator;
typedef PtrVectorConstSkipIterator const_skip_iterator;
PtrVector() {
}
~PtrVector() {
for (typename std::vector<T*>::iterator it = internal_vector_.begin();
it != internal_vector_.end();
++it) {
if (!null_allowed || (*it != nullptr)) {
delete *it;
}
}
}
inline size_type size() const {
return internal_vector_.size();
}
inline size_type max_size() const {
return internal_vector_.max_size();
}
inline size_type capacity() const {
return internal_vector_.capacity();
}
inline bool empty() const {
return internal_vector_.empty();
}
/**
* @brief Check whether this vector contains any actual objects. Unlike
* empty(), this returns true if the vector has some elements, but
* they are all NULL.
*
* @return Whether this PtrVector is empty of actual objects.
**/
bool emptyNullCheck() const {
if (null_allowed) {
for (typename std::vector<T*>::const_iterator it = internal_vector_.begin();
it != internal_vector_.end();
++it) {
if (*it != nullptr) {
return false;
}
}
return true;
} else {
return empty();
}
}
inline void reserve(size_type n) {
internal_vector_.reserve(n);
}
// Iterators
iterator begin() {
return iterator(internal_vector_.begin());
}
iterator end() {
return iterator(internal_vector_.end());
}
const_iterator begin() const {
return const_iterator(internal_vector_.begin());
}
const_iterator end() const {
return const_iterator(internal_vector_.end());
}
/**
* @brief Get an iterator at the beginning of this PtrVector which
* automatically skips over NULL entries.
*
* @return An iterator at the beginning of this PtrVector which automatically
* skips over NULL entries.
**/
const_skip_iterator begin_skip() const {
return const_skip_iterator(internal_vector_.begin(), &internal_vector_);
}
/**
* @brief Get an iterator at one past the end of this PtrVector which
* automatically skips over NULL entries.
*
* @return An iterator at one past the end of this PtrVector which
* automatically skips over NULL entries.
**/
const_skip_iterator end_skip() const {
return const_skip_iterator(internal_vector_.end(), &internal_vector_);
}
/**
* @brief Check whether the element at the specified position is NULL.
*
* @param n The position in this PtrVector to check.
* @return Whether the element at position n is NULL.
**/
inline bool elementIsNull(const size_type n) const {
if (null_allowed && (internal_vector_[n] == nullptr)) {
return true;
} else {
return false;
}
}
/**
* @brief Check whether the element at the specified position is NULL.
* @note This is similar to elementIsNull(), but uses std::vector::at(),
* which throws std::out_of_range exceptions.
*
* @param n The position in this PtrVector to check.
* @return Whether the element at position n is NULL.
**/
inline bool elementIsNullAt(const size_type n) const {
if (null_allowed && (internal_vector_.at(n) == nullptr)) {
return true;
} else {
return false;
}
}
inline T& front() {
if (null_allowed) {
typename std::vector<T*>::iterator it = internal_vector_.begin();
while ((it != internal_vector_.end()) && (*it == nullptr)) {
++it;
}
return **it;
} else {
return *(internal_vector_.front());
}
}
inline const T& front() const {
if (null_allowed) {
typename std::vector<T*>::const_iterator it = internal_vector_.begin();
while ((it != internal_vector_.end()) && (*it == nullptr)) {
++it;
}
return **it;
} else {
return *(internal_vector_.front());
}
}
inline T& back() {
if (null_allowed) {
typename std::vector<T*>::reverse_iterator it = internal_vector_.rbegin();
while ((it != internal_vector_.rend()) && (*it == nullptr)) {
++it;
}
return **it;
} else {
return *(internal_vector_.back());
}
}
inline const T& back() const {
if (null_allowed) {
typename std::vector<T*>::const_reverse_iterator it = internal_vector_.rbegin();
while ((it != internal_vector_.rend()) && (*it == nullptr)) {
++it;
}
return **it;
} else {
return *(internal_vector_.back());
}
}
inline T& operator[](const size_type n) {
if (null_allowed) {
DCHECK(!elementIsNull(n));
}
return *(internal_vector_[n]);
}
inline const T& operator[](const size_type n) const {
if (null_allowed) {
DCHECK(!elementIsNull(n));
}
return *(internal_vector_[n]);
}
inline T& at(const size_type n) {
if (null_allowed) {
DCHECK(!elementIsNullAt(n));
}
return *(internal_vector_.at(n));
}
inline const T& at(const size_type n) const {
if (null_allowed) {
DCHECK(!elementIsNullAt(n));
}
return *(internal_vector_.at(n));
}
inline void push_back(T *value_ptr) {
if (!null_allowed) {
DCHECK(value_ptr != nullptr);
}
internal_vector_.push_back(value_ptr);
}
/**
* @brief Delete an element at the specified index and replace it with a new
* value.
*
* @param n The position of the element to replace.
* @param value_ptr The new value to place at position n. This PtrVector
* takes ownership of the pointed-to value.
**/
void replaceElement(const size_type n, T *value_ptr) {
if (!null_allowed) {
DCHECK(value_ptr != nullptr);
}
if (!null_allowed || (internal_vector_[n] != nullptr)) {
delete internal_vector_[n];
}
internal_vector_[n] = value_ptr;
}
/**
* @brief Replace an element at the specified index with a new value, and
* release ownership and return the original value.
*
* @param n The position of the element to replace.
* @param value_ptr The new value to place at position n. This PtrVector
* takes ownership of the pointed-to value.
* @return A pointer to the original value at position n. This PtrVector no
* longer owns the pointed-to object, which should now be managed
* by the caller.
**/
T* releaseAndReplaceElement(const size_type n, T *value_ptr) {
if (!null_allowed) {
DCHECK(value_ptr != nullptr);
}
T *original_value = internal_vector_[n];
internal_vector_[n] = value_ptr;
return original_value;
}
/**
* @brief Delete an element and set it to null. Only usable if null_allowed
* is true.
*
* @param n The position of the element to delete.
**/
void deleteElement(const size_type n) {
DCHECK(null_allowed);
if (internal_vector_[n] != nullptr) {
delete internal_vector_[n];
internal_vector_[n] = nullptr;
}
}
/**
* @brief Delete the last element and truncate the vector. Only usable if
* null_allowed is false.
**/
void removeBack() {
DCHECK(!null_allowed);
DCHECK(!internal_vector_.empty());
delete internal_vector_.back();
internal_vector_.resize(internal_vector_.size() - 1);
}
/**
* @brief Get a const reference to the internal vector of pointers.
*
* @return A const reference to the internal vector of pointers.
**/
const std::vector<T*>& getInternalVector() const {
return internal_vector_;
}
/**
* @brief Get a mutable pointer to the internal vector of pointers.
* @warning Only call this if you really know what you are doing.
*
* @return A mutable pointer to the internal vector of pointers.
**/
std::vector<T*>* getInternalVectorMutable() {
return &internal_vector_;
}
private:
std::vector<T*> internal_vector_;
DISALLOW_COPY_AND_ASSIGN(PtrVector);
};
template <typename T>
typename PtrVector<T>::PtrVectorIterator operator+(
const typename PtrVector<T>::PtrVectorIterator::difference_type n,
const typename PtrVector<T>::PtrVectorIterator &it) {
return it + n;
}
/** @} */
} // namespace quickstep
#endif // QUICKSTEP_UTILITY_PTR_VECTOR_HPP_