mFrame
載入中...
搜尋中...
無符合項目
Iterator.h
1
7#ifndef MFRAME_9DAB1F2F_D976_4CB4_9CDD_36EE14C28322
8#define MFRAME_9DAB1F2F_D976_4CB4_9CDD_36EE14C28322
9
10/* ***************************************************************************************
11 * Include
12 */
13
14//----------------------------------------------------------------------------------------
15#include "./../lang/Iterable.h"
16#include "./../lang/Object.h"
17
18//----------------------------------------------------------------------------------------
19
20/* ***************************************************************************************
21 * Namespace
22 */
23namespace ufm::util {
24 template <class E>
25 class Iterator;
26} // namespace ufm::util
27
28/* ***************************************************************************************
29 * Class/Interface/Struct/Enum
30 */
31
256template <class E = void*>
258 /* *************************************************************************************
259 * Variable
260 */
261 private:
263 ufm::lang::Iterable<E>& vIterable;
265 int vIndex;
266
267 /* *************************************************************************************
268 * Construct Method
269 */
270 public:
277 Iterator(ufm::lang::Iterable<E>& iterable, int index = 0) noexcept
278 : vIterable(iterable), vIndex(index) {
279 return;
280 }
281
287 virtual ~Iterator(void) override {
288 return;
289 }
290
291 /* *************************************************************************************
292 * Operator Method
293 */
294 public:
300 inline E& operator*() const {
301 return *this->get();
302 }
303
310 this->next();
311 return *this;
312 }
313
320 inline bool operator!=(const Iterator& other) const {
321 return this->index() != other.index();
322 }
323 /* *************************************************************************************
324 * Public Method
325 */
326 public:
332 bool hasNext(void) const {
333 return (this->vIterable.nextIndex(this->vIndex) != -1);
334 }
335
341 void next(void) {
342 this->vIndex = this->vIterable.nextIndex(this->vIndex);
343 if (this->vIndex == -1) {
344 UFM_THROW("Iterator has reached the end of the collection.",
346 }
347 }
348
354 E* get(void) const {
355 return this->vIterable.elementAt(this->vIndex);
356 }
357
363 int index(void) const {
364 return this->vIndex;
365 }
366 /* *************************************************************************************
367 * Override -
368 */
369
370 /* *************************************************************************************
371 * Public Method
372 */
373
374 /* *************************************************************************************
375 * Protected Method
376 */
377
378 /* *************************************************************************************
379 * Private Method
380 */
381
382 /* *************************************************************************************
383 * Static Variable
384 */
385
386 /* *************************************************************************************
387 * Static Method
388 */
389};
390
391/* ***************************************************************************************
392 * End of file
393 */
394
395#endif /* MFRAME_9DAB1F2F_D976_4CB4_9CDD_36EE14C28322 */
物件基底類別
Definition Object.h:63
迭代器類別,提供遍歷集合元素的標準介面。
Definition Iterator.h:257
Iterator & operator++()
前置遞增運算子,移動到下一個元素
Definition Iterator.h:309
bool hasNext(void) const
檢查是否還有下一個元素
Definition Iterator.h:332
void next(void)
移動迭代器到下一個元素
Definition Iterator.h:341
bool operator!=(const Iterator &other) const
不等於運算子,比較兩個迭代器位置
Definition Iterator.h:320
virtual ~Iterator(void) override
解構 Iterator 物件
Definition Iterator.h:287
E * get(void) const
取得目前元素的指標
Definition Iterator.h:354
int index(void) const
取得目前迭代位置的索引
Definition Iterator.h:363
E & operator*() const
解引用運算子,取得目前元素的參考
Definition Iterator.h:300
Iterator(ufm::lang::Iterable< E > &iterable, int index=0) noexcept
建構 Iterator 物件
Definition Iterator.h:277
@ INDEX_OUT_OF_BOUNDS
索引超出範圍,泛指各種集合
Definition Iterable.h:28
[Interface] 定義可迭代集合介面
Definition Iterable.h:49
virtual E * elementAt(int index) const override
獲取集合中指定索引處的元素。
virtual int nextIndex(int index) const override
返回當前索引的下一個有效索引。