FastLED 3.9.3
Loading...
Searching...
No Matches
slice.h
1#pragma once
2
3#include <stdint.h>
4#include <stddef.h>
5
6template<typename T>
7class Slice {
8public:
9 Slice() : mData(nullptr), mSize(0) {}
10 Slice(T* data, size_t size) : mData(data), mSize(size) {}
11
12 Slice(const Slice& other) : mData(other.mData), mSize(other.mSize) {}
13
14
15 Slice& operator=(const Slice& other) {
16 mData = other.mData;
17 mSize = other.mSize;
18 return *this;
19 }
20
21 // Automatic promotion to const Slice<const T>
22 operator Slice<const T>() const {
23 return *this;
24 }
25
26 T& operator[](size_t index) {
27 // No bounds checking in embedded environment
28 return mData[index];
29 }
30
31 const T& operator[](size_t index) const {
32 // No bounds checking in embedded environment
33 return mData[index];
34 }
35
36 T* begin() const {
37 return mData;
38 }
39
40 T* end() const {
41 return mData + mSize;
42 }
43
44 size_t length() const {
45 return mSize;
46 }
47
48 const T* data() const {
49 return mData;
50 }
51
52 T* data() {
53 return mData;
54 }
55
56 size_t size() const {
57 return mSize;
58 }
59
60 Slice<T> slice(size_t start, size_t end) const {
61 // No bounds checking in embedded environment
62 return Slice<T>(mData + start, end - start);
63 }
64
65 Slice<T> slice(size_t start) const {
66 // No bounds checking in embedded environment
67 return Slice<T>(mData + start, mSize - start);
68 }
69
70 // Find the first occurrence of a value in the slice
71 // Returns the index of the first occurrence if found, or size_t(-1) if not found
72 size_t find(const T& value) const {
73 for (size_t i = 0; i < mSize; ++i) {
74 if (mData[i] == value) {
75 return i;
76 }
77 }
78 return size_t(-1);
79 }
80
81private:
82 T* mData;
83 size_t mSize;
84};
Definition slice.h:7