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
|
/**
* @file
* @brief Fixed size 2D vector class that asserts if you do something bad.
**/
#pragma once
#include <algorithm>
#include <vector>
#include <map>
#include "fixedvector.h"
using std::pair;
using std::vector;
template <class TYPE, int WIDTH, int HEIGHT> class FixedArray
{
public:
typedef TYPE value_type;
typedef TYPE& reference;
typedef const TYPE& const_reference;
typedef TYPE* pointer;
typedef const TYPE* const_pointer;
typedef unsigned long size_type;
typedef long difference_type;
// operator[] should return one of these to avoid breaking
// client code (if inlining is on there won't be a speed hit)
typedef FixedVector<TYPE, HEIGHT> Column;
public:
~FixedArray() {}
FixedArray() {}
FixedArray(TYPE def)
{
init(def);
}
FixedArray(const FixedArray &other)
{
copy_from(other);
}
FixedArray& operator = (const FixedArray &other)
{
copy_from(other);
return *this;
}
public:
// ----- Size -----
bool empty() const { return WIDTH == 0 || HEIGHT == 0; }
int size() const { return WIDTH*HEIGHT; }
int width() const { return WIDTH; }
int height() const { return HEIGHT; }
// ----- Access -----
Column& operator[](unsigned long index) { return mData[index]; }
const Column& operator[](unsigned long index) const
{
return mData[index];
}
template<class Indexer>
TYPE& operator () (const Indexer &i)
{
return mData[i.x][i.y];
}
template<class First, class Second>
TYPE& operator () (const std::pair<First,Second> &p)
{
return mData[p.first][p.second];
}
template<class Indexer>
const TYPE& operator () (const Indexer &i) const
{
return mData[i.x][i.y];
}
template<class First, class Second>
const TYPE& operator () (const pair<First,Second> &p) const
{
return mData[p.first][p.second];
}
void init(const TYPE& def)
{
for (auto &col : mData)
col.init(def);
}
protected:
FixedVector<Column, WIDTH> mData;
void copy_from(const FixedArray &other)
{
for (int i = 0; i < width(); i++)
for (int j = 0; j < height(); j++)
(*this)[i][j] = other[i][j];
}
};
// A fixed array centered around the origin.
template <class TYPE, int RADIUS> class SquareArray
{
public:
typedef TYPE value_type;
typedef TYPE& reference;
typedef const TYPE& const_reference;
typedef TYPE* pointer;
typedef const TYPE* const_pointer;
typedef unsigned long size_type;
typedef long difference_type;
public:
~SquareArray() {}
SquareArray() {}
SquareArray(TYPE def)
{
init(def);
}
public:
// ----- Size -----
bool empty() const { return data.empty(); }
int size() const { return data.size(); }
int width() const { return data.width(); }
int height() const { return data.height(); }
// ----- Access -----
template<class Indexer> TYPE& operator () (const Indexer &i)
{
return data[i.x+RADIUS][i.y+RADIUS];
}
template<class Indexer> const TYPE& operator () (const Indexer &i) const
{
return data[i.x+RADIUS][i.y+RADIUS];
}
void init(const TYPE& def)
{
data.init(def);
}
protected:
FixedArray<TYPE, 2*RADIUS+1, 2*RADIUS+1> data;
};
|