Skip to content

Commit 1e38be2

Browse files
alxbilgerfredroy
authored andcommitted
replace std::ranges::iota_view by a custom equivalent
1 parent c8a6fcf commit 1e38be2

File tree

6 files changed

+102
-2
lines changed

6 files changed

+102
-2
lines changed

Sofa/Component/SolidMechanics/FEM/HyperElastic/src/sofa/component/solidmechanics/fem/hyperelastic/TetrahedronHyperelasticityFEMDrawing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void drawHyperelasticTets(const core::visual::VisualParams* vparams,
116116
core::topology::BaseMeshTopology* topology,
117117
const std::string& materialName)
118118
{
119-
const auto indices = std::ranges::iota_view(static_cast<sofa::Size>(0), topology->getNbTetrahedra());
119+
const auto indices = sofa::helper::IotaView(static_cast<sofa::Size>(0), topology->getNbTetrahedra());
120120
drawHyperelasticTets<DataTypes>(vparams, x, topology, materialName, indices);
121121
}
122122

Sofa/framework/Core/src/sofa/core/visual/DrawMesh.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <sofa/core/config.h>
2525
#include <sofa/core/topology/BaseMeshTopology.h>
2626
#include <sofa/core/topology/Topology.h>
27+
#include <sofa/helper/IotaView.h>
2728
#include <sofa/helper/visual/DrawTool.h>
2829

2930
#include <ranges>
@@ -66,7 +67,7 @@ struct BaseDrawMesh
6667
const ColorContainer& colors = Derived::defaultColors)
6768
{
6869
const auto totalNbElements = topology->getNbElements<typename Derived::ElementType>();
69-
const auto elementsToDraw = std::ranges::iota_view(static_cast<decltype(totalNbElements)>(0), totalNbElements);
70+
const auto elementsToDraw = sofa::helper::IotaView(static_cast<decltype(totalNbElements)>(0), totalNbElements);
7071
drawSomeElements(drawTool, position, topology, elementsToDraw, colors);
7172
}
7273

Sofa/framework/Helper/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ set(HEADER_FILES
7070
${SRC_ROOT}/hash.h
7171
${SRC_ROOT}/init.h
7272
${SRC_ROOT}/integer_id.h
73+
${SRC_ROOT}/IotaView.h
7374
${SRC_ROOT}/accessor/ReadAccessor.h
7475
${SRC_ROOT}/accessor/ReadAccessorFixedArray.h
7576
${SRC_ROOT}/accessor/ReadAccessorVector.h
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#pragma once
2+
3+
#include <iterator>
4+
5+
namespace sofa::helper
6+
{
7+
8+
/**
9+
* Simple alternative to std::ranges::iota_view, compatible with pre-C++20
10+
*/
11+
template <typename T>
12+
class IotaView
13+
{
14+
public:
15+
struct iota_iterator
16+
{
17+
using value_type = T;
18+
using difference_type = std::ptrdiff_t;
19+
using iterator_category = std::forward_iterator_tag;
20+
using reference = T&;
21+
using pointer = T*;
22+
23+
explicit iota_iterator(T val) : val_(val) {}
24+
25+
T operator*() const { return val_; }
26+
iota_iterator& operator++() { ++val_; return *this; }
27+
bool operator!=(const iota_iterator& other) const { return val_ != other.val_; }
28+
29+
private:
30+
T val_;
31+
};
32+
33+
using iterator = iota_iterator;
34+
using value_type = T;
35+
36+
IotaView(T start, T end) : m_start(start), m_end(end) {}
37+
38+
iterator begin() const { return iterator(m_start); }
39+
iterator end() const { return iterator(m_end); }
40+
41+
[[nodiscard]] T size() const { return m_end - m_start; }
42+
[[nodiscard]] T operator[](T i) const { return m_start + i; }
43+
T front() const { return m_start; }
44+
T back() const { return m_end - 1; }
45+
[[nodiscard]] bool empty() const { return m_start == m_end; }
46+
47+
private:
48+
T m_start;
49+
T m_end;
50+
};
51+
52+
53+
}

Sofa/framework/Helper/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project(Sofa.Helper_test)
55
set(SOURCE_FILES
66
DiffLib_test.cpp
77
Factory_test.cpp
8+
IotaView_test.cpp
89
KdTree_test.cpp
910
NameDecoder_test.cpp
1011
OptionsGroup_test.cpp
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <sofa/helper/IotaView.h>
2+
#include <gtest/gtest.h>
3+
4+
namespace sofa
5+
{
6+
7+
TEST(IotaView, loop)
8+
{
9+
const auto range = sofa::helper::IotaView{0, 10};
10+
11+
int i = 0;
12+
for (const auto value : range)
13+
{
14+
EXPECT_EQ(value, i);
15+
++i;
16+
}
17+
}
18+
19+
TEST(IotaView, empty)
20+
{
21+
{
22+
const auto range = sofa::helper::IotaView{0, 10};
23+
EXPECT_FALSE(range.empty());
24+
}
25+
{
26+
const auto range = sofa::helper::IotaView{0, 0};
27+
EXPECT_TRUE(range.empty());
28+
}
29+
}
30+
31+
TEST(IotaView, size)
32+
{
33+
const auto range = sofa::helper::IotaView{0, 10};
34+
EXPECT_EQ(range.size(), 10);
35+
}
36+
37+
TEST(IotaView, access)
38+
{
39+
const auto range = sofa::helper::IotaView{4, 10};
40+
EXPECT_EQ(range[0], 4);
41+
EXPECT_EQ(range[9], 4+9);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)