DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

Array_alg(C++)


Array_alg -- introduction to Array Algorithms

Synopsis

   #include <Array_alg.h>  
   namespace SCO_SC {
   

// Array Algorithms

template <class T> const T* bin_loc( const T& val, const T* b, const T* e ); template <class T> const T* bin_loc_r( int (*rel)(const T)* p1,const T)* p2), const T& val, const T* b, const T* e ); template <class T> const T* bin_search( const T& val, const T* b, const T* e ); template <class T> const T* bin_search_r( int (*rel)(const T)* p1,const T)* p2), const T& val, const T* b, const T* e );

// etc. for the remaining functions }

Description

Array Algorithms implement nearly 100 common (and not-so-common) algorithms on arrays. But because a Block can always be used wherever an array is called for (see Block(3C++)), Array Algorithms can also be used with Blocks. In fact, these two components were actually designed to be used together.

All Array Algorithms are declared in one header file, Array_alg.h and in namespace SCO_SC. Array Algorithms are an early ancestor of the C++ Standard Template Library.

User-defined function types

int (*relation)(const T* p1,const T* p2); A so-called relation. The intended semantics of relations varies from function to function. For example, some functions (like sort_r) require that a relation "defines a total ordering relation on T;" these expect the relation to return a negative, zero, or positive value, depending on whether the first argument is less than, equal to, or greater than the second argument, respectively (the C library function strcmp(3C) is an example of a such a function). Other functions (e.g., subs_r()) require only that a relation "defines an equivalence relation on T;" these expect the relation to return 0 if its two arguments are equal.

int (*predicate)(const T* p); A so-called predicate. Predicates return non-zero if the object pointed to by p passes a test, the nature of which varies from function to function.

void (*fun1)(T* p);

void (*fun2)(ptrdiff_t i,T* p); Used by functions that apply functions to the elements of an array (for_each(3C++), generate(3C++)).

Array Algorithms

   template <class T>
   const T* bin_loc(
       const T& val,
       const T* b,
       const T* e
   );
   template <class T>
   const T* bin_loc_r(
       int (*rel)(const T)* p1,const T)* p2),
       const T& val,
       const T* b,
       const T* e
   );
   template <class T>
   const T* bin_search(
       const T& val,
       const T* b,
       const T* e
   );
   template <class T>
   const T* bin_search_r(
       int (*rel)(const T)* p1,const T)* p2),
       const T& val,
       const T* b,
       const T* e
   );

etc.

Function names are constructed using a system of prefixes and suffixes. The prefix indicates broadly what the function does (e.g., sort, count, remove), while the suffix denotes one or more versions of the functionality. There are 32 prefixes. Since versions are documented together on the same manpage, there are 32 manpages, one for each prefix. Suffixes consist of an underscore followed by a sequence of between one and three letters. Suffixes have the following meaning:

The functions operate on one, two, or possibly three arrays. Each array is explicitly or implicitly identified by a pair of pointers that point to the beginning and end of the array, respectively. In writing the function prototypes, we have used the formal parameter names b and e when there is just one array, b1,e1 and b2,e2 when there are two arrays, and so-on. The first pointer of each pair points to the first cell of the array, while the second pointer points just beyond the last cell of the array. For example, random(3C++) takes a single array and returns a pointer to a random cell:
       template <class T>
       const T* random(const T* b,const T* e);
       //see random(3C++)

To call random(),

       Block<int> b(10);      // see Block(3C++)
       ... 
       const int* p = random(&b[0],b.end());

rem_dup_c() takes an input array and an output array; it copies unique elements to the output array, leaving the input array undisturbed:

       T rem_dup_c(T* b1,T* e1,T* b2);  // see rem_dup(3C++)

To call rem_dup_c(),

       Block<int> input(10);    // see Block(3C++)
       int output[10];
       ... 
       int* p = rem_dup_c(&input[0],input.end(),output);

Note that the second array is identified by a single pointer only (b2); its size must be large enough to hold all the unique elements (in the worst case, there would be ten unique elements). It is the programmer's responsibility to ensure that such assumptions are correct.

Some functions have parameters or return type of ptrdiff_t, a machine-dependent type defined in stddef.h. A ptrdiff_t is an integral type capable of representing the difference between any two pointers of the same type.

Bugs

Several functions (e.g., random(3C++) and shuffle(3C++)) make calls to the C library pseudo-random number generator, drand48(3C). To insure repeatable results, which may be desirable while programs are still under development, the client may initialize drand48(3C) by calling srand48() with a fixed "seed" (or may rely on default initialization). Doing this will cause drand48(3C) to yield an identical sequence of random numbers. Repeatability may be difficult (requiring multiple initializations) or impossible to obtain if random numbers are withdrawn from the sequence elsewhere in client code.

Example

Given an array containing at least one even element, the following code displays a random even element:

       main.c
   

#include <Array_alg.h>

int even(const int* a); const int SIZE = 100; int a[SIZE] = {...}; must contain at least one even element

main(){ cout << *random(a,part_ps(even,a,a+SIZE)); } int even(const int* a){ return (*a)%2 == 0; }

References

Block(C++), bin_loc(C++), bin_search(C++), copy(C++), count(C++), drand48(S), fill(C++), for_each(C++), generate(C++), ins_sort(C++), insert(C++), merge(C++), merge_sort(C++), minimum(C++), mismatch(C++), part(C++), pos(C++), random(C++), rem(C++), rem_dup(C++), reverse(C++), rotate(C++), rt_pos(C++), search(C++), select(C++), set_diff(C++), set_insert(C++), set_inter(C++), set_remove(C++), set_sdiff(C++), set_union(C++), shuffle(C++), sort(C++), strcmp(S), subs(C++), unique(C++)
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 - 01 June 2005