[json-glib] object: Use a GQueue for members_ordered
- From: Garrett Regier <gregier src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib] object: Use a GQueue for members_ordered
- Date: Mon, 10 Jul 2017 14:18:43 +0000 (UTC)
commit 7b3c405df41c0b6f6fe58fdb05c922614f08c4c4
Author: Garrett Regier <garrettregier gmail com>
Date: Thu Oct 20 19:42:56 2016 -0700
object: Use a GQueue for members_ordered
This makes the list always ordered
and removes the g_list_reverse()
in json_object_get_members().
https://bugzilla.gnome.org/show_bug.cgi?id=773504
json-glib/json-object.c | 27 ++++++++++-----------------
json-glib/json-types-private.h | 3 +--
2 files changed, 11 insertions(+), 19 deletions(-)
---
diff --git a/json-glib/json-object.c b/json-glib/json-object.c
index be458ed..b6c08a5 100644
--- a/json-glib/json-object.c
+++ b/json-glib/json-object.c
@@ -67,7 +67,7 @@ json_object_new (void)
object->members = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free,
(GDestroyNotify) json_node_unref);
- object->members_ordered = NULL;
+ g_queue_init (&object->members_ordered);
return object;
}
@@ -108,9 +108,8 @@ json_object_unref (JsonObject *object)
if (--object->ref_count == 0)
{
- g_list_free (object->members_ordered);
+ g_queue_clear (&object->members_ordered);
g_hash_table_destroy (object->members);
- object->members_ordered = NULL;
object->members = NULL;
g_slice_free (JsonObject, object);
@@ -177,7 +176,7 @@ object_set_member_internal (JsonObject *object,
gchar *name = g_strdup (member_name);
if (g_hash_table_lookup (object->members, name) == NULL)
- object->members_ordered = g_list_prepend (object->members_ordered, name);
+ g_queue_push_tail (&object->members_ordered, name);
else
{
GList *l;
@@ -186,7 +185,7 @@ object_set_member_internal (JsonObject *object,
* pointer to its name, to avoid keeping invalid pointers
* once we replace the key in the hash table
*/
- l = g_list_find_custom (object->members_ordered, name, (GCompareFunc) strcmp);
+ l = g_queue_find_custom (&object->members_ordered, name, (GCompareFunc) strcmp);
if (l != NULL)
l->data = name;
}
@@ -483,13 +482,9 @@ json_object_set_object_member (JsonObject *object,
GList *
json_object_get_members (JsonObject *object)
{
- GList *copy;
-
g_return_val_if_fail (object != NULL, NULL);
- copy = g_list_copy (object->members_ordered);
-
- return g_list_reverse (copy);
+ return g_list_copy (object->members_ordered.head);
}
/**
@@ -512,7 +507,7 @@ json_object_get_values (JsonObject *object)
g_return_val_if_fail (object != NULL, NULL);
values = NULL;
- for (l = object->members_ordered; l != NULL; l = l->next)
+ for (l = object->members_ordered.tail; l != NULL; l = l->prev)
values = g_list_prepend (values, g_hash_table_lookup (object->members, l->data));
return values;
@@ -855,13 +850,13 @@ json_object_remove_member (JsonObject *object,
g_return_if_fail (object != NULL);
g_return_if_fail (member_name != NULL);
- for (l = object->members_ordered; l != NULL; l = l->next)
+ for (l = object->members_ordered.head; l != NULL; l = l->next)
{
const gchar *name = l->data;
if (g_strcmp0 (name, member_name) == 0)
{
- object->members_ordered = g_list_delete_link (object->members_ordered, l);
+ g_queue_delete_link (&object->members_ordered, l);
break;
}
}
@@ -889,14 +884,12 @@ json_object_foreach_member (JsonObject *object,
JsonObjectForeach func,
gpointer data)
{
- GList *members, *l;
+ GList *l;
g_return_if_fail (object != NULL);
g_return_if_fail (func != NULL);
- /* the list is stored in reverse order to have constant time additions */
- members = g_list_last (object->members_ordered);
- for (l = members; l != NULL; l = l->prev)
+ for (l = object->members_ordered.head; l != NULL; l = l->next)
{
const gchar *member_name = l->data;
JsonNode *member_node = g_hash_table_lookup (object->members, member_name);
diff --git a/json-glib/json-types-private.h b/json-glib/json-types-private.h
index db5dea6..fc7646b 100644
--- a/json-glib/json-types-private.h
+++ b/json-glib/json-types-private.h
@@ -102,8 +102,7 @@ struct _JsonObject
{
GHashTable *members;
- /* the members of the object, ordered in reverse */
- GList *members_ordered;
+ GQueue members_ordered;
guint immutable_hash; /* valid iff immutable */
volatile gint ref_count;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]