Predicates

Predicates — common predicate functions that map some value to TRUE or FALSE.

Functions

gboolean (*GXPred) ()
gboolean gx_is_even ()
gboolean gx_is_odd ()
gboolean gx_is_prime ()
gboolean gx_is_str_equal ()

Description

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

For example, we can get a list of the prime numbers up to 100 by taking an list of 1..100, then filter out all elements that are not prime.

1
2
3
4
5
6
7
8
9
GList *lst, *cur;

lst = gx_list_filter_in_place (
  gx_list_iota (100, 1, 1), (GXPred)gx_is_prime, NULL, NULL);

for (cur = lst; cur; cur = g_list_next (cur))
   g_print ("%d\n", GPOINTER_TO_INT(cur->data));

g_list_free (lst);

Functions

GXPred ()

gboolean
(*GXPred) (gconstpointer data,
           gconstpointer user_data);

Prototype for a predicate function that takes a pointer and some user-provided data, and returns either TRUE or FALSE.

It can be used with gx_list_filter() and gx_list_filter_in_place().

Parameters

data

a data pointer

 

user_data

a user data pointer

 

gx_is_even ()

gboolean
gx_is_even (gint i);

Predicate function that returns TRUE if i is an even number, FALSE otherwise. An even number is a number that is divisible by 2.

1
2
g_assert_cmpuint (gx_is_even (2),==, TRUE);
g_assert_cmpuint (gx_is_even (3),==, FALSE);

Parameters

i

an integer

 

Returns

TRUE if i is even, FALSE otherwise.


gx_is_odd ()

gboolean
gx_is_odd (gint i);

Predicate function that returns TRUE if i is an odd number, FALSE otherwise. An odd number is a number that is not divisible by 2.

1
2
g_assert_cmpuint (gx_is_odd (2),==, FALSE);
g_assert_cmpuint (gx_is_odd (3),==, TRUE);

Parameters

i

an integer

 

Returns

TRUE if i is odd, FALSE otherwise.


gx_is_prime ()

gboolean
gx_is_prime (gint i);

Predicate function that returns TRUE if i is a prime number, FALSE otherwise. A prime number is a positive number that is only divisible by itself and 1.

1
2
g_assert_cmpuint (gx_is_prime (13),==, TRUE);
g_assert_cmpuint (gx_is_prime (52),==, FALSE);

Parameters

i

an integer

 

Returns

TRUE if i is prime; FALSE otherwise.


gx_is_str_equal ()

gboolean
gx_is_str_equal (const char *s1,
                 const char *s2);

Predicate function that returns TRUE if the strings are equal; FALSE otherwise. Safe for NULL strings.

1
2
g_assert_true (gx_is_str_equal ("foo", "foo"));
g_assert_false (gx_is_str_equal ("foo", "bar"));

Parameters

s1

a string

 

s2

another string

 

Returns

TRUE if s1 and s2 are equal, FALSE otherwise.

Types and Values