implement vector
This commit is contained in:
@ -1,40 +1,61 @@
|
||||
#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;
|
||||
#include <cstddef>
|
||||
|
||||
protected:
|
||||
Type* ptr;
|
||||
namespace bse {
|
||||
|
||||
public:
|
||||
Iterator(Type* ptr) : ptr(ptr) {}
|
||||
template<typename T>
|
||||
class AbstractIterator {
|
||||
public:
|
||||
T* ptr;
|
||||
|
||||
// I only implement the least necessary operators
|
||||
virtual Iterator& operator++() {
|
||||
this->ptr = this->ptr + 1;
|
||||
return *this;
|
||||
}
|
||||
// *this is always <= other
|
||||
virtual std::size_t dist(const AbstractIterator& other) const = 0;
|
||||
|
||||
Type* operator->() {
|
||||
return this->ptr;
|
||||
}
|
||||
AbstractIterator(T* ptr) : ptr(ptr) {}
|
||||
|
||||
Type& operator*() {
|
||||
return *this->ptr;
|
||||
}
|
||||
T* operator->() { return this->ptr; }
|
||||
T& operator*() { return *this->ptr; }
|
||||
bool operator==(const AbstractIterator& other) const { return this->ptr == other.ptr; }
|
||||
bool operator!=(const AbstractIterator& other) const { return !(*this == other); }
|
||||
|
||||
bool operator==(const Iterator& other) const {
|
||||
return this->ptr == other.ptr;
|
||||
}
|
||||
friend std::size_t distance(const AbstractIterator& first, const AbstractIterator& last) {
|
||||
return first.dist(last);
|
||||
}
|
||||
};
|
||||
|
||||
bool operator!=(const Iterator& other) const {
|
||||
return !(*this == other); // Use our == implementation
|
||||
}
|
||||
};
|
||||
// This iterator works for structures where the elements are adjacent in memory.
|
||||
template<typename T>
|
||||
class ContinuousIterator : public AbstractIterator<T> {
|
||||
public:
|
||||
std::size_t dist(const AbstractIterator<T>& other) const override {
|
||||
return other.ptr - this->ptr;
|
||||
}
|
||||
|
||||
ContinuousIterator(T* ptr) : AbstractIterator<T>(ptr) {}
|
||||
|
||||
friend ContinuousIterator& operator++(ContinuousIterator& rhs) {
|
||||
++rhs.ptr;
|
||||
return rhs;
|
||||
}
|
||||
|
||||
friend ContinuousIterator& operator--(ContinuousIterator& rhs) {
|
||||
--rhs.ptr;
|
||||
return rhs;
|
||||
}
|
||||
|
||||
friend ContinuousIterator operator+(ContinuousIterator lhs, std::size_t add) {
|
||||
lhs.ptr += add;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
friend ContinuousIterator operator-(ContinuousIterator lhs, std::size_t sub) {
|
||||
lhs.ptr -= sub;
|
||||
return lhs;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bse
|
||||
|
||||
#endif
|
||||
|
203
c_os/user/lib/Vector.h
Normal file
203
c_os/user/lib/Vector.h
Normal file
@ -0,0 +1,203 @@
|
||||
#ifndef __VECTOR_INCLUDE_H_
|
||||
#define __VECTOR_INCLUDE_H_
|
||||
|
||||
// NOTE: I decided to implement this because I wanted some sort of dynamic array (for example for the keyeventmanager).
|
||||
// Also I wanted to template the Queue (for the scheduler) but with this I can just replace the Queue and use the
|
||||
// ArrayList instead
|
||||
|
||||
#include "Iterator.h"
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
|
||||
// https://en.cppreference.com/w/cpp/container/vector
|
||||
|
||||
namespace bse {
|
||||
|
||||
template<typename T>
|
||||
class Vector {
|
||||
public:
|
||||
using Iterator = ContinuousIterator<T>;
|
||||
|
||||
private:
|
||||
static constexpr const std::size_t default_cap = 10; // Arbitrary but very small because this isn't a real OS :(
|
||||
static constexpr const std::size_t min_cap = 5; // Slots to allocate extra when array full
|
||||
|
||||
T* buf = nullptr; // Heap allocated as size needs to change during runtime
|
||||
// Can't use Array for the same reason so we use a C Style array
|
||||
std::size_t buf_pos = 0;
|
||||
std::size_t buf_cap = 0;
|
||||
|
||||
void init() {
|
||||
buf = new T[Vector::default_cap];
|
||||
buf_cap = Vector::default_cap;
|
||||
}
|
||||
|
||||
std::size_t get_rem_cap() const {
|
||||
return buf_cap - size();
|
||||
}
|
||||
|
||||
// Enlarges the buffer if we run out of space
|
||||
std::size_t expand() {
|
||||
// Init if necessary
|
||||
if (buf == nullptr) {
|
||||
init();
|
||||
return buf_cap; // Dont have to realloc after init
|
||||
}
|
||||
|
||||
// Since we only ever add single elements this should never get below zero
|
||||
if (get_rem_cap() < min_cap) {
|
||||
std::size_t new_cap = buf_cap + min_cap;
|
||||
|
||||
// Alloc new array
|
||||
T* new_buf = new T[new_cap];
|
||||
|
||||
// Swap current elements to new array
|
||||
for (std::size_t i = 0; i < size(); ++i) {
|
||||
new_buf[i] = std::move(buf[i]);
|
||||
buf[i].~T();
|
||||
}
|
||||
|
||||
// Move new array to buf, deleting the old array
|
||||
delete[] buf;
|
||||
buf = new_buf;
|
||||
buf_cap = new_cap;
|
||||
}
|
||||
|
||||
return buf_cap;
|
||||
}
|
||||
|
||||
// Returns new pos, both do element copying if necessary, -1 if failed
|
||||
// Index is location where space should be made/removed
|
||||
std::size_t copy_right(std::size_t i) {
|
||||
if (i > size()) {
|
||||
// Error: No elements here
|
||||
return 0;
|
||||
}
|
||||
|
||||
expand();
|
||||
|
||||
// Otherwise i == pos and we don't need to copy anything
|
||||
if (i < size()) {
|
||||
// Enough space to copy elements after pos i
|
||||
for (std::size_t idx = size(); idx > i; --idx) { // idx > i so idx - 1 is never < 0
|
||||
buf[idx] = std::move(buf[idx - 1]);
|
||||
buf[idx - 1].~T();
|
||||
}
|
||||
++buf_pos;
|
||||
}
|
||||
|
||||
return size();
|
||||
}
|
||||
|
||||
// Don't realloc here, we don't need to shring the buffer every time
|
||||
// One could introduce a limit of free space but I don't care for now
|
||||
// Would be bad if the scheduler triggers realloc everytime a thread is removed (if used as readyqueue)...
|
||||
std::size_t copy_left(std::size_t i) {
|
||||
if (i >= size()) {
|
||||
// Error: No elements here
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Decrement before loop because we overwrite 1 element (1 copy less than expand)
|
||||
--buf_pos;
|
||||
|
||||
for (std::size_t idx = i; idx < size(); ++idx) { // idx < pos so idx + 1 is never outside of size limit
|
||||
buf[idx] = std::move(buf[idx + 1]);
|
||||
buf[idx + 1].~T();
|
||||
}
|
||||
|
||||
return size();
|
||||
}
|
||||
|
||||
public:
|
||||
~Vector() {
|
||||
for (std::size_t i; i < size(); ++i) {
|
||||
buf[i].~T();
|
||||
}
|
||||
delete[] buf;
|
||||
}
|
||||
|
||||
// Iterator
|
||||
Iterator begin() { return Iterator(&buf[0]); }
|
||||
Iterator end() { return Iterator(&buf[size()]); }
|
||||
|
||||
// Add elements
|
||||
void push_back(const T& copy) {
|
||||
if (buf == nullptr) {
|
||||
init();
|
||||
}
|
||||
|
||||
buf[size()] = copy;
|
||||
++buf_pos;
|
||||
expand();
|
||||
}
|
||||
|
||||
void push_back(T&& move) {
|
||||
if (buf == nullptr) {
|
||||
init();
|
||||
}
|
||||
|
||||
buf[size()] = std::move(move);
|
||||
++buf_pos;
|
||||
expand();
|
||||
}
|
||||
|
||||
Iterator insert(Iterator pos, const T& copy) {
|
||||
std::size_t idx = distance(begin(), pos);
|
||||
copy_right(idx);
|
||||
buf[idx] = copy;
|
||||
return Iterator(&buf[idx]);
|
||||
}
|
||||
|
||||
Iterator insert(Iterator pos, T&& move) {
|
||||
std::size_t idx = distance(begin(), pos);
|
||||
copy_right(idx);
|
||||
buf[idx] = std::move(move);
|
||||
return Iterator(&buf[idx]);
|
||||
}
|
||||
|
||||
// Remove elements
|
||||
Iterator erase(Iterator pos) {
|
||||
std::size_t idx = distance(begin(), pos);
|
||||
copy_left(idx);
|
||||
return Iterator(&buf[idx]);
|
||||
}
|
||||
|
||||
// Access
|
||||
T& front() {
|
||||
return buf[0];
|
||||
}
|
||||
|
||||
const T& front() const {
|
||||
return buf[0];
|
||||
}
|
||||
|
||||
T& back() {
|
||||
return buf[size() - 1];
|
||||
}
|
||||
|
||||
const T& back() const {
|
||||
return buf[size() - 1];
|
||||
}
|
||||
|
||||
T& operator[](std::size_t pos) {
|
||||
return buf[pos];
|
||||
}
|
||||
|
||||
const T& operator[](std::size_t pos) const {
|
||||
return buf[pos];
|
||||
}
|
||||
|
||||
// Information
|
||||
bool empty() const {
|
||||
return !size();
|
||||
}
|
||||
|
||||
std::size_t size() const {
|
||||
return buf_pos;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace bse
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user