os/ossrv/genericopenlibs/cstdlib/LSTDLIB/QSORT.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// Map ANSI bsearch and qsort onto EPOC32 functions
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <e32std.h>
sl@0
    19
#include <stdlib.h>
sl@0
    20
sl@0
    21
NONSHARABLE_CLASS(TAnsiKey) : public TKey
sl@0
    22
	{
sl@0
    23
public:
sl@0
    24
	TAnsiKey(const TAny* key, const TAny* base, TInt length, TInt (*compar)(const TAny*, const TAny*))
sl@0
    25
		: iSearchKey(key), iCmp(compar)
sl@0
    26
		{ SetPtr(base); iKeyLength=length; }
sl@0
    27
sl@0
    28
	virtual TInt Compare(TInt aLeft,TInt aRight) const;
sl@0
    29
	virtual TAny *At(TInt anIndex) const;
sl@0
    30
private:
sl@0
    31
	const TAny* iSearchKey;
sl@0
    32
	TInt (*iCmp)(const TAny*, const TAny*);
sl@0
    33
	};
sl@0
    34
sl@0
    35
TInt TAnsiKey::Compare (TInt aLeft,TInt aRight) const
sl@0
    36
	{
sl@0
    37
	if (aRight==KIndexPtr)
sl@0
    38
		return (*iCmp)(At(aLeft),iSearchKey);
sl@0
    39
	else
sl@0
    40
		return (*iCmp)(At(aLeft),At(aRight));
sl@0
    41
	}
sl@0
    42
sl@0
    43
TAny* TAnsiKey::At (TInt aPos) const
sl@0
    44
	{
sl@0
    45
	return (TUint8*)iPtr+(aPos*iKeyLength);
sl@0
    46
	}
sl@0
    47
sl@0
    48
NONSHARABLE_CLASS(TAnsiSwap) : public TSwap
sl@0
    49
	{
sl@0
    50
public:
sl@0
    51
	TAnsiSwap(const TAny* base, TInt length) : iPtr(base), iLength(length) {}
sl@0
    52
sl@0
    53
	virtual void Swap(TInt aLeft,TInt aRight) const;
sl@0
    54
private:
sl@0
    55
	TUint8* At(TInt aPos) const {return (TUint8*)iPtr+(aPos*iLength);}
sl@0
    56
sl@0
    57
	const TAny* iPtr;
sl@0
    58
	TInt  iLength;
sl@0
    59
	};
sl@0
    60
sl@0
    61
void TAnsiSwap::Swap(TInt aLeft,TInt aRight) const
sl@0
    62
	{
sl@0
    63
	TUint8* left=At(aLeft);
sl@0
    64
	TUint8* right=At(aRight);
sl@0
    65
	TUint8 tmp;
sl@0
    66
sl@0
    67
	for (TInt i=iLength; i>0; i--)
sl@0
    68
		{
sl@0
    69
		tmp=*left;
sl@0
    70
		*left++=*right;
sl@0
    71
		*right++=tmp;
sl@0
    72
		}
sl@0
    73
	}
sl@0
    74
sl@0
    75
