[Bf-blender-cvs] [9b3a4c16979] functions: real SmallVector implementation + make types const

Jacques Lucke noreply at git.blender.org
Sun Feb 10 20:24:50 CET 2019


Commit: 9b3a4c1697994128139bcfe0b2e594012ab4c5c4
Author: Jacques Lucke
Date:   Tue Jan 22 15:00:17 2019 +0100
Branches: functions
https://developer.blender.org/rB9b3a4c1697994128139bcfe0b2e594012ab4c5c4

real SmallVector implementation + make types const

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

M	source/blender/blenlib/BLI_small_vector.hpp
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/intern/function.cpp
M	source/blender/functions/types/numeric.hpp

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

diff --git a/source/blender/blenlib/BLI_small_vector.hpp b/source/blender/blenlib/BLI_small_vector.hpp
index 28fb0b4c27a..68153dba55a 100644
--- a/source/blender/blenlib/BLI_small_vector.hpp
+++ b/source/blender/blenlib/BLI_small_vector.hpp
@@ -1,23 +1,28 @@
 #pragma once
 
 #include "BLI_utildefines.h"
-#include <vector>
+#include <cstdlib>
 
 namespace BLI {
 
 	template<typename T, uint N = 4>
 	class SmallVector {
 	private:
-		using elements_t = std::vector<T>;
-		elements_t elements;
+		T m_small_buffer[N];
+		T *m_elements;
+		uint m_size = 0;
+		uint m_capacity = N;
 
 	public:
-		using iterator = typename elements_t::iterator;
-		using const_iterator = typename elements_t::const_iterator;
-
-		SmallVector() {}
+		SmallVector()
+		{
+			this->m_elements = this->m_small_buffer;
+			this->m_capacity = N;
+			this->m_size = 0;
+		}
 
 		SmallVector(std::initializer_list<T> values)
+			: SmallVector()
 		{
 			for (T value : values) {
 				this->append(value);
@@ -26,35 +31,47 @@ namespace BLI {
 
 		void append(T value)
 		{
-			this->elements.push_back(value);
+			if (this->m_size >= this->m_capacity) {
+				this->m_capacity *= 2;
+				uint new_byte_size = sizeof(T) * this->m_capacity;
+				if (this->m_elements == this->m_small_buffer) {
+					this->m_elements = (T *)std::malloc(new_byte_size);
+				}
+				else {
+					this->m_elements = (T *)std::realloc(this->m_elements, new_byte_size);
+				}
+			}
+
+			this->m_elements[this->m_size] = value;
+			this->m_size++;
 		}
 
 		uint size() const
 		{
-			return this->elements.size();
+			return this->m_size;
 		}
 
 		T &operator[](const int index)
 		{
 			BLI_assert(index >= 0 && index < this->size());
-			return this->elements[index];
+			return this->m_elements[index];
 		}
 
 		T operator[](const int index) const
 		{
 			BLI_assert(index >= 0 && index < this->size());
-			return this->elements[index];
+			return this->m_elements[index];
 		}
 
-		const_iterator begin() const
-		{ return this->elements.begin(); }
-		const_iterator end() const
-		{ return this->elements.end(); }
+		T *begin() const
+		{ return this->m_elements; }
+		T *end() const
+		{ return this->begin() + this->size(); }
 
-		const_iterator cbegin() const
-		{ return this->elements.cbegin(); }
-		const_iterator cend() const
-		{ return this->elements.cend(); }
+		const T *cbegin() const
+		{ return this->begin(); }
+		const T *cend() const
+		{ return this->end(); }
 	};
 
 } /* namespace BLI */
\ No newline at end of file
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index b3481502ce8..0f11e920578 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -15,7 +15,7 @@ namespace FN {
 	class Signature;
 	class Function;
 
-	using SmallTypeVector = SmallVector<Type *>;
+	using SmallTypeVector = SmallVector<const Type *>;
 
 	class Type {
 	public:
diff --git a/source/blender/functions/intern/function.cpp b/source/blender/functions/intern/function.cpp
index 28cbd0c1e10..84c186bd0e9 100644
--- a/source/blender/functions/intern/function.cpp
+++ b/source/blender/functions/intern/function.cpp
@@ -25,7 +25,7 @@ ValueArray::ValueArray(SmallTypeVector types)
 	: types(types)
 {
 	int total_size = 0;
-	for (Type *type : types) {
+	for (const Type *type : types) {
 		this->offsets.append(total_size);
 		total_size += type->size();
 	}
diff --git a/source/blender/functions/types/numeric.hpp b/source/blender/functions/types/numeric.hpp
index 57f6be2b07e..1757fccf148 100644
--- a/source/blender/functions/types/numeric.hpp
+++ b/source/blender/functions/types/numeric.hpp
@@ -32,8 +32,8 @@ namespace FN::Types {
 		}
 	};
 
-	static FloatType *float_ty = new FloatType();
-	static Int32Type *int32_ty = new Int32Type();
-	static FloatVectorType<3> *floatvec3d_ty = new FloatVectorType<3>();
+	static const FloatType *float_ty = new FloatType();
+	static const Int32Type *int32_ty = new Int32Type();
+	static const FloatVectorType<3> *floatvec3d_ty = new FloatVectorType<3>();
 
 } /* namespace FN::Types */
\ No newline at end of file



More information about the Bf-blender-cvs mailing list