123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- #ifndef CORE_LIST_HPP
- #define CORE_LIST_HPP
- #include "utils/AlignedData.hpp"
- #include "utils/ArrayString.hpp"
- #include "utils/New.hpp"
- namespace Core {
- template<typename T>
- class List final {
- int length;
- int capacity;
- T* data;
- public:
- List() : length(0), capacity(0), data(nullptr) {
- }
- List(const List& other) = delete;
- List(List&& other) : List() {
- swap(other);
- }
- ~List() {
- clear();
- delete[] reinterpret_cast<AlignedType<T>*>(data);
- }
- List& operator=(const List& other) = delete;
- List& operator=(List&& other) {
- swap(other);
- return *this;
- }
- check_return Error copyFrom(const List& other) {
- List copy;
- CORE_RETURN_ERROR(allocate(copy.data, other.capacity));
- copy.capacity = other.capacity;
- for(int i = 0; i < other.length; i++) {
- copy.unsafeAdd(other[i]);
- }
- swap(copy);
- return Error::NONE;
- }
- T* begin() {
- return data;
- }
- T* end() {
- return data + length;
- }
- const T* begin() const {
- return data;
- }
- const T* end() const {
- return data + length;
- }
- check_return Error reserve(int n) {
- if(n <= capacity) {
- return Error::NONE;
- }
- List copy;
- CORE_RETURN_ERROR(allocate(copy.data, n));
- copy.capacity = n;
- for(int i = 0; i < length; i++) {
- copy.unsafeAdd(Core::move(data[i]));
- }
- swap(copy);
- return Error::NONE;
- }
- check_return Error shrink() {
- if(length == capacity) {
- return Error::NONE;
- }
- List copy;
- CORE_RETURN_ERROR(allocate(copy.data, length));
- copy.capacity = length;
- for(int i = 0; i < length; i++) {
- copy.unsafeAdd(Core::move(data[i]));
- }
- swap(copy);
- return Error::NONE;
- }
- check_return Error resize(int n, const T& t) {
- if(length < n) {
- CORE_RETURN_ERROR(reserve(n));
- for(int i = length; i < n; i++) {
- unsafeAdd(t);
- }
- } else if(length > n) {
- for(int i = n; i < length; i++) {
- data[i].~T();
- }
- length = n;
- }
- return Error::NONE;
- }
- check_return Error resize(int n) {
- if(length < n) {
- CORE_RETURN_ERROR(reserve(n));
- for(int i = length; i < n; i++) {
- unsafeAdd(T());
- }
- } else if(length > n) {
- for(int i = n; i < length; i++) {
- data[i].~T();
- }
- length = n;
- }
- return Error::NONE;
- }
- template<typename... Args>
- check_return Error put(T*& t, Args&&... args) {
- CORE_RETURN_ERROR(ensureCapacity());
- t = unsafeAdd(Core::forward<Args>(args)...);
- return Error::NONE;
- }
- template<typename... Args>
- check_return Error add(Args&&... args) {
- T* t = nullptr;
- return put(t, Core::forward<Args>(args)...);
- }
- T& operator[](int index) {
- return data[index];
- }
- const T& operator[](int index) const {
- return data[index];
- }
- int getLength() const {
- return length;
- }
- int getCapacity() const {
- return capacity;
- }
- void clear() {
- for(int i = 0; i < length; i++) {
- data[i].~T();
- }
- length = 0;
- }
- check_return Error removeBySwap(int index) {
- if(index < 0 || index >= length) {
- return Error::INVALID_INDEX;
- }
- length--;
- if(index != length) {
- data[index] = Core::move(data[length]);
- }
- data[length].~T();
- return Error::NONE;
- }
- check_return Error remove(int index) {
- if(index < 0 || index >= length) {
- return Error::INVALID_INDEX;
- }
- length--;
- for(int i = index; i < length; i++) {
- data[i] = Core::move(data[i + 1]);
- }
- data[length].~T();
- return Error::NONE;
- }
- check_return Error removeLast() {
- return removeBySwap(length - 1);
- }
- template<typename String>
- check_return Error toString(String& s) const {
- return Core::toString(s, *this);
- }
- private:
- static Error allocate(T*& t, int n) {
- if(n <= 0) {
- return Error::NEGATIVE_ARGUMENT;
- }
- t = reinterpret_cast<T*>(
- new AlignedType<T>[static_cast<size_t>(n)]);
- return t == nullptr ? Error::OUT_OF_MEMORY : Error::NONE;
- }
- void swap(List& other) {
- Core::swap(length, other.length);
- Core::swap(capacity, other.capacity);
- Core::swap(data, other.data);
- }
- check_return Error ensureCapacity() {
- return length >= capacity
- ? reserve(capacity + Core::Math::max(4, capacity / 4))
- : Error::NONE;
- }
- // does not check for capacity
- template<typename... Args>
- T* unsafeAdd(Args&&... args) {
- return new(data + length++) T(Core::forward<Args>(args)...);
- }
- };
- }
- #endif
|