[Bf-blender-cvs] [04350ca9764] functions: initial value iterator

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


Commit: 04350ca9764ceefa2818cc6dea08b8a37ce108b7
Author: Jacques Lucke
Date:   Thu Feb 7 14:28:56 2019 +0100
Branches: functions
https://developer.blender.org/rB04350ca9764ceefa2818cc6dea08b8a37ce108b7

initial value iterator

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

M	source/blender/blenlib/BLI_small_map.hpp
M	source/blender/functions/core/core.hpp

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

diff --git a/source/blender/blenlib/BLI_small_map.hpp b/source/blender/blenlib/BLI_small_map.hpp
index 1af9692b100..c512320bda8 100644
--- a/source/blender/blenlib/BLI_small_map.hpp
+++ b/source/blender/blenlib/BLI_small_map.hpp
@@ -7,8 +7,6 @@ namespace BLI {
 	template<typename K, typename V, uint N = 4>
 	class SmallMap {
 	private:
-	public:
-		/* only public until there are proper iterators */
 		struct Entry {
 			K key;
 			V value;
@@ -20,6 +18,9 @@ namespace BLI {
 
 		SmallVector<Entry> m_entries;
 
+	public:
+		class ValueIterator;
+
 		SmallMap() = default;
 
 		void add(K key, V value)
@@ -69,5 +70,55 @@ namespace BLI {
 		{
 			return this->m_entries.size();
 		}
+
+		ValueIterator values() const
+		{
+			return ValueIterator(*this);
+		}
+
+		class ValueIterator {
+		private:
+			const SmallMap &m_map;
+			uint m_index;
+
+			ValueIterator(const SmallMap &map, uint index)
+				: m_map(map), m_index(index) {}
+		public:
+			ValueIterator(const SmallMap &map)
+				: ValueIterator(map, 0) {}
+
+			ValueIterator begin() const
+			{
+				return ValueIterator(this->m_map, 0);
+			}
+
+			ValueIterator end() const
+			{
+				return ValueIterator(this->m_map, this->m_map.size());
+			}
+
+			ValueIterator &operator++()
+			{
+				this->m_index++;
+				return *this;
+			}
+
+			ValueIterator operator++(int)
+			{
+				ValueIterator iterator = *this;
+				++*this;
+				return iterator;
+			}
+
+			bool operator!=(const ValueIterator &iterator) const
+			{
+				return this->m_index != iterator.m_index;
+			}
+
+			V operator*() const
+			{
+				return this->m_map.m_entries[this->m_index].value;
+			}
+		};
 	};
 };
\ No newline at end of file
diff --git a/source/blender/functions/core/core.hpp b/source/blender/functions/core/core.hpp
index 990b9013e49..6db5e5f5959 100644
--- a/source/blender/functions/core/core.hpp
+++ b/source/blender/functions/core/core.hpp
@@ -23,6 +23,13 @@ namespace FN {
 	public:
 		typedef void (*FreeFunction)(void *value);
 
+	private:
+		struct Entry {
+			void *value;
+			FreeFunction free;
+		};
+
+	public:
 		template<typename T>
 		void add(T *value)
 		{
@@ -43,17 +50,12 @@ namespace FN {
 
 		~Composition()
 		{
-			for (SmallMap<uint64_t, Entry>::Entry &entry : this->m_elements.m_entries) {
-				entry.value.free(entry.value.value);
+			for (const Entry &entry : this->m_elements.values()) {
+				entry.free(entry.value);
 			}
 		}
 
 	private:
-		struct Entry {
-			void *value;
-			FreeFunction free;
-		};
-
 		template<typename T>
 		static uint64_t get_key()
 		{



More information about the Bf-blender-cvs mailing list