#pragma once #include <stdexcept> #include "iterator/VectorIterator.hpp" #include "array/Helpers.hpp" #include "../common/compat.hpp" #include "../common/errors.hpp" #include "string.h" #include <stdlib.h> namespace sprawl { namespace collections { template<typename T> class Vector; } } template<typename T> class sprawl::collections::Vector { public: typedef VectorIterator<T, Vector<T>> iterator; typedef VectorIterator<T, Vector<T>> const const_iterator; explicit Vector(sprawl::collections::Capacity startingCapacity = sprawl::collections::Capacity(16)) : m_array(startingCapacity.count > 0 ? (T*)(malloc(startingCapacity.count * sizeof(T))) : nullptr) , m_size(0) , m_capacity(startingCapacity.count) { // } Vector(Fill fillCount) : m_array(fillCount.count > 0 ? (T*)(malloc(fillCount.count * sizeof(T))) : nullptr) , m_size(fillCount.count) , m_capacity(fillCount.count) { for(ssize_t i = 0; i < m_size; ++i) { new(&m_array[i]) T(); } } Vector(Fill fillCount, T const& value) : m_array(fillCount.count > 0 ? (T*)(malloc(fillCount.count * sizeof(T))) : nullptr) , m_size(fillCount.count) , m_capacity(fillCount.count) { for(ssize_t i = 0; i < m_size; ++i) { new(&m_array[i]) T(value); } } template<typename... Params> Vector(Fill fillCount, Params &&... params) : m_array(fillCount.count > 0 ? (T*)(malloc(fillCount.count * sizeof(T))) : nullptr) , m_size(fillCount.count) , m_capacity(fillCount.count) { for(ssize_t i = 0; i < m_size; ++i) { new(&m_array[i]) T(std::forward<Params>(params)...); } } Vector(Vector const& other) : m_array(other.m_size > 0 ? (T*)(malloc(other.m_size * sizeof(T))) : nullptr) , m_size(other.m_size) , m_capacity(other.m_size) { for(ssize_t i = 0; i < m_size; ++i) { new(&m_array[i]) T(other.m_array[i]); } } Vector(Vector&& other) : m_array(other.m_array) , m_size(other.m_size) , m_capacity(other.m_capacity) { other.m_array = nullptr; other.m_size = 0; other.m_capacity = 0; } Vector& operator=(Vector const& other) { for(ssize_t i = 0; i < m_size; ++i) { m_array[i].~T(); } m_size = 0; Reserve(other.m_size); m_size = other.m_size; for(ssize_t i = 0; i < m_size; ++i) { new(&m_array[i]) T(other.m_array[i]); } return *this; } Vector& operator=(Vector&& other) { cleanup_(); m_array = other.m_array; m_size = other.m_size; m_capacity = other.m_capacity; other.m_array = nullptr; other.m_size = 0; other.m_capacity = 0; return *this; } ~Vector() { cleanup_(); } T& operator[](ssize_t index) { if (index < 0) { index += m_size; } return m_array[index]; } sprawl::ErrorState<T&> At(ssize_t index) { if (index < 0) { index += m_size; } if(index > m_size) { SPRAWL_THROW_EXCEPTION(sprawl::OutOfRangeError()); } return m_array[index]; } T const& operator[](ssize_t index) const { if (index < 0) { index += m_size; } return m_array[index]; } sprawl::ErrorState<T const&> At(ssize_t index) const { if (index < 0) { index += m_size; } if(index > m_size) { SPRAWL_THROW_EXCEPTION(sprawl::OutOfRangeError()); } return m_array[index]; } T& Front() { return m_array[0]; } T& Back() { return m_array[m_size -1]; } T const& Front() const { return m_array[0]; } T const& Back() const { return m_array[m_size -1]; } void PushBack(T const& value) { checkGrow_(1); new(&m_array[m_size++]) T(value); } void PushBack(T&& value) { checkGrow_(1); new(&m_array[m_size++]) T(std::move(value)); } template<typename... Params> void EmplaceBack(Params &&... params) { checkGrow_(1); new(&m_array[m_size++]) T(std::forward<Params>(params)...); } T* Data() { return m_array; } T const* Data() const { return m_array; } void PopBack() { m_array[--m_size].~T(); } void ShrinkToFit() { if(m_size != m_capacity) { m_capacity = m_size; reallocate_(); } } ssize_t Size() const { return m_size; } ssize_t Capacity() const { return m_capacity; } void Reserve(ssize_t newCapacity) { if(newCapacity > m_capacity) { m_capacity = newCapacity; reallocate_(); } } void Resize(ssize_t newSize) { Resize(newSize, T()); } void Resize(ssize_t newSize, T const& value) { Reserve(newSize); //Either delete lost items or create new ones; one loop happens, the other doesn't for(ssize_t i = newSize; i < m_size; ++i) { m_array[i].~T(); } for(ssize_t i = m_size; i < newSize; ++i) { new(&m_array[i]) T(value); } m_size = newSize; } bool Empty() { return m_size == 0; } void Clear() { for(int i = 0; i < m_size; ++i) { m_array[i].~T(); } m_size = 0; } void Swap(Vector& other) { char tmpData[sizeof(Vector)]; memcpy(tmpData, this, sizeof(Vector)); memcpy(this, &other, sizeof(Vector)); memcpy(&other, tmpData, sizeof(Vector)); } void Insert(const_iterator position, T const& value) { int idx = &position.Value() - m_array; shiftRight_(idx, 1); new(&m_array[idx]) T(value); } void Insert(const_iterator position, T&& value) { int idx = &position.Value() - m_array; shiftRight_(idx, 1); new(&m_array[idx]) T(std::move(value)); } template<typename... Params> void Emplace(const_iterator position, Params &&... params) { int idx = &position.Value() - m_array; shiftRight_(idx, 1); new(&m_array[idx]) T(std::forward<Params>(params)...); } void Erase(const_iterator position) { int idx = &position.Value() - m_array; shiftLeft_(idx, 1); } iterator begin() { return iterator(m_array, this); } iterator end() { return iterator(m_array + m_size, this); } const_iterator cbegin() const { return const_iterator(m_array, this); } const_iterator cend() const { return const_iterator(m_array + m_size, this); } const_iterator begin() const { return cbegin(); } const_iterator end() const { return cend(); } private: void shiftRight_(ssize_t idx, ssize_t amount) { checkGrow_(amount); for(ssize_t i = (m_size + amount) - 1; i > idx; --i) { if(idx >= m_size) { new(&m_array[i]) T(std::move(m_array[i-amount])); } else { m_array[i] = std::move(m_array[i-amount]); } } m_size += amount; } void shiftLeft_(ssize_t idx, ssize_t amount) { for(ssize_t i = idx; i < m_size - amount; ++i) { m_array[i] = std::move(m_array[i + amount]); } for(ssize_t i = m_size-amount; i < m_size; ++i) { m_array[i].~T(); } m_size -= amount; } void checkGrow_(ssize_t amount) { if(m_size + amount > m_capacity) { grow_(amount); } } void grow_(ssize_t amount) { m_capacity = m_size + (amount > m_size ? amount : m_size); reallocate_(); } void reallocate_() { T* newArray = (T*)(malloc(m_capacity * sizeof(T))); for(ssize_t i = 0; i < m_size; ++i) { new (&newArray[i]) T(std::move(m_array[i])); m_array[i].~T(); } free(m_array); m_array = newArray; } void cleanup_() { if(m_array) { for(ssize_t i = 0; i < m_size; ++i) { m_array[i].~T(); } free(m_array); m_array = nullptr; } } T* m_array; ssize_t m_size; ssize_t m_capacity; };
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#8 | 16768 | ShadauxCat |
Improvements to error handling in builds with exceptions disabled: - In debug builds or with SPRAWL_ERRORSTATE_STRICT enabled, ErrorState will output a message to stderr and terminate if Get() is called when an error flag is set. (In release buils or with SPRAWL_ERRORSTATE_PERMISSIVE defined, Get() will return junk memory in this case.) - In debug builds or with SPRAWL_ERRORSTATE_STRICT enabled, ErrorState will output a message to stderr and terminate if its destructor is called without checking the errorstate if an error is present (equivalent to an exception terminating the application if no catch() block is present for it). - On linux builds and when running "Analyze" through visual studio, a warning will be issued if any function returning ErrorState has its return value ignored. (This only applies to builds with exceptions not enabled; when exceptions are enabled no warning is issued) - Many functions that could return ErrorState were having their return values silently ignored in internal sprawl code so the user would not find out about errors if exceptions are disabled; now anything in sprawl code that calls a function returning ErrorState will either handle the error, or (in most cases) surface it back up to the user. - As a positive side-effect of the warnings for ignoring ErrorState, several constructors that were capable of throwing exceptions are no longer capable of doing so. #review-16769 |
||
#7 | 16052 | ShadauxCat |
- Changed default block size for concurrent queue to a more reasonable value - Changed some memory orders to memory_order_seq_cst when they don't actually need to be that to get around a bug in visual studio 2013 - debug builds assert when memory_order_acq_rel is used for a compare_exchange_strong (this is a standard library bug and is fixed in VS2015) - Added Event API - events are an alternative to condition variables that do not require a mutex and are guaranteed not to miss any signals, even if the signal comes while the thread is not listening for it. Unlike condition variables, however, they do not support broadcasting (and in fact, in general, are not safe to use with multiple threads listening for the same event simultaneously - though notifying on the same event is fine) - Rewrote ThreadManager around ConcurrentQueue and Event API so it is now lock-free. Also improved some behaviors of the staged thread manager operation so it now supports tasks that can be run on multiple stages via a bitmask. - Fixed an issue where the Coroutine copy constructor was calling the std::function constructor instead and another where initializing with a stack might try to call the wrong constructor and vice-versa - Fixed Coroutine never calling munmap() on its stack in linux and causing a memory leak - Added default arguments to time functions - Attempted to fix some issues with BinaryTree. Fixed some but not all. It's currently not suitable for use, sadly. - Logging Improvements: - - Added thread ID to logging - - Fixed some issues with category handlers - - Added backtraces - - Added the following additional log macros: - - - LOG_IF - - - LOG_EVERY_N - - - LOG_FIRST_N - - - LOG_IF_EVERY_N - - - LOG_IF_FIRST_N - - - LOG_ASSERT - - Added the ability to set extra info callbacks to get data such as script backtraces - - Removed the thread-related handlers and replaced them with RunHandler_Threaded and RunHandler_ThreadManager, which will enable any passed-in handler to be run in a threaded fashion - Removed StaticPoolAllocator and renamed DynamicPoolAllocator to PoolAllocator; adjusted unit tests accordingly - PoolAllocator now allocates its pool with mmap and VirtualAlloc, rather than with malloc - Fixed a bug with Vector copy assignment operator - Improved performance of StringBuilder considerably for cases where there are no modifier strings - Removed Copy-On-Write behavior of JSONToken as it was broken; copies are now performed with explicit DeepCopy() and ShallowCopy() functions - Fixed some parser bugs with JSONToken - Added iteration to JSONToken to iterate its children - Fixed crash when reading a negative number of bytes from a file - Changed StringBuilder to favor speed instead of memory by default - Added some performance unit tests for JSON token #review-16053 |
||
#6 | 14216 | ShadauxCat |
-Moved some global sprawl::Strings into local scope in json serialization test because of initialization order issues in the memory allocator on mac. This is a temporary fix, and a real fix will come by making the pool allocator work in explicitly-sized pieces and putting all the code for those pieces into a cpp file. -Fixed a large number of warnings on mac/linux that were exposed by fixes to csbuild -Fixed compile errors on mac due to malloc and alloca not being defined, fixed by #include <stdlib.h> in appropriate places -Fixed mac os x trying to link against pthread erroneously -Provided os x implementation of time library -Fixed compile errors on os x due to std::unordered_map whining about the difference between an allocator that allocates std::pair<key, value> and one that allocates std::pair<key const, value>, which, of course, is that the allocator will be no different at all. -Fixed an actual issue where one unordered_map was allocating only key_type instead of std::pair<key_type, value_type> -Fixed a memory leak where coroutine objects would never be cleaned up because either Yield() or reactivate_() will never return (and thus never clean up their stack memory and thus never release any dynamic memory held in stack objects) depending on the situation - if the function runs to completion, reactivate_() never returns after calling swapcontext(); meanwhile, if the function does not run to completion, Yield() never returns after calling Pause(). This behavior will need to be well-documented because it will affect client-side code as well. Stack memory within a coroutine should not rely on RAII behavior. -Fixed compile failure when creating a StlWrapper with a const value_type #review-14217 |
||
#5 | 14168 | ShadauxCat |
-Implemented Array -Fixed compile error when calling Vector::Swap() -Fixed missing constness on functions called from const functions in Deque -Added fill, copy, and move constructor unit tests for Vector and Deque, and Swap() unit test for Vector #review-14169 |
||
#4 | 14121 | ShadauxCat |
-Fixed msvc compile errors (msvc sucks at decltype, btw...) -Fixed BitVector and BitSet being broken on msvc due to 1L being 32-bit rather than 64-bit -Fixed Deque being broken on 32-bit systems due to an errant int64_t -Changed types of deque indexes from size_t to ssize_t; since they have to be signed for a moment to handle circling the buffer, the sign bit is lost for capacity anyway, and using signed indexes means... -Deque and Vector now support negative indexing a la python list #review-14122 |
||
#3 | 14100 | ShadauxCat |
-Added Deque implementation using circular buffer. -Fixed List::Insert() and ForwardList::Insert() inserting after the iterator instead of before it. Adjusted the unit tests to compensate. -Fixed a couple of small vector bugs #review-14101 |
||
#2 | 14091 | ShadauxCat |
-Created Vector class, iterator, and unit test -Made list iterator a bidirectional iterator when created from a doubly-linked list. (It still has operator-- when created from singly-linked list, but it won't compile if it's used.) -Changed front() and back() in list classes to Front() and Back() #review-14083 |
||
#1 | 11496 | ShadauxCat | Initial checkin: Current states for csbuild and libSprawl |