diff --git a/include/tinystl/LICENSE b/include/tinystl/LICENSE index bfea5915f..f03d947b0 100644 --- a/include/tinystl/LICENSE +++ b/include/tinystl/LICENSE @@ -1,4 +1,4 @@ - Copyright 2012-2018 Matthew Endsley + Copyright 2012 Matthew Endsley All rights reserved Redistribution and use in source and binary forms, with or without diff --git a/include/tinystl/allocator.h b/include/tinystl/allocator.h index ce3efe0a3..a0cce7ba3 100644 --- a/include/tinystl/allocator.h +++ b/include/tinystl/allocator.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,7 +27,9 @@ #ifndef TINYSTL_ALLOCATOR_H #define TINYSTL_ALLOCATOR_H -#include +#include "stddef.h" + +#ifndef TINYSTL_ALLOCATOR namespace tinystl { @@ -42,8 +44,7 @@ namespace tinystl { }; } -#ifndef TINYSTL_ALLOCATOR # define TINYSTL_ALLOCATOR ::tinystl::allocator -#endif +#endif // TINYSTL_ALLOCATOR #endif diff --git a/include/tinystl/buffer.h b/include/tinystl/buffer.h index efc5c9cc6..4351ec202 100644 --- a/include/tinystl/buffer.h +++ b/include/tinystl/buffer.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012-1015 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,9 +27,8 @@ #ifndef TINYSTL_BUFFER_H #define TINYSTL_BUFFER_H -#include -#include -#include +#include "new.h" +#include "traits.h" namespace tinystl { @@ -141,7 +140,7 @@ namespace tinystl { template static inline void buffer_reserve(buffer* b, size_t capacity) { - if (b->first && b->first + capacity <= b->capacity) + if (b->first + capacity <= b->capacity) return; typedef T* pointer; @@ -175,21 +174,19 @@ namespace tinystl { template static inline void buffer_shrink_to_fit(buffer* b) { - if (b->capacity != b->last) { - if (b->last == b->first) { - const size_t capacity = (size_t)(b->capacity - b->first); - Alloc::static_deallocate(b->first, sizeof(T)*capacity); - b->capacity = b->first = b->last = nullptr; - } else { - const size_t capacity = (size_t)(b->capacity - b->first); - const size_t size = (size_t)(b->last - b->first); - T* newfirst = (T*)Alloc::static_allocate(sizeof(T) * size); - buffer_move_urange(newfirst, b->first, b->last); - Alloc::static_deallocate(b->first, sizeof(T) * capacity); - b->first = newfirst; - b->last = newfirst + size; - b->capacity = b->last; - } + if (b->last == b->first) { + const size_t capacity = (size_t)(b->last - b->first); + Alloc::static_deallocate(b->first, sizeof(T)*capacity); + b->capacity = b->first; + } else if (b->capacity != b->last) { + const size_t capacity = (size_t)(b->capacity - b->first); + const size_t size = (size_t)(b->last - b->first); + T* newfirst = (T*)Alloc::static_allocate(sizeof(T) * size); + buffer_move_urange(newfirst, b->first, b->last); + Alloc::static_deallocate(b->first, sizeof(T) * capacity); + b->first = newfirst; + b->last = newfirst + size; + b->capacity = b->last; } } @@ -203,7 +200,7 @@ namespace tinystl { static inline T* buffer_insert_common(buffer* b, T* where, size_t count) { const size_t offset = (size_t)(where - b->first); const size_t newsize = (size_t)((b->last - b->first) + count); - if (!b->first || b->first + newsize > b->capacity) + if (b->first + newsize > b->capacity) buffer_reserve(b, (newsize * 3) / 2); where = b->first + offset; @@ -218,21 +215,7 @@ namespace tinystl { template static inline void buffer_insert(buffer* b, T* where, const Param* first, const Param* last) { - typedef const char* pointer; - const size_t count = last - first; - const bool frombuf = ((pointer)b->first <= (pointer)first && (pointer)b->last >= (pointer)last); - size_t offset; - if (frombuf) { - offset = (pointer)first - (pointer)b->first; - if ((pointer)where <= (pointer)first) - offset += count * sizeof(T); - where = buffer_insert_common(b, where, count); - first = (Param*)((pointer)b->first + offset); - last = first + count; - } - else { - where = buffer_insert_common(b, where, count); - } + where = buffer_insert_common(b, where, last - first); for (; first != last; ++first, ++where) new(placeholder(), where) T(*first); } @@ -240,7 +223,7 @@ namespace tinystl { template static inline void buffer_insert(buffer* b, T* where, size_t count) { where = buffer_insert_common(b, where, count); - for (T* end = where+count; where != end; ++where) + for (size_t i = 0; i < count; ++i) new(placeholder(), where) T(); } @@ -267,28 +250,28 @@ namespace tinystl { template static inline T* buffer_erase(buffer* b, T* first, T* last) { typedef T* pointer; - const size_t count = (last - first); + const size_t range = (last - first); for (pointer it = last, end = b->last, dest = first; it != end; ++it, ++dest) move(*dest, *it); - buffer_destroy_range(b->last - count, b->last); + buffer_destroy_range(b->last - range, b->last); - b->last -= count; + b->last -= range; return first; } template static inline T* buffer_erase_unordered(buffer* b, T* first, T* last) { typedef T* pointer; - const size_t count = (last - first); + const size_t range = (last - first); const size_t tail = (b->last - last); - pointer it = b->last - ((count < tail) ? count : tail); + pointer it = b->last - ((range < tail) ? range : tail); for (pointer end = b->last, dest = first; it != end; ++it, ++dest) move(*dest, *it); - buffer_destroy_range(b->last - count, b->last); + buffer_destroy_range(b->last - range, b->last); - b->last -= count; + b->last -= range; return first; } @@ -299,12 +282,6 @@ namespace tinystl { b->first = other->first, b->last = other->last, b->capacity = other->capacity; other->first = tfirst, other->last = tlast, other->capacity = tcapacity; } - - template - static inline void buffer_move(buffer* dst, buffer* src) { - dst->first = src->first, dst->last = src->last, dst->capacity = src->capacity; - src->first = src->last = src->capacity = nullptr; - } } -#endif //TINYSTL_BUFFER_H +#endif diff --git a/include/tinystl/hash.h b/include/tinystl/hash.h index 55f97881e..787ddae7c 100644 --- a/include/tinystl/hash.h +++ b/include/tinystl/hash.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,7 +27,7 @@ #ifndef TINYSTL_STRINGHASH_H #define TINYSTL_STRINGHASH_H -#include +#include "stddef.h" namespace tinystl { diff --git a/include/tinystl/hash_base.h b/include/tinystl/hash_base.h index aac60f0fd..f20675db4 100644 --- a/include/tinystl/hash_base.h +++ b/include/tinystl/hash_base.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,98 +27,42 @@ #ifndef TINYSTL_HASH_BASE_H #define TINYSTL_HASH_BASE_H -#include -#include +#include "stddef.h" namespace tinystl { template struct pair { + typedef Key first_type; + typedef Value second_type; + pair(); - pair(const pair& other); - pair(pair&& other); pair(const Key& key, const Value& value); - pair(Key&& key, Value&& value); - - pair& operator=(const pair& other); - pair& operator=(pair&& other); Key first; Value second; - - using first_type = Key; - using second_type = Value; }; template - inline pair::pair() { - } - - template - inline pair::pair(const pair& other) - : first(other.first) - , second(other.second) - { - } - - template - inline pair::pair(pair&& other) - : first(static_cast(other.first)) - , second(static_cast(other.second)) - { + pair::pair() { } template - inline pair::pair(const Key& key, const Value& value) + pair::pair(const Key& key, const Value& value) : first(key) , second(value) { } - template - inline pair::pair(Key&& key, Value&& value) - : first(static_cast(key)) - , second(static_cast(value)) - { - } - - template - inline pair& pair::operator=(const pair& other) { - first = other.first; - second = other.second; - return *this; - } - - template - inline pair& pair::operator=(pair&& other) { - first = static_cast(other.first); - second = static_cast(other.second); - return *this; - } - - // Replaced the following function by the old-school const& variant, until - // https://github.com/mendsley/tinystl/issues/25 is fixed. -#if 0 - template - static inline pair::type, typename remove_reference::type> - make_pair(Key&& key, Value&& value) { - return pair::type, typename remove_reference::type>( - static_cast(key) - , static_cast(value) - ); - } -#else template static inline pair make_pair(const Key& key, const Value& value) { return pair(key, value); } -#endif template struct unordered_hash_node { unordered_hash_node(const Key& key, const Value& value); - unordered_hash_node(Key&& key, Value&& value); const Key first; Value second; @@ -130,23 +74,15 @@ namespace tinystl { }; template - inline unordered_hash_node::unordered_hash_node(const Key& key, const Value& value) + unordered_hash_node::unordered_hash_node(const Key& key, const Value& value) : first(key) , second(value) { } - template - inline unordered_hash_node::unordered_hash_node(Key&& key, Value&& value) - : first(static_cast(key)) - , second(static_cast(value)) - { - } - template struct unordered_hash_node { - explicit unordered_hash_node(const Key& key); - explicit unordered_hash_node(Key&& key); + unordered_hash_node(const Key& key); const Key first; unordered_hash_node* next; @@ -157,19 +93,13 @@ namespace tinystl { }; template - inline unordered_hash_node::unordered_hash_node(const Key& key) + unordered_hash_node::unordered_hash_node(const Key& key) : first(key) { } - template - inline unordered_hash_node::unordered_hash_node(Key&& key) - : first(static_cast(key)) - { - } - template - static inline void unordered_hash_node_insert(unordered_hash_node* node, size_t hash, unordered_hash_node** buckets, size_t nbuckets) { + static void unordered_hash_node_insert(unordered_hash_node* node, size_t hash, unordered_hash_node** buckets, size_t nbuckets) { size_t bucket = hash & (nbuckets - 1); unordered_hash_node* it = buckets[bucket + 1]; @@ -293,7 +223,6 @@ namespace tinystl { template static inline Node unordered_hash_find(const Key& key, Node* buckets, size_t nbuckets) { - if (!buckets) return 0; const size_t bucket = hash(key) & (nbuckets - 2); for (Node it = buckets[bucket], end = buckets[bucket+1]; it != end; it = it->next) if (it->first == key) diff --git a/include/tinystl/new.h b/include/tinystl/new.h index 62bb072d7..9c86dd8c2 100644 --- a/include/tinystl/new.h +++ b/include/tinystl/new.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,7 +27,7 @@ #ifndef TINYSTL_NEW_H #define TINYSTL_NEW_H -#include +#include "stddef.h" namespace tinystl { diff --git a/include/tinystl/stddef.h b/include/tinystl/stddef.h index a31dd347b..21c1b1ec9 100644 --- a/include/tinystl/stddef.h +++ b/include/tinystl/stddef.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -29,13 +29,10 @@ #if defined(_WIN64) typedef long long unsigned int size_t; - typedef long long int ptrdiff_t; #elif defined(_WIN32) typedef unsigned int size_t; - typedef int ptrdiff_t; -#elif defined (__linux__) && defined(__SIZE_TYPE__) && defined(__PTRDIFF_TYPE__) +#elif defined (__linux__) && defined(__SIZE_TYPE__) typedef __SIZE_TYPE__ size_t; - typedef __PTRDIFF_TYPE__ ptrdiff_t; #else # include #endif diff --git a/include/tinystl/string.h b/include/tinystl/string.h index 3c3cb653f..4d90c2ae4 100644 --- a/include/tinystl/string.h +++ b/include/tinystl/string.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,37 +27,34 @@ #ifndef TINYSTL_STRING_H #define TINYSTL_STRING_H -#include -#include -#include +#include // strlen +#include "stddef.h" +#include "hash.h" namespace tinystl { - template - class basic_string { + template + class stringT { public: - basic_string(); - basic_string(const basic_string& other); - basic_string(basic_string&& other); - basic_string(const char* sz); - basic_string(const char* sz, size_t len); - ~basic_string(); + stringT(); + stringT(const stringT& other); + stringT(const char* sz); + stringT(const char* sz, size_t len); + ~stringT(); - basic_string& operator=(const basic_string& other); - basic_string& operator=(basic_string&& other); + stringT& operator=(const stringT& other); const char* c_str() const; size_t size() const; + bool empty() const; - void reserve(size_t size); - void resize(size_t size); + void reserve(size_t _size); + void resize(size_t _size); - void clear(); void append(const char* first, const char* last); - void assign(const char* s, size_t n); + void append(const char* str); - void shrink_to_fit(); - void swap(basic_string& other); + void swap(stringT& other); private: typedef char* pointer; @@ -69,8 +66,10 @@ namespace tinystl { char m_buffer[12]; }; - template - inline basic_string::basic_string() + typedef stringT string; + + template + inline stringT::stringT() : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -78,8 +77,8 @@ namespace tinystl { resize(0); } - template - inline basic_string::basic_string(const basic_string& other) + template + inline stringT::stringT(const stringT& other) : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -88,27 +87,8 @@ namespace tinystl { append(other.m_first, other.m_last); } - template - inline basic_string::basic_string(basic_string&& other) - { - if (other.m_first == other.m_buffer) { - m_first = m_buffer; - m_last = m_buffer; - m_capacity = m_buffer + c_nbuffer; - reserve(other.size()); - append(other.m_first, other.m_last); - } else { - m_first = other.m_first; - m_last = other.m_last; - m_capacity = other.m_capacity; - } - other.m_first = other.m_last = other.m_buffer; - other.m_capacity = other.m_buffer + c_nbuffer; - other.resize(0); - } - - template - inline basic_string::basic_string(const char* sz) + template + inline stringT::stringT(const char* sz) : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -121,8 +101,8 @@ namespace tinystl { append(sz, sz + len); } - template - inline basic_string::basic_string(const char* sz, size_t len) + template + inline stringT::stringT(const char* sz, size_t len) : m_first(m_buffer) , m_last(m_buffer) , m_capacity(m_buffer + c_nbuffer) @@ -131,73 +111,68 @@ namespace tinystl { append(sz, sz + len); } - template - inline basic_string::~basic_string() { + template + inline stringT::~stringT() { if (m_first != m_buffer) - allocator::static_deallocate(m_first, m_capacity - m_first); - } - - template - inline basic_string& basic_string::operator=(const basic_string& other) { - basic_string(other).swap(*this); - return *this; + Alloc::static_deallocate(m_first, m_capacity - m_first); } - template - inline basic_string& basic_string::operator=(basic_string&& other) { - basic_string(static_cast(other)).swap(*this); + template + inline stringT& stringT::operator=(const stringT& other) { + stringT(other).swap(*this); return *this; } - template - inline const char* basic_string::c_str() const { + template + inline const char* stringT::c_str() const { return m_first; } - template - inline size_t basic_string::size() const + template + inline size_t stringT::size() const { return (size_t)(m_last - m_first); } - template - inline void basic_string::reserve(size_t capacity) { - if (m_first + capacity + 1 <= m_capacity) + template + inline bool stringT::empty() const + { + return 0 == size(); + } + + template + inline void stringT::reserve(size_t capacity) { + if (m_first + capacity + 1 <= m_capacity) { return; + } - const size_t size = (size_t)(m_last - m_first); + const size_t _size = (size_t)(m_last - m_first); - pointer newfirst = (pointer)allocator::static_allocate(capacity + 1); - for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit) + pointer newfirst = (pointer)Alloc::static_allocate(capacity + 1); + for (pointer it = m_first, newit = newfirst, end = m_last; it != end; ++it, ++newit) { *newit = *it; - if (m_first != m_buffer) - allocator::static_deallocate(m_first, m_capacity - m_first); + } + + if (m_first != m_buffer) { + Alloc::static_deallocate(m_first, m_capacity - m_first); + } m_first = newfirst; - m_last = newfirst + size; + m_last = newfirst + _size; m_capacity = m_first + capacity; } - template - inline void basic_string::resize(size_t size) { - const size_t prevSize = m_last-m_first; - reserve(size); - if (size > prevSize) - for (pointer it = m_last, end = m_first + size + 1; it < end; ++it) - *it = 0; - else if (m_last != m_first) - m_first[size] = 0; - - m_last = m_first + size; - } + template + inline void stringT::resize(size_t _size) { + reserve(_size); + for (pointer it = m_last, end = m_first + _size + 1; it < end; ++it) + *it = 0; - template - inline void basic_string::clear() { - resize(0); + m_last += _size; } - template - inline void basic_string::append(const char* first, const char* last) { + template + inline void stringT::append(const char* first, const char* last) { const size_t newsize = (size_t)((m_last - m_first) + (last - first) + 1); if (m_first + newsize > m_capacity) reserve((newsize * 3) / 2); @@ -207,35 +182,13 @@ namespace tinystl { *m_last = 0; } - template - inline void basic_string::assign(const char* sz, size_t n) { - clear(); - append(sz, sz+n); - } - - template - inline void basic_string::shrink_to_fit() { - if (m_first == m_buffer) { - } else if (m_last == m_first) { - const size_t capacity = (size_t)(m_capacity - m_first); - if (capacity) - allocator::static_deallocate(m_first, capacity+1); - m_capacity = m_first; - } else if (m_capacity != m_last) { - const size_t size = (size_t)(m_last - m_first); - char* newfirst = (pointer)allocator::static_allocate(size+1); - for (pointer in = m_first, out = newfirst; in != m_last + 1; ++in, ++out) - *out = *in; - if (m_first != m_capacity) - allocator::static_deallocate(m_first, m_capacity+1-m_first); - m_first = newfirst; - m_last = newfirst+size; - m_capacity = m_last; - } + template + inline void stringT::append(const char* str) { + append(str, str + strlen(str) ); } - template - inline void basic_string::swap(basic_string& other) { + template + inline void stringT::swap(stringT& other) { const pointer tfirst = m_first, tlast = m_last, tcapacity = m_capacity; m_first = other.m_first, m_last = other.m_last, m_capacity = other.m_capacity; other.m_first = tfirst, other.m_last = tlast, other.m_capacity = tcapacity; @@ -267,8 +220,8 @@ namespace tinystl { } } - template - inline bool operator==(const basic_string& lhs, const basic_string& rhs) { + template + inline bool operator==(const stringT& lhs, const stringT& rhs) { typedef const char* pointer; const size_t lsize = lhs.size(), rsize = rhs.size(); @@ -284,12 +237,10 @@ namespace tinystl { return true; } - template - static inline size_t hash(const basic_string& value) { + template + static inline size_t hash(const stringT& value) { return hash_string(value.c_str(), value.size()); } - - typedef basic_string string; } #endif diff --git a/include/tinystl/string_view.h b/include/tinystl/string_view.h deleted file mode 100644 index 13a714d21..000000000 --- a/include/tinystl/string_view.h +++ /dev/null @@ -1,147 +0,0 @@ -/*- - * Copyright 2012-1017 Matthew Endsley - * All rights reserved - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted providing that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef TINYSTL_STRING_VIEW_H -#define TINYSTL_STRING_VIEW_H - -#include - -namespace tinystl { - - class string_view - { - public: - typedef char value_type; - typedef char* pointer; - typedef const char* const_pointer; - typedef char& reference; - typedef const char& const_reference; - typedef const_pointer iterator; - typedef const_pointer const_iterator; - typedef size_t size_type; - typedef ptrdiff_t difference_type; - - static constexpr size_type npos = size_type(-1); - - constexpr string_view(); - constexpr string_view(const char* s, size_type count); - constexpr string_view(const char* s); - constexpr string_view(const string_view&) = default; - string_view& operator=(const string_view&) = default; - - constexpr const char* data() const; - constexpr char operator[](size_type pos) const; - constexpr size_type size() const; - constexpr bool empty() const; - constexpr iterator begin() const; - constexpr const_iterator cbegin() const; - constexpr iterator end() const; - constexpr const_iterator cend() const; - constexpr string_view substr(size_type pos = 0, size_type count = npos) const; - constexpr void swap(string_view& v); - - private: - string_view(decltype(nullptr)) = delete; - - static constexpr size_type strlen(const char*); - - const char* m_str; - size_type m_size; - }; - - constexpr string_view::string_view() - : m_str(nullptr) - , m_size(0) - { - } - - constexpr string_view::string_view(const char* s, size_type count) - : m_str(s) - , m_size(count) - { - } - - constexpr string_view::string_view(const char* s) - : m_str(s) - , m_size(strlen(s)) - { - } - - constexpr const char* string_view::data() const { - return m_str; - } - - constexpr char string_view::operator[](size_type pos) const { - return m_str[pos]; - } - - constexpr string_view::size_type string_view::size() const { - return m_size; - } - - constexpr bool string_view::empty() const { - return 0 == m_size; - } - - constexpr string_view::iterator string_view::begin() const { - return m_str; - } - - constexpr string_view::const_iterator string_view::cbegin() const { - return m_str; - } - - constexpr string_view::iterator string_view::end() const { - return m_str + m_size; - } - - constexpr string_view::const_iterator string_view::cend() const { - return m_str + m_size; - } - - constexpr string_view string_view::substr(size_type pos, size_type count) const { - return string_view(m_str + pos, npos == count ? m_size - pos : count); - } - - constexpr void string_view::swap(string_view& v) { - const char* strtmp = m_str; - size_type sizetmp = m_size; - m_str = v.m_str; - m_size = v.m_size; - v.m_str = strtmp; - v.m_size = sizetmp; - } - - constexpr string_view::size_type string_view::strlen(const char* s) { - for (size_t len = 0; ; ++len) { - if (0 == s[len]) { - return len; - } - } - } -} - -#endif // TINYSTL_STRING_VIEW_H diff --git a/include/tinystl/traits.h b/include/tinystl/traits.h index 6cb64fce9..11b18e70c 100644 --- a/include/tinystl/traits.h +++ b/include/tinystl/traits.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,9 +27,9 @@ #ifndef TINYSTL_TRAITS_H #define TINYSTL_TRAITS_H -#include +#include "new.h" -#if defined(__GNUC__) +#if defined(__GNUC__) && defined(__is_pod) # define TINYSTL_TRY_POD_OPTIMIZATION(t) __is_pod(t) #elif defined(_MSC_VER) # define TINYSTL_TRY_POD_OPTIMIZATION(t) (!__is_class(t) || __is_pod(t)) @@ -80,21 +80,6 @@ namespace tinystl { static inline void move_construct(T* a, T& b) { move_construct_impl(a, b, (T*)0); } - - template - struct remove_reference { - typedef T type; - }; - - template - struct remove_reference { - typedef T type; - }; - - template - struct remove_reference { - typedef T type; - }; } #endif diff --git a/include/tinystl/unordered_map.h b/include/tinystl/unordered_map.h index cc7df21c6..64f845749 100644 --- a/include/tinystl/unordered_map.h +++ b/include/tinystl/unordered_map.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,10 +27,9 @@ #ifndef TINYSTL_UNORDERED_MAP_H #define TINYSTL_UNORDERED_MAP_H -#include -#include -#include -#include +#include "buffer.h" +#include "hash.h" +#include "hash_base.h" namespace tinystl { @@ -39,14 +38,13 @@ namespace tinystl { public: unordered_map(); unordered_map(const unordered_map& other); - unordered_map(unordered_map&& other); ~unordered_map(); unordered_map& operator=(const unordered_map& other); - unordered_map& operator=(unordered_map&& other); - typedef pair value_type; + typedef pair value_type; + typedef unordered_hash_iterator > const_iterator; typedef unordered_hash_iterator > iterator; @@ -63,7 +61,6 @@ namespace tinystl { const_iterator find(const Key& key) const; iterator find(const Key& key); pair insert(const pair& p); - pair emplace(pair&& p); void erase(const_iterator where); Value& operator[](const Key& key); @@ -72,23 +69,22 @@ namespace tinystl { private: - void rehash(size_t nbuckets); - typedef unordered_hash_node* pointer; size_t m_size; - tinystl::buffer m_buckets; + buffer m_buckets; }; template - inline unordered_map::unordered_map() + unordered_map::unordered_map() : m_size(0) { buffer_init(&m_buckets); + buffer_resize(&m_buckets, 9, 0); } template - inline unordered_map::unordered_map(const unordered_map& other) + unordered_map::unordered_map(const unordered_map& other) : m_size(other.m_size) { const size_t nbuckets = (size_t)(other.m_buckets.last - other.m_buckets.first); @@ -104,32 +100,17 @@ namespace tinystl { } template - inline unordered_map::unordered_map(unordered_map&& other) - : m_size(other.m_size) - { - buffer_move(&m_buckets, &other.m_buckets); - other.m_size = 0; - } - - template - inline unordered_map::~unordered_map() { - if (m_buckets.first != m_buckets.last) - clear(); + unordered_map::~unordered_map() { + clear(); buffer_destroy(&m_buckets); } template - inline unordered_map& unordered_map::operator=(const unordered_map& other) { + unordered_map& unordered_map::operator=(const unordered_map& other) { unordered_map(other).swap(*this); return *this; } - template - inline unordered_map& unordered_map::operator=(unordered_map&& other) { - unordered_map(static_cast(other)).swap(*this); - return *this; - } - template inline typename unordered_map::iterator unordered_map::begin() { iterator it; @@ -198,25 +179,6 @@ namespace tinystl { return result; } - template - inline void unordered_map::rehash(size_t nbuckets) { - if (m_size + 1 > 4 * nbuckets) { - pointer root = *m_buckets.first; - - const size_t newnbuckets = ((size_t)(m_buckets.last - m_buckets.first) - 1) * 8; - m_buckets.last = m_buckets.first; - buffer_resize(&m_buckets, newnbuckets + 1, 0); - unordered_hash_node** buckets = m_buckets.first; - - while (root) { - const pointer next = root->next; - root->next = root->prev = 0; - unordered_hash_node_insert(root, hash(root->first), buckets, newnbuckets); - root = next; - } - } - } - template inline pair::iterator, bool> unordered_map::insert(const pair& p) { pair result; @@ -225,41 +187,29 @@ namespace tinystl { result.first = find(p.first); if (result.first.node != 0) return result; - + unordered_hash_node* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node))) unordered_hash_node(p.first, p.second); newnode->next = newnode->prev = 0; - if(!m_buckets.first) buffer_resize(&m_buckets, 9, 0); const size_t nbuckets = (size_t)(m_buckets.last - m_buckets.first); unordered_hash_node_insert(newnode, hash(p.first), m_buckets.first, nbuckets - 1); ++m_size; - rehash(nbuckets); - - result.first.node = newnode; - result.second = true; - return result; - } - - template - inline pair::iterator, bool> unordered_map::emplace(pair&& p) { - pair result; - result.second = false; - - result.first = find(p.first); - if (result.first.node != 0) - return result; - - const size_t keyhash = hash(p.first); - unordered_hash_node* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node))) unordered_hash_node(static_cast(p.first), static_cast(p.second)); - newnode->next = newnode->prev = 0; - - if (!m_buckets.first) buffer_resize(&m_buckets, 9, 0); - const size_t nbuckets = (size_t)(m_buckets.last - m_buckets.first); - unordered_hash_node_insert(newnode, keyhash, m_buckets.first, nbuckets - 1); + if (m_size + 1 > 4 * nbuckets) { + pointer root = *m_buckets.first; + + const size_t newnbuckets = ((size_t)(m_buckets.last - m_buckets.first) - 1) * 8; + m_buckets.last = m_buckets.first; + buffer_resize(&m_buckets, newnbuckets + 1, 0); + unordered_hash_node** buckets = m_buckets.first; - ++m_size; - rehash(nbuckets); + while (root) { + const pointer next = root->next; + root->next = root->prev = 0; + unordered_hash_node_insert(root, hash(root->first), buckets, newnbuckets); + root = next; + } + } result.first.node = newnode; result.second = true; @@ -267,7 +217,7 @@ namespace tinystl { } template - inline void unordered_map::erase(const_iterator where) { + void unordered_map::erase(const_iterator where) { unordered_hash_node_erase(where.node, hash(where->first), m_buckets.first, (size_t)(m_buckets.last - m_buckets.first) - 1); where->~unordered_hash_node(); @@ -276,12 +226,12 @@ namespace tinystl { } template - inline Value& unordered_map::operator[](const Key& key) { + Value& unordered_map::operator[](const Key& key) { return insert(pair(key, Value())).first->second; } template - inline void unordered_map::swap(unordered_map& other) { + void unordered_map::swap(unordered_map& other) { size_t tsize = other.m_size; other.m_size = m_size, m_size = tsize; buffer_swap(&m_buckets, &other.m_buckets); diff --git a/include/tinystl/unordered_set.h b/include/tinystl/unordered_set.h index 66a6a0bd5..976a412b1 100644 --- a/include/tinystl/unordered_set.h +++ b/include/tinystl/unordered_set.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,10 +27,9 @@ #ifndef TINYSTL_UNORDERED_SET_H #define TINYSTL_UNORDERED_SET_H -#include -#include -#include -#include +#include "buffer.h" +#include "hash.h" +#include "hash_base.h" namespace tinystl { @@ -39,11 +38,9 @@ namespace tinystl { public: unordered_set(); unordered_set(const unordered_set& other); - unordered_set(unordered_set&& other); ~unordered_set(); unordered_set& operator=(const unordered_set& other); - unordered_set& operator=(unordered_set&& other); typedef unordered_hash_iterator > const_iterator; typedef const_iterator iterator; @@ -57,7 +54,6 @@ namespace tinystl { iterator find(const Key& key) const; pair insert(const Key& key); - pair emplace(Key&& key); void erase(iterator where); size_t erase(const Key& key); @@ -65,16 +61,14 @@ namespace tinystl { private: - void rehash(size_t nbuckets); - typedef unordered_hash_node* pointer; size_t m_size; - tinystl::buffer m_buckets; + buffer m_buckets; }; template - inline unordered_set::unordered_set() + unordered_set::unordered_set() : m_size(0) { buffer_init(&m_buckets); @@ -82,7 +76,7 @@ namespace tinystl { } template - inline unordered_set::unordered_set(const unordered_set& other) + unordered_set::unordered_set(const unordered_set& other) : m_size(other.m_size) { const size_t nbuckets = (size_t)(other.m_buckets.last - other.m_buckets.first); @@ -97,32 +91,17 @@ namespace tinystl { } template - inline unordered_set::unordered_set(unordered_set&& other) - : m_size(other.m_size) - { - buffer_move(&m_buckets, &other.m_buckets); - other.m_size = 0; - } - - template - inline unordered_set::~unordered_set() { - if (m_buckets.first != m_buckets.last) - clear(); + unordered_set::~unordered_set() { + clear(); buffer_destroy(&m_buckets); } template - inline unordered_set& unordered_set::operator=(const unordered_set& other) { + unordered_set& unordered_set::operator=(const unordered_set& other) { unordered_set(other).swap(*this); return *this; } - template - inline unordered_set& unordered_set::operator=(unordered_set&& other) { - unordered_set(static_cast(other)).swap(*this); - return *this; - } - template inline typename unordered_set::iterator unordered_set::begin() const { iterator cit; @@ -170,25 +149,6 @@ namespace tinystl { return result; } - template - inline void unordered_set::rehash(size_t nbuckets) { - if (m_size + 1 > 4 * nbuckets) { - pointer root = *m_buckets.first; - - const size_t newnbuckets = ((size_t)(m_buckets.last - m_buckets.first) - 1) * 8; - m_buckets.last = m_buckets.first; - buffer_resize(&m_buckets, newnbuckets + 1, 0); - unordered_hash_node** buckets = m_buckets.first; - - while (root) { - const pointer next = root->next; - root->next = root->prev = 0; - unordered_hash_node_insert(root, hash(root->first), buckets, newnbuckets); - root = next; - } - } - } - template inline pair::iterator, bool> unordered_set::insert(const Key& key) { pair result; @@ -205,31 +165,21 @@ namespace tinystl { unordered_hash_node_insert(newnode, hash(key), m_buckets.first, nbuckets - 1); ++m_size; - rehash(nbuckets); - - result.first.node = newnode; - result.second = true; - return result; - } - - template - inline pair::iterator, bool> unordered_set::emplace(Key&& key) { - pair result; - result.second = false; - - result.first = find(key); - if (result.first.node != 0) - return result; - - const size_t keyhash = hash(key); - unordered_hash_node* newnode = new(placeholder(), Alloc::static_allocate(sizeof(unordered_hash_node))) unordered_hash_node(static_cast(key)); - newnode->next = newnode->prev = 0; + if (m_size + 1 > 4 * nbuckets) { + pointer root = *m_buckets.first; - const size_t nbuckets = (size_t)(m_buckets.last - m_buckets.first); - unordered_hash_node_insert(newnode, keyhash, m_buckets.first, nbuckets - 1); + const size_t newnbuckets = ((size_t)(m_buckets.last - m_buckets.first) - 1) * 8; + m_buckets.last = m_buckets.first; + buffer_resize(&m_buckets, newnbuckets + 1, 0); + unordered_hash_node** buckets = m_buckets.first; - ++m_size; - rehash(nbuckets); + while (root) { + const pointer next = root->next; + root->next = root->prev = 0; + unordered_hash_node_insert(root, hash(root->first), buckets, newnbuckets); + root = next; + } + } result.first.node = newnode; result.second = true; diff --git a/include/tinystl/vector.h b/include/tinystl/vector.h index e248f626e..6885feb4f 100644 --- a/include/tinystl/vector.h +++ b/include/tinystl/vector.h @@ -1,5 +1,5 @@ /*- - * Copyright 2012-2018 Matthew Endsley + * Copyright 2012-1015 Matthew Endsley * All rights reserved * * Redistribution and use in source and binary forms, with or without @@ -27,25 +27,23 @@ #ifndef TINYSTL_VECTOR_H #define TINYSTL_VECTOR_H -#include -#include -#include -#include +#include "buffer.h" +#include "new.h" +#include "stddef.h" namespace tinystl { + template class vector { public: vector(); vector(const vector& other); - vector(vector&& other); - vector(size_t size); - vector(size_t size, const T& value); + vector(size_t _size); + vector(size_t _size, const T& value); vector(const T* first, const T* last); ~vector(); vector& operator=(const vector& other); - vector& operator=(vector&& other); void assign(const T* first, const T* last); @@ -66,7 +64,7 @@ namespace tinystl { void resize(size_t size); void resize(size_t size, const T& value); void clear(); - void reserve(size_t capacity); + void reserve(size_t _capacity); void push_back(const T& t); void pop_back(); @@ -119,20 +117,15 @@ namespace tinystl { } template - inline vector::vector(vector&& other) { - buffer_move(&m_buffer, &other.m_buffer); - } - - template - inline vector::vector(size_t size) { + inline vector::vector(size_t _size) { buffer_init(&m_buffer); - buffer_resize(&m_buffer, size); + buffer_resize(&m_buffer, _size); } template - inline vector::vector(size_t size, const T& value) { + inline vector::vector(size_t _size, const T& value) { buffer_init(&m_buffer); - buffer_resize(&m_buffer, size, value); + buffer_resize(&m_buffer, _size, value); } template @@ -152,13 +145,6 @@ namespace tinystl { return *this; } - template - vector& vector::operator=(vector&& other) { - buffer_destroy(&m_buffer); - buffer_move(&m_buffer, &other.m_buffer); - return *this; - } - template inline void vector::assign(const T* first, const T* last) { buffer_clear(&m_buffer); @@ -221,13 +207,13 @@ namespace tinystl { } template - inline void vector::resize(size_t size) { - buffer_resize(&m_buffer, size); + inline void vector::resize(size_t _size) { + buffer_resize(&m_buffer, _size); } template - inline void vector::resize(size_t size, const T& value) { - buffer_resize(&m_buffer, size, value); + inline void vector::resize(size_t _size, const T& value) { + buffer_resize(&m_buffer, _size, value); } template @@ -236,8 +222,8 @@ namespace tinystl { } template - inline void vector::reserve(size_t capacity) { - buffer_reserve(&m_buffer, capacity); + inline void vector::reserve(size_t _capacity) { + buffer_reserve(&m_buffer, _capacity); } template @@ -292,7 +278,7 @@ namespace tinystl { } template - inline void vector::insert(typename vector::iterator where) { + inline void vector::insert(iterator where) { buffer_insert(&m_buffer, where, 1); } @@ -328,9 +314,9 @@ namespace tinystl { template template - void vector::emplace(typename vector::iterator where, const Param& param) { + void vector::emplace(iterator where, const Param& param) { buffer_insert(&m_buffer, where, ¶m, ¶m + 1); } } -#endif // TINYSTL_VECTOR_H +#endif