sequence: add g_sequence_is_empty()

This function provides an O(1) check to determine if a sequence is empty.
Compare this to the two following alternatives to perform the same check.

O(h):  if (0 == g_sequence_get_length (seq))
O(2h): if (g_sequence_get_begin_iter(seq) == g_sequence_get_end_iter(seq))

Where `h' is the height of the tree.

https://bugzilla.gnome.org/show_bug.cgi?id=756316
This commit is contained in:
Christian Hergert
2015-10-15 12:54:09 -07:00
parent 0b84596f6e
commit 8fccf8e4e3
3 changed files with 48 additions and 0 deletions

View File

@@ -1243,6 +1243,26 @@ g_sequence_get_length (GSequence *seq)
return node_get_length (seq->end_node) - 1;
}
/**
* g_sequence_is_empty:
* @seq: a #GSequence
*
* Returns %TRUE if the sequence contains zero items.
*
* This function is functionally identical to checking the result of
* g_sequence_get_length() being equal to zero. However this function is
* implemented in O(1) running time.
*
* Returns: %TRUE if the sequence is empty, otherwise %FALSE.
*
* Since: 2.48
*/
gboolean
g_sequence_is_empty (GSequence *seq)
{
return (seq->end_node->parent == NULL) && (seq->end_node->left == NULL);
}
/**
* g_sequence_get_end_iter:
* @seq: a #GSequence