extern "C" {
sl@0
    76
sl@0
    77
/*
sl@0
    78
FUNCTION
sl@0
    79
<<bsearch>>---binary search
sl@0
    80
sl@0
    81
INDEX
sl@0
    82
	bsearch
sl@0
    83
sl@0
    84
ANSI_SYNOPSIS
sl@0
    85
	#include <stdlib.h>
sl@0
    86
	void *bsearch(const void *<[key]>, const void *<[base]>,
sl@0
    87
		size_t <[nmemb]>, size_t <[size]>,
sl@0
    88
		int (*<[compar]>)(const void *, const void *));
sl@0
    89
sl@0
    90
TRAD_SYNOPSIS
sl@0
    91
	#include <stdlib.h>
sl@0
    92
	char *bsearch(<[key]>, <[base]>, <[nmemb]>, <[size]>, <[compar]>)
sl@0
    93
	char *<[key]>;
sl@0
    94
	char *<[base]>;
sl@0
    95
	size_t <[nmemb]>, <[size]>;
sl@0
    96
	int (*<[compar]>)();
sl@0
    97
sl@0
    98
DESCRIPTION
sl@0
    99
<<bsearch>> searches an array beginning at <[base]> for any element
sl@0
   100
that matches <[key]>, using binary search.  <[nmemb]> is the element
sl@0
   101
count of the array; <[size]> is the size of each element.
sl@0
   102
sl@0
   103
The array must be sorted in ascending order with respect to the
sl@0
   104
comparison function <[compar]> (which you supply as the last argument of
sl@0
   105
<<bsearch>>).
sl@0
   106
sl@0
   107
You must define the comparison function <<(*<[compar]>)>> to have two
sl@0
   108
arguments; its result must be negative if the first argument is
sl@0
   109
less than the second, zero if the two arguments match, and
sl@0
   110
positive if the first argument is greater than the second (where
sl@0
   111
``less than'' and ``greater than'' refer to whatever arbitrary
sl@0
   112
ordering is appropriate).
sl@0
   113
sl@0
   114
RETURNS
sl@0
   115
Returns a pointer to an element of <[array]> that matches <[key]>.  If
sl@0
   116
more than one matching element is available, the result may point to
sl@0
   117
any of them. Returns NULL if no matching element is found.
sl@0
   118
sl@0
   119
PORTABILITY
sl@0
   120
<<bsearch>> is ANSI.
sl@0
   121
sl@0
   122
No supporting OS subroutines are required.
sl@0
   123
*/
sl@0
   124
sl@0
   125
/**
sl@0
   126
searches an array beginning at <[base]> for any element
sl@0
   127
that matches <[key]>, using binary search
sl@0
   128
@return a pointer to an element of <[array]> that matches <[key]>.  If
sl@0
   129
more than one matching element is available, the result may point to
sl@0
   130
any of them. Returns NULL if no matching element is found.
sl@0
   131
@param key
sl@0
   132
@param base
sl@0
   133
@param nmemb
sl@0
   134
@param size
sl@0
   135
*/
sl@0
   136
EXPORT_C void* bsearch (const void* key, const void* base, size_t nmemb, size_t size,
sl@0
   137
	int (*compar)(const void*, const void*))
sl@0
   138
	{
sl@0
   139
	TAnsiKey searchMe(key, base, size, compar);
sl@0
   140
	TInt result=KIndexPtr;
sl@0
   141
	TInt r=User::BinarySearch(nmemb, searchMe, result);
sl@0
   142
	if (r==0)
sl@0
   143
		return searchMe.At(result);
sl@0
   144
	else
sl@0
   145
		return NULL;
sl@0
   146
	}
sl@0
   147
sl@0
   148
/*
sl@0
   149
FUNCTION
sl@0
   150
<<qsort>>---sort an array
sl@0
   151
sl@0
   152
INDEX
sl@0
   153
	qsort
sl@0
   154
sl@0
   155
ANSI_SYNOPSIS
sl@0
   156
	#include <stdlib.h>
sl@0
   157
	void qsort(void *<[base]>, size_t <[nmemb]>, size_t <[size]>,
sl@0
   158
		   int (*<[compar]>)(const void *, const void *) );
sl@0
   159
sl@0
   160
TRAD_SYNOPSIS
sl@0
   161
	#include <stdlib.h>
sl@0
   162
	qsort(<[base]>, <[nmemb]>, <[size]>, <[compar]> )
sl@0
   163
	char *<[base]>;
sl@0
   164
	size_t <[nmemb]>;
sl@0
   165
	size_t <[size]>;
sl@0
   166
	int (*<[compar]>)();
sl@0
   167
sl@0
   168
DESCRIPTION
sl@0
   169
<<qsort>> sorts an array (beginning at <[base]>) of <[nmemb]> objects.
sl@0
   170
<[size]> describes the size of each element of the array.
sl@0
   171
sl@0
   172
You must supply a pointer to a comparison function, using the argument
sl@0
   173
shown as <[compar]>.  (This permits sorting objects of unknown
sl@0
   174
properties.)  Define the comparison function to accept two arguments,
sl@0
   175
each a pointer to an element of the array starting at <[base]>.  The
sl@0
   176
result of <<(*<[compar]>)>> must be negative if the first argument is
sl@0
   177
less than the second, zero if the two arguments match, and positive if
sl@0
   178
the first argument is greater than the second (where ``less than'' and
sl@0
   179
``greater than'' refer to whatever arbitrary ordering is appropriate).
sl@0
   180
sl@0
   181
The array is sorted in place; that is, when <<qsort>> returns, the
sl@0
   182
array elements beginning at <[base]> have been reordered.
sl@0
   183
sl@0
   184
RETURNS
sl@0
   185
<<qsort>> does not return a result.
sl@0
   186
sl@0
   187
PORTABILITY
sl@0
   188
<<qsort>> is required by ANSI (without specifying the sorting algorithm).
sl@0
   189
*/
sl@0
   190
sl@0
   191
/**
sl@0
   192
Sort an array.
sl@0
   193
@param base 
sl@0
   194
@param nmemb
sl@0
   195
@param size describes the size of each element of the array
sl@0
   196
*/
sl@0
   197
EXPORT_C void qsort (void* base, size_t nmemb, size_t size,
sl@0
   198
	int (*compar)(const void*, const void*))
sl@0
   199
	{
sl@0
   200
	TAnsiKey  searchMe(NULL, base, size, compar);
sl@0
   201
	TAnsiSwap swapUs(base,size);
sl@0
   202
	User::QuickSort(nmemb, searchMe, swapUs);
sl@0
   203
	return;
sl@0
   204
	}
sl@0
   205
sl@0
   206
} // extern "C"