Chapter Index | Slide Index | Frames [ Windows ] | Narrative On | <- prev | next -> |

Testing Generic Sort and Binary Search

/*  fgss.cpp   "generic sort & search"

    functionality test of generic sort and generic binary search
    on TVector<T>, TDeque<T>, and arrays of T
*/

typedef char                        ElementType; const char* e_t = "char";

typedef LessThan <ElementType>    PredicateType1;
typedef GreaterThan <ElementType> PredicateType2;

Vector    < ElementType > V;  // a vector
Deque     < ElementType > Q;  // a deque
ElementType *             A;  // an array
PredicateType1            LT; // predicate object
PredicateType2            GT; // predicate object

Vector    < ElementType > ::Iterator Vitr, Vloc;
Deque     < ElementType > ::Iterator Qitr, Qloc;
ElementType                         * Aitr, * Aloc;

ElementType e, s; // entry, sentinal
unsigned int i, size;
// assign values to V, then copy to Q and A
// ...

// apply generic sort to each container
g_selection_sort(V.Begin(), V.End(), GT);
g_selection_sort(Q.Begin(), Q.End(), GT);
g_selection_sort(A, A + size, GT);
// ...

// apply generic sort again to each container
g_selection_sort(V.Begin(), V.End());
g_selection_sort(Q.Begin(), Q.End());
g_selection_sort(A, A + size, LT);
// ...

// now do binary search
Vloc = g_lower_bound(V.Begin(), V.End(), e, LT);
// ...
Vloc = g_upper_bound(V.Begin(), V.End(), e, LT);
// ...
Qloc = g_lower_bound(Q.Begin(), Q.End(), e, LT);
// ...
Qloc = g_upper_bound(Q.Begin(), Q.End(), e, LT);
// ...
Aloc = g_lower_bound(A, A + size, e, LT);
// ...
Aloc = g_upper_bound(A, A + size, e, LT);
// ...
if (!g_binary_search(V.Begin(), V.End(), e, LT))
  // ...
if (!g_binary_search(Q.Begin(), Q.End(), e, LT))
  // ...
if (!g_binary_search(A, A + size, e, LT))
  // ...

<- prev | next -> | Top of Page | 8. Generic Algorithms - 11 of 12