Skip to content

Commit a62caf6

Browse files
author
Aidan Lee
committed
Move stable sorter to implementation file
Only used by non templated ArrayBase class
1 parent 21d2df8 commit a62caf6

2 files changed

Lines changed: 78 additions & 76 deletions

File tree

include/Array.h

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#ifndef HX_ARRAY_H
22
#define HX_ARRAY_H
3-
#include <limits>
4-
#include <vector>
53
#include <cpp/FastIterator.h>
64

75
// --- hx::ReturnNull ------------------------------------------------------
@@ -59,78 +57,6 @@ template<> struct ArrayTraits<Dynamic> { enum { StoreType = arrayObject }; };
5957
template<> struct ArrayTraits<String> { enum { StoreType = arrayString }; };
6058
template<> struct ArrayTraits< ::cpp::Int64> { enum { StoreType = arrayInt64 }; };
6159

62-
template<class ELEM>
63-
class SafeSorter
64-
{
65-
typedef
66-
#if (HXCPP_API_LEVEL>=500)
67-
::hx::Callable<int(Dynamic, Dynamic)>
68-
#else
69-
Dynamic
70-
#endif
71-
SorterFunc;
72-
73-
struct ArraySorter
74-
{
75-
ELEM* mArray;
76-
SorterFunc mSorter;
77-
78-
ArraySorter(ELEM* inArray, SorterFunc inSorter) : mArray(inArray), mSorter(inSorter) {};
79-
80-
bool operator()(int inA, int inB)
81-
{
82-
return mSorter(mArray[inA], mArray[inB]) < 0;
83-
}
84-
};
85-
86-
template<class STORE>
87-
static void SortImpl(ELEM* inArray, const int inLength, SorterFunc inSorter)
88-
{
89-
auto index = std::vector<STORE>(inLength);
90-
for (auto i = 0; i < inLength; i++)
91-
{
92-
index[i] = static_cast<STORE>(i);
93-
}
94-
95-
std::stable_sort(index.begin(), index.end(), ArraySorter(inArray, inSorter));
96-
97-
// Put the results back ...
98-
for (int i = 0; i < inLength; i++)
99-
{
100-
int from = index[i];
101-
while (from < i)
102-
from = index[from];
103-
if (from != i)
104-
{
105-
std::swap(inArray[i], inArray[from]);
106-
index[i] = from;
107-
}
108-
}
109-
}
110-
111-
public:
112-
static void Sort(ELEM* base, const int length, SorterFunc sorter)
113-
{
114-
if (length < 2)
115-
{
116-
return;
117-
}
118-
119-
if (length <= std::numeric_limits<uint8_t>::max())
120-
{
121-
SortImpl<uint8_t>(base, length, sorter);
122-
}
123-
else if (length <= std::numeric_limits<uint16_t>::max())
124-
{
125-
SortImpl<uint16_t>(base, length, sorter);
126-
}
127-
else
128-
{
129-
SortImpl<uint32_t>(base, length, sorter);
130-
}
131-
}
132-
};
133-
13460
}
13561

13662

src/Array.cpp

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,87 @@
11
#include <hxcpp.h>
22
#include <vector>
3+
#include <limits>
34
#include <cpp/Pointer.h>
45

56
#ifdef HXCPP_TELEMETRY
67
extern void __hxt_new_array(void* obj, int size);
78
#endif
89

10+
namespace
11+
{
12+
template<class ELEM>
13+
class SafeSorter
14+
{
15+
typedef
16+
#if (HXCPP_API_LEVEL>=500)
17+
::hx::Callable<int(Dynamic, Dynamic)>
18+
#else
19+
Dynamic
20+
#endif
21+
SorterFunc;
22+
23+
struct ArraySorter
24+
{
25+
ELEM* mArray;
26+
SorterFunc mSorter;
27+
28+
ArraySorter(ELEM* inArray, SorterFunc inSorter) : mArray(inArray), mSorter(inSorter) {};
29+
30+
bool operator()(int inA, int inB)
31+
{
32+
return mSorter(mArray[inA], mArray[inB]) < 0;
33+
}
34+
};
35+
36+
template<class STORE>
37+
static void SortImpl(ELEM* inArray, const int inLength, SorterFunc inSorter)
38+
{
39+
auto index = std::vector<STORE>(inLength);
40+
for (auto i = 0; i < inLength; i++)
41+
{
42+
index[i] = static_cast<STORE>(i);
43+
}
44+
45+
std::stable_sort(index.begin(), index.end(), ArraySorter(inArray, inSorter));
46+
47+
// Put the results back ...
48+
for (int i = 0; i < inLength; i++)
49+
{
50+
int from = index[i];
51+
while (from < i)
52+
from = index[from];
53+
if (from != i)
54+
{
55+
std::swap(inArray[i], inArray[from]);
56+
index[i] = from;
57+
}
58+
}
59+
}
60+
61+
public:
62+
static void Sort(ELEM* base, const int length, SorterFunc sorter)
63+
{
64+
if (length < 2)
65+
{
66+
return;
67+
}
68+
69+
if (length <= std::numeric_limits<uint8_t>::max())
70+
{
71+
SortImpl<uint8_t>(base, length, sorter);
72+
}
73+
else if (length <= std::numeric_limits<uint16_t>::max())
74+
{
75+
SortImpl<uint16_t>(base, length, sorter);
76+
}
77+
else
78+
{
79+
SortImpl<uint32_t>(base, length, sorter);
80+
}
81+
}
82+
};
83+
}
84+
985
using namespace hx;
1086

1187

@@ -492,9 +568,9 @@ String ArrayBase::joinArray(ArrayBase *inBase, String inSeparator)
492568
void ArrayBase::safeSort(DynamicSorterFunc inSorter, bool inIsString)
493569
{
494570
if (inIsString)
495-
hx::SafeSorter<String>::Sort((String *)mBase, length,inSorter);
571+
SafeSorter<String>::Sort((String *)mBase, length,inSorter);
496572
else
497-
hx::SafeSorter<Dynamic>::Sort((Dynamic *)mBase, length,inSorter);
573+
SafeSorter<Dynamic>::Sort((Dynamic *)mBase, length,inSorter);
498574
}
499575

500576

0 commit comments

Comments
 (0)