File: Grid.h

package info (click to toggle)
pentobi 29.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,892 kB
  • sloc: cpp: 25,719; javascript: 875; xml: 40; makefile: 13; sh: 6
file content (214 lines) | stat: -rw-r--r-- 5,616 bytes parent folder | download
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
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Grid.h
    @author Markus Enzenberger
    @copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------

#ifndef LIBBOARDGAME_BASE_GRID_H
#define LIBBOARDGAME_BASE_GRID_H

#include <algorithm>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <type_traits>
#include "Geometry.h"

namespace libboardgame_base {

using namespace std;

//-----------------------------------------------------------------------------

template<class T>
string grid_to_string(const T& grid, const Geometry<typename T::Point>& geo)
{
    ostringstream buffer;
    size_t max_len = 0;
    for (auto p : geo)
    {
        buffer.str("");
        buffer << grid[p];
        max_len = max(max_len, buffer.str().length());
    }
    buffer.str("");
    auto width = geo.get_width();
    auto height = geo.get_height();
    string empty(max_len, ' ');
    for (unsigned y = 0; y < height; ++y)
    {
        for (unsigned x = 0; x < width; ++x)
        {
            auto p = geo.get_point(x, y);
            if (! p.is_null())
                buffer << setw(int(max_len)) << grid[p];
            else
                buffer << empty;
            if (x < width - 1)
                buffer << ' ';
        }
        buffer << '\n';
    }
    return buffer.str();
}

//-----------------------------------------------------------------------------

template<class P, typename T> class GridExt;

/** Elements assigned to on-board points.
    The elements must be default-constructible. This class is a POD if the
    element type is a POD.
    @tparam P An instantiation of Point (or compatible class)
    @tparam T The element type. */
template<class P, typename T>
class Grid
{
    friend class GridExt<P, T>; // for GridExt::copy_from(Grid)

public:
    using Point = P;

    using Geometry = libboardgame_base::Geometry<P>;


    T& operator[](const Point& p);

    const T& operator[](const Point& p) const;

    /** Fill all on-board points for a given geometry with a value. */
    void fill(const T& val, const Geometry& geo);

    /** Fill points with a value. */
    void fill_all(const T& val);

    string to_string(const Geometry& geo) const;

    void copy_from(const Grid& grid, const Geometry& geo);

    /** Specialized version for trivially copyable elements.
        Can be used instead of copy_from if the compiler is not smart enough to
        figure out that it can use memcpy.
        @pre std::is_trivially_copyable_v<T> */
    void memcpy_from(const Grid& grid, const Geometry& geo);

private:
    T m_a[Point::range_onboard];
};


template<class P, typename T>
inline T& Grid<P, T>::operator[](const Point& p)
{
    LIBBOARDGAME_ASSERT(! p.is_null());
    return m_a[p.to_int()];
}

template<class P, typename T>
inline const T& Grid<P, T>::operator[](const Point& p) const
{
    LIBBOARDGAME_ASSERT(! p.is_null());
    return m_a[p.to_int()];
}

template<class P, typename T>
inline void Grid<P, T>::copy_from(const Grid& grid, const Geometry& geo)
{
    copy(grid.m_a, grid.m_a + geo.get_range(), m_a);
}

template<class P, typename T>
inline void Grid<P, T>::fill(const T& val, const Geometry& geo)
{
    std::fill(m_a, m_a + geo.get_range(), val);
}

template<class P, typename T>
inline void Grid<P, T>::fill_all(const T& val)
{
    std::fill(m_a, m_a + Point::range_onboard, val);
}

template<class P, typename T>
void Grid<P, T>::memcpy_from(const Grid& grid, const Geometry& geo)
{
    static_assert(is_trivially_copyable_v<T>);
    memcpy(m_a, grid.m_a, geo.get_range() * sizeof(T));
}

template<class P, typename T>
string Grid<P, T>::to_string(const Geometry& geo) const
{
    return grid_to_string(*this, geo);
}

//-----------------------------------------------------------------------------

/** Like Grid, but allows Point::null() as index. */
template<class P, typename T>
class GridExt
{
public:
    using Point = P;

    using Geometry = libboardgame_base::Geometry<P>;


    T& operator[](const Point& p) { return m_a[p.to_int()]; }

    const T& operator[](const Point& p) const { return m_a[p.to_int()]; }

    /** Fill all on-board points for a given geometry with a value. */
    void fill(const T& val, const Geometry& geo);

    /** Fill points with a value. */
    void fill_all(const T& val);

    string to_string(const Geometry& geo) const;

    void copy_from(const Grid<P, T>& grid, const Geometry& geo);

    void copy_from(const GridExt& grid, const Geometry& geo);

private:
    T m_a[Point::range];
};


template<class P, typename T>
inline void GridExt<P, T>::fill(const T& val, const Geometry& geo)
{
    std::fill(m_a, m_a + geo.get_range(), val);
}

template<class P, typename T>
inline void GridExt<P, T>::fill_all(const T& val)
{
    std::fill(m_a, m_a + Point::range, val);
}

template<class P, typename T>
inline void GridExt<P, T>::copy_from(const Grid<P, T>& grid,
                                     const Geometry& geo)
{
    copy(grid.m_a, grid.m_a + geo.get_range(), m_a);
}

template<class P, typename T>
inline void GridExt<P, T>::copy_from(const GridExt& grid,
                                          const Geometry& geo)
{
    copy(grid.m_a, grid.m_a + geo.get_range(), m_a);
}

template<class P, typename T>
string GridExt<P, T>::to_string(const Geometry& geo) const
{
    return grid_to_string(*this, geo);
}

//-----------------------------------------------------------------------------

} // namespace libboardgame_base

#endif // LIBBOARDGAME_BASE_GRID_H