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
41template <class E = void*>
43 /* *************************************************************************************
44 * Variable
45 */
46 private:
47 ufm::lang::Iterable<E>& vIterable;
48 int vIndex;
49
50 /* *************************************************************************************
51 * Construct Method
52 */
53 public:
59 Iterator(ufm::lang::Iterable<E>& iterable, int index = 0) noexcept
60 : vIterable(iterable), vIndex(index) {
61 return;
62 }
63
69 virtual ~Iterator(void) override {
70 return;
71 }
72
73 /* *************************************************************************************
74 * Operator Method
75 */
76 public:
83 inline E& operator*() const {
84 return *this->get();
85 }
86
93 inline Iterator& operator++() {
94 this->next();
95 return *this;
96 }
97
106 inline bool operator!=(const Iterator& other) const {
107 return this->index() != other.index();
108 }
109 /* *************************************************************************************
110 * Public Method
111 */
112 public:
119 bool hasNext(void) const {
120 return (this->vIterable.nextIndex(this->vIndex) != -1);
121 }
122
128 void next(void) {
129 this->vIndex = this->vIterable.nextIndex(this->vIndex);
130 if (this->vIndex == -1) {
131 UFM_THROW("Iterator has reached the end of the collection.",
133 }
134 }
135
142 E* get(void) const {
143 return this->vIterable.elementAt(this->vIndex);
144 }
145
151 int index(void) const {
152 return this->vIndex;
153 }
154 /* *************************************************************************************
155 * Override -
156 */
157
158 /* *************************************************************************************
159 * Public Method
160 */
161
162 /* *************************************************************************************
163 * Protected Method
164 */
165
166 /* *************************************************************************************
167 * Private Method
168 */
169
170 /* *************************************************************************************
171 * Static Variable
172 */
173
174 /* *************************************************************************************
175 * Static Method
176 */
177};
178
179/* ***************************************************************************************
180 * End of file
181 */
182
183#endif /* MFRAME_9DAB1F2F_D976_4CB4_9CDD_36EE14C28322 */
物件基底類別
Definition Object.h:63
[Class] 迭代器介面
Definition Iterator.h:42
Iterator & operator++()
前置遞增運算子,將迭代器移動到下一個元素。
Definition Iterator.h:93
bool hasNext(void) const
判斷是否還有下一個元素可供遍歷。
Definition Iterator.h:119
void next(void)
將迭代器移動到下一個元素。
Definition Iterator.h:128
bool operator!=(const Iterator &other) const
不等於運算子,判斷兩個迭代器是否指向不同位置。
Definition Iterator.h:106
virtual ~Iterator(void) override
解構 Iterator 物件。
Definition Iterator.h:69
E * get(void) const
取得目前元素的參考。
Definition Iterator.h:142
int index(void) const
重新設定該迭代器至初始狀態。
Definition Iterator.h:151
E & operator*() const
取得目前元素的參考(解引用運算子)。
Definition Iterator.h:83
Iterator(ufm::lang::Iterable< E > &iterable, int index=0) noexcept
建構 Iterator 物件。
Definition Iterator.h:59
@ 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
返回當前索引的下一個有效索引。