1

add list interface with iterator

This commit is contained in:
2022-07-16 18:23:42 +02:00
parent a563bd4e5a
commit 8d56ca1355

68
c_os/user/lib/List.h Normal file
View File

@ -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<typename List>
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<typename T>
class List {
public:
using ValType = T;
using Iterator = ListIterator<List<T>>;
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