|
// CollectionSorter.h: interface for the CCollectionSorter class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_COLLECTIONSORTER_H__05FCF147_A48C_4E76_B34F_6D4301B53748__INCLUDED_)
#define AFX_COLLECTIONSORTER_H__05FCF147_A48C_4E76_B34F_6D4301B53748__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <afxtempl.h>
#include "IComparer.h"
#include "DefaultComparer.h"
#include "DefaultPtrComparer.h"
#include "ICollectionAccessor.h"
#include "ListAccessor.h"
#include "ArrayAccessor.h"
template<class TYPE, class ARG_TYPE>
class CCollectionSorter
{
public:
CCollectionSorter() {};
virtual ~CCollectionSorter() {};
void SortCollection(CList<TYPE, ARG_TYPE> &list, IComparer<ARG_TYPE> &cmp = CDefaultComparer<ARG_TYPE>())
{
CListAccessor<TYPE, ARG_TYPE> la(&list);
Sort(0, la.GetCount() - 1, la, cmp);
}
void SortCollection(CArray<TYPE, ARG_TYPE> &array, IComparer<ARG_TYPE> &cmp = CDefaultComparer<ARG_TYPE>())
{
CArrayAccessor<TYPE, ARG_TYPE> aa(&array);
Sort(0, aa.GetCount() - 1, aa, cmp);
}
void SortPointerCollection(CList<TYPE, ARG_TYPE> &list, IComparer<ARG_TYPE> &cmp = CDefaultPtrComparer<ARG_TYPE>())
{
CListAccessor<TYPE, ARG_TYPE> la(&list);
Sort(0, la.GetCount() - 1, la, cmp);
}
void SortPointerCollection(CArray<TYPE, ARG_TYPE> &array, IComparer<ARG_TYPE> &cmp = CDefaultPtrComparer<ARG_TYPE>())
{
CArrayAccessor<TYPE, ARG_TYPE> aa(&array);
Sort(0, aa.GetCount() - 1, aa, cmp);
}
void SortCollection(ICollectionAccessor<ARG_TYPE> &ca, IComparer<ARG_TYPE> &cmp = CDefaultComparer<ARG_TYPE>())
{
Sort(0, ca.GetCount() - 1, ca, cmp);
}
protected:
void Sort(long il, long ih, ICollectionAccessor<ARG_TYPE> &ca, IComparer<ARG_TYPE> &cmp)
{
long Lo = il;
long Hi = ih;
ARG_TYPE Mid = ca.GetAt((Lo + Hi) / 2);
do
{
bool stop = false;
cmp.SetB(Mid);
// comma operator - last expression sued as test for while loop.
while (cmp.SetA(ca.GetAt(Lo)),
cmp.IsBBigger())
{
Lo++;
}
while (cmp.SetA(ca.GetAt(Hi)),
cmp.IsABigger())
{
Hi--;
}
if (Lo <= Hi)
{
// swap elemtns:
ca.Swap(Lo, Hi);
Lo++;
Hi--;
}
} while (Lo <= Hi);
if (Hi > il) Sort(il, Hi, ca, cmp);
if (Lo < ih) Sort(Lo, ih, ca, cmp);
return;
}
};
#endif // !defined(AFX_COLLECTIONSORTER_H__05FCF147_A48C_4E76_B34F_6D4301B53748__INCLUDED_)
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Andrew is currently a student at the University of Cambridge, working towards a degree in Computer Science. The word 'working' is used here in a vague sense, with the hope that the reader will realise that the same sentence contained the word 'student'.
Aside from computing he has a strong interest in music, and enjoys the outdoors, particularly when the weather permits going out in them... To Andrew, cycling is fun, sailing is more fun, and the odd camping trip and walk is certainly what the doctor ordered.
In terms of programming experience, he first started writing programs for the Commodore Amiga using BASIC, after which he learned the joys of C and has never looked back. Since, he has added C++ and C# to his repotoire, along with a bunch of other crazy languages like ML that they like to teach in college.