Makefile: remove spurious tab
[xcsoar.git] / src / Util / AllocatedGrid.hpp
blob6671522a530a2f73ffd21fba4eda52dc69a21f31
1 /*
2 * Copyright (C) 2010 Max Kellermann <max@duempel.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef XCSOAR_ALLOCATED_GRID_HPP
31 #define XCSOAR_ALLOCATED_GRID_HPP
33 #include "AllocatedArray.hpp"
35 #include <assert.h>
37 /**
38 * A two dimensional array allocated on the heap with a length
39 * determined at runtime.
41 template<class T>
42 class AllocatedGrid {
43 protected:
44 AllocatedArray<T> array;
45 unsigned width, height;
47 public:
48 typedef typename AllocatedArray<T>::iterator iterator;
49 typedef typename AllocatedArray<T>::const_iterator const_iterator;
51 constexpr AllocatedGrid():width(0), height(0) {}
52 AllocatedGrid(unsigned _width, unsigned _height)
53 :array(_width * _height), width(_width), height(_height) {}
55 constexpr bool IsDefined() const {
56 return array.size() > 0;
59 constexpr unsigned GetWidth() const {
60 return width;
63 constexpr unsigned GetHeight() const {
64 return height;
67 constexpr unsigned GetSize() const {
68 return width * height;
71 const T &Get(unsigned x, unsigned y) const {
72 assert(x < width);
73 assert(y < height);
75 return array[y * width + x];
78 T &Get(unsigned x, unsigned y) {
79 assert(x < width);
80 assert(y < height);
82 return array[y * width + x];
85 const T &GetLinear(unsigned i) const {
86 assert(i < GetSize());
88 return array[i];
91 T &GetLinear(unsigned i) {
92 assert(i < GetSize());
94 return array[i];
97 iterator begin() {
98 return array.begin();
101 constexpr const_iterator begin() const {
102 return array.begin();
105 iterator end() {
106 return begin() + GetSize();
109 constexpr const_iterator end() const {
110 return begin() + GetSize();
113 iterator GetPointerAt(unsigned x, unsigned y) {
114 assert(x < width);
115 assert(y < height);
117 return begin() + y * width + x;
120 const_iterator GetPointerAt(unsigned x, unsigned y) const {
121 assert(x < width);
122 assert(y < height);
124 return begin() + y * width + x;
127 void Reset() {
128 width = height = 0;
129 array.ResizeDiscard(0);
132 void GrowDiscard(unsigned _width, unsigned _height) {
133 array.GrowDiscard(_width * _height);
134 width = _width;
135 height = _height;
139 * Resize the grid, preserving as many old values as fit into the
140 * new dimensions, and fill newly allocated array slots.
142 void GrowPreserveFill(unsigned _width, unsigned _height, const T &fill=T()) {
143 if (_width < width) {
144 const unsigned h = std::min(height, _height);
145 const auto end = array.begin() + h * width;
147 for (auto in = array.begin() + width, out = array.begin() + _width;
148 in < end; in += width) {
149 out = std::move(in, in + _width, out);
152 width = _width;
155 array.GrowPreserve(_width * _height, width * height);
157 if (_width > width) {
158 const unsigned h = std::min(height, _height);
159 const auto end = array.begin();
161 for (auto in = array.begin() + (h - 1) * width,
162 out = array.begin() + (h - 1) * _width + width;
163 in < end; in -= width, out -= _width) {
164 std::move_backward(in, in + width, out);
165 std::fill(out, out + _width - width, fill);
168 width = _width;
171 std::fill(array.begin() + width * height, array.end(), fill);
173 height = _height;
177 #endif