[Bf-blender-cvs] [8b13cd86ec3] functions: more centralized solution to getting an array ref for a range
Jacques Lucke
noreply at git.blender.org
Wed Jul 10 17:17:52 CEST 2019
Commit: 8b13cd86ec301481924577bba84144940f56f669
Author: Jacques Lucke
Date: Wed Jul 10 14:21:53 2019 +0200
Branches: functions
https://developer.blender.org/rB8b13cd86ec301481924577bba84144940f56f669
more centralized solution to getting an array ref for a range
===================================================================
M source/blender/blenlib/BLI_range.hpp
M source/blender/blenlib/CMakeLists.txt
A source/blender/blenlib/intern/BLI_range.cpp
M source/blender/simulations/bparticles/core.cpp
M source/blender/simulations/bparticles/core.hpp
M source/blender/simulations/bparticles/simulate.cpp
M tests/gtests/blenlib/BLI_range_test.cc
===================================================================
diff --git a/source/blender/blenlib/BLI_range.hpp b/source/blender/blenlib/BLI_range.hpp
index 9c1d8ebefd4..0da8f36e7e9 100644
--- a/source/blender/blenlib/BLI_range.hpp
+++ b/source/blender/blenlib/BLI_range.hpp
@@ -7,6 +7,8 @@
#include "BLI_utildefines.h"
#include "BLI_small_vector.hpp"
+#define RANGE_AS_ARRAY_REF_MAX_LEN 10000
+
namespace BLI {
template<typename T> class Range {
@@ -116,6 +118,8 @@ template<typename T> class Range {
}
return values;
}
+
+ ArrayRef<T> as_array_ref() const;
};
template<typename T> class ChunkedRange {
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 4c07a7744c1..2968faa42c9 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -249,6 +249,7 @@ set(SRC
BLI_object_pool.hpp
BLI_optional.hpp
BLI_range.hpp
+ intern/BLI_range.cpp
BLI_shared.hpp
BLI_small_vector.hpp
BLI_small_map.hpp
diff --git a/source/blender/blenlib/intern/BLI_range.cpp b/source/blender/blenlib/intern/BLI_range.cpp
new file mode 100644
index 00000000000..cbf26f344c1
--- /dev/null
+++ b/source/blender/blenlib/intern/BLI_range.cpp
@@ -0,0 +1,25 @@
+#include "BLI_range.hpp"
+
+namespace BLI {
+
+static bool array_is_initialized = false;
+static uint array[RANGE_AS_ARRAY_REF_MAX_LEN];
+
+template<typename T> ArrayRef<T> Range<T>::as_array_ref() const
+{
+ BLI_assert(m_start >= 0);
+ BLI_assert(m_one_after_last <= RANGE_AS_ARRAY_REF_MAX_LEN);
+ BLI_assert(sizeof(T) == sizeof(uint));
+ if (!array_is_initialized) {
+ for (uint i = 0; i < RANGE_AS_ARRAY_REF_MAX_LEN; i++) {
+ array[i] = i;
+ }
+ array_is_initialized = true;
+ }
+ return ArrayRef<T>((T *)array + m_start, this->size());
+}
+
+template ArrayRef<uint> Range<uint>::as_array_ref() const;
+template ArrayRef<int> Range<int>::as_array_ref() const;
+
+} // namespace BLI
diff --git a/source/blender/simulations/bparticles/core.cpp b/source/blender/simulations/bparticles/core.cpp
index e2e9e8a2f3e..045aa802f35 100644
--- a/source/blender/simulations/bparticles/core.cpp
+++ b/source/blender/simulations/bparticles/core.cpp
@@ -99,7 +99,7 @@ ParticleSets ParticleAllocator::request(StringRef particle_type_name, uint size)
SmallVector<ParticleSet> sets;
for (uint i = 0; i < blocks.size(); i++) {
- sets.append(ParticleSet(*blocks[i], static_number_range_ref(ranges[i])));
+ sets.append(ParticleSet(*blocks[i], ranges[i].as_array_ref()));
}
return ParticleSets(particle_type_name, attributes_info, sets);
diff --git a/source/blender/simulations/bparticles/core.hpp b/source/blender/simulations/bparticles/core.hpp
index adda1a2ef5f..f93d361f286 100644
--- a/source/blender/simulations/bparticles/core.hpp
+++ b/source/blender/simulations/bparticles/core.hpp
@@ -584,27 +584,6 @@ class TypeAttributeInterface {
ArrayRef<AttributeType> types();
};
-/* Utility to get ranges as arrays
- ********************************************/
-
-BLI_LAZY_INIT_STATIC(SmallVector<uint>, static_number_range_vector)
-{
- return Range<uint>(0, 10000).to_small_vector();
-}
-
-inline ArrayRef<uint> static_number_range_ref(uint start, uint length)
-{
- return ArrayRef<uint>(static_number_range_vector()).slice(start, length);
-}
-
-inline ArrayRef<uint> static_number_range_ref(Range<uint> range)
-{
- if (range.size() == 0) {
- return {};
- }
- return static_number_range_ref(range.first(), range.size());
-}
-
/* Event inline functions
********************************************/
diff --git a/source/blender/simulations/bparticles/simulate.cpp b/source/blender/simulations/bparticles/simulate.cpp
index 7fddc2b708b..4c327724ad2 100644
--- a/source/blender/simulations/bparticles/simulate.cpp
+++ b/source/blender/simulations/bparticles/simulate.cpp
@@ -298,7 +298,7 @@ BLI_NOINLINE static void simulate_with_max_n_events(
VectorAdaptor<float> durations_output(durations_A, amount_left);
simulate_to_next_event(array_allocator,
particle_allocator,
- ParticleSet(block, static_number_range_ref(0, amount_left)),
+ ParticleSet(block, Range<uint>(0, amount_left).as_array_ref()),
attribute_offsets,
durations,
end_time,
@@ -406,7 +406,7 @@ BLI_NOINLINE static void simulate_block(ArrayAllocator &array_allocator,
ArrayRef<Event *> events = particle_type.events();
if (events.size() == 0) {
- ParticleSet all_particles_in_block(block, static_number_range_ref(block.active_range()));
+ ParticleSet all_particles_in_block(block, block.active_range().as_array_ref());
apply_remaining_offsets(all_particles_in_block, attribute_offsets);
}
else {
diff --git a/tests/gtests/blenlib/BLI_range_test.cc b/tests/gtests/blenlib/BLI_range_test.cc
index b55a5b8a335..329362bc000 100644
--- a/tests/gtests/blenlib/BLI_range_test.cc
+++ b/tests/gtests/blenlib/BLI_range_test.cc
@@ -2,6 +2,7 @@
#include "BLI_range.hpp"
#include "BLI_small_vector.hpp"
+using BLI::ArrayRef;
using IntRange = BLI::Range<int>;
using ChunkedIntRange = BLI::ChunkedRange<int>;
using IntVector = BLI::SmallVector<int>;
@@ -114,6 +115,17 @@ TEST(range, OneAfterEnd)
EXPECT_EQ(range.one_after_last(), 8);
}
+TEST(range, AsArrayRef)
+{
+ IntRange range = IntRange(4, 10);
+ ArrayRef<int> ref = range.as_array_ref();
+ EXPECT_EQ(ref.size(), 6);
+ EXPECT_EQ(ref[0], 4);
+ EXPECT_EQ(ref[1], 5);
+ EXPECT_EQ(ref[2], 6);
+ EXPECT_EQ(ref[3], 7);
+}
+
TEST(chunked_range, ChunksExact)
{
IntRange range = IntRange(10, 50);
More information about the Bf-blender-cvs
mailing list