From d06170f697bed566b823a0a300b41678773ce954 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 6 Jun 2025 08:09:28 +0200 Subject: lib: switch to PtrArray for TurnOrder storage --- lib/src/turns-turn-order.c | 44 ++++++++++++++++++++------------------------ 1 file changed, 20 insertions(+), 24 deletions(-) (limited to 'lib') diff --git a/lib/src/turns-turn-order.c b/lib/src/turns-turn-order.c index b192d74..c90e958 100644 --- a/lib/src/turns-turn-order.c +++ b/lib/src/turns-turn-order.c @@ -28,7 +28,7 @@ struct _TurnsTurnOrder GObject parent_instance; gsize active; - GSList * participants; + GPtrArray * participants; gboolean running; TurnsTurnOrderSortMode sort_mode; }; @@ -72,7 +72,7 @@ static void sort_participants(TurnsTurnOrder * self) { auto const sort_mode = turns_turn_order_get_sort_mode(self); - self->participants = g_slist_sort_with_data(self->participants, &compare_participant, (void *)sort_mode); + g_ptr_array_sort_values_with_data(self->participants, &compare_participant, (gpointer)sort_mode); auto const participant_count = turns_turn_order_get_participant_count(self); @@ -83,8 +83,8 @@ static void clear_participants(TurnsTurnOrder * self) { g_return_if_fail(TURNS_IS_TURN_ORDER(self)); - g_slist_free_full(self->participants, g_object_unref); - self->participants = nullptr; + g_ptr_array_unref(self->participants); + self->participants = g_ptr_array_new_full(5, g_object_unref); g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]); } @@ -150,7 +150,7 @@ static void turns_turn_order_set_property(GObject * self, guint id, GValue const static void turns_turn_order_init(TurnsTurnOrder * self) { self->active = 0; - self->participants = nullptr; + self->participants = g_ptr_array_new_full(5, g_object_unref); self->running = false; self->sort_mode = TURNS_TURN_ORDER_SORT_MODE_DESCENDING; } @@ -159,7 +159,7 @@ static void turns_turn_order_finalize(GObject * self) { auto instance = TURNS_TURN_ORDER(self); - clear_participants(instance); + g_ptr_array_unref(instance->participants); G_OBJECT_CLASS(turns_turn_order_parent_class)->finalize(self); } @@ -213,7 +213,7 @@ static void turns_turn_order_class_init(TurnsTurnOrderClass * klass) static gpointer turns_turn_order_list_model_get_item(GListModel * self, guint position) { g_return_val_if_fail(position < turns_turn_order_get_participant_count(TURNS_TURN_ORDER(self)), nullptr); - return g_object_ref(g_slist_nth_data(TURNS_TURN_ORDER(self)->participants, position)); + return g_object_ref(TURNS_TURN_ORDER(self)->participants->pdata[position]); } static guint turns_turn_order_list_model_get_n_items(GListModel * self) @@ -245,14 +245,15 @@ void turns_turn_order_add(TurnsTurnOrder * self, TurnsParticipant * participant) g_signal_connect(participant, "notify::priority", (GCallback)&handle_participant_property_changed, self); - auto sort_mode = turns_turn_order_get_sort_mode(self); + auto was_empty = self->participants->len == 0; - auto old_head = self->participants; - self->participants = g_slist_insert_sorted_with_data(self->participants, g_object_ref(participant), compare_participant, (void *)sort_mode); + g_ptr_array_add(self->participants, g_object_ref(participant)); + sort_participants(self); - auto position = g_slist_index(self->participants, participant); + auto position = 0u; + g_ptr_array_find(self->participants, participant, &position); - if (old_head == nullptr && !turns_turn_order_get_empty(self)) + if (was_empty && !turns_turn_order_get_empty(self)) { g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]); } @@ -279,19 +280,14 @@ void turns_turn_order_clear(TurnsTurnOrder * self) void turns_turn_order_remove_at(TurnsTurnOrder * self, guint position) { g_return_if_fail(TURNS_IS_TURN_ORDER(self)); + g_return_if_fail(position < turns_turn_order_get_participant_count(self)); - auto element = g_slist_nth(self->participants, position); - - if (!element) - { - return; - } + auto element = TURNS_PARTICIPANT(self->participants->pdata[position]); + g_return_if_fail(TURNS_IS_PARTICIPANT(element)); - g_object_unref(element->data); - auto old_head = self->participants; - self->participants = g_slist_delete_link(self->participants, element); + g_ptr_array_remove_index(self->participants, position); - if (old_head != nullptr && turns_turn_order_get_empty(self)) + if (turns_turn_order_get_empty(self)) { g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_EMPTY]); set_running(self, false); @@ -303,13 +299,13 @@ void turns_turn_order_remove_at(TurnsTurnOrder * self, guint position) gboolean turns_turn_order_get_empty(TurnsTurnOrder const * self) { g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), true); - return self->participants == nullptr; + return self->participants->len == 0; } gsize turns_turn_order_get_participant_count(TurnsTurnOrder const * self) { g_return_val_if_fail(TURNS_IS_TURN_ORDER((TurnsTurnOrder *)self), 0); - return g_slist_length(self->participants); + return self->participants->len; } gboolean turns_turn_order_get_running(TurnsTurnOrder const * self) -- cgit v1.2.3