| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This chapter describes functions for creating and manipulating
combinations. A combination
is represented by an array of
integers in the range 0 to
, where each value
occurs at most once. The combination
corresponds to
indices of
elements chosen from an
element vector.
Combinations are useful for iterating over all
-element subsets
of a set.
The functions described in this chapter are defined in the header file ‘gsl_combination.h’.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
A combination is defined by a structure containing three components, the
values of
and
, and a pointer to the combination array.
The elements of the combination array are all of type size_t, and
are stored in increasing order. The gsl_combination structure
looks like this,
typedef struct
{
size_t n;
size_t k;
size_t *data;
} gsl_combination;
|
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function allocates memory for a new combination with parameters
n, k. The combination is not initialized and its elements
are undefined. Use the function gsl_combination_calloc if you
want to create a combination which is initialized to the
lexicographically first combination. A null pointer is returned if
insufficient memory is available to create the combination.
This function allocates memory for a new combination with parameters n, k and initializes it to the lexicographically first combination. A null pointer is returned if insufficient memory is available to create the combination.
This function initializes the combination c to the
lexicographically first combination, i.e.
.
This function initializes the combination c to the
lexicographically last combination, i.e.
.
This function frees all the memory used by the combination c.
This function copies the elements of the combination src into the combination dest. The two combinations must have the same size.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The following function can be used to access the elements of a combination.
This function returns the value of the i-th element of the
combination c. If i lies outside the allowed range of 0 to
then the error handler is invoked and 0 is returned.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function returns the range (
) of the combination c.
This function returns the number of elements (
) in the combination c.
This function returns a pointer to the array of elements in the combination c.
This function checks that the combination c is valid. The k
elements should lie in the range 0 to
, with each
value occurring once at most and in increasing order.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This function advances the combination c to the next combination
in lexicographic order and returns GSL_SUCCESS. If no further
combinations are available it returns GSL_FAILURE and leaves
c unmodified. Starting with the first combination and
repeatedly applying this function will iterate through all possible
combinations of a given order.
This function steps backwards from the combination c to the
previous combination in lexicographic order, returning
GSL_SUCCESS. If no previous combination is available it returns
GSL_FAILURE and leaves c unmodified.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The library provides functions for reading and writing combinations to a file as binary data or formatted text.
This function writes the elements of the combination c to the
stream stream in binary format. The function returns
GSL_EFAILED if there was a problem writing to the file. Since the
data is written in the native binary format it may not be portable
between different architectures.
This function reads elements from the open stream stream into the
combination c in binary format. The combination c must be
preallocated with correct values of
and
since the
function uses the size of c to determine how many bytes to read.
The function returns GSL_EFAILED if there was a problem reading
from the file. The data is assumed to have been written in the native
binary format on the same architecture.
This function writes the elements of the combination c
line-by-line to the stream stream using the format specifier
format, which should be suitable for a type of size_t. On a
GNU system the type modifier Z represents size_t, so
"%Zu\n" is a suitable format. The function returns
GSL_EFAILED if there was a problem writing to the file.
This function reads formatted data from the stream stream into the
combination c. The combination c must be preallocated with
correct values of
and
since the function uses the size of c to
determine how many numbers to read. The function returns
GSL_EFAILED if there was a problem reading from the file.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The example program below prints all subsets of the set
ordered by size. Subsets of the same size are
ordered lexicographically.
|
Here is the output from the program,
$ ./a.out
|
All 16 subsets are generated, and the subsets of each size are sorted lexicographically.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
Further information on combinations can be found in,
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Autobuild on September, 26 2007 using texi2html 1.78.