diff --git a/c_os/user/lib/List.h b/c_os/user/lib/List.h new file mode 100644 index 0000000..9c78ef8 --- /dev/null +++ b/c_os/user/lib/List.h @@ -0,0 +1,68 @@ +#ifndef __LIST_INCLUDE_H_ +#define __LIST_INCLUDE_H_ + +#include "lib/OutStream.h" + +// Define the list interface for ArrayList/LinkedList implementations with support for Iterators/ranged based for loops + +template +class ListIterator { +private: + using ValType = typename List::ValType; + + ValType* ptr; + +public: + ListIterator(ValType* ptr) : ptr(ptr) {} + + // I only implement the least necessary operators + ListIterator& operator++() { + this->ptr = this->ptr + 1; + return *this; + } + + ValType* operator->() { + return this->ptr; + } + + ValType& operator*() { + return *this->ptr; + } + + bool operator==(const ListIterator& other) const { + return this->ptr == other.ptr; + } + + bool operator!=(const ListIterator& other) const { + return !(*this == other); // Use our == implementation + } +}; + +template +class List { +public: + using ValType = T; + using Iterator = ListIterator>; + + virtual Iterator begin() = 0; + virtual Iterator end() = 0; + + virtual unsigned int insert(T e) = 0; + virtual unsigned int insert_at(T e, unsigned int i) = 0; + + virtual T remove_at(unsigned int i) = 0; + virtual T remove_first() = 0; + virtual T remove_last() = 0; + virtual bool remove(T e) = 0; + + virtual T get(unsigned int i) const = 0; + virtual T first() const = 0; + virtual T last() const = 0; + + virtual bool empty() const = 0; + virtual unsigned int size() const = 0; + + virtual void print(OutStream& out) const = 0; +}; + +#endif