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 << "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();
|
kout.unlock();
|
||||||
scheduler.exit();
|
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
|
// Implement linked traversal by extending the ListIterator
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class LinkedListIterator : public ListIterator<T> {
|
class LinkedListIterator : public Iterator<T> {
|
||||||
public:
|
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 {
|
LinkedListIterator& operator++() override {
|
||||||
// ptr is of type Wrapper<T>*
|
// ptr is of type Wrapper<T>*
|
||||||
|
|||||||
@ -2,47 +2,11 @@
|
|||||||
#define __LIST_INCLUDE_H_
|
#define __LIST_INCLUDE_H_
|
||||||
|
|
||||||
#include "lib/OutStream.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
|
// Define the list interface for ArrayList/LinkedList implementations with support for Iterators/ranged based for loops
|
||||||
|
|
||||||
// TODO: Does it only work on lists?
|
template<typename T, typename I = Iterator<T>>
|
||||||
// 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>>
|
|
||||||
class List {
|
class List {
|
||||||
public:
|
public:
|
||||||
using Type = T; // We make the template argument accessible from the subclasses
|
using Type = T; // We make the template argument accessible from the subclasses
|
||||||
@ -55,6 +19,8 @@ protected:
|
|||||||
public:
|
public:
|
||||||
Iterator begin() { return Iterator(this->begin_ptr()); }
|
Iterator begin() { return Iterator(this->begin_ptr()); }
|
||||||
Iterator end() { return Iterator(this->end_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_at(Type e, unsigned int i) = 0;
|
||||||
virtual unsigned int insert_first(Type e) = 0;
|
virtual unsigned int insert_first(Type e) = 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user