Delete unused spreadsort implementation.

BUG=None

Review-Url: https://codereview.webrtc.org/2546863003
Cr-Commit-Position: refs/heads/master@{#15417}
This commit is contained in:
nisse 2016-12-05 03:03:19 -08:00 committed by Commit bot
parent 29c9dda0f4
commit fa07b910a9
7 changed files with 0 additions and 2580 deletions

View File

@ -25,8 +25,6 @@ modules/audio_device/mac/portaudio/pa_ringbuffer.h
modules/audio_processing/aec/aec_rdft.c
system_wrappers/source/condition_variable_event_win.cc
system_wrappers/source/set_thread_name_win.h
system_wrappers/source/spreadsortlib/constants.hpp
system_wrappers/source/spreadsortlib/spreadsort.hpp
Individual licenses for each file:
-------------------------------------------------------------------------------
@ -457,32 +455,4 @@ purpose and non-infringement.
(F) Platform Limitation - The licenses granted in sections 2(A) and 2(B)
extend only to the software or derivative works that you create that run on a
Microsoft Windows operating system product.
-------------------------------------------------------------------------------
Files:
system_wrappers/source/spreadsortlib/constants.hpp
system_wrappers/source/spreadsortlib/spreadsort.hpp
License:
/*Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.*/

View File

@ -31,7 +31,6 @@ rtc_static_library("system_wrappers") {
"include/rtp_to_ntp.h",
"include/rw_lock_wrapper.h",
"include/sleep.h",
"include/sort.h",
"include/static_instance.h",
"include/stl_util.h",
"include/stringize_macros.h",
@ -61,7 +60,6 @@ rtc_static_library("system_wrappers") {
"source/rw_lock_winxp_win.cc",
"source/rw_lock_winxp_win.h",
"source/sleep.cc",
"source/sort.cc",
"source/timestamp_extrapolator.cc",
"source/trace_impl.cc",
"source/trace_impl.h",

View File

@ -1,65 +0,0 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// Generic unstable sorting routines.
#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SORT_H_
#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SORT_H_
#include "webrtc/common_types.h"
#include "webrtc/typedefs.h"
namespace webrtc {
enum Type {
TYPE_Word8,
TYPE_UWord8,
TYPE_Word16,
TYPE_UWord16,
TYPE_Word32,
TYPE_UWord32,
TYPE_Word64,
TYPE_UWord64,
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.
// 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.
int32_t Sort(void* data, uint32_t 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
// one-to-one correspondence between data elements and key elements, with
// corresponding elements sharing the same position in their respective
// arrays.
//
// data [in/out] A pointer to an array of arbitrary type.
// Upon return it will be sorted in ascending order.
// key [in] A pointer to an array of keys used to sort the
// data 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.
//
int32_t KeySort(void* data, void* key, uint32_t num_of_elements,
uint32_t size_of_element, Type key_type);
} // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_SORT_H_

View File

@ -1,487 +0,0 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// When the platform supports STL, the functions are implemented using a
// templated spreadsort algorithm (http://sourceforge.net/projects/spreadsort/),
// part of the Boost C++ library collection. Otherwise, the C standard library's
// qsort() will be used.
#include "webrtc/system_wrappers/include/sort.h"
#include <assert.h>
#include <string.h> // memcpy
#include <new> // nothrow new
#ifdef NO_STL
#include <stdlib.h> // qsort
#else
#include <algorithm> // std::sort
#include <vector>
// TODO(ajm) upgrade to spreadsort v2.
#include "webrtc/system_wrappers/source/spreadsortlib/spreadsort.hpp"
#endif
#ifdef NO_STL
#define COMPARE_DEREFERENCED(XT, YT) \
do { \
if ((XT) > (YT)) { \
return 1; \
} \
else if ((XT) < (YT)) { \
return -1; \
} \
return 0; \
} while(0)
#define COMPARE_FOR_QSORT(X, Y, TYPE) \
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)
#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_)); \
COMPARE_DEREFERENCED(xT, yT); \
} while(0)
#define KEY_QSORT(SORT_KEY, KEY, NUM_OF_ELEMENTS, KEY_TYPE, COMPARE_FUNC) \
do { \
KEY_TYPE* key_type = (KEY_TYPE*)(key); \
for (uint32_t 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)
#endif
namespace webrtc {
#ifdef NO_STL
struct SortKey {
void* key_;
uint32_t index_;
};
#else
template<typename KeyType>
struct SortKey {
KeyType key_;
uint32_t index_;
};
#endif
namespace { // Unnamed namespace provides internal linkage.
#ifdef NO_STL
int CompareWord8(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, int8_t);
}
int CompareUWord8(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, uint8_t);
}
int CompareWord16(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, int16_t);
}
int CompareUWord16(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, uint16_t);
}
int CompareWord32(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, int32_t);
}
int CompareUWord32(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, uint32_t);
}
int CompareWord64(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, int64_t);
}
int CompareUWord64(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, uint64_t);
}
int CompareFloat32(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, float);
}
int CompareFloat64(const void* x, const void* y) {
COMPARE_FOR_QSORT(x, y, double);
}
int CompareKeyWord8(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, int8_t);
}
int CompareKeyUWord8(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, uint8_t);
}
int CompareKeyWord16(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, int16_t);
}
int CompareKeyUWord16(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, uint16_t);
}
int CompareKeyWord32(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, int32_t);
}
int CompareKeyUWord32(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, uint32_t);
}
int CompareKeyWord64(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, int64_t);
}
int CompareKeyUWord64(const void* sort_key_x, const void* sort_key_y) {
COMPARE_KEY_FOR_QSORT(sort_key_x, sort_key_y, uint64_t);
}
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* 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>& 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>& sort_key,
const unsigned offset) const {
return sort_key.key_ >> offset;
}
};
template <typename DataType>
inline void IntegerSort(void* data, uint32_t 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, uint32_t 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, uint32_t num_of_elements) {
DataType* data_type = static_cast<DataType*>(data);
std::sort(data_type, data_type + num_of_elements);
}
template<typename KeyType>
inline int32_t SetupKeySort(void* key,
SortKey<KeyType>*& ptr_sort_key,
uint32_t num_of_elements) {
ptr_sort_key = new(std::nothrow) SortKey<KeyType>[num_of_elements];
if (ptr_sort_key == NULL) {
return -1;
}
KeyType* key_type = static_cast<KeyType*>(key);
for (uint32_t i = 0; i < num_of_elements; i++) {
ptr_sort_key[i].key_ = key_type[i];
ptr_sort_key[i].index_ = i;
}
return 0;
}
template<typename KeyType>
inline int32_t TeardownKeySort(void* data,
SortKey<KeyType>* ptr_sort_key,
uint32_t num_of_elements,
uint32_t size_of_element) {
uint8_t* ptr_data = static_cast<uint8_t*>(data);
uint8_t* ptr_data_sorted =
new(std::nothrow) uint8_t[num_of_elements * size_of_element];
if (ptr_data_sorted == NULL) {
return -1;
}
for (uint32_t 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(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 int32_t IntegerKeySort(void* data, void* key,
uint32_t num_of_elements,
uint32_t size_of_element) {
SortKey<KeyType>* ptr_sort_key;
if (SetupKeySort<KeyType>(key, ptr_sort_key, num_of_elements) != 0) {
return -1;
}
boost::integer_sort(ptr_sort_key, ptr_sort_key + num_of_elements,
KeyRightShift<KeyType>(), KeyLessThan<KeyType>());
if (TeardownKeySort<KeyType>(data, ptr_sort_key, num_of_elements,
size_of_element) != 0) {
return -1;
}
return 0;
}
template<typename KeyType>
inline int32_t StdKeySort(void* data, void* key,
uint32_t num_of_elements,
uint32_t size_of_element) {
SortKey<KeyType>* ptr_sort_key;
if (SetupKeySort<KeyType>(key, ptr_sort_key, num_of_elements) != 0) {
return -1;
}
std::sort(ptr_sort_key, ptr_sort_key + num_of_elements,
KeyLessThan<KeyType>());
if (TeardownKeySort<KeyType>(data, ptr_sort_key, num_of_elements,
size_of_element) != 0) {
return -1;
}
return 0;
}
#endif
}
int32_t Sort(void* data, uint32_t num_of_elements, Type type) {
if (data == NULL) {
return -1;
}
#ifdef NO_STL
switch (type) {
case TYPE_Word8:
qsort(data, num_of_elements, sizeof(int8_t), CompareWord8);
break;
case TYPE_UWord8:
qsort(data, num_of_elements, sizeof(uint8_t), CompareUWord8);
break;
case TYPE_Word16:
qsort(data, num_of_elements, sizeof(int16_t), CompareWord16);
break;
case TYPE_UWord16:
qsort(data, num_of_elements, sizeof(uint16_t), CompareUWord16);
break;
case TYPE_Word32:
qsort(data, num_of_elements, sizeof(int32_t), CompareWord32);
break;
case TYPE_UWord32:
qsort(data, num_of_elements, sizeof(uint32_t), CompareUWord32);
break;
case TYPE_Word64:
qsort(data, num_of_elements, sizeof(int64_t), CompareWord64);
break;
case TYPE_UWord64:
qsort(data, num_of_elements, sizeof(uint64_t), CompareUWord64);
break;
case TYPE_Float32:
qsort(data, num_of_elements, sizeof(float), CompareFloat32);
break;
case TYPE_Float64:
qsort(data, num_of_elements, sizeof(double), CompareFloat64);
break;
default:
return -1;
}
#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) {
case TYPE_Word8:
IntegerSort<int8_t>(data, num_of_elements);
break;
case TYPE_UWord8:
IntegerSort<uint8_t>(data, num_of_elements);
break;
case TYPE_Word16:
IntegerSort<int16_t>(data, num_of_elements);
break;
case TYPE_UWord16:
IntegerSort<uint16_t>(data, num_of_elements);
break;
case TYPE_Word32:
IntegerSort<int32_t>(data, num_of_elements);
break;
case TYPE_UWord32:
IntegerSort<uint32_t>(data, num_of_elements);
break;
case TYPE_Word64:
StdSort<int64_t>(data, num_of_elements);
break;
case TYPE_UWord64:
StdSort<uint64_t>(data, num_of_elements);
break;
case TYPE_Float32:
StdSort<float>(data, num_of_elements);
break;
case TYPE_Float64:
StdSort<double>(data, num_of_elements);
break;
}
#endif
return 0;
}
int32_t KeySort(void* data, void* key, uint32_t num_of_elements,
uint32_t size_of_element, Type key_type) {
if (data == NULL) {
return -1;
}
if (key == NULL) {
return -1;
}
if ((uint64_t)num_of_elements * size_of_element > 0xffffffff) {
return -1;
}
#ifdef NO_STL
SortKey* ptr_sort_key = new(std::nothrow) SortKey[num_of_elements];
if (ptr_sort_key == NULL) {
return -1;
}
switch (key_type) {
case TYPE_Word8:
KEY_QSORT(ptr_sort_key, key, num_of_elements, int8_t,
CompareKeyWord8);
break;
case TYPE_UWord8:
KEY_QSORT(ptr_sort_key, key, num_of_elements, uint8_t,
CompareKeyUWord8);
break;
case TYPE_Word16:
KEY_QSORT(ptr_sort_key, key, num_of_elements, int16_t,
CompareKeyWord16);
break;
case TYPE_UWord16:
KEY_QSORT(ptr_sort_key, key, num_of_elements, uint16_t,
CompareKeyUWord16);
break;
case TYPE_Word32:
KEY_QSORT(ptr_sort_key, key, num_of_elements, int32_t,
CompareKeyWord32);
break;
case TYPE_UWord32:
KEY_QSORT(ptr_sort_key, key, num_of_elements, uint32_t,
CompareKeyUWord32);
break;
case TYPE_Word64:
KEY_QSORT(ptr_sort_key, key, num_of_elements, int64_t,
CompareKeyWord64);
break;
case TYPE_UWord64:
KEY_QSORT(ptr_sort_key, key, num_of_elements, uint64_t,
CompareKeyUWord64);
break;
case TYPE_Float32:
KEY_QSORT(ptr_sort_key, key, num_of_elements, float,
CompareKeyFloat32);
break;
case TYPE_Float64:
KEY_QSORT(ptr_sort_key, key, num_of_elements, double,
CompareKeyFloat64);
break;
default:
return -1;
}
// Shuffle into sorted position based on index map.
uint8_t* ptr_data = static_cast<uint8_t*>(data);
uint8_t* ptr_data_sorted =
new(std::nothrow) uint8_t[num_of_elements * size_of_element];
if (ptr_data_sorted == NULL) {
return -1;
}
for (uint32_t 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(ptr_data, ptr_data_sorted, num_of_elements * size_of_element);
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 (key_type) {
case TYPE_Word8:
return IntegerKeySort<int8_t>(data, key, num_of_elements,
size_of_element);
case TYPE_UWord8:
return IntegerKeySort<uint8_t>(data, key, num_of_elements,
size_of_element);
case TYPE_Word16:
return IntegerKeySort<int16_t>(data, key, num_of_elements,
size_of_element);
case TYPE_UWord16:
return IntegerKeySort<uint16_t>(data, key, num_of_elements,
size_of_element);
case TYPE_Word32:
return IntegerKeySort<int32_t>(data, key, num_of_elements,
size_of_element);
case TYPE_UWord32:
return IntegerKeySort<uint32_t>(data, key, num_of_elements,
size_of_element);
case TYPE_Word64:
return StdKeySort<int64_t>(data, key, num_of_elements,
size_of_element);
case TYPE_UWord64:
return StdKeySort<uint64_t>(data, key, num_of_elements,
size_of_element);
case TYPE_Float32:
return StdKeySort<float>(data, key, num_of_elements, size_of_element);
case TYPE_Float64:
return StdKeySort<double>(data, key, num_of_elements, size_of_element);
}
assert(false);
return -1;
#endif
}
} // namespace webrtc

View File

@ -1,42 +0,0 @@
/*Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.*/
#ifndef BOOST_SPREADSORT_CONSTANTS
#define BOOST_SPREADSORT_CONSTANTS
namespace boost {
namespace detail {
//Tuning constants
//Sets the minimum number of items per bin.
static const unsigned LOG_MEAN_BIN_SIZE = 2;
//This should be tuned to your processor cache; if you go too large you get cache misses on bins
//The smaller this number, the less worst-case memory usage. If too small, too many recursions slow down spreadsort
static const unsigned MAX_SPLITS = 10;
//Used to force a comparison-based sorting for small bins, if it's faster. Minimum value 0
static const unsigned LOG_MIN_SPLIT_COUNT = 5;
//There is a minimum size below which it is not worth using spreadsort
static const long MIN_SORT_SIZE = 1000;
//This is the constant on the log base n of m calculation; make this larger the faster std::sort is relative to spreadsort
static const unsigned LOG_CONST = 2;
}
}
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,266 +0,0 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include "webrtc/base/timeutils.h"
#include "webrtc/system_wrappers/include/sort.h"
// Excellent work polluting the global namespace Visual Studio...
#undef max
#undef min
#include <limits>
template<typename KeyType>
struct LotsOfData
{
KeyType key;
char data[64];
};
template<typename DataType>
int Compare(const void* dataX, const void* dataY)
{
DataType dataX = (DataType)*(const DataType*)dataX;
DataType dataY = (DataType)*(const DataType*)dataY;
if (dataX > dataY)
{
return 1;
}
else if (dataX < dataY)
{
return -1;
}
return 0;
};
template<typename DataType, typename KeyType>
int CompareKey(const void* dataX, const void* dataY)
{
KeyType keyX = ((const DataType*)dataX)->key;
KeyType keyY = ((const DataType*)dataY)->key;
if (keyX > keyY)
{
return 1;
}
else if (keyX < keyY)
{
return -1;
}
return 0;
}
template<typename DataType>
struct KeyLessThan
{
bool operator()(const DataType &dataX, const DataType &dataY) const
{
return dataX.key < dataY.key;
}
};
const char* TypeEnumToString(webrtc::Type type)
{
switch (type)
{
using namespace webrtc;
case TYPE_Word8:
return "Word8";
case TYPE_UWord8:
return "UWord8";
case TYPE_Word16:
return "Word16";
case TYPE_UWord16:
return "UWord16";
case TYPE_Word32:
return "Word32";
case TYPE_UWord32:
return "UWord32";
case TYPE_Word64:
return "Word64";
case TYPE_UWord64:
return "UWord64";
case TYPE_Float32:
return "Float32";
case TYPE_Float64:
return "Float64";
default:
return "Unrecognized";
}
}
template<typename Type>
Type TypedRand()
{
if (std::numeric_limits<Type>::is_integer)
{
double floatRand = static_cast<double>(rand()) / RAND_MAX;
if (std::numeric_limits<Type>::is_signed)
{
floatRand -= 0.5;
}
// Uniform [-max()/2, max()/2] for signed
// [0, max()] for unsigned
return static_cast<Type>(floatRand * std::numeric_limits<Type>::max());
}
else // Floating point
{
// Uniform [-0.5, 0.5]
// The outer cast is to remove template warnings.
return static_cast<Type>((static_cast<Type>(rand()) / RAND_MAX) - 0.5);
}
}
template<typename KeyType>
void RunSortTest(webrtc::Type sortType, bool keySort)
{
enum { DataLength = 1000 };
enum { NumOfTests = 10000 };
KeyType key[DataLength];
KeyType keyRef[DataLength];
LotsOfData<KeyType> data[DataLength];
LotsOfData<KeyType> dataRef[DataLength];
int32_t retVal = 0;
if (keySort)
{
printf("Running %s KeySort() tests...\n", TypeEnumToString(sortType));
}
else
{
printf("Running %s Sort() tests...\n", TypeEnumToString(sortType));
}
int64_t accTicks;
for (int i = 0; i < NumOfTests; i++)
{
for (int j = 0; j < DataLength; j++)
{
key[j] = TypedRand<KeyType>();
data[j].key = key[j];
// Write index to payload. We use this later for verification.
sprintf(data[j].data, "%d", j);
}
memcpy(dataRef, data, sizeof(data));
memcpy(keyRef, key, sizeof(key));
retVal = 0;
int64_t t0 = rtc::TimeNanos();
if (keySort)
{
retVal = webrtc::KeySort(data, key, DataLength, sizeof(LotsOfData<KeyType>),
sortType);
//std::sort(data, data + DataLength, KeyLessThan<KeyType>());
//qsort(data, DataLength, sizeof(LotsOfData<KeyType>),
// CompareKey<LotsOfData<KeyType>, KeyType>);
}
else
{
retVal = webrtc::Sort(key, DataLength, sortType);
//std::sort(key, key + DataLength);
//qsort(key, DataLength, sizeof(KeyType), Compare<KeyType>);
}
int64_t t1 = rtc::TimeNanos();
accTicks += (t1 - t0);
if (retVal != 0)
{
printf("Test failed at iteration %d:\n", i);
printf("Sort returned an error. ");
printf("It likely does not support the requested type\nExiting...\n");
exit(0);
}
// Reference sort.
if (!keySort)
{
std::sort(keyRef, keyRef + DataLength);
}
if (keySort)
{
for (int j = 0; j < DataLength - 1; j++)
{
if (data[j].key > data[j + 1].key)
{
printf("Test failed at iteration %d:\n", i);
printf("Keys are not monotonically increasing\nExiting...\n");
exit(0);
}
int index = atoi(data[j].data);
if (index < 0 || index >= DataLength || data[j].key != dataRef[index].key)
{
printf("Test failed at iteration %d:\n", i);
printf("Payload data is corrupt\nExiting...\n");
exit(0);
}
}
}
else
{
for (int j = 0; j < DataLength - 1; j++)
{
if (key[j] > key[j + 1])
{
printf("Test failed at iteration %d:\n", i);
printf("Data is not monotonically increasing\nExiting...\n");
exit(0);
}
}
if (memcmp(key, keyRef, sizeof(key)) != 0)
{
printf("Test failed at iteration %d:\n", i);
printf("Sort data differs from std::sort reference\nExiting...\n");
exit(0);
}
}
}
printf("Compliance test passed over %d iterations\n", NumOfTests);
int64_t executeTime = accTicks / rtc::kNumNanosecsPerMillisec;
printf("Execute time: %.2f s\n\n", (float)executeTime / 1000);
}
int main()
{
// Seed rand().
srand(42);
bool keySort = false;
for (int i = 0; i < 2; i++) {
RunSortTest<int8_t>(webrtc::TYPE_Word8, keySort);
RunSortTest<uint8_t>(webrtc::TYPE_UWord8, keySort);
RunSortTest<int16_t>(webrtc::TYPE_Word16, keySort);
RunSortTest<uint16_t>(webrtc::TYPE_UWord16, keySort);
RunSortTest<int32_t>(webrtc::TYPE_Word32, keySort);
RunSortTest<uint32_t>(webrtc::TYPE_UWord32, keySort);
RunSortTest<int64_t>(webrtc::TYPE_Word64, keySort);
RunSortTest<uint64_t>(webrtc::TYPE_UWord64, keySort);
RunSortTest<float>(webrtc::TYPE_Float32, keySort);
RunSortTest<double>(webrtc::TYPE_Float64, keySort);
keySort = !keySort;
}
printf("All tests passed\n");
return 0;
}