summaryrefslogtreecommitdiff
path: root/src/amd/compiler/aco_util.h
blob: ec77ba5571658e806878819dbf26a7f9ffc75ee2 (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
/*
 * Copyright Michael Schellenberger Costa
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

#ifndef ACO_UTIL_H
#define ACO_UTIL_H

#include <cassert>
#include <iterator>

namespace aco {

/*! \brief      Definition of a span object
*
*   \details    A "span" is an "array view" type for holding a view of contiguous
*               data. The "span" object does not own the data itself.
*/
template <typename T>
class span {
public:
   using value_type             = T;
   using pointer                = value_type*;
   using const_pointer          = const value_type*;
   using reference              = value_type&;
   using const_reference        = const value_type&;
   using iterator               = pointer;
   using const_iterator         = const_pointer;
   using reverse_iterator       = std::reverse_iterator<iterator>;
   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
   using size_type              = std::size_t;
   using difference_type        = std::ptrdiff_t;

   /*! \brief                  Compiler generated default constructor
   */
   constexpr span() = default;

   /*! \brief                 Constructor taking a pointer and the length of the span
   *   \param[in]   data      Pointer to the underlying data array
   *   \param[in]   length    The size of the span
   */
   constexpr span(pointer data, const size_type length)
       : data{ data } , length{ length } {}

   /*! \brief                 Returns an iterator to the begin of the span
   *   \return                data
   */
   constexpr iterator begin() noexcept {
      return data;
   }

   /*! \brief                 Returns a const_iterator to the begin of the span
   *   \return                data
   */
   constexpr const_iterator begin() const noexcept {
      return data;
   }

   /*! \brief                 Returns an iterator to the end of the span
   *   \return                data + length
   */
   constexpr iterator end() noexcept {
      return std::next(data, length);
   }

   /*! \brief                 Returns a const_iterator to the end of the span
   *   \return                data + length
   */
   constexpr const_iterator end() const noexcept {
      return std::next(data, length);
   }

   /*! \brief                 Returns a const_iterator to the begin of the span
   *   \return                data
   */
   constexpr const_iterator cbegin() const noexcept {
      return data;
   }

   /*! \brief                 Returns a const_iterator to the end of the span
   *   \return                data + length
   */
   constexpr const_iterator cend() const noexcept {
      return std::next(data, length);
   }

   /*! \brief                 Returns a reverse_iterator to the end of the span
   *   \return                reverse_iterator(end())
   */
   constexpr reverse_iterator rbegin() noexcept {
      return reverse_iterator(end());
   }

   /*! \brief                 Returns a const_reverse_iterator to the end of the span
   *   \return                reverse_iterator(end())
   */
   constexpr const_reverse_iterator rbegin() const noexcept {
      return const_reverse_iterator(end());
   }

   /*! \brief                 Returns a reverse_iterator to the begin of the span
   *   \return                reverse_iterator(begin())
   */
   constexpr reverse_iterator rend() noexcept {
      return reverse_iterator(begin());
   }

   /*! \brief                 Returns a const_reverse_iterator to the begin of the span
   *   \return                reverse_iterator(begin())
   */
   constexpr const_reverse_iterator rend() const noexcept {
      return const_reverse_iterator(begin());
   }

   /*! \brief                 Returns a const_reverse_iterator to the end of the span
   *   \return                rbegin()
   */
   constexpr const_reverse_iterator crbegin() const noexcept {
      return const_reverse_iterator(cend());
   }

   /*! \brief                 Returns a const_reverse_iterator to the begin of the span
   *   \return                rend()
   */
   constexpr const_reverse_iterator crend() const noexcept {
      return const_reverse_iterator(cbegin());
   }

   /*! \brief                 Unchecked access operator
   *   \param[in] index       Index of the element we want to access
   *   \return                *(std::next(data, index))
   */
   constexpr reference operator[](const size_type index) noexcept {
      assert(length > index);
      return *(std::next(data, index));
   }

   /*! \brief                 Unchecked const access operator
   *   \param[in] index       Index of the element we want to access
   *   \return                *(std::next(data, index))
   */
   constexpr const_reference operator[](const size_type index) const noexcept {
      assert(length > index);
      return *(std::next(data, index));
   }

   /*! \brief                 Returns a reference to the last element of the span
   *   \return                *(std::next(data, length - 1))
   */
   constexpr reference back() noexcept {
      assert(length > 0);
      return *(std::next(data, length - 1));
   }

   /*! \brief                 Returns a const_reference to the last element of the span
   *   \return                *(std::next(data, length - 1))
   */
   constexpr const_reference back() const noexcept {
      assert(length > 0);
      return *(std::next(data, length - 1));
   }

   /*! \brief                 Returns a reference to the first element of the span
   *   \return                *begin()
   */
   constexpr reference front() noexcept {
      assert(length > 0);
      return *begin();
   }

   /*! \brief                 Returns a const_reference to the first element of the span
   *   \return                *cbegin()
   */
   constexpr const_reference front() const noexcept {
      assert(length > 0);
      return *cbegin();
   }

   /*! \brief                 Returns true if the span is empty
   *   \return                length == 0
   */
   constexpr bool empty() const noexcept {
      return length == 0;
   }

   /*! \brief                 Returns the size of the span
   *   \return                length == 0
   */
   constexpr size_type size() const noexcept {
      return length;
   }

   /*! \brief                 Decreases the size of the span by 1
   */
   constexpr void pop_back() noexcept {
      assert(length > 0);
      --length;
   }

   /*! \brief                 Clears the span
   */
   constexpr void clear() noexcept {
      data = nullptr;
      length = 0;
   }

private:
   pointer data{ nullptr };   //!> Pointer to the underlying data array
   size_type length{ 0 };     //!> Size of the span
};

} // namespace aco

#endif // ACO_UTIL_H