Reformatted sort.
BUG= TEST=Trybots. Review URL: https://webrtc-codereview.appspot.com/998006 git-svn-id: http://webrtc.googlecode.com/svn/trunk@3316 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
1960219530
commit
6bc5d4dc07
@ -16,9 +16,8 @@
|
||||
#include "webrtc/system_wrappers/interface/constructor_magic.h"
|
||||
|
||||
namespace webrtc {
|
||||
class MapItem {
|
||||
friend class MapWrapper;
|
||||
|
||||
class MapItem {
|
||||
public:
|
||||
MapItem(int id, void* ptr);
|
||||
virtual ~MapItem();
|
||||
@ -28,6 +27,8 @@ class MapItem {
|
||||
void SetItem(void* ptr);
|
||||
|
||||
private:
|
||||
friend class MapWrapper;
|
||||
|
||||
int item_id_;
|
||||
void* item_pointer_;
|
||||
};
|
||||
|
@ -13,13 +13,12 @@
|
||||
#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SORT_H_
|
||||
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SORT_H_
|
||||
|
||||
#include "typedefs.h"
|
||||
#include "common_types.h"
|
||||
#include "webrtc/common_types.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
|
||||
namespace webrtc
|
||||
{
|
||||
enum Type
|
||||
{
|
||||
namespace webrtc {
|
||||
|
||||
enum Type {
|
||||
TYPE_Word8,
|
||||
TYPE_UWord8,
|
||||
TYPE_Word16,
|
||||
@ -31,15 +30,16 @@ namespace webrtc
|
||||
TYPE_Float32,
|
||||
TYPE_Float64
|
||||
};
|
||||
|
||||
// Sorts intrinsic data types.
|
||||
//
|
||||
// data [in/out] A pointer to an array of intrinsic type.
|
||||
// Upon return it will be sorted in ascending order.
|
||||
// numOfElements The number of elements in the array.
|
||||
// dataType Enum corresponding to the type of the array.
|
||||
// num_of_elements The number of elements in the array.
|
||||
// data_type Enum corresponding to the type of the array.
|
||||
//
|
||||
// returns 0 on success, -1 on failure.
|
||||
WebRtc_Word32 Sort(void* data, WebRtc_UWord32 numOfElements, Type dataType);
|
||||
WebRtc_Word32 Sort(void* data, WebRtc_UWord32 num_of_elements, Type data_type);
|
||||
|
||||
// Sorts arbitrary data types. This requires an array of intrinsically typed
|
||||
// key values which will be used to sort the data array. There must be a
|
||||
@ -51,14 +51,15 @@ namespace webrtc
|
||||
// Upon return it will be sorted in ascending order.
|
||||
// key [in] A pointer to an array of keys used to sort the
|
||||
// data array.
|
||||
// numOfElements The number of elements in the arrays.
|
||||
// sizeOfElement The size, in bytes, of the data array.
|
||||
// keyType Enum corresponding to the type of the key array.
|
||||
// num_of_elements The number of elements in the arrays.
|
||||
// size_of_element The size, in bytes, of the data array.
|
||||
// key_type Enum corresponding to the type of the key array.
|
||||
//
|
||||
// returns 0 on success, -1 on failure.
|
||||
//
|
||||
WebRtc_Word32 KeySort(void* data, void* key, WebRtc_UWord32 numOfElements,
|
||||
WebRtc_UWord32 sizeOfElement, Type keyType);
|
||||
}
|
||||
WebRtc_Word32 KeySort(void* data, void* key, WebRtc_UWord32 num_of_elements,
|
||||
WebRtc_UWord32 size_of_element, Type key_type);
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_SORT_H_
|
||||
|
@ -13,7 +13,7 @@
|
||||
// part of the Boost C++ library collection. Otherwise, the C standard library's
|
||||
// qsort() will be used.
|
||||
|
||||
#include "sort.h"
|
||||
#include "webrtc/system_wrappers/interface/sort.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <cstring> // memcpy
|
||||
@ -24,239 +24,195 @@
|
||||
#else
|
||||
#include <algorithm> // std::sort
|
||||
#include <vector>
|
||||
|
||||
#include "spreadsort.hpp" // TODO(ajm) upgrade to spreadsort v2.
|
||||
#endif
|
||||
|
||||
#ifdef NO_STL
|
||||
#define COMPARE_DEREFERENCED(XT, YT) \
|
||||
do \
|
||||
{ \
|
||||
if ((XT) > (YT)) \
|
||||
{ \
|
||||
do { \
|
||||
if ((XT) > (YT)) { \
|
||||
return 1; \
|
||||
} \
|
||||
else if ((XT) < (YT)) \
|
||||
{ \
|
||||
else if ((XT) < (YT)) { \
|
||||
return -1; \
|
||||
} \
|
||||
\
|
||||
return 0; \
|
||||
} \
|
||||
while(0)
|
||||
} while(0)
|
||||
|
||||
#define COMPARE_FOR_QSORT(X, Y, TYPE) \
|
||||
do \
|
||||
{ \
|
||||
do { \
|
||||
TYPE xT = static_cast<TYPE>(*static_cast<const TYPE*>(X)); \
|
||||
TYPE yT = static_cast<TYPE>(*static_cast<const TYPE*>(Y)); \
|
||||
COMPARE_DEREFERENCED(xT, yT); \
|
||||
} \
|
||||
while(0)
|
||||
} while(0)
|
||||
|
||||
#define COMPARE_KEY_FOR_QSORT(SORT_KEY_X, SORT_KEY_Y, TYPE) \
|
||||
do \
|
||||
{ \
|
||||
TYPE xT = static_cast<TYPE>(*static_cast<TYPE*> \
|
||||
(static_cast<const SortKey*>(SORT_KEY_X)->key)); \
|
||||
TYPE yT = static_cast<TYPE>(*static_cast<TYPE*> \
|
||||
(static_cast<const SortKey*>(SORT_KEY_Y)->key)); \
|
||||
do { \
|
||||
TYPE xT = static_cast<TYPE>( \
|
||||
*static_cast<TYPE*>(static_cast<const SortKey*>(SORT_KEY_X)->key_)); \
|
||||
TYPE yT = static_cast<TYPE>( \
|
||||
*static_cast<TYPE*>(static_cast<const SortKey*>(SORT_KEY_Y)->key_)); \
|
||||
COMPARE_DEREFERENCED(xT, yT); \
|
||||
} \
|
||||
while(0)
|
||||
} while(0)
|
||||
|
||||
#define KEY_QSORT(SORT_KEY, KEY, NUM_OF_ELEMENTS, KEY_TYPE, COMPARE_FUNC) \
|
||||
do \
|
||||
{ \
|
||||
KEY_TYPE* keyT = (KEY_TYPE*)(key); \
|
||||
for (WebRtc_UWord32 i = 0; i < (NUM_OF_ELEMENTS); i++) \
|
||||
{ \
|
||||
ptrSortKey[i].key = &keyT[i]; \
|
||||
ptrSortKey[i].index = i; \
|
||||
do { \
|
||||
KEY_TYPE* key_type = (KEY_TYPE*)(key); \
|
||||
for (WebRtc_UWord32 i = 0; i < (NUM_OF_ELEMENTS); ++i) { \
|
||||
ptr_sort_key[i].key_ = &key_type[i]; \
|
||||
ptr_sort_key[i].index_ = i; \
|
||||
} \
|
||||
\
|
||||
qsort((SORT_KEY), (NUM_OF_ELEMENTS), sizeof(SortKey), (COMPARE_FUNC)); \
|
||||
} \
|
||||
while(0)
|
||||
} while(0)
|
||||
#endif
|
||||
|
||||
namespace webrtc
|
||||
{
|
||||
namespace webrtc {
|
||||
|
||||
#ifdef NO_STL
|
||||
struct SortKey
|
||||
{
|
||||
void* key;
|
||||
WebRtc_UWord32 index;
|
||||
struct SortKey {
|
||||
void* key_;
|
||||
WebRtc_UWord32 index_;
|
||||
};
|
||||
#else
|
||||
template<typename KeyType>
|
||||
struct SortKey
|
||||
{
|
||||
KeyType key;
|
||||
WebRtc_UWord32 index;
|
||||
struct SortKey {
|
||||
KeyType key_;
|
||||
WebRtc_UWord32 index_;
|
||||
};
|
||||
#endif
|
||||
|
||||
namespace // Unnamed namespace provides internal linkage.
|
||||
{
|
||||
namespace { // Unnamed namespace provides internal linkage.
|
||||
|
||||
#ifdef NO_STL
|
||||
int CompareWord8(const void* x, const void* y)
|
||||
{
|
||||
int CompareWord8(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_Word8);
|
||||
}
|
||||
|
||||
int CompareUWord8(const void* x, const void* y)
|
||||
{
|
||||
int CompareUWord8(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_UWord8);
|
||||
}
|
||||
|
||||
int CompareWord16(const void* x, const void* y)
|
||||
{
|
||||
int CompareWord16(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_Word16);
|
||||
}
|
||||
|
||||
int CompareUWord16(const void* x, const void* y)
|
||||
{
|
||||
int CompareUWord16(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_UWord16);
|
||||
}
|
||||
|
||||
int CompareWord32(const void* x, const void* y)
|
||||
{
|
||||
int CompareWord32(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_Word32);
|
||||
}
|
||||
|
||||
int CompareUWord32(const void* x, const void* y)
|
||||
{
|
||||
int CompareUWord32(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_UWord32);
|
||||
}
|
||||
|
||||
int CompareWord64(const void* x, const void* y)
|
||||
{
|
||||
int CompareWord64(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_Word64);
|
||||
}
|
||||
|
||||
int CompareUWord64(const void* x, const void* y)
|
||||
{
|
||||
int CompareUWord64(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, WebRtc_UWord64);
|
||||
}
|
||||
|
||||
int CompareFloat32(const void* x, const void* y)
|
||||
{
|
||||
int CompareFloat32(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, float);
|
||||
}
|
||||
|
||||
int CompareFloat64(const void* x, const void* y)
|
||||
{
|
||||
int CompareFloat64(const void* x, const void* y) {
|
||||
COMPARE_FOR_QSORT(x, y, double);
|
||||
}
|
||||
|
||||
int CompareKeyWord8(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_Word8);
|
||||
int CompareKeyWord8(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_Word8);
|
||||
}
|
||||
|
||||
int CompareKeyUWord8(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_UWord8);
|
||||
int CompareKeyUWord8(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_UWord8);
|
||||
}
|
||||
|
||||
int CompareKeyWord16(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_Word16);
|
||||
int CompareKeyWord16(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_Word16);
|
||||
}
|
||||
|
||||
int CompareKeyUWord16(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_UWord16);
|
||||
int CompareKeyUWord16(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_UWord16);
|
||||
}
|
||||
|
||||
int CompareKeyWord32(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_Word32);
|
||||
int CompareKeyWord32(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_Word32);
|
||||
}
|
||||
|
||||
int CompareKeyUWord32(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_UWord32);
|
||||
int CompareKeyUWord32(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_UWord32);
|
||||
}
|
||||
|
||||
int CompareKeyWord64(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_Word64);
|
||||
int CompareKeyWord64(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_Word64);
|
||||
}
|
||||
|
||||
int CompareKeyUWord64(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, WebRtc_UWord64);
|
||||
int CompareKeyUWord64(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, WebRtc_UWord64);
|
||||
}
|
||||
|
||||
int CompareKeyFloat32(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, float);
|
||||
int CompareKeyFloat32(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, float);
|
||||
}
|
||||
|
||||
int CompareKeyFloat64(const void* sortKeyX, const void* sortKeyY)
|
||||
{
|
||||
COMPARE_KEY_FOR_QSORT(sortKeyX, sortKeyY, double);
|
||||
int CompareKeyFloat64(const void* sort_key_x, const void* sort_key_y) {
|
||||
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, double);
|
||||
}
|
||||
#else
|
||||
template <typename KeyType>
|
||||
struct KeyLessThan
|
||||
{
|
||||
bool operator()(const SortKey<KeyType>& sortKeyX,
|
||||
const SortKey<KeyType>& sortKeyY) const
|
||||
{
|
||||
return sortKeyX.key < sortKeyY.key;
|
||||
struct KeyLessThan {
|
||||
bool operator()(const SortKey<KeyType>& sort_key_x,
|
||||
const SortKey<KeyType>& sort_key_y) const {
|
||||
return sort_key_x.key_ < sort_key_y.key_;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename KeyType>
|
||||
struct KeyRightShift
|
||||
{
|
||||
KeyType operator()(const SortKey<KeyType>& sortKey,
|
||||
const unsigned offset) const
|
||||
{
|
||||
return sortKey.key >> offset;
|
||||
struct KeyRightShift {
|
||||
KeyType operator()(const SortKey<KeyType>& sort_key,
|
||||
const unsigned offset) const {
|
||||
return sort_key.key_ >> offset;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename DataType>
|
||||
inline void IntegerSort(void* data, WebRtc_UWord32 numOfElements)
|
||||
{
|
||||
DataType* dataT = static_cast<DataType*>(data);
|
||||
boost::integer_sort(dataT, dataT + numOfElements);
|
||||
inline void IntegerSort(void* data, WebRtc_UWord32 num_of_elements) {
|
||||
DataType* data_type = static_cast<DataType*>(data);
|
||||
boost::integer_sort(data_type, data_type + num_of_elements);
|
||||
}
|
||||
|
||||
template <typename DataType, typename IntegerType>
|
||||
inline void FloatSort(void* data, WebRtc_UWord32 numOfElements)
|
||||
{
|
||||
DataType* dataT = static_cast<DataType*>(data);
|
||||
IntegerType cVal = 0;
|
||||
boost::float_sort_cast(dataT, dataT + numOfElements, cVal);
|
||||
inline void FloatSort(void* data, WebRtc_UWord32 num_of_elements) {
|
||||
DataType* data_type = static_cast<DataType*>(data);
|
||||
IntegerType c_val = 0;
|
||||
boost::float_sort_cast(data_type, data_type + num_of_elements, c_val);
|
||||
}
|
||||
|
||||
template <typename DataType>
|
||||
inline void StdSort(void* data, WebRtc_UWord32 numOfElements)
|
||||
{
|
||||
DataType* dataT = static_cast<DataType*>(data);
|
||||
std::sort(dataT, dataT + numOfElements);
|
||||
inline void StdSort(void* data, WebRtc_UWord32 num_of_elements) {
|
||||
DataType* data_type = static_cast<DataType*>(data);
|
||||
std::sort(data_type, data_type + num_of_elements);
|
||||
}
|
||||
|
||||
template<typename KeyType>
|
||||
inline WebRtc_Word32 SetupKeySort(void* key,
|
||||
SortKey<KeyType>*& ptrSortKey,
|
||||
WebRtc_UWord32 numOfElements)
|
||||
{
|
||||
ptrSortKey = new(std::nothrow) SortKey<KeyType>[numOfElements];
|
||||
if (ptrSortKey == NULL)
|
||||
{
|
||||
SortKey<KeyType>*& ptr_sort_key,
|
||||
WebRtc_UWord32 num_of_elements) {
|
||||
ptr_sort_key = new(std::nothrow) SortKey<KeyType>[num_of_elements];
|
||||
if (ptr_sort_key == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
KeyType* keyT = static_cast<KeyType*>(key);
|
||||
for (WebRtc_UWord32 i = 0; i < numOfElements; i++)
|
||||
{
|
||||
ptrSortKey[i].key = keyT[i];
|
||||
ptrSortKey[i].index = i;
|
||||
KeyType* key_type = static_cast<KeyType*>(key);
|
||||
for (WebRtc_UWord32 i = 0; i < num_of_elements; i++) {
|
||||
ptr_sort_key[i].key_ = key_type[i];
|
||||
ptr_sort_key[i].index_ = i;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@ -264,45 +220,40 @@ namespace webrtc
|
||||
|
||||
template<typename KeyType>
|
||||
inline WebRtc_Word32 TeardownKeySort(void* data,
|
||||
SortKey<KeyType>* ptrSortKey,
|
||||
WebRtc_UWord32 numOfElements, WebRtc_UWord32 sizeOfElement)
|
||||
{
|
||||
WebRtc_UWord8* ptrData = static_cast<WebRtc_UWord8*>(data);
|
||||
WebRtc_UWord8* ptrDataSorted = new(std::nothrow) WebRtc_UWord8
|
||||
[numOfElements * sizeOfElement];
|
||||
if (ptrDataSorted == NULL)
|
||||
{
|
||||
SortKey<KeyType>* ptr_sort_key,
|
||||
WebRtc_UWord32 num_of_elements,
|
||||
WebRtc_UWord32 size_of_element) {
|
||||
WebRtc_UWord8* ptr_data = static_cast<WebRtc_UWord8*>(data);
|
||||
WebRtc_UWord8* ptr_data_sorted =
|
||||
new(std::nothrow) WebRtc_UWord8[num_of_elements * size_of_element];
|
||||
if (ptr_data_sorted == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (WebRtc_UWord32 i = 0; i < numOfElements; i++)
|
||||
{
|
||||
memcpy(ptrDataSorted + i * sizeOfElement, ptrData +
|
||||
ptrSortKey[i].index * sizeOfElement, sizeOfElement);
|
||||
for (WebRtc_UWord32 i = 0; i < num_of_elements; i++) {
|
||||
memcpy(ptr_data_sorted + i * size_of_element, ptr_data +
|
||||
ptr_sort_key[i].index_ * size_of_element, size_of_element);
|
||||
}
|
||||
memcpy(ptrData, ptrDataSorted, numOfElements * sizeOfElement);
|
||||
delete[] ptrSortKey;
|
||||
delete[] ptrDataSorted;
|
||||
memcpy(ptr_data, ptr_data_sorted, num_of_elements * size_of_element);
|
||||
delete[] ptr_sort_key;
|
||||
delete[] ptr_data_sorted;
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename KeyType>
|
||||
inline WebRtc_Word32 IntegerKeySort(void* data, void* key,
|
||||
WebRtc_UWord32 numOfElements,
|
||||
WebRtc_UWord32 sizeOfElement)
|
||||
{
|
||||
SortKey<KeyType>* ptrSortKey;
|
||||
if (SetupKeySort<KeyType>(key, ptrSortKey, numOfElements) != 0)
|
||||
{
|
||||
WebRtc_UWord32 num_of_elements,
|
||||
WebRtc_UWord32 size_of_element) {
|
||||
SortKey<KeyType>* ptr_sort_key;
|
||||
if (SetupKeySort<KeyType>(key, ptr_sort_key, num_of_elements) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
boost::integer_sort(ptrSortKey, ptrSortKey + numOfElements,
|
||||
boost::integer_sort(ptr_sort_key, ptr_sort_key + num_of_elements,
|
||||
KeyRightShift<KeyType>(), KeyLessThan<KeyType>());
|
||||
|
||||
if (TeardownKeySort<KeyType>(data, ptrSortKey, numOfElements,
|
||||
sizeOfElement) != 0)
|
||||
{
|
||||
if (TeardownKeySort<KeyType>(data, ptr_sort_key, num_of_elements,
|
||||
size_of_element) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -311,21 +262,18 @@ namespace webrtc
|
||||
|
||||
template<typename KeyType>
|
||||
inline WebRtc_Word32 StdKeySort(void* data, void* key,
|
||||
WebRtc_UWord32 numOfElements,
|
||||
WebRtc_UWord32 sizeOfElement)
|
||||
{
|
||||
SortKey<KeyType>* ptrSortKey;
|
||||
if (SetupKeySort<KeyType>(key, ptrSortKey, numOfElements) != 0)
|
||||
{
|
||||
WebRtc_UWord32 num_of_elements,
|
||||
WebRtc_UWord32 size_of_element) {
|
||||
SortKey<KeyType>* ptr_sort_key;
|
||||
if (SetupKeySort<KeyType>(key, ptr_sort_key, num_of_elements) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::sort(ptrSortKey, ptrSortKey + numOfElements,
|
||||
std::sort(ptr_sort_key, ptr_sort_key + num_of_elements,
|
||||
KeyLessThan<KeyType>());
|
||||
|
||||
if (TeardownKeySort<KeyType>(data, ptrSortKey, numOfElements,
|
||||
sizeOfElement) != 0)
|
||||
{
|
||||
if (TeardownKeySort<KeyType>(data, ptr_sort_key, num_of_elements,
|
||||
size_of_element) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -334,45 +282,42 @@ namespace webrtc
|
||||
#endif
|
||||
}
|
||||
|
||||
WebRtc_Word32 Sort(void* data, WebRtc_UWord32 numOfElements, Type type)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
WebRtc_Word32 Sort(void* data, WebRtc_UWord32 num_of_elements, Type type) {
|
||||
if (data == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef NO_STL
|
||||
switch (type)
|
||||
{
|
||||
switch (type) {
|
||||
case TYPE_Word8:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_Word8), CompareWord8);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_Word8), CompareWord8);
|
||||
break;
|
||||
case TYPE_UWord8:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_UWord8), CompareUWord8);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_UWord8), CompareUWord8);
|
||||
break;
|
||||
case TYPE_Word16:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_Word16), CompareWord16);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_Word16), CompareWord16);
|
||||
break;
|
||||
case TYPE_UWord16:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_UWord16), CompareUWord16);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_UWord16), CompareUWord16);
|
||||
break;
|
||||
case TYPE_Word32:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_Word32), CompareWord32);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_Word32), CompareWord32);
|
||||
break;
|
||||
case TYPE_UWord32:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_UWord32), CompareUWord32);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_UWord32), CompareUWord32);
|
||||
break;
|
||||
case TYPE_Word64:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_Word64), CompareWord64);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_Word64), CompareWord64);
|
||||
break;
|
||||
case TYPE_UWord64:
|
||||
qsort(data, numOfElements, sizeof(WebRtc_UWord64), CompareUWord64);
|
||||
qsort(data, num_of_elements, sizeof(WebRtc_UWord64), CompareUWord64);
|
||||
break;
|
||||
case TYPE_Float32:
|
||||
qsort(data, numOfElements, sizeof(float), CompareFloat32);
|
||||
qsort(data, num_of_elements, sizeof(float), CompareFloat32);
|
||||
break;
|
||||
case TYPE_Float64:
|
||||
qsort(data, numOfElements, sizeof(double), CompareFloat64);
|
||||
qsort(data, num_of_elements, sizeof(double), CompareFloat64);
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
@ -380,108 +325,101 @@ namespace webrtc
|
||||
#else
|
||||
// Fall back to std::sort for 64-bit types and floats due to compiler
|
||||
// warnings and VS 2003 build crashes respectively with spreadsort.
|
||||
switch (type)
|
||||
{
|
||||
switch (type) {
|
||||
case TYPE_Word8:
|
||||
IntegerSort<WebRtc_Word8>(data, numOfElements);
|
||||
IntegerSort<WebRtc_Word8>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_UWord8:
|
||||
IntegerSort<WebRtc_UWord8>(data, numOfElements);
|
||||
IntegerSort<WebRtc_UWord8>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_Word16:
|
||||
IntegerSort<WebRtc_Word16>(data, numOfElements);
|
||||
IntegerSort<WebRtc_Word16>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_UWord16:
|
||||
IntegerSort<WebRtc_UWord16>(data, numOfElements);
|
||||
IntegerSort<WebRtc_UWord16>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_Word32:
|
||||
IntegerSort<WebRtc_Word32>(data, numOfElements);
|
||||
IntegerSort<WebRtc_Word32>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_UWord32:
|
||||
IntegerSort<WebRtc_UWord32>(data, numOfElements);
|
||||
IntegerSort<WebRtc_UWord32>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_Word64:
|
||||
StdSort<WebRtc_Word64>(data, numOfElements);
|
||||
StdSort<WebRtc_Word64>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_UWord64:
|
||||
StdSort<WebRtc_UWord64>(data, numOfElements);
|
||||
StdSort<WebRtc_UWord64>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_Float32:
|
||||
StdSort<float>(data, numOfElements);
|
||||
StdSort<float>(data, num_of_elements);
|
||||
break;
|
||||
case TYPE_Float64:
|
||||
StdSort<double>(data, numOfElements);
|
||||
StdSort<double>(data, num_of_elements);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
WebRtc_Word32 KeySort(void* data, void* key, WebRtc_UWord32 numOfElements,
|
||||
WebRtc_UWord32 sizeOfElement, Type keyType)
|
||||
{
|
||||
if (data == NULL)
|
||||
{
|
||||
WebRtc_Word32 KeySort(void* data, void* key, WebRtc_UWord32 num_of_elements,
|
||||
WebRtc_UWord32 size_of_element, Type key_type) {
|
||||
if (data == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (key == NULL)
|
||||
{
|
||||
if (key == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((WebRtc_UWord64)numOfElements * sizeOfElement > 0xffffffff)
|
||||
{
|
||||
if ((WebRtc_UWord64)num_of_elements * size_of_element > 0xffffffff) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef NO_STL
|
||||
SortKey* ptrSortKey = new(std::nothrow) SortKey[numOfElements];
|
||||
if (ptrSortKey == NULL)
|
||||
{
|
||||
SortKey* ptr_sort_key = new(std::nothrow) SortKey[num_of_elements];
|
||||
if (ptr_sort_key == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (keyType)
|
||||
{
|
||||
switch (key_type) {
|
||||
case TYPE_Word8:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_Word8,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_Word8,
|
||||
CompareKeyWord8);
|
||||
break;
|
||||
case TYPE_UWord8:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_UWord8,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_UWord8,
|
||||
CompareKeyUWord8);
|
||||
break;
|
||||
case TYPE_Word16:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_Word16,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_Word16,
|
||||
CompareKeyWord16);
|
||||
break;
|
||||
case TYPE_UWord16:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_UWord16,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_UWord16,
|
||||
CompareKeyUWord16);
|
||||
break;
|
||||
case TYPE_Word32:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_Word32,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_Word32,
|
||||
CompareKeyWord32);
|
||||
break;
|
||||
case TYPE_UWord32:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_UWord32,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_UWord32,
|
||||
CompareKeyUWord32);
|
||||
break;
|
||||
case TYPE_Word64:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_Word64,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_Word64,
|
||||
CompareKeyWord64);
|
||||
break;
|
||||
case TYPE_UWord64:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, WebRtc_UWord64,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, WebRtc_UWord64,
|
||||
CompareKeyUWord64);
|
||||
break;
|
||||
case TYPE_Float32:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, float,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, float,
|
||||
CompareKeyFloat32);
|
||||
break;
|
||||
case TYPE_Float64:
|
||||
KEY_QSORT(ptrSortKey, key, numOfElements, double,
|
||||
KEY_QSORT(ptr_sort_key, key, num_of_elements, double,
|
||||
CompareKeyFloat64);
|
||||
break;
|
||||
default:
|
||||
@ -489,61 +427,59 @@ namespace webrtc
|
||||
}
|
||||
|
||||
// Shuffle into sorted position based on index map.
|
||||
WebRtc_UWord8* ptrData = static_cast<WebRtc_UWord8*>(data);
|
||||
WebRtc_UWord8* ptrDataSorted = new(std::nothrow) WebRtc_UWord8
|
||||
[numOfElements * sizeOfElement];
|
||||
if (ptrDataSorted == NULL)
|
||||
{
|
||||
WebRtc_UWord8* ptr_data = static_cast<WebRtc_UWord8*>(data);
|
||||
WebRtc_UWord8* ptr_data_sorted =
|
||||
new(std::nothrow) WebRtc_UWord8[num_of_elements * size_of_element];
|
||||
if (ptr_data_sorted == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (WebRtc_UWord32 i = 0; i < numOfElements; i++)
|
||||
{
|
||||
memcpy(ptrDataSorted + i * sizeOfElement, ptrData +
|
||||
ptrSortKey[i].index * sizeOfElement, sizeOfElement);
|
||||
for (WebRtc_UWord32 i = 0; i < num_of_elements; i++) {
|
||||
memcpy(ptr_data_sorted + i * size_of_element, ptr_data +
|
||||
ptr_sort_key[i].index_ * size_of_element, size_of_element);
|
||||
}
|
||||
memcpy(ptrData, ptrDataSorted, numOfElements * sizeOfElement);
|
||||
memcpy(ptr_data, ptr_data_sorted, num_of_elements * size_of_element);
|
||||
|
||||
delete[] ptrSortKey;
|
||||
delete[] ptrDataSorted;
|
||||
delete[] ptr_sort_key;
|
||||
delete[] ptr_data_sorted;
|
||||
|
||||
return 0;
|
||||
#else
|
||||
// Fall back to std::sort for 64-bit types and floats due to compiler
|
||||
// warnings and errors respectively with spreadsort.
|
||||
switch (keyType)
|
||||
{
|
||||
switch (key_type) {
|
||||
case TYPE_Word8:
|
||||
return IntegerKeySort<WebRtc_Word8>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return IntegerKeySort<WebRtc_Word8>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_UWord8:
|
||||
return IntegerKeySort<WebRtc_UWord8>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return IntegerKeySort<WebRtc_UWord8>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_Word16:
|
||||
return IntegerKeySort<WebRtc_Word16>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return IntegerKeySort<WebRtc_Word16>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_UWord16:
|
||||
return IntegerKeySort<WebRtc_UWord16>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return IntegerKeySort<WebRtc_UWord16>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_Word32:
|
||||
return IntegerKeySort<WebRtc_Word32>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return IntegerKeySort<WebRtc_Word32>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_UWord32:
|
||||
return IntegerKeySort<WebRtc_UWord32>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return IntegerKeySort<WebRtc_UWord32>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_Word64:
|
||||
return StdKeySort<WebRtc_Word64>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return StdKeySort<WebRtc_Word64>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_UWord64:
|
||||
return StdKeySort<WebRtc_UWord64>(data, key, numOfElements,
|
||||
sizeOfElement);
|
||||
return StdKeySort<WebRtc_UWord64>(data, key, num_of_elements,
|
||||
size_of_element);
|
||||
case TYPE_Float32:
|
||||
return StdKeySort<float>(data, key, numOfElements, sizeOfElement);
|
||||
return StdKeySort<float>(data, key, num_of_elements, size_of_element);
|
||||
case TYPE_Float64:
|
||||
return StdKeySort<double>(data, key, numOfElements, sizeOfElement);
|
||||
return StdKeySort<double>(data, key, num_of_elements, size_of_element);
|
||||
}
|
||||
assert(false);
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
|
Loading…
x
Reference in New Issue
Block a user