Explorar o código

Updated tinystl.

Branimir Karadžić %!s(int64=11) %!d(string=hai) anos
pai
achega
28645519d3

+ 17 - 1
include/tinystl/buffer.h

@@ -118,7 +118,7 @@ namespace tinystl {
 	template<typename T, typename Alloc>
 	static inline void buffer_destroy(buffer<T, Alloc>* 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<typename T, typename Alloc>
@@ -146,6 +146,22 @@ namespace tinystl {
 		b->last = b->first + size;
 	}
 
+	template<typename T, typename Alloc>
+	static inline void buffer_shrink_to_fit(buffer<T, Alloc>* 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<typename T, typename Alloc>
 	static inline void buffer_clear(buffer<T, Alloc>* b) {
 		buffer_destroy_range(b->first, b->last);

+ 3 - 0
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<typename Key, typename Value>

+ 13 - 0
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<typename T, typename Alloc>
+	inline size_t vector<T, Alloc>::capacity() const {
+		return (size_t)(m_buffer.capacity - m_buffer.first);
+	}
+
 	template<typename T, typename Alloc>
 	inline bool vector<T, Alloc>::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<typename T, typename Alloc>
+	inline void vector<T, Alloc>::shrink_to_fit() {
+		buffer_shrink_to_fit(&m_buffer);
+	}
+
 	template<typename T, typename Alloc>
 	inline void vector<T, Alloc>::swap(vector& other) {
 		buffer_swap(&m_buffer, &other.m_buffer);

+ 38 - 0
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 <tinystl/allocator.h>
+#include <tinystl/unordered_map.h>
+
+TEST(uomap_nonpod_compiles) {
+	struct Foo { int bar; };
+
+	// verify this compiles
+	typedef tinystl::unordered_map<int, Foo> map;
+	map m;
+}

+ 68 - 0
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 <tinystl/allocator.h>
+#include <tinystl/vector.h>
+
+TEST(vector_shrinktofit) {
+	typedef tinystl::vector<int> 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);
+	}
+}