소스 검색

Merged latest tintystl.

Branimir Karadžić 10 년 전
부모
커밋
137a52ccf7
2개의 변경된 파일27개의 추가작업 그리고 7개의 파일을 삭제
  1. 22 0
      include/tinystl/buffer.h
  2. 5 7
      include/tinystl/vector.h

+ 22 - 0
include/tinystl/buffer.h

@@ -179,9 +179,11 @@ namespace tinystl {
 			Alloc::static_deallocate(b->first, sizeof(T)*capacity);
 			Alloc::static_deallocate(b->first, sizeof(T)*capacity);
 			b->capacity = b->first;
 			b->capacity = b->first;
 		} else if (b->capacity != b->last) {
 		} 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);
 			const size_t size = (size_t)(b->last - b->first);
 			T* newfirst = (T*)Alloc::static_allocate(sizeof(T) * size);
 			T* newfirst = (T*)Alloc::static_allocate(sizeof(T) * size);
 			buffer_move_urange(newfirst, b->first, b->last);
 			buffer_move_urange(newfirst, b->first, b->last);
+			Alloc::static_deallocate(b->first, sizeof(T) * capacity);
 			b->first = newfirst;
 			b->first = newfirst;
 			b->last = newfirst + size;
 			b->last = newfirst + size;
 			b->capacity = b->last;
 			b->capacity = b->last;
@@ -225,6 +227,26 @@ namespace tinystl {
 			new(placeholder(), where) T();
 			new(placeholder(), where) T();
 	}
 	}
 
 
+	template<typename T, typename Alloc, typename Param>
+	static inline void buffer_append(buffer<T, Alloc>* b, const Param* param) {
+		if (b->capacity != b->last) {
+			new(placeholder(), b->last) T(*param);
+			++b->last;
+		} else {
+			buffer_insert(b, b->last, param, param + 1);
+		}
+	}
+
+	template<typename T, typename Alloc>
+	static inline void buffer_append(buffer<T, Alloc>* b) {
+		if (b->capacity != b->last) {
+			new(placeholder(), b->last) T();
+			++b->last;
+		} else {
+			buffer_insert(b, b->last, 1);
+		}
+	}
+
 	template<typename T, typename Alloc>
 	template<typename T, typename Alloc>
 	static inline T* buffer_erase(buffer<T, Alloc>* b, T* first, T* last) {
 	static inline T* buffer_erase(buffer<T, Alloc>* b, T* first, T* last) {
 		typedef T* pointer;
 		typedef T* pointer;

+ 5 - 7
include/tinystl/vector.h

@@ -228,20 +228,18 @@ namespace tinystl {
 
 
 	template<typename T, typename Alloc>
 	template<typename T, typename Alloc>
 	inline void vector<T, Alloc>::push_back(const T& t) {
 	inline void vector<T, Alloc>::push_back(const T& t) {
-		buffer_insert(&m_buffer, m_buffer.last, &t, &t + 1);
+		buffer_append(&m_buffer, &t);
 	}
 	}
 
 
 	template<typename T, typename Alloc>
 	template<typename T, typename Alloc>
-	inline void vector<T, Alloc>::emplace_back()
-	{
-		buffer_insert(&m_buffer, m_buffer.last, 1);
+	inline void vector<T, Alloc>::emplace_back() {
+		buffer_append(&m_buffer);
 	}
 	}
 
 
 	template<typename T, typename Alloc>
 	template<typename T, typename Alloc>
 	template<typename Param>
 	template<typename Param>
-	inline void vector<T, Alloc>::emplace_back(const Param& param)
-	{
-		buffer_insert(&m_buffer, m_buffer.last, &param, &param + 1);
+	inline void vector<T, Alloc>::emplace_back(const Param& param) {
+		buffer_append(&m_buffer, &param);
 	}
 	}
 
 
 	template<typename T, typename Alloc>
 	template<typename T, typename Alloc>