CSTRINGARRAY sort

zhaozj2021-02-16  43

CStringArray is a container class for MS VC , we write a sort function for it.

Function declaration:

Void Sort (CSTRINGARRAY & CA, // Sort Object

Bool ascending, // true = ascending, false = descending order;

Bool caasensitive; // true = difference case, false = ignore the case

We use C and C methods to achieve sort, first look at C method.

================= C is ============================== =======

We use the QSort function of the C standard library. Qsort's statement is like this:

Void Qsort (void * base, size_t num, size_t width, int (__ cdecl * compare) (Const Void * Elem2);

Where parameters:

Base = ca.getdata ();

Num = ca.getsize ();

Width = sizeof (cstring);

The fourth parameter is a pointer to the "Compare" function. This "comparison" function must be provided by us, but the problem is that the "Compare" function can only be connected.

By two parameters, we can't pass Ascending and CaseSensitive to it, that is, we need 4 "comparison" letters.

Number. The following is the statement of these four "comparison" functions:

INT COMPARE00 (Const Void * Elem1);

INT COMPARE01 (Const Void * Elem2);

INT COMPARE10 (Const Void * Elem1, Const Void * ELEM2);

INT COMPARE11 (Const Void * Elem1);

The number behind Compare corresponds to the value of Ascending and CaseSensitive, such as the Compare10 table.

Sign from Ascending = true, caasensitive = false.

Let's see the implementation of Sort:

Void Sort (CStringArray & Ca, Bool Ascending, Bool CaseSensitive)

{

INT f = 0;

IF (ascending)

f = 2;

CaseSensitive

f | = 1;

Switch (f)

{

Case 0x00:

Qsort (ca.getdata (), ca.getsize (), sizeof (cstring), compare00);

Break;

Case 0x01:

Qsort (ca.getdata (), ca.getsize (), sizeof (cstring), compare01);

Break;

Case 0x10:

Qsort (ca.getdata (), ca.getsize (), sizeof (cstring), compare10;

Break;

Case 0x11:

Qsort (ca.getdata (), ca.getsize (), sizeof (cstring), compare11;

Break;

}

}

4 "Compare" functions are also easy to implement, for example:

INT COMPARE00 (Const Void * Elem1)

{

Return ((cstring *) ELEM1) -> Comparenocase (* ((cstring *) ELEM2) * - 1);

}

================= c implementation =============================== =======

We use C STL's Sort Function Template:

Template

Void Sort (Ranit First, Ranit Last, PRED PR)

Where parameters:

FigSt = ca.getdata ();

Last = ca.getdata () ca.getsize ();

The third parameter is a "Function Object", we don't imagine 4 "comparison" functions as the C method, but design a "comparison" class:

Class CstringCompare

{

PUBLIC:

CStringCompare (Bool Ascending, Bool CaseSensitive)

{

CaseSensitive

M_PCompFunc = CSTRING :: Compare;

Else

m_pcompfunc = cstring :: Comparenocase;

m_asc = ascending;

}

Bool Operator () (Const CSTRING & CS1, Const Cstring & CS2) Const

{

RETURN (M_ASC? (CS1. * m_pcompfunc) (CS2) <0: ((CS1. * m_pcompfunc) (CS2)> = 0));

}

protected:

Typedef int (cstring :: * comparememberfunc) (const tchar *) const;

Comparememberfunc m_pcompfunc;

BOOL M_ASC;

}

SORT C implementation:

Void Sort (CStringArray & Ca, Bool Ascending, Bool CaseSensitive)

{

CStringCompare CP (Ascending, CaseSensitive);

Sort (ca.getdata (), ca.getdata () ca.getsize (), CP);

}

======================================================================================================================================================================================================================================== =========

Speed ​​testing, it will find that the implementation of C is slower than the implementation of C, because there is a lot of generated during C .

Temporary CString. Let's improve:

Class CstringCompare

{

PUBLIC:

Typedef struct {char data [sizeof (cstring)];

CStringCompare (CStringArray & Ca, Bool Ascending, Bool CaseSensitive: M_CA (CA)

{

CaseSensitive

M_PCompFunc = CSTRING :: Compare;

Else

m_pcompfunc = cstring :: Comparenocase;

m_asc = ascending;

}

Bool Operator () (Const CSTRING & CS1, Const Cstring & CS2) Const

{

const cstring * pcs1 = reinterpret_cast

(& CS1);

Const CString * PCS2 = Reinterpret_cast

(& CS2);

RETURN (M_ASC? (PCS1 -> * m_pcompfunc) (* PCS2) <0: ((PCS1 -> * m_pcompfunc) (* PCS2)> = 0));

}

CString * Begin ()

{

Return (ReinterPret_cast

(m_ca.getdata ()));

}

CString * end ()

{

Return (ReinterPret_cast

(m_ca.getdata () m_ca.getsize ()));

}

protected:

Typedef int (cstring :: * comparememberfunc) (const tchar *) const;

Comparememberfunc m_pcompfunc;

BOOL M_ASC;

CSTRINGARRAY & M_CA;

}

Void Sort (CStringArray & Ca, Bool Ascending, Bool CaseSensitive)

{

CSTRINGCompare CP (CA, Ascending, CaseSensitive);

sort (cp.begin (), cp.end (), cp);

}

Now the speed is almost, but is it safe to use with ReinterPret_CAST?

In numerous C compilers, MS VC is a good implementation, but it should be seen until the latest VC7,

All standard C (ISO98) is placed in a gray position, and many features are not implemented.

Some people say that it will use VC to understand C probably therefore.