Binary and ternary functions

Binary and ternary functions — common functions that map 2 or 3 parameters to some value.

Functions

 gpointer (*GXBinaryFunc) () gconstpointer gx_identity () gpointer (*GXTernaryFunc) () gint gx_plus () gint gx_times () gint gx_max () gint gx_min ()

Description

These functions are designed as simple adapters for use with functions such as gx_list_fold() .

For example, let's calculate the product of 5, 10, 15, 20, 25.

 1 2 3 4 5 6 7 8 GList *lst; int sum; lst = gx_list_iota (5, 5, 5); // 5, 10, 15, 20, 25 sum = GPOINTER_TO_INT(gx_list_fold (lst, (GXTernaryFunc)gx_times, GINT_TO_POINTER(1), NULL, NULL)); g_assert_cmpint (sum,==, 375000); g_list_free (lst);

Obviously, for such a simple case, the gx_list_fold() solution looks a bit convoluted compared to an explicit loop.

Functions

GXBinaryFunc ()

gpointer
(*GXBinaryFunc) (gconstpointer ptr,
gconstpointer user_data);

Prototype for a binary function to be used in e.g. gx_list_map(), that takes some data-pointer and and an optional user-provided pointer, and returns a pointer as result.

Parameters

 ptr a pointer user_data a user-provided data pointer.

Returns

some pointer value for the result.

gx_identity ()

gconstpointer
gx_identity (gconstpointer ptr);

The identity function; maps a value to itself.

 1 2 3 const char *str; str = "FOO"; g_assert (gx_identity (str) == str);

Parameters

 ptr a pointer

Returns

the pointer ptr .

GXTernaryFunc ()

gpointer
(*GXTernaryFunc) (gconstpointer p1,
gconstpointer p2,
gconstpointer user_data);

Prototype for a ternary function to be used in e.g. gx_list_fold(), that takes two data-pointers and an optional user-provided pointer, and returns a pointer as result.

Parameters

 p1 first pointer argument p2 second pointer argument user_data a user-provided data pointer.

Returns

some pointer value for the result.

gx_plus ()

gint
gx_plus (gint i,
gint j);

Calculates the sum of i and j . This function is useful when composing it with other functions, such as gx_list_fold().

 1 g_assert_cmpint (gx_plus (3, 4),==, 3 + 4);

Parameters

 i an integer value j another integer value

i + j

gx_times ()

gint
gx_times (gint i,
gint j);

Calculates the product of i and j . This function is useful when composing it with other functions, such as gx_list_fold().

 1 g_assert_cmpint (gx_times (8, 7),==, 8 * 7);

Parameters

 i an integer value j another integer value

i ✕ j

gx_max ()

gint
gx_max (gint i,
gint j);

Get the greatest value of i and j . This function is useful when composing it with other functions, such as gx_list_fold().

 1 g_assert_cmpint (gx_max (100, 1000),==, MAX(100, 1000));

Parameters

 i an integer value j another integer value

Returns

the greatest of i and j .

gx_min ()

gint
gx_min (gint i,
gint j);

Get the smallest value of i and j . This function is useful when composing it with other functions, such as gx_list_fold().

 1 g_assert_cmpint (gx_min (123456, 54321),==, MIN(123456, 54321));

Parameters

 i an integer value j another integer value

Returns

the smallest of i and j .