Module array

Useful Array Macros.

These operate on llib arrays, supporting mapping, filtering and inline iteration. With all these macros, the dummy variable for accessing the current array value is _ (underscore). The map & filter macros all declare an array of the given type, and create a reference-counted array if the name ends in ‘R’.

See test-array.c.

Macros

FORA Iterate over array values.
FORAP Iterate over pointers to elements.
MAPA Declare an array which is [F(x)|x in A]
MAPAR Declare a reference array which is [F(x)|x in A]
FILTA New array containing matching elements from A.
FILTAR New reference array containing matching elements from A.
FILTAP Array of pointers to elements of array matching expression.
FINDA Index into array where expression matches value.
FINDAP Like FINDA but expression variable is pointer.
RFINDA Like FINDA but starts searching at end.
FINDZ Like FINDA but assumes array ends with NULL.


Macros

FORA
Iterate over array values. _ is A[i]
  • A the array (can be an expression)
  • expr the expression containing _

Usage:

    FORA(ints,printf("%i ",_));
FORAP
Iterate over pointers to elements. _ is A+i. Useful with arrays of structs to avoid copying, and when you need to modify the array
  • A the array
  • expr the expression containing _

Usage:

    FORAP(if(*_ < 0) *_ = 0);
MAPA
Declare an array which is [F(x)|x in A]
  • T the resulting type
  • v the new array name
  • F the expression containing _
  • A the source array

Usage:

        char** ss = str_strings("one","two","three",NULL);
        MAPA(int,ssl,strlen(_),ss);
        assert(ssl[0] == 3);
        assert(ssl[1] == 3);
        assert(ssl[2] == 5);
MAPAR
Declare a reference array which is [F(x)|x in A]
  • T the resulting type
  • v the new array name
  • expr the expression containing _
  • arr the source array

Usage:

    MAPAR(char*,strs,str_fmt("%d",_),ints);
FILTA
New array containing matching elements from A.
  • T array type
  • v the new array name
  • A the input array
  • expr the filter expression in _

Usage:

    FILTA(int,non_zero,ints,_ > 0);
FILTAR
New reference array containing matching elements from A.
  • T array type
  • v the new array name
  • A the input array
  • expr the filter expression in _

Usage:

FILTAP
Array of pointers to elements of array matching expression. Useful if you needed selected ‘views’ of an array of structs, without the need for copying them.
  • v the name of the new array
  • A the input array
  • expr the filter expresion in _
FINDA
Index into array where expression matches value. Declares a variable v, which will be -1 if no match is possible.
  • v new index
  • A the array
  • istart where to start searching (usually 0)
  • expr expression in _

Usage:

FINDAP
Like FINDA but expression variable is pointer. Declares a variable v, which will be -1 if no match is possible.
  • v new index
  • A the array
  • istart where to start searching (usually 0)
  • expr expression in _
RFINDA
Like FINDA but starts searching at end. Declares a variable v, which will be -1 if no match is possible.
  • v new index
  • A the array
  • istart where to start searching (-ve from end, +v is valid index)
  • expr expression in _
FINDZ
Like FINDA but assumes array ends with NULL. Declares a variable v, which will be -1 if no match is possible.
  • v new index
  • A the array
  • expr expression in _
generated by LDoc 1.4.3 Last updated 2015-04-27 12:35:55