|
@@ -1,14 +1,12 @@
|
|
#ifndef LIST_H
|
|
#ifndef LIST_H
|
|
#define LIST_H
|
|
#define LIST_H
|
|
|
|
|
|
-#include <new>
|
|
|
|
-#include <utility>
|
|
|
|
-
|
|
|
|
|
|
+#include "utils/UninitializedArray.h"
|
|
#include "utils/StringBuffer.h"
|
|
#include "utils/StringBuffer.h"
|
|
|
|
|
|
template<typename T, int N>
|
|
template<typename T, int N>
|
|
class List final {
|
|
class List final {
|
|
- char data[sizeof (T) * N];
|
|
|
|
|
|
+ UninitializedArray<T, N> data;
|
|
int length = 0;
|
|
int length = 0;
|
|
|
|
|
|
void copy(const List& other) {
|
|
void copy(const List& other) {
|
|
@@ -28,7 +26,7 @@ public:
|
|
|
|
|
|
void clear() {
|
|
void clear() {
|
|
for(int i = 0; i < length; i++) {
|
|
for(int i = 0; i < length; i++) {
|
|
- (*this)[i].~T();
|
|
|
|
|
|
+ data.destroy(i);
|
|
}
|
|
}
|
|
length = 0;
|
|
length = 0;
|
|
}
|
|
}
|
|
@@ -64,26 +62,26 @@ public:
|
|
}
|
|
}
|
|
|
|
|
|
T* begin() {
|
|
T* begin() {
|
|
- return reinterpret_cast<T*> (data);
|
|
|
|
|
|
+ return data.begin();
|
|
}
|
|
}
|
|
|
|
|
|
T* end() {
|
|
T* end() {
|
|
- return reinterpret_cast<T*> (data) + length;
|
|
|
|
|
|
+ return data.end();
|
|
}
|
|
}
|
|
|
|
|
|
const T* begin() const {
|
|
const T* begin() const {
|
|
- return reinterpret_cast<const T*> (data);
|
|
|
|
|
|
+ return data.begin();
|
|
}
|
|
}
|
|
|
|
|
|
const T* end() const {
|
|
const T* end() const {
|
|
- return reinterpret_cast<const T*> (data) + length;
|
|
|
|
|
|
+ return data.end();
|
|
}
|
|
}
|
|
|
|
|
|
bool add(const T& t) {
|
|
bool add(const T& t) {
|
|
if(length >= N) {
|
|
if(length >= N) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
- new (end()) T(t);
|
|
|
|
|
|
+ data.init(length, t);
|
|
length++;
|
|
length++;
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -92,7 +90,7 @@ public:
|
|
if(length >= N) {
|
|
if(length >= N) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
- new (end()) T(std::move(t));
|
|
|
|
|
|
+ data.init(length, std::move(t));
|
|
length++;
|
|
length++;
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
@@ -102,17 +100,17 @@ public:
|
|
if(length >= N) {
|
|
if(length >= N) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
- new (end()) T(std::forward<Args>(args)...);
|
|
|
|
|
|
+ data.init(length, std::forward<Args>(args)...);
|
|
length++;
|
|
length++;
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
T& operator[](int index) {
|
|
T& operator[](int index) {
|
|
- return begin()[index];
|
|
|
|
|
|
+ return data[index];
|
|
}
|
|
}
|
|
|
|
|
|
const T& operator[](int index) const {
|
|
const T& operator[](int index) const {
|
|
- return begin()[index];
|
|
|
|
|
|
+ return data[index];
|
|
}
|
|
}
|
|
|
|
|
|
int getLength() const {
|
|
int getLength() const {
|
|
@@ -123,11 +121,11 @@ public:
|
|
void toString(StringBuffer<L>& s) const {
|
|
void toString(StringBuffer<L>& s) const {
|
|
s.append("[");
|
|
s.append("[");
|
|
for(int i = 0; i < length - 1; i++) {
|
|
for(int i = 0; i < length - 1; i++) {
|
|
- s.append((*this)[i]);
|
|
|
|
|
|
+ s.append(data[i]);
|
|
s.append(", ");
|
|
s.append(", ");
|
|
}
|
|
}
|
|
if(length > 0) {
|
|
if(length > 0) {
|
|
- s.append((*this)[length - 1]);
|
|
|
|
|
|
+ s.append(data[length - 1]);
|
|
}
|
|
}
|
|
s.append("]");
|
|
s.append("]");
|
|
}
|
|
}
|
|
@@ -136,12 +134,12 @@ public:
|
|
if(index < 0 || index >= length) {
|
|
if(index < 0 || index >= length) {
|
|
return true;
|
|
return true;
|
|
} else if(index + 1 == length) {
|
|
} else if(index + 1 == length) {
|
|
- (*this)[index].~T();
|
|
|
|
|
|
+ data.destroy(index);
|
|
length--;
|
|
length--;
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
- (*this)[index] = std::move((*this)[length - 1]);
|
|
|
|
- (*this)[length - 1].~T();
|
|
|
|
|
|
+ data[index] = std::move(data[length - 1]);
|
|
|
|
+ data.destroy(length - 1);
|
|
length--;
|
|
length--;
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|