const iterator + separate iterator from list.h
This commit is contained in:
@ -92,6 +92,11 @@ void ArrayListDemo::run() {
|
||||
kout << "List contains element: " << dec << i << endl;
|
||||
}
|
||||
|
||||
kout << "Const iterator" << endl;
|
||||
for (const int i : this->list) {
|
||||
kout << "List contains element: " << dec << i << endl;
|
||||
}
|
||||
|
||||
kout.unlock();
|
||||
scheduler.exit();
|
||||
}
|
||||
|
40
c_os/user/lib/Iterator.h
Normal file
40
c_os/user/lib/Iterator.h
Normal file
@ -0,0 +1,40 @@
|
||||
#ifndef __Iterator_Include_H_
|
||||
#define __Iterator_Include_H_
|
||||
|
||||
// This iterator works for structures where the elements are adjacent in memory.
|
||||
// For things like LinkedList, the operator++ has to be overriden to implement the traversal.
|
||||
template<typename T>
|
||||
class Iterator {
|
||||
public:
|
||||
using Type = T;
|
||||
|
||||
protected:
|
||||
Type* ptr;
|
||||
|
||||
public:
|
||||
Iterator(Type* ptr) : ptr(ptr) {}
|
||||
|
||||
// I only implement the least necessary operators
|
||||
virtual Iterator& operator++() {
|
||||
this->ptr = this->ptr + 1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Type* operator->() {
|
||||
return this->ptr;
|
||||
}
|
||||
|
||||
Type& operator*() {
|
||||
return *this->ptr;
|
||||
}
|
||||
|
||||
bool operator==(const Iterator& other) const {
|
||||
return this->ptr == other.ptr;
|
||||
}
|
||||
|
||||
bool operator!=(const Iterator& other) const {
|
||||
return !(*this == other); // Use our == implementation
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
@ -23,11 +23,11 @@ public:
|
||||
|
||||
// Implement linked traversal by extending the ListIterator
|
||||
template<typename T>
|
||||
class LinkedListIterator : public ListIterator<T> {
|
||||
class LinkedListIterator : public Iterator<T> {
|
||||
public:
|
||||
using Type = typename ListIterator<T>::Type;
|
||||
using Type = typename Iterator<T>::Type;
|
||||
|
||||
LinkedListIterator(Type* ptr) : ListIterator<T>(ptr) {}
|
||||
LinkedListIterator(Type* ptr) : Iterator<T>(ptr) {}
|
||||
|
||||
LinkedListIterator& operator++() override {
|
||||
// ptr is of type Wrapper<T>*
|
||||
|
@ -2,47 +2,11 @@
|
||||
#define __LIST_INCLUDE_H_
|
||||
|
||||
#include "lib/OutStream.h"
|
||||
#include "user/lib/Iterator.h"
|
||||
|
||||
// Define the list interface for ArrayList/LinkedList implementations with support for Iterators/ranged based for loops
|
||||
|
||||
// TODO: Does it only work on lists?
|
||||
// This iterator works for structures where the elements are adjacent in memory.
|
||||
// For things like LinkedList, the operator++ has to be overriden to implement the traversal.
|
||||
template<typename T>
|
||||
class ListIterator {
|
||||
public:
|
||||
using Type = T;
|
||||
|
||||
protected:
|
||||
Type* ptr;
|
||||
|
||||
public:
|
||||
ListIterator(Type* ptr) : ptr(ptr) {}
|
||||
|
||||
// I only implement the least necessary operators
|
||||
virtual ListIterator& operator++() {
|
||||
this->ptr = this->ptr + 1;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Type* operator->() {
|
||||
return this->ptr;
|
||||
}
|
||||
|
||||
Type& 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, typename I = ListIterator<T>>
|
||||
template<typename T, typename I = Iterator<T>>
|
||||
class List {
|
||||
public:
|
||||
using Type = T; // We make the template argument accessible from the subclasses
|
||||
@ -55,6 +19,8 @@ protected:
|
||||
public:
|
||||
Iterator begin() { return Iterator(this->begin_ptr()); }
|
||||
Iterator end() { return Iterator(this->end_ptr()); }
|
||||
constexpr Iterator begin() const { return this->begin(); }
|
||||
constexpr Iterator end() const { return this->end(); }
|
||||
|
||||
virtual unsigned int insert_at(Type e, unsigned int i) = 0;
|
||||
virtual unsigned int insert_first(Type e) = 0;
|
||||
|
Reference in New Issue
Block a user