adapt arraylist to updated interface
This commit is contained in:
@ -10,8 +10,13 @@
|
|||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
|
||||||
// I put most of the implementation in the header because the templating makes it cumbersome to split
|
// I put most of the implementation in the header because the templating makes it cumbersome to split
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
class ArrayList : public List<T> {
|
class ArrayList : public List<T> {
|
||||||
|
public:
|
||||||
|
using Type = typename List<T>::Type; // Use this just in case T changes from the List type
|
||||||
|
using Iterator = typename List<T>::Iterator;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const unsigned int default_size = 10; // Arbitrary but very small because this isn't a real OS :(
|
const unsigned int default_size = 10; // Arbitrary but very small because this isn't a real OS :(
|
||||||
const unsigned int expand_size = 5; // Slots to allocate extra when array full
|
const unsigned int expand_size = 5; // Slots to allocate extra when array full
|
||||||
@ -21,7 +26,7 @@ private:
|
|||||||
|
|
||||||
// TODO: Use user/lib/Array
|
// TODO: Use user/lib/Array
|
||||||
// I manage the size so I can use pointer arithmetic
|
// I manage the size so I can use pointer arithmetic
|
||||||
T* buffer = NULL;
|
Type* buffer = NULL;
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
this->buffer = new T[this->default_size];
|
this->buffer = new T[this->default_size];
|
||||||
@ -45,7 +50,7 @@ private:
|
|||||||
if (this->get_free_space() < this->expand_size) {
|
if (this->get_free_space() < this->expand_size) {
|
||||||
// We need to realloc the buffer
|
// We need to realloc the buffer
|
||||||
const unsigned int new_size = this->buffer_size + this->expand_size;
|
const unsigned int new_size = this->buffer_size + this->expand_size;
|
||||||
T* new_buffer = new T[new_size];
|
Type* new_buffer = new Type[new_size];
|
||||||
// TODO: Use move semantics and managed pointers
|
// TODO: Use move semantics and managed pointers
|
||||||
for (unsigned int idx = 0; idx < this->buffer_pos; ++idx) {
|
for (unsigned int idx = 0; idx < this->buffer_pos; ++idx) {
|
||||||
new_buffer[idx] = this->buffer[idx];
|
new_buffer[idx] = this->buffer[idx];
|
||||||
@ -113,17 +118,18 @@ private:
|
|||||||
return this->buffer_pos;
|
return this->buffer_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
Type* begin_ptr() override {
|
||||||
|
return this->buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
Type* end_ptr() override {
|
||||||
|
return this->buffer + this->buffer_pos;
|
||||||
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typename List<T>::Iterator begin() override {
|
|
||||||
return typename List<T>::Iterator(&this->buffer[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
typename List<T>::Iterator end() override {
|
|
||||||
return typename List<T>::Iterator(&this->buffer[this->buffer_pos]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns new pos
|
// Returns new pos
|
||||||
unsigned int insert(T e) override {
|
unsigned int insert(Type e) override {
|
||||||
this->expand();
|
this->expand();
|
||||||
this->buffer[this->buffer_pos] = e;
|
this->buffer[this->buffer_pos] = e;
|
||||||
this->buffer_pos = this->buffer_pos + 1;
|
this->buffer_pos = this->buffer_pos + 1;
|
||||||
@ -131,7 +137,7 @@ public:
|
|||||||
return this->buffer_pos;
|
return this->buffer_pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int insert_at(T e, unsigned int i) override {
|
unsigned int insert_at(Type e, unsigned int i) override {
|
||||||
if (i > this->buffer_pos) {
|
if (i > this->buffer_pos) {
|
||||||
// Error: Space between elements
|
// Error: Space between elements
|
||||||
return -1;
|
return -1;
|
||||||
@ -150,28 +156,28 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns removed element
|
// Returns removed element
|
||||||
T remove_at(unsigned int i) override {
|
Type remove_at(unsigned int i) override {
|
||||||
if (i >= this->buffer_pos) {
|
if (i >= this->buffer_pos) {
|
||||||
// ERROR: No element here
|
// ERROR: No element here
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
T e = this->buffer[i];
|
Type e = this->buffer[i];
|
||||||
this->copy_left(i);
|
this->copy_left(i);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
|
|
||||||
T remove_first() override {
|
Type remove_first() override {
|
||||||
return this->remove_at(0);
|
return this->remove_at(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
T remove_last() override {
|
Type remove_last() override {
|
||||||
// If index -1 unsigned int will overflow and remove_at will catch that
|
// If index -1 unsigned int will overflow and remove_at will catch that
|
||||||
return this->remove_at(this->buffer_pos - 1);
|
return this->remove_at(this->buffer_pos - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns true on success
|
// Returns true on success
|
||||||
bool remove(T e) override {
|
bool remove(Type e) override {
|
||||||
for (unsigned int i = 0; i < this->buffer_pos; ++i) {
|
for (unsigned int i = 0; i < this->buffer_pos; ++i) {
|
||||||
if (this->buffer[i] == e) {
|
if (this->buffer[i] == e) {
|
||||||
this->copy_left(i);
|
this->copy_left(i);
|
||||||
@ -182,7 +188,7 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
T get(unsigned int i) const override {
|
Type get(unsigned int i) const override {
|
||||||
if (i >= this->buffer_pos) {
|
if (i >= this->buffer_pos) {
|
||||||
// ERROR: No element there
|
// ERROR: No element there
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -191,11 +197,11 @@ public:
|
|||||||
return this->buffer[i];
|
return this->buffer[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
T first() const override {
|
Type first() const override {
|
||||||
return this->get(0);
|
return this->get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
T last() const override {
|
Type last() const override {
|
||||||
return this->get(this->buffer_pos - 1); // Underflow gets catched by get(unsigned int i)
|
return this->get(this->buffer_pos - 1); // Underflow gets catched by get(unsigned int i)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user