[Bf-blender-cvs] [a9322b7e58b] functions: small buffer inherits small vector

Jacques Lucke noreply at git.blender.org
Sun Feb 10 20:25:07 CET 2019


Commit: a9322b7e58b1f56b66379c3effd78e1e87862fa3
Author: Jacques Lucke
Date:   Wed Jan 23 16:06:02 2019 +0100
Branches: functions
https://developer.blender.org/rBa9322b7e58b1f56b66379c3effd78e1e87862fa3

small buffer inherits small vector

===================================================================

M	source/blender/blenlib/BLI_small_buffer.hpp
M	source/blender/blenlib/BLI_small_vector.hpp

===================================================================

diff --git a/source/blender/blenlib/BLI_small_buffer.hpp b/source/blender/blenlib/BLI_small_buffer.hpp
index a8a79471347..34d2745db37 100644
--- a/source/blender/blenlib/BLI_small_buffer.hpp
+++ b/source/blender/blenlib/BLI_small_buffer.hpp
@@ -1,43 +1,30 @@
 #pragma once
 
-#include "BLI_utildefines.h"
-#include <vector>
 #include <cstring>
+#include "BLI_utildefines.h"
+#include "BLI_small_vector.hpp"
 
 namespace BLI {
 
 	template<uint N = 16>
-	class SmallBuffer {
+	class SmallBuffer : private SmallVector<char, N> {
 	public:
 		SmallBuffer() {}
 
-		SmallBuffer(int size)
-		{
-			this->size = size;
-			if (size > N) {
-				this->buffer = new char[size];
-			}
-			else {
-				this->buffer = this->internal_buffer;
-			}
-		}
+		SmallBuffer(uint size)
+			: SmallVector<char, N>(size) { }
 
 		void copy_in(uint dst, void *src, uint amount)
 		{
-			BLI_assert(dst + amount <= this->size);
-			memcpy(this->buffer + dst, src, amount);
+			BLI_assert(dst + amount <= this->size());
+			memcpy(this->begin() + dst, src, amount);
 		}
 
 		void copy_out(void *dst, uint src, uint amount) const
 		{
-			BLI_assert(src + amount <= this->size);
-			memcpy(dst, this->buffer + src, amount);
+			BLI_assert(src + amount <= this->size());
+			memcpy(dst, this->begin() + src, amount);
 		}
-
-	private:
-		uint size;
-		char *buffer;
-		char internal_buffer[N];
 	};
 
 } /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 5b123e61571..59d309e6a66 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -22,9 +22,26 @@ namespace BLI {
 			this->m_size = 0;
 		}
 
+		SmallVector(uint size)
+		{
+			if (size > N) {
+				this->m_elements = (T *)std::malloc(sizeof(T) * size);
+				this->m_capacity = size;
+			}
+			else {
+				this->m_elements = this->m_small_buffer;
+				this->m_capacity = N;
+			}
+			for (uint i = 0; i < size; i++) {
+				this->m_elements[i] = T();
+			}
+			this->m_size = size;
+		}
+
 		SmallVector(std::initializer_list<T> values)
 			: SmallVector()
 		{
+			this->reserve(values.size());
 			for (T value : values) {
 				this->append(value);
 			}
@@ -65,23 +82,28 @@ namespace BLI {
 			return *this;
 		}
 
+		void reserve(uint size)
+		{
+			this->grow(size);
+		}
+
 		void append(T value)
 		{
 			if (this->m_size >= this->m_capacity) {
-				this->m_capacity *= 2;
-				uint new_byte_size = sizeof(T) * this->m_capacity;
-				if (this->is_small()) {
-					this->m_elements = (T *)std::malloc(new_byte_size);
-				}
-				else {
-					this->m_elements = (T *)std::realloc(this->m_elements, new_byte_size);
-				}
+				this->grow(std::max(this->m_capacity * 2, (uint)1));
 			}
 
 			this->m_elements[this->m_size] = value;
 			this->m_size++;
 		}
 
+		void fill(T value)
+		{
+			for (uint i = 0; i < this->m_size; i++) {
+				this->m_elements[i] = value;
+			}
+		}
+
 		uint size() const
 		{
 			return this->m_size;
@@ -115,6 +137,22 @@ namespace BLI {
 			return this->m_elements == this->m_small_buffer;
 		}
 
+		void grow(uint min_capacity)
+		{
+			if (this->m_capacity >= min_capacity) {
+				return;
+			}
+
+			this->m_capacity = min_capacity;
+			uint new_byte_size = sizeof(T) * this->m_capacity;
+			if (this->is_small()) {
+				this->m_elements = (T *)std::malloc(new_byte_size);
+			}
+			else {
+				this->m_elements = (T *)std::realloc(this->m_elements, new_byte_size);
+			}
+		}
+
 		void free_own_buffer()
 		{
 			if (!this->is_small()) {



More information about the Bf-blender-cvs mailing list