Optimize list-based katze_array_ functions

This commit is contained in:
Christian Dywan 2012-02-10 23:01:07 +01:00
parent 9c323ff149
commit b6f86b0ca6

View file

@ -112,7 +112,7 @@ _katze_array_clear (KatzeArray* array)
GObject* item; GObject* item;
while ((item = g_list_nth_data (array->items, 0))) while ((item = g_list_nth_data (array->items, 0)))
katze_array_remove_item (array, item); g_signal_emit (array, signals[REMOVE_ITEM], 0, item);
g_list_free (array->items); g_list_free (array->items);
array->items = NULL; array->items = NULL;
} }
@ -217,14 +217,11 @@ katze_array_init (KatzeArray* array)
static void static void
katze_array_finalize (GObject* object) katze_array_finalize (GObject* object)
{ {
KatzeArray* array; KatzeArray* array = KATZE_ARRAY (object);
guint i; GList* items;
gpointer item;
array = KATZE_ARRAY (object); for (items = array->items; items; items = g_list_next (items))
i = 0; g_object_unref (items->data);
while ((item = g_list_nth_data (array->items, i++)))
g_object_unref (item);
g_list_free (array->items); g_list_free (array->items);
G_OBJECT_CLASS (katze_array_parent_class)->finalize (object); G_OBJECT_CLASS (katze_array_parent_class)->finalize (object);
@ -364,37 +361,39 @@ katze_array_get_item_index (KatzeArray* array,
/** /**
* katze_array_find_token: * katze_array_find_token:
* @array: a #KatzeArray * @array: a #KatzeArray
* @token: a token string * @token: a token string, or "token keywords" string
* *
* Looks up an item in the array which has the specified token. * Looks up an item in the array which has the specified token.
* *
* This function will silently fail if the type of the list * This function will fail if the type of the list
* is not based on #GObject and only #KatzeItem children * is not based on #KatzeItem children.
* are checked for their token, any other objects are skipped.
* *
* Note that @token is by definition unique to one item. * Note that @token is by definition unique to one item.
* *
* Since 0.4.4 @token can be a "token keywords" string.
*
* Return value: an item, or %NULL * Return value: an item, or %NULL
**/ **/
gpointer gpointer
katze_array_find_token (KatzeArray* array, katze_array_find_token (KatzeArray* array,
const gchar* token) const gchar* token)
{ {
guint i; goffset token_length;
gpointer item; GList* items;
g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL); g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);
g_return_val_if_fail (katze_array_is_a (array, KATZE_TYPE_ITEM), NULL);
g_return_val_if_fail (token != NULL, NULL);
i = 0; token_length = strchr (token, ' ') - token;
while ((item = g_list_nth_data (array->items, i++))) if (token_length < 1)
token_length = strlen (token);
for (items = array->items; items; items = g_list_next (items))
{ {
const gchar* found_token; const gchar* found_token = ((KatzeItem*)items->data)->token;
if (found_token != NULL && !strncmp (token, found_token, token_length))
if (!KATZE_IS_ITEM (item)) return items->data;
continue;
found_token = ((KatzeItem*)item)->token;
if (!g_strcmp0 (found_token, token))
return item;
} }
return NULL; return NULL;
} }
@ -406,9 +405,8 @@ katze_array_find_token (KatzeArray* array,
* *
* Looks up an item in the array which has the specified URI. * Looks up an item in the array which has the specified URI.
* *
* This function will silently fail if the type of the list * This function will fail if the type of the list
* is not based on #GObject and only #KatzeItem children * is not based on #KatzeItem children.
* are checked for their token, any other objects are skipped.
* *
* Return value: an item, or %NULL * Return value: an item, or %NULL
* *
@ -418,19 +416,17 @@ gpointer
katze_array_find_uri (KatzeArray* array, katze_array_find_uri (KatzeArray* array,
const gchar* uri) const gchar* uri)
{ {
guint i; GList* items;
gpointer item;
i = 0; g_return_val_if_fail (KATZE_IS_ARRAY (array), NULL);
while ((item = g_list_nth_data (array->items, i++))) g_return_val_if_fail (katze_array_is_a (array, KATZE_TYPE_ITEM), NULL);
g_return_val_if_fail (uri != NULL, NULL);
for (items = array->items; items; items = g_list_next (items))
{ {
const gchar* found_uri; const gchar* found_uri = ((KatzeItem*)items->data)->uri;
if (found_uri != NULL && !strcmp (found_uri, uri))
if (!KATZE_IS_ITEM (item)) return items->data;
continue;
found_uri = ((KatzeItem*)item)->uri;
if (!g_strcmp0 (found_uri, uri))
return item;
} }
return NULL; return NULL;
} }