FastLED 3.9.15
Loading...
Searching...
No Matches
slice.h
Go to the documentation of this file.
1#pragma once
2
3#include <stddef.h>
4#include <stdint.h>
5
6#include "fl/clamp.h"
7#include "fl/geometry.h"
8#include "fl/allocator.h"
9
10namespace fl {
11
12template <typename T, size_t INLINED_SIZE> class FixedVector;
13
14template <typename T, typename Allocator> class HeapVector;
15
16template <typename T, size_t INLINED_SIZE> class InlinedVector;
17
18// Slice<int> is equivalent to int* with a length. It is used to pass around
19// arrays of integers with a length, without needing to pass around a separate
20// length parameter.
21// It works just like an array of objects, but it also knows its length.
22template <typename T> class Slice {
23 public:
24 Slice() : mData(nullptr), mSize(0) {}
25 Slice(T *data, size_t size) : mData(data), mSize(size) {}
26
27 template<typename Alloc>
30
31 template <size_t INLINED_SIZE>
34
35 template <size_t INLINED_SIZE>
38
39 template <typename U, typename Alloc>
42
43 template <typename U, size_t INLINED_SIZE>
46
47 template <typename U, size_t INLINED_SIZE>
50
51 template <size_t ARRAYSIZE>
52 Slice(T (&array)[ARRAYSIZE]) : mData(array), mSize(ARRAYSIZE) {}
53
54 template <typename U, size_t ARRAYSIZE>
55 Slice(T (&array)[ARRAYSIZE]) : mData(array), mSize(ARRAYSIZE) {}
56
57 template <typename Iterator>
58 Slice(Iterator begin, Iterator end)
59 : mData(&(*begin)), mSize(end - begin) {}
60
61 Slice(const Slice &other) : mData(other.mData), mSize(other.mSize) {}
62
63 Slice &operator=(const Slice &other) {
64 mData = other.mData;
65 mSize = other.mSize;
66 return *this;
67 }
68
69 // Automatic promotion to const Slice<const T>
70 operator Slice<const T>() const { return Slice<const T>(mData, mSize); }
71
72 T &operator[](size_t index) {
73 // No bounds checking in embedded environment
74 return mData[index];
75 }
76
77 const T &operator[](size_t index) const {
78 // No bounds checking in embedded environment
79 return mData[index];
80 }
81
82 T *begin() const { return mData; }
83
84 T *end() const { return mData + mSize; }
85
86 size_t length() const { return mSize; }
87
88 const T *data() const { return mData; }
89
90 T *data() { return mData; }
91
92 size_t size() const { return mSize; }
93
94 Slice<T> slice(size_t start, size_t end) const {
95 // No bounds checking in embedded environment
96 return Slice<T>(mData + start, end - start);
97 }
98
99 Slice<T> slice(size_t start) const {
100 // No bounds checking in embedded environment
101 return Slice<T>(mData + start, mSize - start);
102 }
103
104 // Find the first occurrence of a value in the slice
105 // Returns the index of the first occurrence if found, or size_t(-1) if not
106 // found
107 size_t find(const T &value) const {
108 for (size_t i = 0; i < mSize; ++i) {
109 if (mData[i] == value) {
110 return i;
111 }
112 }
113 return size_t(-1);
114 }
115
116 bool pop_front() {
117 if (mSize == 0) {
118 return false;
119 }
120 ++mData;
121 --mSize;
122 return true;
123 }
124
125 bool pop_back() {
126 if (mSize == 0) {
127 return false;
128 }
129 --mSize;
130 return true;
131 }
132
133 T &front() { return *mData; }
134
135 const T &front() const { return *mData; }
136
137 T &back() { return *(mData + mSize - 1); }
138
139 const T &back() const { return *(mData + mSize - 1); }
140
141 bool empty() { return mSize == 0; }
142
143 private:
145 size_t mSize;
146};
147template <typename T> class MatrixSlice {
148 public:
149 // represents a window into a matrix
150 // bottom-left and top-right corners are passed as plain ints
151 MatrixSlice() = default;
152 MatrixSlice(T *data, int32_t dataWidth, int32_t dataHeight,
153 int32_t bottomLeftX, int32_t bottomLeftY, int32_t topRightX,
154 int32_t topRightY)
155 : mData(data), mDataWidth(dataWidth), mDataHeight(dataHeight),
156 mBottomLeft{bottomLeftX, bottomLeftY},
157 mTopRight{topRightX, topRightY} {}
158
159 MatrixSlice(const MatrixSlice &other) = default;
160 MatrixSlice &operator=(const MatrixSlice &other) = default;
161
162 // outputs a vec2 but takes x,y as inputs
163 vec2<int32_t> getParentCoord(int32_t x_local, int32_t y_local) const {
164 return {x_local + mBottomLeft.x, y_local + mBottomLeft.y};
165 }
166
167 vec2<int32_t> getLocalCoord(int32_t x_world, int32_t y_world) const {
168 // clamp to [mBottomLeft, mTopRight]
169 int32_t x_clamped = fl::clamp(x_world, mBottomLeft.x, mTopRight.x);
170 int32_t y_clamped = fl::clamp(y_world, mBottomLeft.y, mTopRight.y);
171 // convert to local
172 return {x_clamped - mBottomLeft.x, y_clamped - mBottomLeft.y};
173 }
174
175 // element access via (x,y)
176 T &operator()(int32_t x, int32_t y) { return at(x, y); }
177
178 // Add access like slice[y][x]
179 T *operator[](int32_t row) {
180 int32_t parentRow = row + mBottomLeft.y;
181 return mData + parentRow * mDataWidth + mBottomLeft.x;
182 }
183
184 T &at(int32_t x, int32_t y) {
185 auto parent = getParentCoord(x, y);
186 return mData[parent.x + parent.y * mDataWidth];
187 }
188
189 const T &at(int32_t x, int32_t y) const {
190 auto parent = getParentCoord(x, y);
191 return mData[parent.x + parent.y * mDataWidth];
192 }
193
194 private:
195 T *mData = nullptr;
196 int32_t mDataWidth = 0;
197 int32_t mDataHeight = 0;
200};
201
202} // namespace fl
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:82
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:83
T * operator[](int32_t row)
Definition slice.h:179
int32_t mDataHeight
Definition slice.h:197
MatrixSlice(T *data, int32_t dataWidth, int32_t dataHeight, int32_t bottomLeftX, int32_t bottomLeftY, int32_t topRightX, int32_t topRightY)
Definition slice.h:152
vec2< int32_t > getLocalCoord(int32_t x_world, int32_t y_world) const
Definition slice.h:167
T & operator()(int32_t x, int32_t y)
Definition slice.h:176
const T & at(int32_t x, int32_t y) const
Definition slice.h:189
MatrixSlice & operator=(const MatrixSlice &other)=default
MatrixSlice()=default
vec2< int32_t > mBottomLeft
Definition slice.h:198
T & at(int32_t x, int32_t y)
Definition slice.h:184
MatrixSlice(const MatrixSlice &other)=default
int32_t mDataWidth
Definition slice.h:196
vec2< int32_t > getParentCoord(int32_t x_local, int32_t y_local) const
Definition slice.h:163
vec2< int32_t > mTopRight
Definition slice.h:199
Slice(const Slice &other)
Definition slice.h:61
Slice(Iterator begin, Iterator end)
Definition slice.h:58
const T & back() const
Definition slice.h:139
Slice(const HeapVector< U, Alloc > &vector)
Definition slice.h:40
T * data()
Definition slice.h:90
Slice(const InlinedVector< T, INLINED_SIZE > &vector)
Definition slice.h:36
Slice< T > slice(size_t start) const
Definition slice.h:99
bool pop_back()
Definition slice.h:125
Slice< T > slice(size_t start, size_t end) const
Definition slice.h:94
Slice(T(&array)[ARRAYSIZE])
Definition slice.h:55
T * begin() const
Definition slice.h:82
T & operator[](size_t index)
Definition slice.h:72
Slice(const HeapVector< T, Alloc > &vector)
Definition slice.h:28
T * mData
Definition slice.h:144
size_t length() const
Definition slice.h:86
bool pop_front()
Definition slice.h:116
Slice(const FixedVector< U, INLINED_SIZE > &vector)
Definition slice.h:44
Slice(const FixedVector< T, INLINED_SIZE > &vector)
Definition slice.h:32
T & back()
Definition slice.h:137
Slice()
Definition slice.h:24
const T & front() const
Definition slice.h:135
bool empty()
Definition slice.h:141
const T * data() const
Definition slice.h:88
Slice & operator=(const Slice &other)
Definition slice.h:63
const T & operator[](size_t index) const
Definition slice.h:77
size_t size() const
Definition slice.h:92
T * end() const
Definition slice.h:84
T & front()
Definition slice.h:133
Slice(T(&array)[ARRAYSIZE])
Definition slice.h:52
size_t find(const T &value) const
Definition slice.h:107
Slice(T *data, size_t size)
Definition slice.h:25
Slice(const InlinedVector< U, INLINED_SIZE > &vector)
Definition slice.h:48
size_t mSize
Definition slice.h:145
A fixed-size array implementation similar to std::array.
Definition array.h:19
FASTLED_FORCE_INLINE T clamp(T value, T min, T max)
Definition clamp.h:10
HeapVector< T, Allocator > vector
Definition vector.h:1074
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16