1. 求STL中vector中insert函數的源代碼 最好加上注釋
還是直接看vector standard header比較好,直接貼出來不好閱讀。
源代碼VC和VS的目錄里有,比如VC8路徑:C:\Program Files\Microsoft Visual Studio 8\VC\include里的vector。
//insert
_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,
const _Myt& _Right, size_type _Roff, size_type _Count)
{ // insert _Right [_Roff, _Roff + _Count) at _Off
if (_Mysize < _Off || _Right.size() < _Roff)
_String_base::_Xran(); // _Off or _Roff off end
size_type _Num = _Right.size() - _Roff;
if (_Num < _Count)
_Count = _Num; // trim _Count to size
if (npos - _Mysize <= _Count)
_String_base::_Xlen(); // result too long
if (0 < _Count && _Grow(_Num = _Mysize + _Count))
{ // make room and insert new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off, _Mysize - _Off); // empty out hole
if (this == &_Right)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + (_Off < _Roff ? _Roff + _Count : _Roff),
_Count); // substring
else
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Right._Myptr() + _Roff, _Count); // fill hole
_Eos(_Num);
}
return (*this);
}
_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,
const _Elem *_Ptr, size_type _Count)
{ // insert [_Ptr, _Ptr + _Count) at _Off
if (_Inside(_Ptr))
return (insert(_Off, *this,
_Ptr - _Myptr(), _Count)); // substring
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (npos - _Mysize <= _Count)
_String_base::_Xlen(); // result too long
size_type _Num;
if (0 < _Count && _Grow(_Num = _Mysize + _Count))
{ // make room and insert new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off, _Mysize - _Off); // empty out hole
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off, _Ptr, _Count); // fill hole
_Eos(_Num);
}
return (*this);
}
_Myt& __CLR_OR_THIS_CALL insert(size_type _Off, const _Elem *_Ptr)
{ // insert [_Ptr, <null>) at _Off
return (insert(_Off, _Ptr, _Traits::length(_Ptr)));
}
_Myt& __CLR_OR_THIS_CALL insert(size_type _Off,
size_type _Count, _Elem _Ch)
{ // insert _Count * _Ch at _Off
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (npos - _Mysize <= _Count)
_String_base::_Xlen(); // result too long
size_type _Num;
if (0 < _Count && _Grow(_Num = _Mysize + _Count))
{ // make room and insert new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off, _Mysize - _Off); // empty out hole
_Chassign(_Off, _Count, _Ch); // fill hole
_Eos(_Num);
}
return (*this);
}
iterator __CLR_OR_THIS_CALL insert(iterator _Where)
{ // insert <null> at _Where
return (insert(_Where, _Elem()));
}
iterator __CLR_OR_THIS_CALL insert(iterator _Where, _Elem _Ch)
{ // insert _Ch at _Where
size_type _Off = _Pdif(_Where, begin());
insert(_Off, 1, _Ch);
return (begin() + _Off);
}
void __CLR_OR_THIS_CALL insert(iterator _Where, size_type _Count, _Elem _Ch)
{ // insert _Count * _Elem at _Where
size_type _Off = _Pdif(_Where, begin());
insert(_Off, _Count, _Ch);
}
template<class _It>
void __CLR_OR_THIS_CALL insert(iterator _Where, _It _First, _It _Last)
{ // insert [_First, _Last) at _Where
_Insert(_Where, _First, _Last, _Iter_cat(_First));
}
template<class _It>
void __CLR_OR_THIS_CALL _Insert(iterator _Where, _It _Count, _It _Ch,
_Int_iterator_tag)
{ // insert _Count * _Ch at _Where
insert(_Where, (size_type)_Count, (_Elem)_Ch);
}
template<class _It>
void __CLR_OR_THIS_CALL _Insert(iterator _Where, _It _First, _It _Last,
input_iterator_tag)
{ // insert [_First, _Last) at _Where, input iterators
replace(_Where, _Where, _First, _Last);
}
void __CLR_OR_THIS_CALL insert(iterator _Where, const_pointer _First, const_pointer _Last)
{ // insert [_First, _Last) at _Where, const pointers
replace(_Where, _Where, _First, _Last);
}
void __CLR_OR_THIS_CALL insert(iterator _Where, const_iterator _First, const_iterator _Last)
{ // insert [_First, _Last) at _Where, const_iterators
replace(_Where, _Where, _First, _Last);
}
//replace
_Myt& __CLR_OR_THIS_CALL replace(size_type _Off, size_type _N0, const _Myt& _Right)
{ // replace [_Off, _Off + _N0) with _Right
return (replace(_Off, _N0, _Right, 0, npos));
}
_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,
size_type _N0, const _Myt& _Right, size_type _Roff, size_type _Count)
{ // replace [_Off, _Off + _N0) with _Right [_Roff, _Roff + _Count)
if (_Mysize < _Off || _Right.size() < _Roff)
_String_base::_Xran(); // _Off or _Roff off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off; // trim _N0 to size
size_type _Num = _Right.size() - _Roff;
if (_Num < _Count)
_Count = _Num; // trim _Count to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen(); // result too long
size_type _Nm = _Mysize - _N0 - _Off; // length of preserved tail
size_type _Newsize = _Mysize + _Count - _N0;
if (_Mysize < _Newsize)
_Grow(_Newsize);
if (this != &_Right)
{ // no overlap, just move down and in new stuff
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // empty hole
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Right._Myptr() + _Roff, _Count); // fill hole
}
else if (_Count <= _N0)
{ // hole doesn't get larger, just in substring
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + _Roff, _Count); // fill hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
}
else if (_Roff <= _Off)
{ // hole gets larger, substring begins before hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + _Roff, _Count); // fill hole
}
else if (_Off + _N0 <= _Roff)
{ // hole gets larger, substring begins after hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + (_Roff + _Count - _N0), _Count); // fill hole
}
else
{ // hole gets larger, substring begins in hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off, _Myres - _Off,
_Myptr() + _Roff, _N0); // fill old hole
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _N0, _Myres - _Off - _N0, _Myptr() + _Roff + _Count,
_Count - _N0); // fill rest of new hole
}
_Eos(_Newsize);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,
size_type _N0, const _Elem *_Ptr, size_type _Count)
{ // replace [_Off, _Off + _N0) with [_Ptr, _Ptr + _Count)
if (_Inside(_Ptr))
return (replace(_Off, _N0, *this,
_Ptr - _Myptr(), _Count)); // substring, replace carefully
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off; // trim _N0 to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen(); // result too long
size_type _Nm = _Mysize - _N0 - _Off;
if (_Count < _N0)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // smaller hole, move tail up
size_type _Num;
if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0))
{ // make room and rearrange
if (_N0 < _Count)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Traits_helper::_s<_Traits>(_Myptr() + _Off, _Myres - _Off, _Ptr, _Count); // fill hole
_Eos(_Num);
}
return (*this);
}
_Myt& __CLR_OR_THIS_CALL replace(size_type _Off, size_type _N0, const _Elem *_Ptr)
{ // replace [_Off, _Off + _N0) with [_Ptr, <null>)
return (replace(_Off, _N0, _Ptr, _Traits::length(_Ptr)));
}
_Myt& __CLR_OR_THIS_CALL replace(size_type _Off,
size_type _N0, size_type _Count, _Elem _Ch)
{ // replace [_Off, _Off + _N0) with _Count * _Ch
if (_Mysize < _Off)
_String_base::_Xran(); // _Off off end
if (_Mysize - _Off < _N0)
_N0 = _Mysize - _Off; // trim _N0 to size
if (npos - _Count <= _Mysize - _N0)
_String_base::_Xlen(); // result too long
size_type _Nm = _Mysize - _N0 - _Off;
if (_Count < _N0)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // smaller hole, move tail up
size_type _Num;
if ((0 < _Count || 0 < _N0) && _Grow(_Num = _Mysize + _Count - _N0))
{ // make room and rearrange
if (_N0 < _Count)
_Traits_helper::move_s<_Traits>(_Myptr() + _Off + _Count, _Myres - _Off - _Count,
_Myptr() + _Off + _N0, _Nm); // move tail down
_Chassign(_Off, _Count, _Ch); // fill hole
_Eos(_Num);
}
return (*this);
}
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Myt& _Right)
{ // replace [_First, _Last) with _Right
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Right));
}
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Elem *_Ptr,
size_type _Count)
{ // replace [_First, _Last) with [_Ptr, _Ptr + _Count)
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Ptr, _Count));
}
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last, const _Elem *_Ptr)
{ // replace [_First, _Last) with [_Ptr, <null>)
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Ptr));
}
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
size_type _Count, _Elem _Ch)
{ // replace [_First, _Last) with _Count * _Ch
return (replace(
_Pdif(_First, begin()), _Pdif(_Last, _First), _Count, _Ch));
}
template<class _It>
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
_It _First2, _It _Last2)
{ // replace [_First, _Last) with [_First2, _Last2)
return (_Replace(_First, _Last,
_First2, _Last2, _Iter_cat(_First2)));
}
template<class _It>
_Myt& __CLR_OR_THIS_CALL _Replace(iterator _First, iterator _Last,
_It _Count, _It _Ch, _Int_iterator_tag)
{ // replace [_First, _Last) with _Count * _Ch
return (replace(_First, _Last, (size_type)_Count, (_Elem)_Ch));
}
template<class _It>
_Myt& __CLR_OR_THIS_CALL _Replace(iterator _First, iterator _Last,
_It _First2, _It _Last2, input_iterator_tag)
{ // replace [_First, _Last) with [_First2, _Last2), input iterators
_Myt _Right(_First2, _Last2);
replace(_First, _Last, _Right);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
const_pointer _First2, const_pointer _Last2)
{ // replace [_First, _Last) with [_First2, _Last2), const pointers
if (_First2 == _Last2)
erase(_Pdif(_First, begin()), _Pdif(_Last, _First));
else
replace(_Pdif(_First, begin()), _Pdif(_Last, _First),
&*_First2, _Last2 - _First2);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL replace(iterator _First, iterator _Last,
const_iterator _First2, const_iterator _Last2)
{ // replace [_First, _Last) with [_First2, _Last2), const_iterators
if (_First2 == _Last2)
erase(_Pdif(_First, begin()), _Pdif(_Last, _First));
else
replace(_Pdif(_First, begin()), _Pdif(_Last, _First),
&*_First2, _Last2 - _First2);
return (*this);
}
2. 《STL源碼剖析》pdf下載在線閱讀,求百度網盤雲資源
《STL源碼剖析》侯捷電子書網盤下載免費在線閱讀
鏈接:
書名:STL源碼剖析
作者名:侯捷
豆瓣評分:8.7
出版社:華中科技大學出版社
出版年份:2002-6
頁數:493
內容介紹:
學習編程的人都知道,閱讀、剖析名家代碼乃是提高水平的捷徑。源碼之前,了無秘密。大師們的縝密思維、經驗結晶、技術思路、獨到風格,都原原本本體現在源碼之中。這本書所呈現的源碼,使讀者看到vector的實現、list的實現、heap的實現、deque的實現、Red Black tree的實現、hash table的實現、set/map的實現;看到各種演算法(排序、查找、排列組合、數據移動與復制技術)的實現;甚至還能夠看到底層的memory pool和高階抽象的traits機制的實現。
作者介紹:
侯捷,出生於1961年09月28日,台南縣柳營鄉人,南京大學客座教授。第一次在大陸大學授課: 南京大學軟體學院 and 同濟大學軟體學院。
3. 《STL源碼分析》中如何priority_queue使用greater函數對象
首先查看手冊,priority_queue的定義如下:
template<classT, classContainer=std::vector<T>, classCompare=std::less<typenameContainer::value_type>>classpriority_queue;
然後繼續看模板的三個參數的說明
—————————以下直接摘抄的—————
Template parameters
T - The type of the stored elements.The behavior is undefined ifTis not the same type asContainer::value_type.(since C++17)
Container - The type of the underlying container to use to store the elements. The container must satisfy the requirements ofSequenceContainer, and its iterators must satisfy the requirements ofLegacyRandomAccessIterator. Additionally, it must provide the following functions with the usual semantics:
front()
push_back()
pop_back()
The standard containersstd::vectorandstd::dequesatisfy these requirements.
Compare - AComparetype providing a strict weak ordering.
—————————以上直接摘抄的—————
故可知,使用priority_queue需要給三個類來實現模板,其中第三個類就是那個比較函數,你問的,為什麼要priority_queue<int, vector<int>, greater<int> > q1;已經回答完畢。
另外,可以參考std::less的定義,更深入學習第三個類的含義。已附在引用部分,自行查閱。
std::priority_queuestd::less
PS:第一個那傢伙回答的什麼東西!我本來是不想回答的。。。看見那傢伙胡謅一氣,氣不過。
4. c庫函數源碼
不是你表達不清,也許只是你根本不想仔細看一睛VC下面目錄的源碼,事實上就是有的。後附其中的qsort.c,以證明所言不虛。
VC的庫是提供源碼的,這東西也不值錢。
X:\Program Files\Microsoft Visual Studio\VCXX\CRT\SRC
注意有些可能本身是用匯編寫的。
/***
*qsort.c - quicksort algorithm; qsort() library function for sorting arrays
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* To implement the qsort() routine for sorting arrays.
*
*******************************************************************************/
#include <cruntime.h>
#include <stdlib.h>
#include <search.h>
/* prototypes for local routines */
static void __cdecl shortsort(char *lo, char *hi, unsigned width,
int (__cdecl *comp)(const void *, const void *));
static void __cdecl swap(char *p, char *q, unsigned int width);
/* this parameter defines the cutoff between using quick sort and
insertion sort for arrays; arrays with lengths shorter or equal to the
below value use insertion sort */
#define CUTOFF 8 /* testing shows that this is good value */
/***
*qsort(base, num, wid, comp) - quicksort function for sorting arrays
*
*Purpose:
* quicksort the array of elements
* side effects: sorts in place
*
*Entry:
* char *base = pointer to base of array
* unsigned num = number of elements in the array
* unsigned width = width in bytes of each array element
* int (*comp)() = pointer to function returning analog of strcmp for
* strings, but supplied by user for comparing the array elements.
* it accepts 2 pointers to elements and returns neg if 1<2, 0 if
* 1=2, pos if 1>2.
*
*Exit:
* returns void
*
*Exceptions:
*
*******************************************************************************/
/* sort the array between lo and hi (inclusive) */
void __cdecl qsort (
void *base,
unsigned num,
unsigned width,
int (__cdecl *comp)(const void *, const void *)
)
{
char *lo, *hi; /* ends of sub-array currently sorting */
char *mid; /* points to middle of subarray */
char *loguy, *higuy; /* traveling pointers for partition step */
unsigned size; /* size of the sub-array */
char *lostk[30], *histk[30];
int stkptr; /* stack for saving sub-array to be processed */
/* Note: the number of stack entries required is no more than
1 + log2(size), so 30 is sufficient for any array */
if (num < 2 || width == 0)
return; /* nothing to do */
stkptr = 0; /* initialize stack */
lo = base;
hi = (char *)base + width * (num-1); /* initialize limits */
/* this entry point is for pseudo-recursion calling: setting
lo and hi and jumping to here is like recursion, but stkptr is
prserved, locals aren't, so we preserve stuff on the stack */
recurse:
size = (hi - lo) / width + 1; /* number of el's to sort */
/* below a certain size, it is faster to use a O(n^2) sorting method */
if (size <= CUTOFF) {
shortsort(lo, hi, width, comp);
}
else {
/* First we pick a partititioning element. The efficiency of the
algorithm demands that we find one that is approximately the
median of the values, but also that we select one fast. Using
the first one proces bad performace if the array is already
sorted, so we use the middle one, which would require a very
wierdly arranged array for worst case performance. Testing shows
that a median-of-three algorithm does not, in general, increase
performance. */
mid = lo + (size / 2) * width; /* find middle element */
swap(mid, lo, width); /* swap it to beginning of array */
/* We now wish to partition the array into three pieces, one
consisiting of elements <= partition element, one of elements
equal to the parition element, and one of element >= to it. This
is done below; comments indicate conditions established at every
step. */
loguy = lo;
higuy = hi + width;
/* Note that higuy decreases and loguy increases on every iteration,
so loop must terminate. */
for (;;) {
/* lo <= loguy < hi, lo < higuy <= hi + 1,
A[i] <= A[lo] for lo <= i <= loguy,
A[i] >= A[lo] for higuy <= i <= hi */
do {
loguy += width;
} while (loguy <= hi && comp(loguy, lo) <= 0);
/* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
either loguy > hi or A[loguy] > A[lo] */
do {
higuy -= width;
} while (higuy > lo && comp(higuy, lo) >= 0);
/* lo-1 <= higuy <= hi, A[i] >= A[lo] for higuy < i <= hi,
either higuy <= lo or A[higuy] < A[lo] */
if (higuy < loguy)
break;
/* if loguy > hi or higuy <= lo, then we would have exited, so
A[loguy] > A[lo], A[higuy] < A[lo],
loguy < hi, highy > lo */
swap(loguy, higuy, width);
/* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top
of loop is re-established */
}
/* A[i] >= A[lo] for higuy < i <= hi,
A[i] <= A[lo] for lo <= i < loguy,
higuy < loguy, lo <= higuy <= hi
implying:
A[i] >= A[lo] for loguy <= i <= hi,
A[i] <= A[lo] for lo <= i <= higuy,
A[i] = A[lo] for higuy < i < loguy */
swap(lo, higuy, width); /* put partition element in place */
/* OK, now we have the following:
A[i] >= A[higuy] for loguy <= i <= hi,
A[i] <= A[higuy] for lo <= i < higuy
A[i] = A[lo] for higuy <= i < loguy */
/* We've finished the partition, now we want to sort the subarrays
[lo, higuy-1] and [loguy, hi].
We do the smaller one first to minimize stack usage.
We only sort arrays of length 2 or more.*/
if ( higuy - 1 - lo >= hi - loguy ) {
if (lo + width < higuy) {
lostk[stkptr] = lo;
histk[stkptr] = higuy - width;
++stkptr;
} /* save big recursion for later */
if (loguy < hi) {
lo = loguy;
goto recurse; /* do small recursion */
}
}
else {
if (loguy < hi) {
lostk[stkptr] = loguy;
histk[stkptr] = hi;
++stkptr; /* save big recursion for later */
}
if (lo + width < higuy) {
hi = higuy - width;
goto recurse; /* do small recursion */
}
}
}
/* We have sorted the array, except for any pending sorts on the stack.
Check if there are any, and do them. */
--stkptr;
if (stkptr >= 0) {
lo = lostk[stkptr];
hi = histk[stkptr];
goto recurse; /* pop subarray from stack */
}
else
return; /* all subarrays done */
}
/***
*shortsort(hi, lo, width, comp) - insertion sort for sorting short arrays
*
*Purpose:
* sorts the sub-array of elements between lo and hi (inclusive)
* side effects: sorts in place
* assumes that lo < hi
*
*Entry:
* char *lo = pointer to low element to sort
* char *hi = pointer to high element to sort
* unsigned width = width in bytes of each array element
* int (*comp)() = pointer to function returning analog of strcmp for
* strings, but supplied by user for comparing the array elements.
* it accepts 2 pointers to elements and returns neg if 1<2, 0 if
* 1=2, pos if 1>2.
*
*Exit:
* returns void
*
*Exceptions:
*
*******************************************************************************/
static void __cdecl shortsort (
char *lo,
char *hi,
unsigned width,
int (__cdecl *comp)(const void *, const void *)
)
{
char *p, *max;
/* Note: in assertions below, i and j are alway inside original bound of
array to sort. */
while (hi > lo) {
/* A[i] <= A[j] for i <= j, j > hi */
max = lo;
for (p = lo+width; p <= hi; p += width) {
/* A[i] <= A[max] for lo <= i < p */
if (comp(p, max) > 0) {
max = p;
}
/* A[i] <= A[max] for lo <= i <= p */
}
/* A[i] <= A[max] for lo <= i <= hi */
swap(max, hi, width);
/* A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, j >= hi */
hi -= width;
/* A[i] <= A[j] for i <= j, j > hi, loop top condition established */
}
/* A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] for i < j,
so array is sorted */
}
/***
*swap(a, b, width) - swap two elements
*
*Purpose:
* swaps the two array elements of size width
*
*Entry:
* char *a, *b = pointer to two elements to swap
* unsigned width = width in bytes of each array element
*
*Exit:
* returns void
*
*Exceptions:
*
*******************************************************************************/
static void __cdecl swap (
char *a,
char *b,
unsigned width
)
{
char tmp;
if ( a != b )
/* Do the swap one character at a time to avoid potential alignment
problems. */
while ( width-- ) {
tmp = *a;
*a++ = *b;
*b++ = tmp;
}
}