Skip to content

Commit fa03642

Browse files
committed
ArrayIterationUnroller: minor tidying.
1 parent 0c69644 commit fa03642

3 files changed

Lines changed: 29 additions & 42 deletions

File tree

modules/squarepine_core/misc/ArrayIterationUnroller.cpp

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ class ArrayIterationUnroller::InternalPerform final
55
InternalPerform (ArrayIterationUnroller& o) :
66
owner (o)
77
{
8-
jumpTable[0] = &InternalPerform::singles;
9-
jumpTable[1] = &InternalPerform::pairs;
10-
jumpTable[2] = &InternalPerform::triples;
11-
jumpTable[3] = &InternalPerform::quads;
12-
jumpTable[4] = &InternalPerform::quints;
13-
jumpTable[5] = &InternalPerform::sexts;
14-
jumpTable[6] = &InternalPerform::septs;
15-
jumpTable[7] = &InternalPerform::octs;
16-
jumpTable[8] = &InternalPerform::nonuples;
17-
jumpTable[9] = &InternalPerform::decas;
8+
jumpTable =
9+
{
10+
std::bind (&InternalPerform::singles, this, std::placeholders::_1),
11+
std::bind (&InternalPerform::pairs, this, std::placeholders::_1),
12+
std::bind (&InternalPerform::triples, this, std::placeholders::_1),
13+
std::bind (&InternalPerform::quads, this, std::placeholders::_1),
14+
std::bind (&InternalPerform::quints, this, std::placeholders::_1),
15+
std::bind (&InternalPerform::sexts, this, std::placeholders::_1),
16+
std::bind (&InternalPerform::septs, this, std::placeholders::_1),
17+
std::bind (&InternalPerform::octs, this, std::placeholders::_1),
18+
std::bind (&InternalPerform::nonuples, this, std::placeholders::_1),
19+
std::bind (&InternalPerform::decas, this, std::placeholders::_1)
20+
};
1821
}
1922

2023
//==============================================================================
@@ -94,21 +97,17 @@ class ArrayIterationUnroller::InternalPerform final
9497
}
9598

9699
//==============================================================================
97-
/** This will iterate through the array with a jump table */
98-
void runBranched (int index, int offset)
99-
{
100-
(this->*jumpTable[index]) (offset);
101-
}
100+
/** Typedef for creating a member-function-pointer array. */
101+
using MethodPointer = std::function<void (int)>;
102102

103-
//==============================================================================
104103
/** This will perform an iteration with the specified increment.
105104
106105
Be sure to specify the correct method to the increment!
107106
108107
@param[out] remainder The remainder of elements that need to be iterated through.
109108
@param increment The amount of items to process and therefore to increment by.
110109
*/
111-
template <void (InternalPerform::*unrollMethod) (int index)>
110+
template <auto unrollMethod>
112111
void callMethodWithIncrement (int& remainder, int increment)
113112
{
114113
// Find out how many blocks of "increment" elements we can iterate through:
@@ -124,12 +123,7 @@ class ArrayIterationUnroller::InternalPerform final
124123
private:
125124
//==============================================================================
126125
ArrayIterationUnroller& owner;
127-
128-
//==============================================================================
129-
/** Typedef for creating a member-function-pointer array. */
130-
typedef void (InternalPerform::*MethodPointer) (int);
131-
132-
MethodPointer jumpTable[10]; // NB: Do NOT make this static - this will not work in a multi-threaded environment!
126+
std::array<MethodPointer, 10> jumpTable;
133127

134128
//==============================================================================
135129
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InternalPerform)
@@ -142,10 +136,6 @@ ArrayIterationUnroller::ArrayIterationUnroller (int arraySize) :
142136
jassert (sizeOfArray >= 0);
143137
}
144138

145-
ArrayIterationUnroller::~ArrayIterationUnroller()
146-
{
147-
}
148-
149139
//==============================================================================
150140
void ArrayIterationUnroller::resize (int newSize)
151141
{
@@ -179,18 +169,19 @@ void ArrayIterationUnroller::run()
179169

180170
auto remainder = sizeOfArray;
181171

182-
#if SQUAREPINE_ARRAY_ITERATION_UNROLLER_CHECK_BIG_NUMS
183172
while (remainder >= 100)
184173
perf.callMethodWithIncrement<&InternalPerform::hundred> (remainder, 100);
185-
#endif
186174

187175
while (remainder >= 20)
188176
perf.callMethodWithIncrement<&InternalPerform::twenty> (remainder, 20);
189177

190-
if (remainder >= 10)
178+
while (remainder >= 10)
191179
perf.callMethodWithIncrement<&InternalPerform::decas> (remainder, 10);
192180

193-
if (remainder > 0)
194-
perf.runBranched (remainder - 1, sizeOfArray - remainder);
181+
while (remainder >= 2)
182+
perf.callMethodWithIncrement<&InternalPerform::pairs> (remainder, 2);
183+
184+
while (remainder > 0)
185+
perf.callMethodWithIncrement<&InternalPerform::singles> (remainder, 1);
195186
#endif
196187
}

modules/squarepine_core/misc/ArrayIterationUnroller.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,26 @@ class ArrayIterationUnroller
1313
public:
1414
/** Constructor.
1515
16-
@param arraySize
16+
@param arraySize The number of elements.
1717
*/
1818
ArrayIterationUnroller (int arraySize = 0);
1919

2020
/** Destructor. */
21-
virtual ~ArrayIterationUnroller();
21+
virtual ~ArrayIterationUnroller() = default;
2222

2323
//==============================================================================
2424
/** Call this to begin performing whatever it is you need in your subclass!
2525
2626
This will simply try to minimise the amount of run-throughs
27-
by iterating through an array in blocks of 10.
27+
by iterating through ranges of blocks of indices in one fell swoop.
2828
*/
2929
void run();
3030

3131
protected:
3232
//==============================================================================
33-
/** */
33+
/** Changes the size of the unroller.
34+
Don't forget to do this if you set the initial size to 0 in the constructor!
35+
*/
3436
void resize (int newSize);
3537

3638
/** Perform whatever task it is you need to be done quickly in this method.

modules/squarepine_core/squarepine_core.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,6 @@
8585
#define SQUAREPINE_ARRAY_ITERATION_UNROLLER_MAKE_LINEAR 0
8686
#endif
8787

88-
/** Config: SQUAREPINE_ARRAY_ITERATION_UNROLLER_CHECK_BIG_NUMS
89-
*/
90-
#ifndef SQUAREPINE_ARRAY_ITERATION_UNROLLER_CHECK_BIG_NUMS
91-
#define SQUAREPINE_ARRAY_ITERATION_UNROLLER_CHECK_BIG_NUMS 0
92-
#endif
93-
9488
/** Config: SQUAREPINE_LOG_NETWORK_CALLS
9589
9690
Enable or disable logging networking calls.

0 commit comments

Comments
 (0)