mFrame
載入中...
搜尋中...
無符合項目
Span.h
1
7#ifndef MFRAME_C222A481_2EF7_4AD5_8C4E_DF6A3BF277C3
8#define MFRAME_C222A481_2EF7_4AD5_8C4E_DF6A3BF277C3
9
10/* ***************************************************************************************
11 * Include
12 */
13
14//----------------------------------------------------------------------------------------
15#include "./../lang/Iterable.h"
16#include "./../lang/Object.h"
17#include "./../util/Iterator.h"
18
19//----------------------------------------------------------------------------------------
20
21/* ***************************************************************************************
22 * Namespace
23 */
24namespace ufm::util {
25 template <typename T>
26 class Span;
27} // namespace ufm::util
28
29/* ***************************************************************************************
30 * Class/Interface/Struct/Enum
31 */
32
49template <typename T>
51 /* *************************************************************************************
52 * Variable
53 */
54 private:
59 T const* vData;
60
65 const size_t vSize;
66
67 /* *************************************************************************************
68 * Abstract Method
69 */
70
71 /* *************************************************************************************
72 * Construct Method
73 */
74 public:
84 Span(T const* data, size_t size) noexcept : vData(data), vSize(size) {
85 return;
86 }
87
101 template <size_t N>
102 Span(T const (&data)[N]) noexcept : vData(data), vSize(N) {
103 return;
104 }
105
110 virtual ~Span(void) override {
111 return;
112 }
113
114 /* *************************************************************************************
115 * Operator Method
116 */
117 public:
125 Span(const Span& other) noexcept = default;
126
134 Span& operator=(const Span& other) noexcept = default;
135
142 Span(Span&& other) noexcept = default;
143
149 Span& operator=(Span&& other) noexcept = default;
150
151 /* *************************************************************************************
152 * Override - ufm::lang::Iterable<T*>
153 */
154 public:
167 virtual void forEach(ufm::func::Consumer<T&>& action) override {
168 for (size_t i = 0; i < this->vSize; ++i) {
169 action.accept(const_cast<T&>(this->vData[i]));
170 }
171 return;
172 }
173
183 virtual T* elementAt(int index) const override {
184 if (index < 0 || index >= static_cast<int>(this->vSize)) { return nullptr; }
185 return const_cast<T*>(&this->vData[index]);
186 }
187
213 virtual int nextIndex(int index) const override {
214 if (index < -1 || index > static_cast<int>(this->vSize)) return -1;
215
216 return index + 1;
217 }
218
225 virtual ufm::util::Iterator<T> begin(void) override {
226 return ufm::util::Iterator<T>(*this, 0);
227 }
228
235 virtual ufm::util::Iterator<T> end(void) override {
236 return ufm::util::Iterator<T>(*this, static_cast<int>(this->vSize));
237 }
238
239 /* *************************************************************************************
240 * Public Method
241 */
242 public:
249 size_t size(void) const noexcept {
250 return this->vSize;
251 }
252
257 bool empty(void) const noexcept {
258 return this->vSize == 0;
259 }
260
269 T const* data(void) const noexcept {
270 return this->vData;
271 }
272
282 T const& operator[](size_t index) const noexcept {
283 return this->vData[index];
284 }
285
286 /* *************************************************************************************
287 * Protected Method
288 */
289
290 /* *************************************************************************************
291 * Private Method
292 */
293
294 /* *************************************************************************************
295 * Static Variable
296 */
297
298 /* *************************************************************************************
299 * Static Method
300 */
301};
302
303/* ***************************************************************************************
304 * End of file
305 */
306
307#endif /* MFRAME_C222A481_2EF7_4AD5_8C4E_DF6A3BF277C3 */
物件基底類別
Definition Object.h:63
迭代器類別,提供遍歷集合元素的標準介面。
Definition Iterator.h:257
輕量級連續記憶體區域包裝器,提供對陣列或記憶體片段的非擁有性引用
Definition Span.h:50
T const * data(void) const noexcept
獲取指向數據起始位置的指針
Definition Span.h:269
T const & operator[](size_t index) const noexcept
安全的陣列下標操作符
Definition Span.h:282
bool empty(void) const noexcept
檢查 Span 是否為空
Definition Span.h:257
Span(const Span &other) noexcept=default
複製建構函式
Span & operator=(const Span &other) noexcept=default
複製賦值操作符
virtual T * elementAt(int index) const override
獲取指定索引處的元素指針
Definition Span.h:183
Span & operator=(Span &&other) noexcept=default
移動賦值操作符
Span(Span &&other) noexcept=default
移動建構函式
virtual ufm::util::Iterator< T > begin(void) override
獲取指向集合開始的迭代器
Definition Span.h:225
Span(T const *data, size_t size) noexcept
從指針和大小構造 Span 對象
Definition Span.h:84
virtual int nextIndex(int index) const override
返回當前索引的下一個有效索引。
Definition Span.h:213
virtual ufm::util::Iterator< T > end(void) override
獲取指向集合結尾的迭代器
Definition Span.h:235
virtual void forEach(ufm::func::Consumer< T & > &action) override
對 Span 中的每個元素執行指定操作
Definition Span.h:167
Span(T const (&data)[N]) noexcept
從固定大小陣列構造 Span 對象
Definition Span.h:102
virtual ~Span(void) override
虛擬解構函數
Definition Span.h:110
size_t size(void) const noexcept
獲取 Span 包含的元素數量
Definition Span.h:249
Definition Iterable.h:28
[Interface] 消費者函數式介面模板
Definition Consumer.h:43
virtual void accept(T &t) override
對給定的參數執行操作
[Interface] 定義可迭代集合介面
Definition Iterable.h:49