1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 // File: vogl_fixed_array.h
30 #include "vogl_core.h"
34 template <typename T, uint N>
38 typedef T element_type;
49 inline uint size() const
54 inline uint size_in_bytes() const
59 inline const T &operator[](uint i) const
64 inline T &operator[](uint i)
70 inline const T &front() const
79 inline const T &back() const
88 inline const T *begin() const
97 inline const T *end() const
106 inline const T *get_ptr() const
115 inline void set_all(const T &val)
117 for (uint i = 0; i < N; i++)
121 inline void bitwise_zero(void)
123 utils::zero_this(this);
126 inline void sort(void)
128 //std::sort(begin(), end());
129 introsort(begin(), end());
136 template <typename T, uint W, uint H>
142 total_elements = W * H,
147 typedef T value_type;
148 typedef T &reference;
149 typedef const T &const_reference;
151 typedef const T *const_pointer;
153 inline fixed_array2D()
158 inline fixed_array2D(const fixed_array2D &other)
163 inline fixed_array2D &operator=(const fixed_array2D &rhs)
168 for (uint i = 0; i < total_elements; i++)
169 m_vec[i] = rhs.m_vec[i];
179 void resize(uint width, uint height, bool preserve = true)
181 (void)width, (void)height, (void)preserve;
182 // blindly ignoring resize for compat with vector2D
185 inline uint width() const
189 inline uint height() const
193 inline uint size() const
195 return total_elements;
198 inline uint size_in_bytes() const
200 return total_elements * sizeof(T);
203 inline const T *get_ptr() const
212 inline const T &operator[](uint i) const
214 return m_vec[math::open_range_check(i, total_elements)];
216 inline T &operator[](uint i)
218 return m_vec[math::open_range_check(i, total_elements)];
221 inline const T &operator()(uint x, uint y) const
223 VOGL_ASSERT((x < array_width) && (y < array_height));
224 return m_vec[x + y * array_width];
227 inline T &operator()(uint x, uint y)
229 VOGL_ASSERT((x < array_width) && (y < array_height));
230 return m_vec[x + y * array_width];
233 inline const T &at(uint x, uint y) const
235 if ((x >= array_width) || (y >= array_height))
240 return m_vec[x + y * array_width];
243 inline T &at(uint x, uint y)
245 if ((x >= array_width) || (y >= array_height))
250 return m_vec[x + y * array_width];
253 inline const T &at_clamped(int x, int y) const
255 x = math::clamp<int>(x, 0, array_width - 1);
256 y = math::clamp<int>(y, 0, array_height - 1);
257 return m_vec[x + y * array_width];
260 inline T &at_clamped(int x, int y)
262 x = math::clamp<int>(x, 0, array_width - 1);
263 y = math::clamp<int>(y, 0, array_height - 1);
264 return m_vec[x + y * array_width];
267 inline const T &at_wrapped(int x, int y) const
269 x = math::posmod(x, array_width);
270 y = math::posmod(y, array_height);
271 return m_vec[x + y * array_width];
274 inline T &at_wrapped(int x, int y)
276 x = math::posmod(x, array_width);
277 y = math::posmod(y, array_height);
278 return m_vec[x + y * array_width];
281 inline void swap(fixed_array2D &other)
283 m_vec.swap(other.m_vec);
284 std::swap(array_width, other.array_width);
285 std::swap(array_height, other.array_height);
288 inline void set_all(const T &x)
290 for (uint i = 0; i < total_elements; i++)
294 inline void bitwise_zero(void)
296 utils::zero_this(this);
299 inline void extract_block_clamped(fixed_array2D &dst, uint dst_x, uint dst_y, int src_x, int src_y, uint w, uint h) const
301 for (uint y = 0; y < h; y++)
302 for (uint x = 0; x < w; x++)
303 dst.at(dst_x + x, dst_y + y) = at_clamped(src_x + x, src_y + y);
306 inline void extract_block_wrapped(fixed_array2D &dst, uint dst_x, uint dst_y, int src_x, int src_y, uint w, uint h) const
308 for (uint y = 0; y < h; y++)
309 for (uint x = 0; x < w; x++)
310 dst.at(dst_x + x, dst_y + y) = at_wrapped(src_x + x, src_y + y);
313 inline bool operator==(const fixed_array2D &rhs) const
315 for (uint i = 0; i < total_elements; i++)
316 if (m_vec[i] != rhs.m_vec[i])