[Bf-blender-cvs] [3a17600bed5] functions: move ChunkedRange to separate file

Jacques Lucke noreply at git.blender.org
Thu Sep 5 19:11:32 CEST 2019


Commit: 3a17600bed5aa0b654b1b32230eea88455aa1c43
Author: Jacques Lucke
Date:   Thu Sep 5 11:30:48 2019 +0200
Branches: functions
https://developer.blender.org/rB3a17600bed5aa0b654b1b32230eea88455aa1c43

move ChunkedRange to separate file

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

A	source/blender/blenlib/BLI_chunked_range.hpp
M	source/blender/blenlib/BLI_range.hpp
M	source/blender/blenlib/BLI_task.hpp
M	source/blender/blenlib/CMakeLists.txt
M	tests/gtests/blenlib/BLI_range_test.cc

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

diff --git a/source/blender/blenlib/BLI_chunked_range.hpp b/source/blender/blenlib/BLI_chunked_range.hpp
new file mode 100644
index 00000000000..4146ed10705
--- /dev/null
+++ b/source/blender/blenlib/BLI_chunked_range.hpp
@@ -0,0 +1,57 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bli
+ *
+ * Utility class that represents a range that has been split up into chunks.
+ */
+
+#pragma once
+
+#include "BLI_range.hpp"
+
+namespace BLI {
+
+template<typename T> class ChunkedRange {
+ private:
+  Range<T> m_total_range;
+  uint m_chunk_size;
+  uint m_chunk_amount;
+
+ public:
+  ChunkedRange(Range<T> total_range, uint chunk_size)
+      : m_total_range(total_range),
+        m_chunk_size(chunk_size),
+        m_chunk_amount(std::ceil(m_total_range.size() / (float)m_chunk_size))
+  {
+  }
+
+  uint chunks() const
+  {
+    return m_chunk_amount;
+  }
+
+  Range<T> chunk_range(uint index) const
+  {
+    BLI_assert(index < m_chunk_amount);
+    T start = m_total_range[index * m_chunk_size];
+    T one_after_last = std::min<T>(start + m_chunk_size, m_total_range.one_after_last());
+    return Range<T>(start, one_after_last);
+  }
+};
+
+}  // namespace BLI
diff --git a/source/blender/blenlib/BLI_range.hpp b/source/blender/blenlib/BLI_range.hpp
index 9d792cf1cc8..3d9b355e486 100644
--- a/source/blender/blenlib/BLI_range.hpp
+++ b/source/blender/blenlib/BLI_range.hpp
@@ -18,7 +18,8 @@
  * \ingroup bli
  *
  * Allows passing iterators over ranges of integers without actually allocating an array or passing
- * separate values.
+ * separate values. A range always has a step of one. If other step sizes are required in some
+ * cases, a separate data structure should be used.
  */
 
 #pragma once
@@ -176,7 +177,7 @@ template<typename T> class Range {
   Range<T> slice(uint start, uint size) const
   {
     uint new_start = m_start + start;
-    BLI_assert(new_start + size <= m_one_after_last);
+    BLI_assert(new_start + size <= m_one_after_last || size == 0);
     return Range<T>(new_start, new_start + size);
   }
 
@@ -187,32 +188,4 @@ template<typename T> class Range {
   ArrayRef<T> as_array_ref() const;
 };
 
-template<typename T> class ChunkedRange {
- private:
-  Range<T> m_total_range;
-  uint m_chunk_size;
-  uint m_chunk_amount;
-
- public:
-  ChunkedRange(Range<T> total_range, uint chunk_size)
-      : m_total_range(total_range),
-        m_chunk_size(chunk_size),
-        m_chunk_amount(std::ceil(m_total_range.size() / (float)m_chunk_size))
-  {
-  }
-
-  uint chunks() const
-  {
-    return m_chunk_amount;
-  }
-
-  Range<T> chunk_range(uint index) const
-  {
-    BLI_assert(index < m_chunk_amount);
-    T start = m_total_range[index * m_chunk_size];
-    T one_after_last = std::min<T>(start + m_chunk_size, m_total_range.one_after_last());
-    return Range<T>(start, one_after_last);
-  }
-};
-
 }  // namespace BLI
diff --git a/source/blender/blenlib/BLI_task.hpp b/source/blender/blenlib/BLI_task.hpp
index 1385ad9cb5f..c0cc1810cef 100644
--- a/source/blender/blenlib/BLI_task.hpp
+++ b/source/blender/blenlib/BLI_task.hpp
@@ -27,7 +27,7 @@
 #include "BLI_task.h"
 #include "BLI_array_ref.hpp"
 #include "BLI_map.hpp"
-#include "BLI_range.hpp"
+#include "BLI_chunked_range.hpp"
 
 namespace BLI {
 namespace Task {
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index e4ca4fc0b0e..e658d5726ae 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -257,6 +257,7 @@ set(SRC
   BLI_set.hpp
   BLI_set_vector.hpp
   BLI_stack.hpp
+  BLI_chunked_range.hpp
   BLI_string_map.hpp
   BLI_string_ref.hpp
   BLI_temporary_allocator.h
diff --git a/tests/gtests/blenlib/BLI_range_test.cc b/tests/gtests/blenlib/BLI_range_test.cc
index 26b99fed57c..6f02185e3db 100644
--- a/tests/gtests/blenlib/BLI_range_test.cc
+++ b/tests/gtests/blenlib/BLI_range_test.cc
@@ -1,5 +1,6 @@
 #include "testing/testing.h"
 #include "BLI_range.hpp"
+#include "BLI_chunked_range.hpp"
 #include "BLI_vector.hpp"
 
 using BLI::ArrayRef;



More information about the Bf-blender-cvs mailing list