Some more stdlib (array and vector, untested so far).
This commit is contained in:
parent
164f05bd59
commit
ff3214fa5a
15
SConstruct
15
SConstruct
@ -8,7 +8,7 @@ env['CXX'] = 'i686-elf-g++'
|
|||||||
env['LD'] = 'i686-elf-g++'
|
env['LD'] = 'i686-elf-g++'
|
||||||
env.Append(CXXFLAGS = ['-ffreestanding', '-fno-exceptions', '-fno-rtti', '-std=c++20'])
|
env.Append(CXXFLAGS = ['-ffreestanding', '-fno-exceptions', '-fno-rtti', '-std=c++20'])
|
||||||
env.Append(LINKFLAGS = ['-T', 'linker.ld', '-ffreestanding', '-nostdlib'])
|
env.Append(LINKFLAGS = ['-T', 'linker.ld', '-ffreestanding', '-nostdlib'])
|
||||||
env.Append(CPPPATH = ['#include'])
|
env.Append(CPPPATH = ['#include', '#stdlib/include'])
|
||||||
env.Append(CCFLAGS = ['-g', '-O0'])
|
env.Append(CCFLAGS = ['-g', '-O0'])
|
||||||
|
|
||||||
def get_crt_object(name: str) -> str:
|
def get_crt_object(name: str) -> str:
|
||||||
@ -31,12 +31,13 @@ os_sources = Split('''
|
|||||||
src/os/panic.cpp
|
src/os/panic.cpp
|
||||||
src/os/tty.cpp
|
src/os/tty.cpp
|
||||||
|
|
||||||
src/stdlib/assert.cpp
|
src/cstdlib/assert.cpp
|
||||||
src/stdlib/exception.cpp
|
src/cstdlib/stdio.cpp
|
||||||
src/stdlib/new.cpp
|
src/cstdlib/stdlib.cpp
|
||||||
src/stdlib/stdio.cpp
|
src/cstdlib/string.cpp
|
||||||
src/stdlib/stdlib.cpp
|
|
||||||
src/stdlib/string.cpp
|
stdlib/src/exception.cpp
|
||||||
|
stdlib/src/new.cpp
|
||||||
''')
|
''')
|
||||||
env['LINKCOM'] = env['LINKCOM'].replace('$_LIBFLAGS', f'{crti_o[0]} {crtbegin_o} $_LIBFLAGS {crtend_o} {crtn_o[0]}')
|
env['LINKCOM'] = env['LINKCOM'].replace('$_LIBFLAGS', f'{crti_o[0]} {crtbegin_o} $_LIBFLAGS {crtend_o} {crtn_o[0]}')
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#if !defined(BAD_APPLE_OS_STDIO_H_INCLUDED)
|
#if !defined(BAD_APPLE_OS_STDIO_H_INCLUDED)
|
||||||
#define BAD_APPLE_OS_STDIO_H_INCLUDED
|
#define BAD_APPLE_OS_STDIO_H_INCLUDED
|
||||||
|
|
||||||
#include <cstdarg>
|
#include "../stdlib/include/cstdarg"
|
||||||
#include "./detail/common.h"
|
#include "./detail/common.h"
|
||||||
|
|
||||||
BA_EXTERN_C_BEGIN
|
BA_EXTERN_C_BEGIN
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <vector>
|
||||||
|
|
||||||
extern "C" void main()
|
extern "C" void main()
|
||||||
{
|
{
|
||||||
|
std::vector<int> someInts;
|
||||||
|
someInts.resize(100);
|
||||||
}
|
}
|
||||||
|
12
stdlib/include/algorithm
Normal file
12
stdlib/include/algorithm
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(BAD_APPLE_OS_ALGORITHM_INCLUDED)
|
||||||
|
#define BAD_APPLE_OS_ALGORITHM_INCLUDED
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BAD_APPLE_OS_ALGORITHM_INCLUDED)
|
146
stdlib/include/array
Normal file
146
stdlib/include/array
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(BAD_APPLE_OS_ARRAY_INCLUDED)
|
||||||
|
#define BAD_APPLE_OS_ARRAY_INCLUDED
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template<typename T, std::size_t N>
|
||||||
|
class array
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
using size_type = std::size_t;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
using reference = value_type&;
|
||||||
|
using const_reference = const value_type&;
|
||||||
|
using pointer = value_type*;
|
||||||
|
using const_pointer = const value_type*;
|
||||||
|
using iterator = pointer;
|
||||||
|
using const_iterator = const_pointer;
|
||||||
|
|
||||||
|
T _elements;
|
||||||
|
|
||||||
|
constexpr reference operator[](size_type pos) noexcept
|
||||||
|
{
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr const_reference operator[](size_type pos) const noexcept
|
||||||
|
{
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr reference at(size_type pos)
|
||||||
|
{
|
||||||
|
if (pos >= N) {
|
||||||
|
__ba_throw out_of_range();
|
||||||
|
}
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_reference at(size_type pos) const
|
||||||
|
{
|
||||||
|
if (pos >= N) {
|
||||||
|
__ba_throw out_of_range();
|
||||||
|
}
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr reference front() noexcept
|
||||||
|
{
|
||||||
|
return _elements[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_reference front() const noexcept
|
||||||
|
{
|
||||||
|
return _elements[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr reference back() noexcept
|
||||||
|
{
|
||||||
|
return _elements[N - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_reference back() const noexcept
|
||||||
|
{
|
||||||
|
return _elements[N - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr pointer data() noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_pointer data() const noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr iterator begin() noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator begin() const noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator cbegin() const noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr iterator end() noexcept
|
||||||
|
{
|
||||||
|
return &_elements[N];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator end() const noexcept
|
||||||
|
{
|
||||||
|
return &_elements[N];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator cend() const noexcept
|
||||||
|
{
|
||||||
|
return &_elements[N];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr bool empty() const noexcept
|
||||||
|
{
|
||||||
|
return N == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr size_type size() const noexcept
|
||||||
|
{
|
||||||
|
return N;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr size_type max_size() const noexcept
|
||||||
|
{
|
||||||
|
return N;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void fill(const T& value)
|
||||||
|
{
|
||||||
|
for (T& ele : *this) {
|
||||||
|
ele = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void swap(array& other)
|
||||||
|
{
|
||||||
|
for (size_type idx = 0; idx < N; ++idx) {
|
||||||
|
std::swap((*this)[idx], other[idx]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BAD_APPLE_OS_ARRAY_INCLUDED)
|
15
stdlib/include/cstddef
Normal file
15
stdlib/include/cstddef
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(BAD_APPLE_OS_CSTDDEF_INCLUDED)
|
||||||
|
#define BAD_APPLE_OS_CSTDDEF_INCLUDED
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
using ::ptrdiff_t;
|
||||||
|
using ::size_t;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BAD_APPLE_OS_CSTDDEF_INCLUDED)
|
@ -2,6 +2,7 @@
|
|||||||
#if !defined(BAD_APPLE_OS_NEW_INCLUDED)
|
#if !defined(BAD_APPLE_OS_NEW_INCLUDED)
|
||||||
#define BAD_APPLE_OS_NEW_INCLUDED
|
#define BAD_APPLE_OS_NEW_INCLUDED
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
namespace std
|
namespace std
|
||||||
@ -16,4 +17,14 @@ public:
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void* operator new(std::size_t, void* placement) noexcept
|
||||||
|
{
|
||||||
|
return placement;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void* operator new[](std::size_t, void* placement) noexcept
|
||||||
|
{
|
||||||
|
return placement;
|
||||||
|
}
|
||||||
|
|
||||||
#endif // !defined(BAD_APPLE_OS_NEW_INCLUDED)
|
#endif // !defined(BAD_APPLE_OS_NEW_INCLUDED)
|
23
stdlib/include/stdexcept
Normal file
23
stdlib/include/stdexcept
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(BAD_APPLE_OS_STDEXCEPT_INCLUDED)
|
||||||
|
#define BAD_APPLE_OS_STDEXCEPT_INCLUDED
|
||||||
|
|
||||||
|
#include "exception"
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
class logic_error : public exception
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class out_of_range : public logic_error
|
||||||
|
{
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BAD_APPLE_OS_STDEXCEPT_INCLUDED)
|
31
stdlib/include/type_traits
Normal file
31
stdlib/include/type_traits
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(BAD_APPLE_OS_TYPE_TRAITS_INCLUDED)
|
||||||
|
#define BAD_APPLE_OS_TYPE_TRAITS_INCLUDED
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
struct remove_reference
|
||||||
|
{
|
||||||
|
using type = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct remove_reference<T&>
|
||||||
|
{
|
||||||
|
using type = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct remove_reference<T&&>
|
||||||
|
{
|
||||||
|
using type = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
using remove_reference_t = typename remove_reference<T>::type;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BAD_APPLE_OS_TYPE_TRAITS_INCLUDED)
|
40
stdlib/include/utility
Normal file
40
stdlib/include/utility
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(BAD_APPLE_OS_UTILITY_INCLUDED)
|
||||||
|
#define BAD_APPLE_OS_UTILITY_INCLUDED
|
||||||
|
|
||||||
|
#include "type_traits"
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
constexpr remove_reference_t<T>&& move(T&& val) noexcept
|
||||||
|
{
|
||||||
|
return static_cast<remove_reference_t<T>&&>(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr void swap(T& first, T& second)
|
||||||
|
{
|
||||||
|
T temp = std::move(first);
|
||||||
|
first = std::move(second);
|
||||||
|
second = std::move(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
[[nodiscard]] constexpr T&& forward(remove_reference_t<T>& value)
|
||||||
|
{
|
||||||
|
return static_cast<T&&>(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T, typename U = T>
|
||||||
|
[[nodiscard]] constexpr T exchange(T& obj, U&& newValue)
|
||||||
|
{
|
||||||
|
T oldValue = std::move(obj);
|
||||||
|
obj = std::forward<U>(newValue);
|
||||||
|
return oldValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BAD_APPLE_OS_UTILITY_INCLUDED)
|
287
stdlib/include/vector
Normal file
287
stdlib/include/vector
Normal file
@ -0,0 +1,287 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(BAD_APPLE_OS_VECTOR_INCLUDED)
|
||||||
|
#define BAD_APPLE_OS_VECTOR_INCLUDED
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <new>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template<typename T> // TODO: allocator
|
||||||
|
class vector
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
using value_type = T;
|
||||||
|
using size_type = std::size_t;
|
||||||
|
using difference_type = std::ptrdiff_t;
|
||||||
|
using reference = value_type&;
|
||||||
|
using const_reference = const value_type&;
|
||||||
|
using pointer = value_type*;
|
||||||
|
using const_pointer = const value_type*;
|
||||||
|
using iterator = pointer;
|
||||||
|
using const_iterator = const_pointer;
|
||||||
|
private:
|
||||||
|
T* _elements = nullptr;
|
||||||
|
size_type _size = 0;
|
||||||
|
size_type _capacity = 0;
|
||||||
|
public:
|
||||||
|
constexpr vector() noexcept = default;
|
||||||
|
constexpr vector(const vector& other) noexcept
|
||||||
|
{
|
||||||
|
resize(other.size());
|
||||||
|
for (size_type idx = 0; idx < size(); ++idx)
|
||||||
|
{
|
||||||
|
(*this)[idx] = other[idx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
constexpr vector(vector&& other) noexcept
|
||||||
|
: _elements(exchange(other._elements, nullptr)),
|
||||||
|
_size(exchange(other._size, 0)),
|
||||||
|
_capacity(exchange(other._capacity, 0))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
constexpr ~vector() noexcept
|
||||||
|
{
|
||||||
|
delete _elements;
|
||||||
|
}
|
||||||
|
constexpr vector& operator=(const vector& other)
|
||||||
|
{
|
||||||
|
if (this == &other) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
resize(other.size());
|
||||||
|
for (size_type idx = 0; idx < size(); ++idx)
|
||||||
|
{
|
||||||
|
(*this)[idx] = other[idx];
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
constexpr vector& operator=(vector&& other)
|
||||||
|
{
|
||||||
|
if (this == &other) {
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
clear();
|
||||||
|
_elements = exchange(other._elements, nullptr);
|
||||||
|
_size = exchange(other._size, 0);
|
||||||
|
_capacity = exchange(other._capacity, 0);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr reference operator[](size_type pos)
|
||||||
|
{
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_reference operator[](size_type pos) const
|
||||||
|
{
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr reference at(size_type pos)
|
||||||
|
{
|
||||||
|
if (pos >= size()) {
|
||||||
|
__ba_throw out_of_range();
|
||||||
|
}
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_reference at(size_type pos) const
|
||||||
|
{
|
||||||
|
if (pos >= size()) {
|
||||||
|
__ba_throw out_of_range();
|
||||||
|
}
|
||||||
|
return _elements[pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr reference front() noexcept
|
||||||
|
{
|
||||||
|
return _elements[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_reference front() const noexcept
|
||||||
|
{
|
||||||
|
return _elements[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr reference back() noexcept
|
||||||
|
{
|
||||||
|
return _elements[size() - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_reference back() const noexcept
|
||||||
|
{
|
||||||
|
return _elements[size() - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr pointer data() noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_pointer data() const noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr iterator begin() noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator begin() const noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator cbegin() const noexcept
|
||||||
|
{
|
||||||
|
return _elements;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr iterator end() noexcept
|
||||||
|
{
|
||||||
|
return &_elements[size()];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator end() const noexcept
|
||||||
|
{
|
||||||
|
return &_elements[size()];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr const_iterator cend() const noexcept
|
||||||
|
{
|
||||||
|
return &_elements[size()];
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr bool empty() const noexcept
|
||||||
|
{
|
||||||
|
return size() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr size_type size() const noexcept
|
||||||
|
{
|
||||||
|
return _size;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr size_type max_size() const noexcept
|
||||||
|
{
|
||||||
|
return size_type(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void reserve(size_type newCapacity)
|
||||||
|
{
|
||||||
|
if (newCapacity <= capacity()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
updateCapacity(newCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] size_type capacity() const noexcept
|
||||||
|
{
|
||||||
|
return _capacity;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void shrink_to_fit()
|
||||||
|
{
|
||||||
|
updateCapacity(size());
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void clear()
|
||||||
|
{
|
||||||
|
resize(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void push_back(const value_type& value)
|
||||||
|
{
|
||||||
|
reserve(size() + 1);
|
||||||
|
::new (&_elements[size()]) T(value);
|
||||||
|
++_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void push_back(value_type&& value)
|
||||||
|
{
|
||||||
|
reserve(size() + 1);
|
||||||
|
::new (&_elements[size()]) T(std::move(value));
|
||||||
|
++_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... TArgs>
|
||||||
|
constexpr reference emplace_back(TArgs&&... args)
|
||||||
|
{
|
||||||
|
reserve(size() + 1);
|
||||||
|
::new (&_elements[size()]) T(std::forward<TArgs>(args)...);
|
||||||
|
++_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void resize(size_type newSize)
|
||||||
|
{
|
||||||
|
if (newSize == size()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reserve(newSize);
|
||||||
|
|
||||||
|
if (newSize > size())
|
||||||
|
{
|
||||||
|
for (size_type idx = size(); idx < newSize; ++idx)
|
||||||
|
{
|
||||||
|
::new (&_elements[idx]) T();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (size_type idx = newSize; idx < size(); ++idx)
|
||||||
|
{
|
||||||
|
_elements[idx].~T();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_size = newSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void resize(size_type newSize, const value_type& initValue)
|
||||||
|
{
|
||||||
|
if (newSize == size()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
reserve(newSize);
|
||||||
|
|
||||||
|
if (newSize > size())
|
||||||
|
{
|
||||||
|
for (size_type idx = size(); idx < newSize; ++idx)
|
||||||
|
{
|
||||||
|
::new (&_elements[idx]) T(initValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (size_type idx = newSize; idx < size(); ++idx)
|
||||||
|
{
|
||||||
|
_elements[idx].~T();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_size = newSize;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
void updateCapacity(size_type newCapacity)
|
||||||
|
{
|
||||||
|
if (capacity() == newCapacity) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
T* newElements = static_cast<T*>(malloc(newCapacity * sizeof(T)));
|
||||||
|
for (size_type idx = 0; idx < size(); ++idx)
|
||||||
|
{
|
||||||
|
::new (&newElements[idx]) T(move(_elements[idx]));
|
||||||
|
_elements[idx].~T();
|
||||||
|
}
|
||||||
|
free(_elements);
|
||||||
|
_elements = newElements;
|
||||||
|
_capacity = newCapacity;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // !defined(BAD_APPLE_OS_VECTOR_INCLUDED)
|
Loading…
x
Reference in New Issue
Block a user