Loading...
Searching...
No Matches
countingIterator.h
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_EXEC_VDF_COUNTING_ITERATOR
8#define PXR_EXEC_VDF_COUNTING_ITERATOR
9
10#include "pxr/pxr.h"
11
12#include <iterator>
13#include <type_traits>
14
15PXR_NAMESPACE_OPEN_SCOPE
16
20template <typename T>
22public:
23 using iterator_category = std::random_access_iterator_tag;
24 using value_type = const T;
25 using reference = const T;
26 using pointer = const T *;
27 using difference_type = std::make_signed_t<value_type>;
28
29 Vdf_CountingIterator() : _integer() {}
30 explicit Vdf_CountingIterator(T i) : _integer(i) {}
31
32 reference operator*() const { return _integer; }
33 pointer operator->() const { return &_integer; }
34 value_type operator[](const difference_type n) const {
35 return _integer + n;
36 }
37
38 Vdf_CountingIterator &operator++() {
39 ++_integer;
40 return *this;
41 }
42
43 Vdf_CountingIterator operator++(int) {
44 Vdf_CountingIterator r(*this);
45 ++_integer;
46 return r;
47 }
48
49 Vdf_CountingIterator operator+(const difference_type n) const {
50 Vdf_CountingIterator r(*this);
51 r._integer += n;
52 return r;
53 }
54
55 Vdf_CountingIterator &operator+=(const difference_type n) {
56 _integer += n;
57 return *this;
58 }
59
60 Vdf_CountingIterator &operator--() {
61 --_integer;
62 return *this;
63 }
64
65 Vdf_CountingIterator operator--(int) {
66 Vdf_CountingIterator r(*this);
67 --_integer;
68 return r;
69 }
70
71 Vdf_CountingIterator operator-(const difference_type n) const {
72 Vdf_CountingIterator r(*this);
73 r._integer -= n;
74 return r;
75 }
76
77 Vdf_CountingIterator &operator-=(const difference_type n) {
78 _integer -= n;
79 return *this;
80 }
81
82 difference_type operator-(const Vdf_CountingIterator &rhs) const {
83 return _integer - rhs._integer;
84 }
85
86 bool operator==(const Vdf_CountingIterator &rhs) const {
87 return _integer == rhs._integer;
88 }
89
90 bool operator!=(const Vdf_CountingIterator &rhs) const {
91 return _integer != rhs._integer;
92 }
93
94 bool operator<(const Vdf_CountingIterator &rhs) const {
95 return _integer < rhs._integer;
96 }
97
98 bool operator<=(const Vdf_CountingIterator &rhs) const {
99 return _integer <= rhs._integer;
100 }
101
102 bool operator>(const Vdf_CountingIterator &rhs) const {
103 return _integer > rhs._integer;
104 }
105
106 bool operator>=(const Vdf_CountingIterator &rhs) const {
107 return _integer >= rhs._integer;
108 }
109
110private:
111 T _integer;
112};
113
114PXR_NAMESPACE_CLOSE_SCOPE
115
116#endif
Random access counting iterator that simply operates on an underlying integer index.