switch to raw ptr, clean moved objects, destructor
This commit is contained in:
@ -6,9 +6,9 @@
|
|||||||
// ArrayList instead, without additional effort.
|
// ArrayList instead, without additional effort.
|
||||||
|
|
||||||
#include "user/lib/List.h"
|
#include "user/lib/List.h"
|
||||||
|
#include "user/lib/Logger.h"
|
||||||
#include "user/lib/mem/UniquePointer.h"
|
#include "user/lib/mem/UniquePointer.h"
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <type_traits>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
// 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
|
||||||
@ -23,16 +23,13 @@ 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 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
|
static constexpr const std::size_t min_cap = 5; // Slots to allocate extra when array full
|
||||||
|
|
||||||
bse::unique_ptr<Type[]> buf; // Heap allocated as size needs to change during runtime
|
Type* 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
|
// Can't use Array for the same reason so we use a C Style array
|
||||||
// NOTE: I wouldn't normally use smart pointers for low level datastructures
|
|
||||||
// but here it doesn't hurt as the unique_ptr basically has no overhead
|
|
||||||
// (And it saved 2 deletes ¯\_(ツ)_/¯)
|
|
||||||
std::size_t buf_pos = 0;
|
std::size_t buf_pos = 0;
|
||||||
std::size_t buf_cap = 0;
|
std::size_t buf_cap = 0;
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
buf = bse::make_unique<Type[]>(ArrayList::default_cap);
|
buf = new Type[ArrayList::default_cap];
|
||||||
buf_cap = ArrayList::default_cap;
|
buf_cap = ArrayList::default_cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +40,7 @@ private:
|
|||||||
// Enlarges the buffer if we run out of space
|
// Enlarges the buffer if we run out of space
|
||||||
std::size_t expand() {
|
std::size_t expand() {
|
||||||
// Init if necessary
|
// Init if necessary
|
||||||
if (!buf) {
|
if (buf == nullptr) {
|
||||||
init();
|
init();
|
||||||
return buf_cap; // Dont have to realloc after init
|
return buf_cap; // Dont have to realloc after init
|
||||||
}
|
}
|
||||||
@ -53,15 +50,17 @@ private:
|
|||||||
std::size_t new_cap = buf_cap + min_cap;
|
std::size_t new_cap = buf_cap + min_cap;
|
||||||
|
|
||||||
// Alloc new array
|
// Alloc new array
|
||||||
bse::unique_ptr<Type[]> new_buf = bse::make_unique<Type[]>(new_cap);
|
Type* new_buf = new Type[new_cap];
|
||||||
|
|
||||||
// Swap current elements to new array
|
// Swap current elements to new array
|
||||||
for (std::size_t i = 0; i < size(); ++i) {
|
for (std::size_t i = 0; i < size(); ++i) {
|
||||||
new_buf[i] = std::move(buf[i]); // Should I have just used a regular pointer?
|
new_buf[i] = std::move(buf[i]);
|
||||||
|
buf[i].~Type();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Move new array to buf, deleting the old array
|
// Move new array to buf, deleting the old array
|
||||||
buf = std::move(new_buf);
|
delete[] buf;
|
||||||
|
buf = new_buf;
|
||||||
buf_cap = new_cap;
|
buf_cap = new_cap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,6 +91,7 @@ private:
|
|||||||
// pos = 4 pos = 5
|
// pos = 4 pos = 5
|
||||||
for (std::size_t idx = size(); idx > i; --idx) { // idx > i so idx - 1 is never < 0
|
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] = std::move(buf[idx - 1]);
|
||||||
|
buf[idx - 1].~Type();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only change pos if elements were copied
|
// Only change pos if elements were copied
|
||||||
@ -118,13 +118,19 @@ private:
|
|||||||
// pos = 3 pos = 2
|
// pos = 3 pos = 2
|
||||||
for (std::size_t idx = i; idx < size(); ++idx) { // idx < pos so idx + 1 is never outside of size limit
|
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] = std::move(buf[idx + 1]);
|
||||||
|
buf[idx + 1].~Type();
|
||||||
}
|
}
|
||||||
|
|
||||||
return size();
|
return size();
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
~ArrayList() = default; // Buffer deletes itself (I hope)
|
~ArrayList() {
|
||||||
|
for (std::size_t i; i < size(); ++i) {
|
||||||
|
buf[i].~Type();
|
||||||
|
}
|
||||||
|
delete[] buf;
|
||||||
|
}
|
||||||
|
|
||||||
Iterator begin() override {
|
Iterator begin() override {
|
||||||
return Iterator(&buf[0]);
|
return Iterator(&buf[0]);
|
||||||
@ -135,6 +141,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns new pos
|
// Returns new pos
|
||||||
|
// NOTE: Insert copies
|
||||||
std::size_t insert_at(Type e, std::size_t i) override {
|
std::size_t insert_at(Type e, std::size_t i) override {
|
||||||
if (i > size()) {
|
if (i > size()) {
|
||||||
// Error: Space between elements
|
// Error: Space between elements
|
||||||
@ -146,8 +153,12 @@ public:
|
|||||||
return insert_last(std::forward<Type>(e));
|
return insert_last(std::forward<Type>(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
copy_right(i); // Changes pos
|
if constexpr (std::is_copy_assignable_v<Type>) {
|
||||||
buf[i] = e;
|
copy_right(i); // Changes pos
|
||||||
|
buf[i] = e;
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return size();
|
return size();
|
||||||
}
|
}
|
||||||
@ -157,21 +168,35 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::size_t insert_last(Type e) override {
|
std::size_t insert_last(Type e) override {
|
||||||
expand();
|
if (!buf) {
|
||||||
buf[size()] = e;
|
init();
|
||||||
++buf_pos;
|
}
|
||||||
|
|
||||||
|
if constexpr (std::is_copy_assignable_v<Type>) {
|
||||||
|
buf[size()] = e;
|
||||||
|
++buf_pos;
|
||||||
|
expand();
|
||||||
|
} else {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return size();
|
return size();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns removed element
|
// Returns removed element
|
||||||
|
// NOTE: Remove moves
|
||||||
std::optional<Type> remove_at(std::size_t i) override {
|
std::optional<Type> remove_at(std::size_t i) override {
|
||||||
if (i >= size()) {
|
if (i >= size()) {
|
||||||
// ERROR: No element here
|
// ERROR: No element here
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Move moves the data if possible, copies otherwise
|
||||||
|
// It does not destroy the original object, an intact
|
||||||
|
// invariant is left so we have to destruct the old object
|
||||||
Type e = std::move(buf[i]);
|
Type e = std::move(buf[i]);
|
||||||
|
buf[i].~Type(); // Cleanup the leftovers from the move
|
||||||
|
|
||||||
copy_left(i);
|
copy_left(i);
|
||||||
return e;
|
return e;
|
||||||
}
|
}
|
||||||
@ -204,7 +229,12 @@ public:
|
|||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
return buf[i];
|
// TODO: assignable or constructable?
|
||||||
|
if constexpr (std::is_copy_assignable_v<Type>) {
|
||||||
|
return buf[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<Type> first() const override {
|
std::optional<Type> first() const override {
|
||||||
@ -225,7 +255,7 @@ public:
|
|||||||
|
|
||||||
void print(OutStream& out) const override {
|
void print(OutStream& out) const override {
|
||||||
// Our stream cannot print all types so enable this only for debugging purposes (only int)
|
// Our stream cannot print all types so enable this only for debugging purposes (only int)
|
||||||
if constexpr (std::is_same<Type, int>::value) {
|
if constexpr (std::is_same_v<Type, int>) {
|
||||||
if (empty()) {
|
if (empty()) {
|
||||||
out << "Print List (0 elements)" << endl;
|
out << "Print List (0 elements)" << endl;
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user