5#ifndef _COLLECTION2_LIST_H_
6#define _COLLECTION2_LIST_H_
12namespace collection2 {
20template <
typename Element,
typename Size =
size_t>
23 bool isEnabled =
false;
29 Node* previous =
nullptr;
40template <
typename Element,
typename Size =
size_t>
51 Size internalDataSize;
85 List& operator=(
const List&) =
delete;
131 Element*
get(
const Size& index);
157 return internalDataSize;
170template <
typename Element,
typename Size>
173template <
typename Element,
typename Size>
175 for (Size i = 0; i < internalDataSize; i++) {
176 if (internalData[i].isEnabled) {
181 internalData[i].isEnabled =
true;
182 internalData[i].next =
nullptr;
183 internalData[i].previous =
nullptr;
184 return &(internalData[i]);
189template <
typename Element,
typename Size>
192 auto* newNode = getNewNode();
193 if (newNode ==
nullptr) {
194 return OperationResult::Overflow;
196 newNode->element = element;
199 if (headPtr ==
nullptr) {
203 return OperationResult::Success;
207 tailPtr->next = newNode;
208 newNode->previous = tailPtr;
212 return OperationResult::Success;
215template <
typename Element,
typename Size>
218 auto* newNode = getNewNode();
219 if (newNode ==
nullptr) {
220 return OperationResult::Overflow;
222 newNode->element = element;
225 if (headPtr ==
nullptr) {
229 return OperationResult::Success;
234 newNode->next = headPtr;
237 return OperationResult::Success;
241 auto* previousNode = headPtr;
242 Size currentIndex = 0;
243 while (previousNode->next !=
nullptr && currentIndex < index - 1) {
244 previousNode = previousNode->next;
249 if (previousNode->next ==
nullptr) {
254 previousNode->next->previous = newNode;
256 newNode->next = previousNode->next;
258 newNode->previous = previousNode;
259 previousNode->next = newNode;
263 return OperationResult::Success;
266template <
typename Element,
typename Size>
268 if (tailPtr ==
nullptr) {
269 return OperationResult::Empty;
273 auto* targetNode = tailPtr;
276 if (element !=
nullptr) {
277 *element = targetNode->element;
281 tailPtr = targetNode->previous;
282 if (tailPtr ==
nullptr) {
288 targetNode->isEnabled =
false;
289 targetNode->next =
nullptr;
290 targetNode->previous =
nullptr;
294 return OperationResult::Success;
297template <
typename Element,
typename Size>
299 if (headPtr ==
nullptr) {
300 return OperationResult::Empty;
306 auto* targetNode = headPtr;
307 headPtr = targetNode->next;
308 if (headPtr ==
nullptr) {
314 if (element !=
nullptr) {
315 *element = targetNode->element;
319 targetNode->isEnabled =
false;
320 targetNode->next =
nullptr;
321 targetNode->previous =
nullptr;
325 return OperationResult::Success;
329 auto* targetNode = headPtr;
330 Size currentIndex = 0;
331 while (targetNode->next !=
nullptr && currentIndex < index) {
332 targetNode = targetNode->next;
337 if (element !=
nullptr) {
338 *element = targetNode->element;
342 if (targetNode->next ==
nullptr) {
344 tailPtr = targetNode->previous;
347 targetNode->next->previous = targetNode->previous;
349 targetNode->previous->next = targetNode->next;
352 targetNode->isEnabled =
false;
353 targetNode->next =
nullptr;
354 targetNode->previous =
nullptr;
358 return OperationResult::Success;
361template <
typename Element,
typename Size>
363 if (headPtr ==
nullptr) {
368 auto* node = headPtr;
369 Size currentIndex = 0;
370 while (node->next !=
nullptr && currentIndex < index) {
376 if (currentIndex < index) {
380 return &(node->element);
リスト
Definition: list.hpp:41
Node< Element > * head() const
リスト先頭へのポインタを取得
Definition: list.hpp:138
OperationResult append(const Element &element)
リストの末尾にデータを追加
Definition: list.hpp:190
OperationResult remove(const Size &index, Element *const element)
リスト内の任意の位置にあるデータを削除し、取り出す
Definition: list.hpp:298
Size amount() const
現在リスト内にあるデータ数を返す
Definition: list.hpp:165
Node< Element > * tail() const
リスト末尾へのポインタを取得
Definition: list.hpp:147
Element * get(const Size &index)
リスト内の要素を参照する
Definition: list.hpp:362
List(Node< Element, Size > *const data, const Size &dataSize)
内部データを扱う領域とそのサイズを指定してリストを初期化
Definition: list.hpp:171
OperationResult pop(Element *const element)
リスト末尾のデータを削除し、取り出す
Definition: list.hpp:267
Size capacity() const
リストの全体長を返す
Definition: list.hpp:156
OperationResult insert(const Size &index, const Element &element)
リスト内の任意の位置にデータを追加
Definition: list.hpp:216
OperationResult
コレクション操作結果
Definition: common.hpp:14
リストの一要素を表す構造体
Definition: list.hpp:21