gdkarray: Add a "stolen" boolean to splice()

If set to TRUE, does not call the free func for the removed items.

This can be used to move items between arrays without having to do the
refcounting dance.
This commit is contained in:
Benjamin Otte 2020-12-23 20:04:29 +01:00
parent 71e616d17e
commit 96e1b85c2c
8 changed files with 15 additions and 12 deletions

View File

@ -182,6 +182,7 @@ G_GNUC_UNUSED static inline void
gdk_array(splice) (GdkArray *self, gdk_array(splice) (GdkArray *self,
gsize pos, gsize pos,
gsize removed, gsize removed,
gboolean stolen,
_T_ *additions, _T_ *additions,
gsize added) gsize added)
{ {
@ -192,8 +193,9 @@ gdk_array(splice) (GdkArray *self,
g_assert (pos + removed <= size); g_assert (pos + removed <= size);
remaining = size - pos - removed; remaining = size - pos - removed;
gdk_array(free_elements) (gdk_array(index) (self, pos), if (!stolen)
gdk_array(index) (self, pos + removed)); gdk_array(free_elements) (gdk_array(index) (self, pos),
gdk_array(index) (self, pos + removed));
gdk_array(reserve) (self, size - removed + added); gdk_array(reserve) (self, size - removed + added);
@ -225,9 +227,9 @@ gdk_array(set_size) (GdkArray *self,
{ {
gsize old_size = gdk_array(get_size) (self); gsize old_size = gdk_array(get_size) (self);
if (new_size > old_size) if (new_size > old_size)
gdk_array(splice) (self, old_size, 0, NULL, new_size - old_size); gdk_array(splice) (self, old_size, 0, FALSE, NULL, new_size - old_size);
else else
gdk_array(splice) (self, new_size, old_size - new_size, NULL, 0); gdk_array(splice) (self, new_size, old_size - new_size, FALSE, NULL, 0);
} }
G_GNUC_UNUSED static void G_GNUC_UNUSED static void
@ -241,6 +243,7 @@ gdk_array(append) (GdkArray *self,
gdk_array(splice) (self, gdk_array(splice) (self,
gdk_array(get_size) (self), gdk_array(get_size) (self),
0, 0,
FALSE,
#ifdef GDK_ARRAY_BY_VALUE #ifdef GDK_ARRAY_BY_VALUE
value, value,
#else #else

View File

@ -100,7 +100,7 @@ gtk_accels_remove (GtkAccels *accels,
position = gtk_accels_find (accels, action_and_target); position = gtk_accels_find (accels, action_and_target);
if (position < gtk_accels_get_size (accels)) if (position < gtk_accels_get_size (accels))
gtk_accels_splice (accels, position, 1, NULL, 0); gtk_accels_splice (accels, position, 1, FALSE, NULL, 0);
} }
/*< private > /*< private >

View File

@ -167,7 +167,7 @@ gtk_css_selector_matches_insert_sorted (GtkCssSelectorMatches *matches,
break; break;
} }
gtk_css_selector_matches_splice (matches, i, 0, (gpointer[1]) { data }, 1); gtk_css_selector_matches_splice (matches, i, 0, FALSE, (gpointer[1]) { data }, 1);
} }
static inline gboolean static inline gboolean

View File

@ -206,7 +206,7 @@ gtk_multi_filter_remove (GtkMultiFilter *self,
filter = gtk_filters_get (&self->filters, position); filter = gtk_filters_get (&self->filters, position);
g_signal_handlers_disconnect_by_func (filter, gtk_multi_filter_changed_cb, self); g_signal_handlers_disconnect_by_func (filter, gtk_multi_filter_changed_cb, self);
gtk_filters_splice (&self->filters, position, 1, NULL, 0); gtk_filters_splice (&self->filters, position, 1, FALSE, NULL, 0);
gtk_filter_changed (GTK_FILTER (self), gtk_filter_changed (GTK_FILTER (self),
GTK_MULTI_FILTER_GET_CLASS (self)->removal_change); GTK_MULTI_FILTER_GET_CLASS (self)->removal_change);

View File

@ -432,7 +432,7 @@ gtk_multi_sorter_remove (GtkMultiSorter *self,
sorter = gtk_sorters_get (&self->sorters, position); sorter = gtk_sorters_get (&self->sorters, position);
g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self); g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
gtk_sorters_splice (&self->sorters, position, 1, NULL, 0); gtk_sorters_splice (&self->sorters, position, 1, FALSE, NULL, 0);
gtk_sorter_changed_with_keys (GTK_SORTER (self), gtk_sorter_changed_with_keys (GTK_SORTER (self),
GTK_SORTER_CHANGE_LESS_STRICT, GTK_SORTER_CHANGE_LESS_STRICT,

View File

@ -1386,7 +1386,7 @@ gtk_snapshot_pop_one (GtkSnapshot *snapshot)
/* Remove all the state's nodes from the list of nodes */ /* Remove all the state's nodes from the list of nodes */
g_assert (state->start_node_index + state->n_nodes == gtk_snapshot_nodes_get_size (&snapshot->nodes)); g_assert (state->start_node_index + state->n_nodes == gtk_snapshot_nodes_get_size (&snapshot->nodes));
gtk_snapshot_nodes_splice (&snapshot->nodes, state->start_node_index, state->n_nodes, NULL, 0); gtk_snapshot_nodes_splice (&snapshot->nodes, state->start_node_index, state->n_nodes, FALSE, NULL, 0);
} }
else else
{ {
@ -1400,7 +1400,7 @@ gtk_snapshot_pop_one (GtkSnapshot *snapshot)
g_assert (previous_state->start_node_index + previous_state->n_nodes == gtk_snapshot_nodes_get_size (&snapshot->nodes)); g_assert (previous_state->start_node_index + previous_state->n_nodes == gtk_snapshot_nodes_get_size (&snapshot->nodes));
} }
gtk_snapshot_states_splice (&snapshot->state_stack, state_index, 1, NULL, 0); gtk_snapshot_states_splice (&snapshot->state_stack, state_index, 1, FALSE, NULL, 0);
return node; return node;
} }

View File

@ -471,7 +471,7 @@ gtk_string_list_splice (GtkStringList *self,
else else
n_additions = 0; n_additions = 0;
objects_splice (&self->items, position, n_removals, NULL, n_additions); objects_splice (&self->items, position, n_removals, FALSE, NULL, n_additions);
for (i = 0; i < n_additions; i++) for (i = 0; i < n_additions; i++)
{ {

View File

@ -77,7 +77,7 @@ gdk_array(test_splice) (void)
for (j = 0; j < add; j++) for (j = 0; j < add; j++)
sum += ++additions[j]; sum += ++additions[j];
gdk_array(splice) (&v, pos, remove, additions, add); gdk_array(splice) (&v, pos, remove, FALSE, additions, add);
{ {
gsize total = 0; gsize total = 0;
for (j = 0; j < gdk_array(get_size) (&v); j++) for (j = 0; j < gdk_array(get_size) (&v); j++)