[glacier] Move LinkedList to glacier.
This commit is contained in:
parent
08abe776a4
commit
64d355b20d
15 changed files with 28 additions and 46 deletions
78
lib/glacier/container/linked_list.h
Normal file
78
lib/glacier/container/linked_list.h
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace glcr {
|
||||
|
||||
template <typename T>
|
||||
class LinkedList {
|
||||
public:
|
||||
LinkedList() {}
|
||||
|
||||
LinkedList(const LinkedList&) = delete;
|
||||
|
||||
bool empty() const { return size_ == 0; }
|
||||
uint64_t size() const { return size_; }
|
||||
|
||||
void PushBack(const T& item) {
|
||||
size_++;
|
||||
ListItem* new_item = new ListItem{
|
||||
.item = item,
|
||||
.next = nullptr,
|
||||
};
|
||||
if (front_ == nullptr) {
|
||||
front_ = new_item;
|
||||
return;
|
||||
}
|
||||
ListItem* litem = front_;
|
||||
while (litem->next != nullptr) {
|
||||
litem = litem->next;
|
||||
}
|
||||
litem->next = new_item;
|
||||
}
|
||||
|
||||
T PopFront() {
|
||||
size_--;
|
||||
|
||||
ListItem* old_front = front_;
|
||||
front_ = front_->next;
|
||||
T ret = old_front->item;
|
||||
delete old_front;
|
||||
return ret;
|
||||
}
|
||||
|
||||
T PeekFront() const { return front_->item; }
|
||||
|
||||
struct ListItem {
|
||||
T item;
|
||||
ListItem* next;
|
||||
};
|
||||
class Iterator {
|
||||
public:
|
||||
Iterator(ListItem* item) : item_(item) {}
|
||||
|
||||
Iterator next() { return {item_->next}; }
|
||||
Iterator& operator++() {
|
||||
item_ = item_->next;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator*() { return item_->item; }
|
||||
T* operator->() { return &item_->item; }
|
||||
bool operator==(const Iterator& other) { return item_ == other.item_; }
|
||||
bool operator!=(const Iterator& other) { return item_ != other.item_; }
|
||||
|
||||
private:
|
||||
ListItem* item_;
|
||||
};
|
||||
|
||||
Iterator begin() { return {front_}; }
|
||||
Iterator end() { return {nullptr}; }
|
||||
|
||||
private:
|
||||
uint64_t size_ = 0;
|
||||
|
||||
ListItem* front_ = nullptr;
|
||||
};
|
||||
|
||||
} // namespace glcr
|
||||
Loading…
Add table
Add a link
Reference in a new issue