diff --git a/include/blt/std/format.h b/include/blt/std/format.h index 1ce0a72..2b57c9e 100644 --- a/include/blt/std/format.h +++ b/include/blt/std/format.h @@ -14,6 +14,7 @@ #include #include #include "memory.h" +#include "vector.h" #include namespace blt::string diff --git a/include/blt/std/vector.h b/include/blt/std/vector.h index ef5e0bd..2f6a84a 100644 --- a/include/blt/std/vector.h +++ b/include/blt/std/vector.h @@ -21,6 +21,7 @@ #include #include +#include "ranges.h" namespace blt { @@ -145,7 +146,7 @@ namespace blt } }; - template + template> class vector { private: @@ -154,6 +155,14 @@ namespace blt size_t capacity_ = 0; size_t size_ = 0; + using value_type = T; + using allocator_type = ALLOC; + using size_type = 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 pointer; using iterator = T*; using const_iterator = const T*; using reverse_iterator = std::reverse_iterator; @@ -182,6 +191,28 @@ namespace blt buffer_ = allocator.allocate(capacity_); } + template, bool> = true> + constexpr vector(std::initializer_list&& list): size_(list.size()), capacity_(list.size()) + { + buffer_ = allocator.allocate(capacity_); + for (auto e : blt::enumerate(list)) + buffer_[e.first] = e.second; + } + + template, G> || std::is_same_v, G>, bool> = true> + constexpr explicit vector(const G& copy): size_(copy.size()), capacity_(copy.capacity()) + { + buffer_ = allocator.allocate(capacity_); + for (auto e : blt::enumerate(copy)) + buffer_[e.first] = e.second; + } + + template, G> || std::is_same_v, G>, bool> = true> + constexpr explicit vector(G&& move): size_(move.size()), capacity_(move.capacity()), buffer_(move.buffer_) + { + move.buffer_ = nullptr; + } + ~vector() { allocator.deallocate(buffer_, capacity_); @@ -202,11 +233,11 @@ namespace blt } template - constexpr inline void emplace_back(Args&&... args) + constexpr inline void emplace_back(Args&& ... args) { if (size_ >= capacity_) expand(); - buffer_[size_++] = T{std::forward(args)...}; + new(&buffer_[size_++]) T(std::forward(args)...); } constexpr inline T& at(size_t index) @@ -217,6 +248,14 @@ namespace blt return buffer_[index]; } + constexpr inline const T& at(size_t index) const + { + if (index >= capacity_) + throw std::runtime_error( + "Array index " + std::to_string(index) + " out of bounds! (Max size: " + std::to_string(capacity_) + ')'); + return buffer_[index]; + } + constexpr inline T& operator[](size_t index) { return buffer_[index];