Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_grid_2d.hpp
1#pragma once
2
3#include "structure/math/spk_vector2.hpp"
4
5namespace spk
6{
18 template <typename T>
19 class Grid2D
20 {
21 public:
25 using Unit = T;
26
31 struct Row
32 {
40 size_t len;
46 Unit &operator[](size_t y)
47 {
48 return ptr[y];
49 }
50
55 const Unit &operator[](size_t y) const
56 {
57 return ptr[y];
58 }
59 };
60
65 struct ConstRow
66 {
70 const Unit *ptr;
74 size_t len;
80 const Unit &operator[](size_t y) const
81 {
82 return ptr[y];
83 }
84 };
85
86 private:
87 spk::Vector2UInt _size{};
88 std::vector<Unit> _data;
89
90 static size_t _index(size_t x, size_t y, size_t height)
91 {
92 return x * height + y;
93 }
94
95 public:
99 Grid2D() = default;
100
106 explicit Grid2D(const spk::Vector2UInt &size, const Unit &fill = Unit{}) :
107 _size(size),
108 _data(static_cast<size_t>(size.x) * size.y, fill)
109 {
110 }
111
116 const spk::Vector2UInt &size() const
117 {
118 return _size;
119 }
120
124 size_t width() const
125 {
126 return _size.x;
127 }
128
132 size_t height() const
133 {
134 return _size.y;
135 }
136
143 Unit &operator()(size_t x, size_t y)
144 {
145 return _data[_index(x, y, _size.y)];
146 }
147
153 const Unit &operator()(size_t x, size_t y) const
154 {
155 return _data[_index(x, y, _size.y)];
156 }
157
163 Row operator[](size_t x)
164 {
165 return Row{_data.data() + x * _size.y, _size.y};
166 }
167
172 ConstRow operator[](size_t x) const
173 {
174 return ConstRow{_data.data() + x * _size.y, _size.y};
175 }
176
182 {
183 return _data.data();
184 }
185
189 const Unit *data() const
190 {
191 return _data.data();
192 }
193 };
194}
const spk::Vector2UInt & size() const
Returns the grid dimensions.
Definition spk_grid_2d.hpp:116
T Unit
Stored element type for each grid cell.
Definition spk_grid_2d.hpp:25
Unit & operator()(size_t x, size_t y)
Mutable access to a cell using column and row indices.
Definition spk_grid_2d.hpp:143
Grid2D()=default
Constructs an empty grid with zero dimensions.
size_t width() const
Reports the horizontal cell count.
Definition spk_grid_2d.hpp:124
const Unit * data() const
Exposes the underlying contiguous buffer (const overload).
Definition spk_grid_2d.hpp:189
Unit * data()
Exposes the underlying contiguous buffer.
Definition spk_grid_2d.hpp:181
Row operator[](size_t x)
Provides a mutable column slice.
Definition spk_grid_2d.hpp:163
size_t height() const
Reports the vertical cell count.
Definition spk_grid_2d.hpp:132
ConstRow operator[](size_t x) const
Provides a read-only column slice.
Definition spk_grid_2d.hpp:172
const Unit & operator()(size_t x, size_t y) const
Read-only access to a cell using column and row indices.
Definition spk_grid_2d.hpp:153
Grid2D(const spk::Vector2UInt &size, const Unit &fill=Unit{})
Builds a grid with the given dimensions and fill value.
Definition spk_grid_2d.hpp:106
Immutable view over a single column in the grid.
Definition spk_grid_2d.hpp:66
size_t len
Number of elements in the column slice.
Definition spk_grid_2d.hpp:74
const Unit & operator[](size_t y) const
Read-only access to an element within the column.
Definition spk_grid_2d.hpp:80
const Unit * ptr
Pointer to the first element of the column slice.
Definition spk_grid_2d.hpp:70
Mutable view over a single column in the grid.
Definition spk_grid_2d.hpp:32
const Unit & operator[](size_t y) const
Read-only access to an element within the column.
Definition spk_grid_2d.hpp:55
Unit * ptr
Pointer to the first element of the column slice.
Definition spk_grid_2d.hpp:36
size_t len
Number of elements in the column slice.
Definition spk_grid_2d.hpp:40
Unit & operator[](size_t y)
Accesses an element within the column.
Definition spk_grid_2d.hpp:46