2023-05-29 14:32:49 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "debug/debug.h"
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class LinkedList {
|
|
|
|
public:
|
|
|
|
LinkedList() {}
|
|
|
|
|
|
|
|
LinkedList(const LinkedList&) = delete;
|
|
|
|
|
2023-05-29 14:59:23 -07:00
|
|
|
uint64_t size() const { return size_; }
|
2023-05-29 14:32:49 -07:00
|
|
|
|
|
|
|
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() {
|
|
|
|
if (size_ == 0 || front_ == nullptr) {
|
|
|
|
panic("Popping from empty list");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_--;
|
|
|
|
|
|
|
|
ListItem* old_front = front_;
|
|
|
|
front_ = front_->next;
|
|
|
|
T ret = old_front->item;
|
|
|
|
delete old_front;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-29 22:54:22 -07:00
|
|
|
/*
|
|
|
|
* Returns the front item in the list and pushes the passed item to the back.
|
|
|
|
*
|
|
|
|
* Done in one function to avoid a memory alloc/dealloc during scheduling.
|
|
|
|
**/
|
|
|
|
T CycleFront(const T& new_item) {
|
2023-05-29 14:32:49 -07:00
|
|
|
if (size_ == 0 || front_ == nullptr) {
|
|
|
|
panic("Cycling empty list");
|
|
|
|
}
|
|
|
|
|
2023-05-29 22:54:22 -07:00
|
|
|
T ret = front_->item;
|
|
|
|
front_->item = new_item;
|
2023-05-29 14:32:49 -07:00
|
|
|
if (size_ == 1) {
|
2023-05-29 22:54:22 -07:00
|
|
|
return ret;
|
2023-05-29 14:32:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ListItem* old_front = front_;
|
|
|
|
ListItem* iter = front_;
|
|
|
|
front_ = front_->next;
|
|
|
|
while (iter->next != nullptr) {
|
|
|
|
iter = iter->next;
|
|
|
|
}
|
|
|
|
iter->next = old_front;
|
|
|
|
old_front->next = nullptr;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-05-29 14:59:23 -07:00
|
|
|
T PeekFront() const { return front_->item; }
|
2023-05-29 14:32:49 -07:00
|
|
|
|
|
|
|
struct ListItem {
|
|
|
|
T item;
|
|
|
|
ListItem* next;
|
|
|
|
};
|
2023-05-29 14:59:23 -07:00
|
|
|
class Iterator {
|
|
|
|
public:
|
|
|
|
Iterator(ListItem* item) : item_(item) {}
|
|
|
|
|
|
|
|
Iterator next() { return {item_->next}; }
|
2023-05-29 15:08:02 -07:00
|
|
|
Iterator& operator++() {
|
|
|
|
item_ = item_->next;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-05-29 14:59:23 -07:00
|
|
|
|
|
|
|
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;
|
2023-05-29 14:32:49 -07:00
|
|
|
|
|
|
|
ListItem* front_ = nullptr;
|
|
|
|
};
|