Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 0 additions & 74 deletions include/Array.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#ifndef HX_ARRAY_H
#define HX_ARRAY_H
#include <limits>
#include <vector>
#include <cpp/FastIterator.h>

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

template<class ELEM>
class SafeSorter
{
typedef
#if (HXCPP_API_LEVEL>=500)
::hx::Callable<int(Dynamic, Dynamic)>
#else
Dynamic
#endif
SorterFunc;

struct ArraySorter
{
ELEM* mArray;
SorterFunc mSorter;

ArraySorter(ELEM* inArray, SorterFunc inSorter) : mArray(inArray), mSorter(inSorter) {};

bool operator()(int inA, int inB)
{
return mSorter(mArray[inA], mArray[inB]) < 0;
}
};

template<class STORE>
static void SortImpl(ELEM* inArray, const int inLength, SorterFunc inSorter)
{
auto index = std::vector<STORE>(inLength);
for (auto i = 0; i < inLength; i++)
{
index[i] = static_cast<STORE>(i);
}

std::stable_sort(index.begin(), index.end(), ArraySorter(inArray, inSorter));

// Put the results back ...
for (int i = 0; i < inLength; i++)
{
int from = index[i];
while (from < i)
from = index[from];
if (from != i)
{
std::swap(inArray[i], inArray[from]);
index[i] = from;
}
}
}

public:
static void Sort(ELEM* base, const int length, SorterFunc sorter)
{
if (length < 2)
{
return;
}

if (length <= std::numeric_limits<uint8_t>::max())
{
SortImpl<uint8_t>(base, length, sorter);
}
else if (length <= std::numeric_limits<uint16_t>::max())
{
SortImpl<uint16_t>(base, length, sorter);
}
else
{
SortImpl<uint32_t>(base, length, sorter);
}
}
};

}


Expand Down
1 change: 0 additions & 1 deletion include/hx/Telemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#define HX_TELEMETRY_VERSION 1

#include <hxcpp.h>
#include <vector>

struct TelemetryFrame
Expand Down
2 changes: 2 additions & 0 deletions include/hxcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ typedef PropertyAccessMode PropertyAccess;
#include <hx/Class.h>
#include "Enum.h"
#include <hx/Interface.h>
#ifdef HXCPP_TELEMETRY
#include <hx/Telemetry.h>
#endif
#if defined(__OBJC__) && defined(HXCPP_OBJC)
#include <hx/ObjcHelpers.h>
#endif
Expand Down
80 changes: 78 additions & 2 deletions src/Array.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,87 @@
#include <hxcpp.h>
#include <vector>
#include <limits>
#include <cpp/Pointer.h>

#ifdef HXCPP_TELEMETRY
extern void __hxt_new_array(void* obj, int size);
#endif

namespace
{
template<class ELEM>
class SafeSorter
{
typedef
#if (HXCPP_API_LEVEL>=500)
::hx::Callable<int(Dynamic, Dynamic)>
#else
Dynamic
#endif
SorterFunc;

struct ArraySorter
{
ELEM* mArray;
SorterFunc mSorter;

ArraySorter(ELEM* inArray, SorterFunc inSorter) : mArray(inArray), mSorter(inSorter) {};

bool operator()(int inA, int inB)
{
return mSorter(mArray[inA], mArray[inB]) < 0;
}
};

template<class STORE>
static void SortImpl(ELEM* inArray, const int inLength, SorterFunc inSorter)
{
auto index = std::vector<STORE>(inLength);
for (auto i = 0; i < inLength; i++)
{
index[i] = static_cast<STORE>(i);
}

std::stable_sort(index.begin(), index.end(), ArraySorter(inArray, inSorter));

// Put the results back ...
for (int i = 0; i < inLength; i++)
{
int from = index[i];
while (from < i)
from = index[from];
if (from != i)
{
std::swap(inArray[i], inArray[from]);
index[i] = from;
}
}
}

public:
static void Sort(ELEM* base, const int length, SorterFunc sorter)
{
if (length < 2)
{
return;
}

if (length <= std::numeric_limits<uint8_t>::max())
{
SortImpl<uint8_t>(base, length, sorter);
}
else if (length <= std::numeric_limits<uint16_t>::max())
{
SortImpl<uint16_t>(base, length, sorter);
}
else
{
SortImpl<uint32_t>(base, length, sorter);
}
}
};
}

using namespace hx;


Expand Down Expand Up @@ -492,9 +568,9 @@ String ArrayBase::joinArray(ArrayBase *inBase, String inSeparator)
void ArrayBase::safeSort(DynamicSorterFunc inSorter, bool inIsString)
{
if (inIsString)
hx::SafeSorter<String>::Sort((String *)mBase, length,inSorter);
SafeSorter<String>::Sort((String *)mBase, length,inSorter);
else
hx::SafeSorter<Dynamic>::Sort((Dynamic *)mBase, length,inSorter);
SafeSorter<Dynamic>::Sort((Dynamic *)mBase, length,inSorter);
}


Expand Down
Loading