From 28645519d386c9bd3d095dd9d88045a8860da567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Tue, 11 Nov 2014 19:26:01 -0800 Subject: [PATCH] Updated tinystl. --- include/tinystl/buffer.h | 18 ++++++++- include/tinystl/hash_base.h | 3 ++ include/tinystl/vector.h | 13 +++++++ tests/unordered_map_nonpod.cpp | 38 +++++++++++++++++++ tests/vector_shrinktofit.cpp | 68 ++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 tests/unordered_map_nonpod.cpp create mode 100644 tests/vector_shrinktofit.cpp diff --git a/include/tinystl/buffer.h b/include/tinystl/buffer.h index fae9ba4..31654c6 100644 --- a/include/tinystl/buffer.h +++ b/include/tinystl/buffer.h @@ -118,7 +118,7 @@ namespace tinystl { template static inline void buffer_destroy(buffer* b) { buffer_destroy_range(b->first, b->last); - Alloc::static_deallocate(b->first, (size_t)((char*)b->first - (char*)b->last)); + Alloc::static_deallocate(b->first, (size_t)((char*)b->capacity - (char*)b->first)); } template @@ -146,6 +146,22 @@ namespace tinystl { b->last = b->first + size; } + template + static inline void buffer_shrink_to_fit(buffer* b) { + 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 size = (size_t)(b->last - b->first); + T* newfirst = (T*)Alloc::static_allocate(sizeof(T) * size); + buffer_move_urange(newfirst, b->first, b->last); + b->first = newfirst; + b->last = newfirst + size; + b->capacity = b->last; + } + } + template static inline void buffer_clear(buffer* b) { buffer_destroy_range(b->first, b->last); diff --git a/include/tinystl/hash_base.h b/include/tinystl/hash_base.h index 20d2da1..9d70ef0 100644 --- a/include/tinystl/hash_base.h +++ b/include/tinystl/hash_base.h @@ -65,6 +65,9 @@ namespace tinystl { Value second; unordered_hash_node* next; unordered_hash_node* prev; + + private: + unordered_hash_node& operator=(const unordered_hash_node&); }; template diff --git a/include/tinystl/vector.h b/include/tinystl/vector.h index 81d9f67..1e05f01 100644 --- a/include/tinystl/vector.h +++ b/include/tinystl/vector.h @@ -50,6 +50,7 @@ namespace tinystl { const T* data() const; T* data(); size_t size() const; + size_t capacity() const; bool empty() const; T& operator[](size_t idx); @@ -66,6 +67,8 @@ namespace tinystl { void push_back(const T& t); void pop_back(); + void shrink_to_fit(); + void swap(vector& other); typedef T value_type; @@ -153,6 +156,11 @@ namespace tinystl { return (size_t)(m_buffer.last - m_buffer.first); } + template + inline size_t vector::capacity() const { + return (size_t)(m_buffer.capacity - m_buffer.first); + } + template inline bool vector::empty() const { return m_buffer.last == m_buffer.first; @@ -208,6 +216,11 @@ namespace tinystl { buffer_erase(&m_buffer, m_buffer.last - 1, m_buffer.last); } + template + inline void vector::shrink_to_fit() { + buffer_shrink_to_fit(&m_buffer); + } + template inline void vector::swap(vector& other) { buffer_swap(&m_buffer, &other.m_buffer); diff --git a/tests/unordered_map_nonpod.cpp b/tests/unordered_map_nonpod.cpp new file mode 100644 index 0000000..c945803 --- /dev/null +++ b/tests/unordered_map_nonpod.cpp @@ -0,0 +1,38 @@ +/*- + * Copyright 2012-2014 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. + */ + +#include "test.h" + +#include +#include + +TEST(uomap_nonpod_compiles) { + struct Foo { int bar; }; + + // verify this compiles + typedef tinystl::unordered_map map; + map m; +} \ No newline at end of file diff --git a/tests/vector_shrinktofit.cpp b/tests/vector_shrinktofit.cpp new file mode 100644 index 0000000..cac2a0d --- /dev/null +++ b/tests/vector_shrinktofit.cpp @@ -0,0 +1,68 @@ +/*- + * Copyright 2012-2014 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. + */ + +#include "test.h" + +#include +#include + +TEST(vector_shrinktofit) { + typedef tinystl::vector vector; + + { + vector v; + CHECK(v.capacity() == 0); + v.clear(); + v.shrink_to_fit(); + CHECK(v.capacity() == 0); + } + + { + vector v(10, 0); + CHECK(v.capacity() >= 10); + v.shrink_to_fit(); + CHECK(v.capacity() == 10); + } + + { + vector v(10, 0); + CHECK(v.capacity() >= 10); + v.erase(v.end() - 1, v.end()); + CHECK(v.size() == 9); + CHECK(v.capacity() >= 10); + v.shrink_to_fit(); + CHECK(v.capacity() == 9); + } + + { + vector v(10, 0); + CHECK(v.capacity() >= 10); + const int* ptr = v.data(); + v.shrink_to_fit(); + CHECK(v.capacity() >= 10); + CHECK(v.data() == ptr); + } +}