mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-11-12 20:00:09 +00:00
Merge branch 'wip/otte/sortlistmodel2' into 'master'
Massively refactor and improve sortlistmodel See merge request GNOME/gtk!2273
This commit is contained in:
commit
63a4345d2c
@ -662,7 +662,6 @@ create_color_grid (void)
|
||||
{
|
||||
GtkWidget *gridview;
|
||||
GtkListItemFactory *factory;
|
||||
GListModel *model, *selection;
|
||||
|
||||
gridview = gtk_grid_view_new ();
|
||||
gtk_scrollable_set_hscroll_policy (GTK_SCROLLABLE (gridview), GTK_SCROLL_NATURAL);
|
||||
@ -676,13 +675,6 @@ create_color_grid (void)
|
||||
gtk_grid_view_set_max_columns (GTK_GRID_VIEW (gridview), 24);
|
||||
gtk_grid_view_set_enable_rubberband (GTK_GRID_VIEW (gridview), TRUE);
|
||||
|
||||
model = G_LIST_MODEL (gtk_sort_list_model_new (gtk_color_list_new (0), NULL));
|
||||
|
||||
selection = G_LIST_MODEL (gtk_multi_selection_new (model));
|
||||
gtk_grid_view_set_model (GTK_GRID_VIEW (gridview), selection);
|
||||
g_object_unref (selection);
|
||||
g_object_unref (model);
|
||||
|
||||
return gridview;
|
||||
}
|
||||
|
||||
@ -835,6 +827,22 @@ update_selection_average (GListModel *model,
|
||||
g_object_unref (color);
|
||||
}
|
||||
|
||||
static void
|
||||
update_progress_cb (GtkSortListModel *model,
|
||||
GParamSpec *pspec,
|
||||
GtkProgressBar *progress)
|
||||
{
|
||||
guint total;
|
||||
guint pending;
|
||||
|
||||
total = g_list_model_get_n_items (G_LIST_MODEL (model));
|
||||
total = MAX (total, 1); /* avoid div by 0 below */
|
||||
pending = gtk_sort_list_model_get_pending (model);
|
||||
|
||||
gtk_widget_set_visible (GTK_WIDGET (progress), pending != 0);
|
||||
gtk_progress_bar_set_fraction (progress, (total - pending) / (double) total);
|
||||
}
|
||||
|
||||
static GtkWidget *window = NULL;
|
||||
|
||||
GtkWidget *
|
||||
@ -842,10 +850,11 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
{
|
||||
if (window == NULL)
|
||||
{
|
||||
GtkWidget *header, *gridview, *sw, *box, *dropdown;
|
||||
GtkMultiSelection *selection;
|
||||
GtkSortListModel *sort_model;
|
||||
GtkWidget *header, *overlay, *gridview, *sw, *box, *dropdown;
|
||||
GtkListItemFactory *factory;
|
||||
GListStore *factories;
|
||||
GListModel *model;
|
||||
GtkSorter *sorter;
|
||||
GtkSorter *multi_sorter;
|
||||
GListStore *sorters;
|
||||
@ -863,6 +872,7 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
GtkWidget *selection_average_picture;
|
||||
GtkWidget *selection_info_toggle;
|
||||
GtkWidget *selection_info_revealer;
|
||||
GtkWidget *progress;
|
||||
GtkCssProvider *provider;
|
||||
|
||||
provider = gtk_css_provider_new ();
|
||||
@ -872,6 +882,10 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
800);
|
||||
g_object_unref (provider);
|
||||
|
||||
sort_model = gtk_sort_list_model_new (gtk_color_list_new (0), NULL);
|
||||
gtk_sort_list_model_set_incremental (sort_model, TRUE);
|
||||
selection = GTK_MULTI_SELECTION (gtk_multi_selection_new (G_LIST_MODEL (sort_model)));
|
||||
|
||||
window = gtk_window_new ();
|
||||
gtk_window_set_title (GTK_WINDOW (window), "Colors");
|
||||
header = gtk_header_bar_new ();
|
||||
@ -882,8 +896,17 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
gtk_widget_get_display (do_widget));
|
||||
g_object_add_weak_pointer (G_OBJECT (window), (gpointer*)&window);
|
||||
|
||||
overlay = gtk_overlay_new ();
|
||||
gtk_window_set_child (GTK_WINDOW (window), overlay);
|
||||
|
||||
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
|
||||
gtk_window_set_child (GTK_WINDOW (window), box);
|
||||
gtk_overlay_set_child (GTK_OVERLAY (overlay), box);
|
||||
|
||||
progress = gtk_progress_bar_new ();
|
||||
gtk_widget_set_hexpand (progress, TRUE);
|
||||
gtk_widget_set_valign (progress, GTK_ALIGN_START);
|
||||
g_signal_connect (sort_model, "notify::pending", G_CALLBACK (update_progress_cb), progress);
|
||||
gtk_overlay_add_overlay (GTK_OVERLAY (overlay), progress);
|
||||
|
||||
selection_info_revealer = gtk_revealer_new ();
|
||||
gtk_box_append (GTK_BOX (box), selection_info_revealer);
|
||||
@ -936,12 +959,12 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
gtk_box_append (GTK_BOX (box), sw);
|
||||
|
||||
gridview = create_color_grid ();
|
||||
gtk_grid_view_set_model (GTK_GRID_VIEW (gridview), G_LIST_MODEL (selection));
|
||||
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), gridview);
|
||||
gtk_widget_set_hexpand (sw, TRUE);
|
||||
gtk_widget_set_vexpand (sw, TRUE);
|
||||
model = gtk_grid_view_get_model (GTK_GRID_VIEW (gridview));
|
||||
|
||||
selection_filter = G_LIST_MODEL (gtk_selection_filter_model_new (GTK_SELECTION_MODEL (model)));
|
||||
selection_filter = G_LIST_MODEL (gtk_selection_filter_model_new (GTK_SELECTION_MODEL (selection)));
|
||||
g_signal_connect (selection_filter, "items-changed", G_CALLBACK (update_selection_count), selection_size_label);
|
||||
g_signal_connect (selection_filter, "items-changed", G_CALLBACK (update_selection_average), selection_average_picture);
|
||||
|
||||
@ -950,9 +973,6 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
g_object_unref (selection_filter);
|
||||
g_object_unref (no_selection);
|
||||
|
||||
model = gtk_multi_selection_get_model (GTK_MULTI_SELECTION (model));
|
||||
g_object_ref (model);
|
||||
|
||||
selection_info_toggle = gtk_toggle_button_new ();
|
||||
gtk_button_set_icon_name (GTK_BUTTON (selection_info_toggle), "emblem-important-symbolic");
|
||||
gtk_widget_set_tooltip_text (selection_info_toggle, "Show selection info");
|
||||
@ -965,7 +985,7 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
button = gtk_button_new_with_mnemonic ("_Refill");
|
||||
g_signal_connect (button, "clicked",
|
||||
G_CALLBACK (refill),
|
||||
gtk_sort_list_model_get_model (GTK_SORT_LIST_MODEL (model)));
|
||||
gtk_sort_list_model_get_model (sort_model));
|
||||
|
||||
gtk_header_bar_pack_start (GTK_HEADER_BAR (header), button);
|
||||
|
||||
@ -980,15 +1000,14 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
gtk_label_set_width_chars (GTK_LABEL (label), len + 2);
|
||||
gtk_label_set_xalign (GTK_LABEL (label), 1);
|
||||
|
||||
g_signal_connect (gtk_grid_view_get_model (GTK_GRID_VIEW (gridview)),
|
||||
"items-changed", G_CALLBACK (items_changed_cb), label);
|
||||
g_signal_connect (selection, "items-changed", G_CALLBACK (items_changed_cb), label);
|
||||
gtk_header_bar_pack_start (GTK_HEADER_BAR (header), label);
|
||||
|
||||
dropdown = gtk_drop_down_new ();
|
||||
gtk_drop_down_set_from_strings (GTK_DROP_DOWN (dropdown), (const char *[]) { "8", "64", "512", "4096", "32768", "262144", "2097152", "16777216", NULL });
|
||||
g_signal_connect (dropdown, "notify::selected",
|
||||
G_CALLBACK (limit_changed_cb),
|
||||
gtk_sort_list_model_get_model (GTK_SORT_LIST_MODEL (model)));
|
||||
gtk_sort_list_model_get_model (sort_model));
|
||||
g_signal_connect (dropdown, "notify::selected",
|
||||
G_CALLBACK (limit_changed_cb2),
|
||||
label);
|
||||
@ -1080,7 +1099,7 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
gtk_drop_down_set_model (GTK_DROP_DOWN (dropdown), G_LIST_MODEL (sorters));
|
||||
g_object_unref (sorters);
|
||||
|
||||
g_object_bind_property (dropdown, "selected-item", model, "sorter", G_BINDING_SYNC_CREATE);
|
||||
g_object_bind_property (dropdown, "selected-item", sort_model, "sorter", G_BINDING_SYNC_CREATE);
|
||||
|
||||
factories = g_list_store_new (GTK_TYPE_LIST_ITEM_FACTORY);
|
||||
|
||||
@ -1112,7 +1131,6 @@ do_listview_colors (GtkWidget *do_widget)
|
||||
g_object_unref (factories);
|
||||
|
||||
g_object_bind_property (dropdown, "selected-item", gridview, "factory", G_BINDING_SYNC_CREATE);
|
||||
g_object_unref (model);
|
||||
}
|
||||
|
||||
if (!gtk_widget_get_visible (window))
|
||||
|
@ -2832,6 +2832,9 @@ gtk_sort_list_model_set_sorter
|
||||
gtk_sort_list_model_get_sorter
|
||||
gtk_sort_list_model_set_model
|
||||
gtk_sort_list_model_get_model
|
||||
gtk_sort_list_model_set_incremental
|
||||
gtk_sort_list_model_get_incremental
|
||||
gtk_sort_list_model_get_peanding
|
||||
<SUBSECTION Standard>
|
||||
GTK_SORT_LIST_MODEL
|
||||
GTK_IS_SORT_LIST_MODEL
|
||||
|
@ -23,6 +23,7 @@
|
||||
|
||||
#include "gtkbuildable.h"
|
||||
#include "gtkintl.h"
|
||||
#include "gtksorterprivate.h"
|
||||
#include "gtktypebuiltins.h"
|
||||
|
||||
#define GDK_ARRAY_TYPE_NAME GtkSorters
|
||||
@ -48,6 +49,141 @@ struct _GtkMultiSorter
|
||||
GtkSorters sorters;
|
||||
};
|
||||
|
||||
typedef struct _GtkMultiSortKey GtkMultiSortKey;
|
||||
typedef struct _GtkMultiSortKeys GtkMultiSortKeys;
|
||||
|
||||
struct _GtkMultiSortKey
|
||||
{
|
||||
gsize offset;
|
||||
GtkSortKeys *keys;
|
||||
};
|
||||
|
||||
struct _GtkMultiSortKeys
|
||||
{
|
||||
GtkSortKeys parent_keys;
|
||||
|
||||
guint n_keys;
|
||||
GtkMultiSortKey keys[];
|
||||
};
|
||||
|
||||
static void
|
||||
gtk_multi_sort_keys_free (GtkSortKeys *keys)
|
||||
{
|
||||
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
|
||||
gsize i;
|
||||
|
||||
for (i = 0; i < self->n_keys; i++)
|
||||
gtk_sort_keys_unref (self->keys[i].keys);
|
||||
|
||||
g_slice_free1 (sizeof (GtkMultiSortKeys) + self->n_keys * sizeof (GtkMultiSortKey), self);
|
||||
}
|
||||
|
||||
static int
|
||||
gtk_multi_sort_keys_compare (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer data)
|
||||
{
|
||||
GtkMultiSortKeys *self = (GtkMultiSortKeys *) data;
|
||||
gsize i;
|
||||
|
||||
for (i = 0; i < self->n_keys; i++)
|
||||
{
|
||||
GtkOrdering result = gtk_sort_keys_compare (self->keys[i].keys,
|
||||
((const char *) a) + self->keys[i].offset,
|
||||
((const char *) b) + self->keys[i].offset);
|
||||
if (result != GTK_ORDERING_EQUAL)
|
||||
return result;
|
||||
}
|
||||
|
||||
return GTK_ORDERING_EQUAL;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gtk_multi_sort_keys_is_compatible (GtkSortKeys *keys,
|
||||
GtkSortKeys *other)
|
||||
{
|
||||
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
|
||||
GtkMultiSortKeys *compare = (GtkMultiSortKeys *) other;
|
||||
gsize i;
|
||||
|
||||
if (keys->klass != other->klass)
|
||||
return FALSE;
|
||||
|
||||
if (self->n_keys != compare->n_keys)
|
||||
return FALSE;
|
||||
|
||||
for (i = 0; i < self->n_keys; i++)
|
||||
{
|
||||
if (!gtk_sort_keys_is_compatible (self->keys[i].keys, compare->keys[i].keys))
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_multi_sort_keys_init_key (GtkSortKeys *keys,
|
||||
gpointer item,
|
||||
gpointer key_memory)
|
||||
{
|
||||
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
|
||||
char *key = (char *) key_memory;
|
||||
gsize i;
|
||||
|
||||
for (i = 0; i < self->n_keys; i++)
|
||||
gtk_sort_keys_init_key (self->keys[i].keys, item, key + self->keys[i].offset);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_multi_sort_keys_clear_key (GtkSortKeys *keys,
|
||||
gpointer key_memory)
|
||||
{
|
||||
GtkMultiSortKeys *self = (GtkMultiSortKeys *) keys;
|
||||
char *key = (char *) key_memory;
|
||||
gsize i;
|
||||
|
||||
for (i = 0; i < self->n_keys; i++)
|
||||
gtk_sort_keys_clear_key (self->keys[i].keys, key + self->keys[i].offset);
|
||||
}
|
||||
|
||||
static const GtkSortKeysClass GTK_MULTI_SORT_KEYS_CLASS =
|
||||
{
|
||||
gtk_multi_sort_keys_free,
|
||||
gtk_multi_sort_keys_compare,
|
||||
gtk_multi_sort_keys_is_compatible,
|
||||
gtk_multi_sort_keys_init_key,
|
||||
gtk_multi_sort_keys_clear_key,
|
||||
};
|
||||
|
||||
static GtkSortKeys *
|
||||
gtk_multi_sort_keys_new (GtkMultiSorter *self)
|
||||
{
|
||||
GtkMultiSortKeys *result;
|
||||
GtkSortKeys *keys;
|
||||
gsize i;
|
||||
|
||||
if (gtk_sorters_get_size (&self->sorters) == 0)
|
||||
return gtk_sort_keys_new_equal ();
|
||||
else if (gtk_sorters_get_size (&self->sorters) == 1)
|
||||
return gtk_sorter_get_keys (gtk_sorters_get (&self->sorters, 0));
|
||||
|
||||
keys = gtk_sort_keys_alloc (>K_MULTI_SORT_KEYS_CLASS,
|
||||
sizeof (GtkMultiSortKeys) + gtk_sorters_get_size (&self->sorters) * sizeof (GtkMultiSortKey),
|
||||
0, 1);
|
||||
result = (GtkMultiSortKeys *) keys;
|
||||
|
||||
result->n_keys = gtk_sorters_get_size (&self->sorters);
|
||||
for (i = 0; i < result->n_keys; i++)
|
||||
{
|
||||
result->keys[i].keys = gtk_sorter_get_keys (gtk_sorters_get (&self->sorters, i));
|
||||
result->keys[i].offset = GTK_SORT_KEYS_ALIGN (keys->key_size, gtk_sort_keys_get_key_align (result->keys[i].keys));
|
||||
keys->key_size = result->keys[i].offset + gtk_sort_keys_get_key_size (result->keys[i].keys);
|
||||
keys->key_align = MAX (keys->key_align, gtk_sort_keys_get_key_align (result->keys[i].keys));
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
static GType
|
||||
gtk_multi_sorter_get_item_type (GListModel *list)
|
||||
{
|
||||
@ -186,7 +322,9 @@ gtk_multi_sorter_changed_cb (GtkSorter *sorter,
|
||||
g_assert_not_reached ();
|
||||
change = GTK_SORTER_CHANGE_DIFFERENT;
|
||||
}
|
||||
gtk_sorter_changed (GTK_SORTER (self), change);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
change,
|
||||
gtk_multi_sort_keys_new (self));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -221,6 +359,10 @@ static void
|
||||
gtk_multi_sorter_init (GtkMultiSorter *self)
|
||||
{
|
||||
gtk_sorters_init (&self->sorters);
|
||||
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_DIFFERENT,
|
||||
gtk_multi_sort_keys_new (self));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -260,7 +402,9 @@ gtk_multi_sorter_append (GtkMultiSorter *self,
|
||||
g_signal_connect (sorter, "changed", G_CALLBACK (gtk_multi_sorter_changed_cb), self);
|
||||
gtk_sorters_append (&self->sorters, sorter);
|
||||
|
||||
gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_MORE_STRICT);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_MORE_STRICT,
|
||||
gtk_multi_sort_keys_new (self));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -290,6 +434,8 @@ gtk_multi_sorter_remove (GtkMultiSorter *self,
|
||||
g_signal_handlers_disconnect_by_func (sorter, gtk_multi_sorter_changed_cb, self);
|
||||
gtk_sorters_splice (&self->sorters, position, 1, NULL, 0);
|
||||
|
||||
gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_LESS_STRICT);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_LESS_STRICT,
|
||||
gtk_multi_sort_keys_new (self));
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "gtknumericsorter.h"
|
||||
|
||||
#include "gtkintl.h"
|
||||
#include "gtksorterprivate.h"
|
||||
#include "gtktypebuiltins.h"
|
||||
|
||||
#include <math.h>
|
||||
@ -69,6 +70,265 @@ static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
|
||||
_result = GTK_ORDERING_EQUAL; \
|
||||
}G_STMT_END
|
||||
|
||||
typedef struct _GtkNumericSortKeys GtkNumericSortKeys;
|
||||
struct _GtkNumericSortKeys
|
||||
{
|
||||
GtkSortKeys keys;
|
||||
|
||||
GtkExpression *expression;
|
||||
};
|
||||
|
||||
static void
|
||||
gtk_numeric_sort_keys_free (GtkSortKeys *keys)
|
||||
{
|
||||
GtkNumericSortKeys *self = (GtkNumericSortKeys *) keys;
|
||||
|
||||
gtk_expression_unref (self->expression);
|
||||
g_slice_free (GtkNumericSortKeys, self);
|
||||
}
|
||||
|
||||
#define COMPARE_FUNC(type, name, _a, _b) \
|
||||
static int \
|
||||
gtk_ ## type ## _sort_keys_compare_ ## name (gconstpointer a, \
|
||||
gconstpointer b, \
|
||||
gpointer unused) \
|
||||
{ \
|
||||
type num1 = *(type *) _a; \
|
||||
type num2 = *(type *) _b; \
|
||||
\
|
||||
if (num1 < num2) \
|
||||
return GTK_ORDERING_SMALLER; \
|
||||
else if (num1 > num2) \
|
||||
return GTK_ORDERING_LARGER; \
|
||||
else \
|
||||
return GTK_ORDERING_EQUAL; \
|
||||
}
|
||||
#define COMPARE_FUNCS(type) \
|
||||
COMPARE_FUNC(type, ascending, a, b) \
|
||||
COMPARE_FUNC(type, descending, b, a)
|
||||
|
||||
#define FLOAT_COMPARE_FUNC(type, name, _a, _b) \
|
||||
static int \
|
||||
gtk_ ## type ## _sort_keys_compare_ ## name (gconstpointer a, \
|
||||
gconstpointer b, \
|
||||
gpointer unused) \
|
||||
{ \
|
||||
type num1 = *(type *) _a; \
|
||||
type num2 = *(type *) _b; \
|
||||
\
|
||||
if (isnan (num1) && isnan (num2)) \
|
||||
return GTK_ORDERING_EQUAL; \
|
||||
else if (isnan (num1)) \
|
||||
return GTK_ORDERING_LARGER; \
|
||||
else if (isnan (num2)) \
|
||||
return GTK_ORDERING_SMALLER; \
|
||||
else if (num1 < num2) \
|
||||
return GTK_ORDERING_SMALLER; \
|
||||
else if (num1 > num2) \
|
||||
return GTK_ORDERING_LARGER; \
|
||||
else \
|
||||
return GTK_ORDERING_EQUAL; \
|
||||
}
|
||||
#define FLOAT_COMPARE_FUNCS(type) \
|
||||
FLOAT_COMPARE_FUNC(type, ascending, a, b) \
|
||||
FLOAT_COMPARE_FUNC(type, descending, b, a)
|
||||
|
||||
COMPARE_FUNCS(char)
|
||||
COMPARE_FUNCS(guchar)
|
||||
COMPARE_FUNCS(int)
|
||||
COMPARE_FUNCS(guint)
|
||||
FLOAT_COMPARE_FUNCS(float)
|
||||
FLOAT_COMPARE_FUNCS(double)
|
||||
COMPARE_FUNCS(long)
|
||||
COMPARE_FUNCS(gulong)
|
||||
COMPARE_FUNCS(gint64)
|
||||
COMPARE_FUNCS(guint64)
|
||||
|
||||
#define NUMERIC_SORT_KEYS(TYPE, key_type, type, default_value) \
|
||||
static void \
|
||||
gtk_ ## type ## _sort_keys_init_key (GtkSortKeys *keys, \
|
||||
gpointer item, \
|
||||
gpointer key_memory) \
|
||||
{ \
|
||||
GtkNumericSortKeys *self = (GtkNumericSortKeys *) keys; \
|
||||
key_type *key = (key_type *) key_memory; \
|
||||
GValue value = G_VALUE_INIT; \
|
||||
\
|
||||
if (gtk_expression_evaluate (self->expression, item, &value)) \
|
||||
*key = g_value_get_ ## type (&value); \
|
||||
else \
|
||||
*key = default_value; \
|
||||
\
|
||||
g_value_unset (&value); \
|
||||
} \
|
||||
\
|
||||
static gboolean \
|
||||
gtk_ ## type ## _sort_keys_is_compatible (GtkSortKeys *keys, \
|
||||
GtkSortKeys *other); \
|
||||
\
|
||||
static const GtkSortKeysClass GTK_ASCENDING_ ## TYPE ## _SORT_KEYS_CLASS = \
|
||||
{ \
|
||||
gtk_numeric_sort_keys_free, \
|
||||
gtk_ ## key_type ## _sort_keys_compare_ascending, \
|
||||
gtk_ ## type ## _sort_keys_is_compatible, \
|
||||
gtk_ ## type ## _sort_keys_init_key, \
|
||||
NULL \
|
||||
}; \
|
||||
\
|
||||
static const GtkSortKeysClass GTK_DESCENDING_ ## TYPE ## _SORT_KEYS_CLASS = \
|
||||
{ \
|
||||
gtk_numeric_sort_keys_free, \
|
||||
gtk_ ## key_type ## _sort_keys_compare_descending, \
|
||||
gtk_ ## type ## _sort_keys_is_compatible, \
|
||||
gtk_ ## type ## _sort_keys_init_key, \
|
||||
NULL \
|
||||
}; \
|
||||
\
|
||||
static gboolean \
|
||||
gtk_ ## type ## _sort_keys_is_compatible (GtkSortKeys *keys, \
|
||||
GtkSortKeys *other) \
|
||||
{ \
|
||||
GtkNumericSorter *self = (GtkNumericSorter *) keys; \
|
||||
GtkNumericSorter *compare = (GtkNumericSorter *) other; \
|
||||
\
|
||||
if (other->klass != >K_ASCENDING_ ## TYPE ## _SORT_KEYS_CLASS && \
|
||||
other->klass != >K_DESCENDING_ ## TYPE ## _SORT_KEYS_CLASS) \
|
||||
return FALSE; \
|
||||
\
|
||||
return self->expression == compare->expression; \
|
||||
}
|
||||
|
||||
NUMERIC_SORT_KEYS(BOOLEAN, char, boolean, FALSE)
|
||||
NUMERIC_SORT_KEYS(CHAR, char, char, G_MININT8)
|
||||
NUMERIC_SORT_KEYS(UCHAR, guchar, uchar, G_MAXUINT8)
|
||||
NUMERIC_SORT_KEYS(INT, int, int, G_MININT)
|
||||
NUMERIC_SORT_KEYS(UINT, guint, uint, G_MAXUINT)
|
||||
NUMERIC_SORT_KEYS(FLOAT, float, float, NAN)
|
||||
NUMERIC_SORT_KEYS(DOUBLE, double, double, NAN)
|
||||
NUMERIC_SORT_KEYS(LONG, long, long, G_MINLONG)
|
||||
NUMERIC_SORT_KEYS(ULONG, gulong, ulong, G_MAXLONG)
|
||||
NUMERIC_SORT_KEYS(INT64, gint64, int64, G_MININT64)
|
||||
NUMERIC_SORT_KEYS(UINT64, guint64, uint64, G_MAXUINT64)
|
||||
|
||||
static GtkSortKeys *
|
||||
gtk_numeric_sort_keys_new (GtkNumericSorter *self)
|
||||
{
|
||||
GtkNumericSortKeys *result;
|
||||
|
||||
if (self->expression == NULL)
|
||||
return gtk_sort_keys_new_equal ();
|
||||
|
||||
switch (gtk_expression_get_value_type (self->expression))
|
||||
{
|
||||
case G_TYPE_BOOLEAN:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_BOOLEAN_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_BOOLEAN_SORT_KEYS_CLASS,
|
||||
sizeof (char),
|
||||
sizeof (char));
|
||||
break;
|
||||
|
||||
case G_TYPE_CHAR:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_CHAR_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_CHAR_SORT_KEYS_CLASS,
|
||||
sizeof (char),
|
||||
sizeof (char));
|
||||
break;
|
||||
|
||||
case G_TYPE_UCHAR:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_UCHAR_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_UCHAR_SORT_KEYS_CLASS,
|
||||
sizeof (guchar),
|
||||
sizeof (guchar));
|
||||
break;
|
||||
|
||||
case G_TYPE_INT:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_INT_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_INT_SORT_KEYS_CLASS,
|
||||
sizeof (int),
|
||||
sizeof (int));
|
||||
break;
|
||||
|
||||
case G_TYPE_UINT:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_UINT_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_UINT_SORT_KEYS_CLASS,
|
||||
sizeof (guint),
|
||||
sizeof (guint));
|
||||
break;
|
||||
|
||||
case G_TYPE_FLOAT:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_FLOAT_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_FLOAT_SORT_KEYS_CLASS,
|
||||
sizeof (float),
|
||||
sizeof (float));
|
||||
break;
|
||||
|
||||
case G_TYPE_DOUBLE:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_DOUBLE_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_DOUBLE_SORT_KEYS_CLASS,
|
||||
sizeof (double),
|
||||
sizeof (double));
|
||||
break;
|
||||
|
||||
case G_TYPE_LONG:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_LONG_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_LONG_SORT_KEYS_CLASS,
|
||||
sizeof (long),
|
||||
sizeof (long));
|
||||
break;
|
||||
|
||||
case G_TYPE_ULONG:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_ULONG_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_ULONG_SORT_KEYS_CLASS,
|
||||
sizeof (gulong),
|
||||
sizeof (gulong));
|
||||
break;
|
||||
|
||||
case G_TYPE_INT64:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_INT64_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_INT64_SORT_KEYS_CLASS,
|
||||
sizeof (gint64),
|
||||
sizeof (gint64));
|
||||
break;
|
||||
|
||||
case G_TYPE_UINT64:
|
||||
result = gtk_sort_keys_new (GtkNumericSortKeys,
|
||||
self->sort_order == GTK_SORT_ASCENDING
|
||||
? >K_ASCENDING_UINT64_SORT_KEYS_CLASS
|
||||
: >K_DESCENDING_UINT64_SORT_KEYS_CLASS,
|
||||
sizeof (guint64),
|
||||
sizeof (guint64));
|
||||
break;
|
||||
|
||||
default:
|
||||
g_critical ("Invalid value type %s for expression\n", g_type_name (gtk_expression_get_value_type (self->expression)));
|
||||
return gtk_sort_keys_new_equal ();
|
||||
}
|
||||
|
||||
result->expression = gtk_expression_ref (self->expression);
|
||||
|
||||
return (GtkSortKeys *) result;
|
||||
}
|
||||
|
||||
static GtkOrdering
|
||||
gtk_numeric_sorter_compare (GtkSorter *sorter,
|
||||
gpointer item1,
|
||||
@ -304,6 +564,10 @@ static void
|
||||
gtk_numeric_sorter_init (GtkNumericSorter *self)
|
||||
{
|
||||
self->sort_order = GTK_SORT_ASCENDING;
|
||||
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_DIFFERENT,
|
||||
gtk_numeric_sort_keys_new (self));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -373,7 +637,9 @@ gtk_numeric_sorter_set_expression (GtkNumericSorter *self,
|
||||
if (expression)
|
||||
self->expression = gtk_expression_ref (expression);
|
||||
|
||||
gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_DIFFERENT);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_DIFFERENT,
|
||||
gtk_numeric_sort_keys_new (self));
|
||||
|
||||
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EXPRESSION]);
|
||||
}
|
||||
@ -396,7 +662,9 @@ gtk_numeric_sorter_set_sort_order (GtkNumericSorter *self,
|
||||
|
||||
self->sort_order = sort_order;
|
||||
|
||||
gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_INVERTED);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_INVERTED,
|
||||
gtk_numeric_sort_keys_new (self));
|
||||
|
||||
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORT_ORDER]);
|
||||
}
|
||||
|
153
gtk/gtksorter.c
153
gtk/gtksorter.c
@ -19,7 +19,7 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gtksorter.h"
|
||||
#include "gtksorterprivate.h"
|
||||
|
||||
#include "gtkintl.h"
|
||||
#include "gtktypebuiltins.h"
|
||||
@ -48,12 +48,26 @@
|
||||
* and provide one's own sorter.
|
||||
*/
|
||||
|
||||
typedef struct _GtkSorterPrivate GtkSorterPrivate;
|
||||
typedef struct _GtkDefaultSortKeys GtkDefaultSortKeys;
|
||||
|
||||
struct _GtkSorterPrivate
|
||||
{
|
||||
GtkSortKeys *keys;
|
||||
};
|
||||
|
||||
struct _GtkDefaultSortKeys
|
||||
{
|
||||
GtkSortKeys keys;
|
||||
GtkSorter *sorter;
|
||||
};
|
||||
|
||||
enum {
|
||||
CHANGED,
|
||||
LAST_SIGNAL
|
||||
};
|
||||
|
||||
G_DEFINE_TYPE (GtkSorter, gtk_sorter, G_TYPE_OBJECT)
|
||||
G_DEFINE_TYPE_WITH_PRIVATE (GtkSorter, gtk_sorter, G_TYPE_OBJECT)
|
||||
|
||||
static guint signals[LAST_SIGNAL] = { 0 };
|
||||
|
||||
@ -73,9 +87,24 @@ gtk_sorter_default_get_order (GtkSorter *self)
|
||||
return GTK_SORTER_ORDER_PARTIAL;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_sorter_dispose (GObject *object)
|
||||
{
|
||||
GtkSorter *self = GTK_SORTER (object);
|
||||
GtkSorterPrivate *priv = gtk_sorter_get_instance_private (self);
|
||||
|
||||
g_clear_pointer (&priv->keys, gtk_sort_keys_unref);
|
||||
|
||||
G_OBJECT_CLASS (gtk_sorter_parent_class)->dispose (object);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_sorter_class_init (GtkSorterClass *class)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
||||
|
||||
object_class->dispose = gtk_sorter_dispose;
|
||||
|
||||
class->compare = gtk_sorter_default_compare;
|
||||
class->get_order = gtk_sorter_default_get_order;
|
||||
|
||||
@ -181,6 +210,98 @@ gtk_sorter_get_order (GtkSorter *self)
|
||||
return GTK_SORTER_GET_CLASS (self)->get_order (self);
|
||||
}
|
||||
|
||||
static int
|
||||
gtk_default_sort_keys_compare (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer data)
|
||||
{
|
||||
GtkDefaultSortKeys *self = data;
|
||||
gpointer *key_a = (gpointer *) a;
|
||||
gpointer *key_b = (gpointer *) b;
|
||||
|
||||
return gtk_sorter_compare (self->sorter, *key_a, *key_b);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_default_sort_keys_free (GtkSortKeys *keys)
|
||||
{
|
||||
GtkDefaultSortKeys *self = (GtkDefaultSortKeys *) keys;
|
||||
|
||||
g_object_unref (self->sorter);
|
||||
|
||||
g_slice_free (GtkDefaultSortKeys, self);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gtk_default_sort_keys_is_compatible (GtkSortKeys *keys,
|
||||
GtkSortKeys *other)
|
||||
{
|
||||
if (keys->klass != other->klass)
|
||||
return FALSE;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_default_sort_keys_init_key (GtkSortKeys *self,
|
||||
gpointer item,
|
||||
gpointer key_memory)
|
||||
{
|
||||
gpointer *key = (gpointer *) key_memory;
|
||||
|
||||
*key = g_object_ref (item);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_default_sort_keys_clear_key (GtkSortKeys *self,
|
||||
gpointer key_memory)
|
||||
{
|
||||
gpointer *key = (gpointer *) key_memory;
|
||||
|
||||
g_object_unref (*key);
|
||||
}
|
||||
|
||||
static const GtkSortKeysClass GTK_DEFAULT_SORT_KEYS_CLASS =
|
||||
{
|
||||
gtk_default_sort_keys_free,
|
||||
gtk_default_sort_keys_compare,
|
||||
gtk_default_sort_keys_is_compatible,
|
||||
gtk_default_sort_keys_init_key,
|
||||
gtk_default_sort_keys_clear_key,
|
||||
};
|
||||
|
||||
/*<private>
|
||||
* gtk_sorter_get_keys:
|
||||
* @self: a #GtkSorter
|
||||
*
|
||||
* Gets a #GtkSortKeys that can be used as an alternative to
|
||||
* @self for faster sorting.
|
||||
*
|
||||
* The sort keys can change every time #GtkSorter::changed is emitted.
|
||||
* When the keys change, you should redo all comparisons with the new
|
||||
* keys.
|
||||
* When gtk_sort_keys_is_compatible() for the old and new keys returns
|
||||
* %TRUE, you can reuse keys you generated previously.
|
||||
*
|
||||
* Returns: (transfer full): the sort keys to sort with
|
||||
**/
|
||||
GtkSortKeys *
|
||||
gtk_sorter_get_keys (GtkSorter *self)
|
||||
{
|
||||
GtkSorterPrivate *priv = gtk_sorter_get_instance_private (self);
|
||||
GtkDefaultSortKeys *fallback;
|
||||
|
||||
g_return_val_if_fail (GTK_IS_SORTER (self), NULL);
|
||||
|
||||
if (priv->keys)
|
||||
return gtk_sort_keys_ref (priv->keys);
|
||||
|
||||
fallback = gtk_sort_keys_new (GtkDefaultSortKeys, >K_DEFAULT_SORT_KEYS_CLASS, sizeof (gpointer), sizeof (gpointer));
|
||||
fallback->sorter = g_object_ref (self);
|
||||
|
||||
return (GtkSortKeys *) fallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* gtk_sorter_changed:
|
||||
* @self: a #GtkSorter
|
||||
@ -205,3 +326,31 @@ gtk_sorter_changed (GtkSorter *self,
|
||||
|
||||
g_signal_emit (self, signals[CHANGED], 0, change);
|
||||
}
|
||||
|
||||
/*<private>
|
||||
* gtk_sorter_changed_with_keys
|
||||
* @self: a #GtkSorter
|
||||
* @change: How the sorter changed
|
||||
* @keys: (not nullable) (transfer full): New keys to use
|
||||
*
|
||||
* Updates the sorter's keys to @keys and then calls gtk_sorter_changed().
|
||||
* If you do not want to update the keys, call that function instead.
|
||||
*
|
||||
* This function should also be called in your_sorter_init() to initialize
|
||||
* the keys to use with your sorter.
|
||||
*/
|
||||
void
|
||||
gtk_sorter_changed_with_keys (GtkSorter *self,
|
||||
GtkSorterChange change,
|
||||
GtkSortKeys *keys)
|
||||
{
|
||||
GtkSorterPrivate *priv = gtk_sorter_get_instance_private (self);
|
||||
|
||||
g_return_if_fail (GTK_IS_SORTER (self));
|
||||
g_return_if_fail (keys != NULL);
|
||||
|
||||
g_clear_pointer (&priv->keys, gtk_sort_keys_unref);
|
||||
priv->keys = keys;
|
||||
|
||||
gtk_sorter_changed (self, change);
|
||||
}
|
||||
|
@ -115,6 +115,7 @@ GDK_AVAILABLE_IN_ALL
|
||||
void gtk_sorter_changed (GtkSorter *self,
|
||||
GtkSorterChange change);
|
||||
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GTK_SORTER_H__ */
|
||||
|
33
gtk/gtksorterprivate.h
Normal file
33
gtk/gtksorterprivate.h
Normal file
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright © 2020 Benjamin Otte
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __GTK_SORTER_PRIVATE_H__
|
||||
#define __GTK_SORTER_PRIVATE_H__
|
||||
|
||||
#include <gtk/gtksorter.h>
|
||||
|
||||
#include "gtk/gtksortkeysprivate.h"
|
||||
|
||||
GtkSortKeys * gtk_sorter_get_keys (GtkSorter *self);
|
||||
|
||||
void gtk_sorter_changed_with_keys (GtkSorter *self,
|
||||
GtkSorterChange change,
|
||||
GtkSortKeys *keys);
|
||||
|
||||
|
||||
#endif /* __GTK_SORTER_PRIVATE_H__ */
|
||||
|
150
gtk/gtksortkeys.c
Normal file
150
gtk/gtksortkeys.c
Normal file
@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright © 2020 Benjamin Otte
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gtksortkeysprivate.h"
|
||||
|
||||
#include "gtkcssstyleprivate.h"
|
||||
#include "gtkstyleproviderprivate.h"
|
||||
|
||||
GtkSortKeys *
|
||||
gtk_sort_keys_alloc (const GtkSortKeysClass *klass,
|
||||
gsize size,
|
||||
gsize key_size,
|
||||
gsize key_align)
|
||||
{
|
||||
GtkSortKeys *self;
|
||||
|
||||
g_return_val_if_fail (key_align > 0, NULL);
|
||||
|
||||
self = g_slice_alloc0 (size);
|
||||
|
||||
self->klass = klass;
|
||||
self->ref_count = 1;
|
||||
|
||||
self->key_size = key_size;
|
||||
self->key_align = key_align;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
GtkSortKeys *
|
||||
gtk_sort_keys_ref (GtkSortKeys *self)
|
||||
{
|
||||
self->ref_count += 1;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
void
|
||||
gtk_sort_keys_unref (GtkSortKeys *self)
|
||||
{
|
||||
self->ref_count -= 1;
|
||||
if (self->ref_count > 0)
|
||||
return;
|
||||
|
||||
self->klass->free (self);
|
||||
}
|
||||
|
||||
gsize
|
||||
gtk_sort_keys_get_key_size (GtkSortKeys *self)
|
||||
{
|
||||
return self->key_size;
|
||||
}
|
||||
|
||||
gsize
|
||||
gtk_sort_keys_get_key_align (GtkSortKeys *self)
|
||||
{
|
||||
return self->key_align;
|
||||
}
|
||||
|
||||
GCompareDataFunc
|
||||
gtk_sort_keys_get_key_compare_func (GtkSortKeys *self)
|
||||
{
|
||||
return self->klass->key_compare;
|
||||
}
|
||||
|
||||
gboolean
|
||||
gtk_sort_keys_is_compatible (GtkSortKeys *self,
|
||||
GtkSortKeys *other)
|
||||
{
|
||||
if (self == other)
|
||||
return TRUE;
|
||||
|
||||
return self->klass->is_compatible (self, other);
|
||||
}
|
||||
|
||||
gboolean
|
||||
gtk_sort_keys_needs_clear_key (GtkSortKeys *self)
|
||||
{
|
||||
return self->klass->clear_key != NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_equal_sort_keys_free (GtkSortKeys *keys)
|
||||
{
|
||||
g_slice_free (GtkSortKeys, keys);
|
||||
}
|
||||
|
||||
static int
|
||||
gtk_equal_sort_keys_compare (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer unused)
|
||||
{
|
||||
return GTK_ORDERING_EQUAL;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gtk_equal_sort_keys_is_compatible (GtkSortKeys *keys,
|
||||
GtkSortKeys *other)
|
||||
{
|
||||
return keys->klass == other->klass;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_equal_sort_keys_init_key (GtkSortKeys *keys,
|
||||
gpointer item,
|
||||
gpointer key_memory)
|
||||
{
|
||||
}
|
||||
|
||||
static const GtkSortKeysClass GTK_EQUAL_SORT_KEYS_CLASS =
|
||||
{
|
||||
gtk_equal_sort_keys_free,
|
||||
gtk_equal_sort_keys_compare,
|
||||
gtk_equal_sort_keys_is_compatible,
|
||||
gtk_equal_sort_keys_init_key,
|
||||
NULL
|
||||
};
|
||||
|
||||
/*<private>
|
||||
* gtk_sort_keys_new_equal:
|
||||
*
|
||||
* Creates a new #GtkSortKeys that compares every element as equal.
|
||||
* This is useful when sorters are in an invalid configuration.
|
||||
*
|
||||
* Returns: a new #GtkSortKeys
|
||||
**/
|
||||
GtkSortKeys *
|
||||
gtk_sort_keys_new_equal (void)
|
||||
{
|
||||
return gtk_sort_keys_new (GtkSortKeys,
|
||||
>K_EQUAL_SORT_KEYS_CLASS,
|
||||
0, 1);
|
||||
}
|
||||
|
97
gtk/gtksortkeysprivate.h
Normal file
97
gtk/gtksortkeysprivate.h
Normal file
@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright © 2020 Benjamin Otte
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __GTK_SORT_KEYS_PRIVATE_H__
|
||||
#define __GTK_SORT_KEYS_PRIVATE_H__
|
||||
|
||||
#include <gdk/gdk.h>
|
||||
#include <gtk/gtkenums.h>
|
||||
#include <gtk/gtksorter.h>
|
||||
|
||||
typedef struct _GtkSortKeys GtkSortKeys;
|
||||
typedef struct _GtkSortKeysClass GtkSortKeysClass;
|
||||
|
||||
struct _GtkSortKeys
|
||||
{
|
||||
const GtkSortKeysClass *klass;
|
||||
gint ref_count;
|
||||
|
||||
gsize key_size;
|
||||
gsize key_align; /* must be power of 2 */
|
||||
};
|
||||
|
||||
struct _GtkSortKeysClass
|
||||
{
|
||||
void (* free) (GtkSortKeys *self);
|
||||
|
||||
GCompareDataFunc key_compare;
|
||||
|
||||
gboolean (* is_compatible) (GtkSortKeys *self,
|
||||
GtkSortKeys *other);
|
||||
|
||||
void (* init_key) (GtkSortKeys *self,
|
||||
gpointer item,
|
||||
gpointer key_memory);
|
||||
void (* clear_key) (GtkSortKeys *self,
|
||||
gpointer key_memory);
|
||||
};
|
||||
|
||||
GtkSortKeys * gtk_sort_keys_alloc (const GtkSortKeysClass *klass,
|
||||
gsize size,
|
||||
gsize key_size,
|
||||
gsize key_align);
|
||||
#define gtk_sort_keys_new(_name, _klass, _key_size, _key_align) \
|
||||
((_name *) gtk_sort_keys_alloc ((_klass), sizeof (_name), (_key_size), (_key_align)))
|
||||
GtkSortKeys * gtk_sort_keys_ref (GtkSortKeys *self);
|
||||
void gtk_sort_keys_unref (GtkSortKeys *self);
|
||||
|
||||
GtkSortKeys * gtk_sort_keys_new_equal (void);
|
||||
|
||||
gsize gtk_sort_keys_get_key_size (GtkSortKeys *self);
|
||||
gsize gtk_sort_keys_get_key_align (GtkSortKeys *self);
|
||||
GCompareDataFunc gtk_sort_keys_get_key_compare_func (GtkSortKeys *self);
|
||||
gboolean gtk_sort_keys_is_compatible (GtkSortKeys *self,
|
||||
GtkSortKeys *other);
|
||||
gboolean gtk_sort_keys_needs_clear_key (GtkSortKeys *self);
|
||||
|
||||
#define GTK_SORT_KEYS_ALIGN(_size,_align) (((_size) + (_align) - 1) & ~((_align) - 1))
|
||||
static inline int
|
||||
gtk_sort_keys_compare (GtkSortKeys *self,
|
||||
gconstpointer a,
|
||||
gconstpointer b)
|
||||
{
|
||||
return self->klass->key_compare (a, b, self);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gtk_sort_keys_init_key (GtkSortKeys *self,
|
||||
gpointer item,
|
||||
gpointer key_memory)
|
||||
{
|
||||
self->klass->init_key (self, item, key_memory);
|
||||
}
|
||||
|
||||
static inline void
|
||||
gtk_sort_keys_clear_key (GtkSortKeys *self,
|
||||
gpointer key_memory)
|
||||
{
|
||||
if (self->klass->clear_key)
|
||||
self->klass->clear_key (self, key_memory);
|
||||
}
|
||||
|
||||
#endif /* __GTK_SORT_KEYS_PRIVATE_H__ */
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -52,6 +52,15 @@ void gtk_sort_list_model_set_model (GtkSortListMode
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
GListModel * gtk_sort_list_model_get_model (GtkSortListModel *self);
|
||||
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
void gtk_sort_list_model_set_incremental (GtkSortListModel *self,
|
||||
gboolean incremental);
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
gboolean gtk_sort_list_model_get_incremental (GtkSortListModel *self);
|
||||
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
guint gtk_sort_list_model_get_pending (GtkSortListModel *self);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GTK_SORT_LIST_MODEL_H__ */
|
||||
|
@ -22,6 +22,7 @@
|
||||
#include "gtkstringsorter.h"
|
||||
|
||||
#include "gtkintl.h"
|
||||
#include "gtksorterprivate.h"
|
||||
#include "gtktypebuiltins.h"
|
||||
|
||||
/**
|
||||
@ -58,70 +59,58 @@ G_DEFINE_TYPE (GtkStringSorter, gtk_string_sorter, GTK_TYPE_SORTER)
|
||||
|
||||
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
|
||||
|
||||
static char *
|
||||
gtk_string_sorter_get_key (GtkExpression *expression,
|
||||
gboolean ignore_case,
|
||||
gpointer item1)
|
||||
{
|
||||
GValue value = G_VALUE_INIT;
|
||||
char *s;
|
||||
|
||||
if (expression == NULL)
|
||||
return NULL;
|
||||
|
||||
if (!gtk_expression_evaluate (expression, item1, &value))
|
||||
return NULL;
|
||||
|
||||
/* If strings are NULL, order them before "". */
|
||||
if (ignore_case)
|
||||
{
|
||||
char *t;
|
||||
|
||||
t = g_utf8_casefold (g_value_get_string (&value), -1);
|
||||
s = g_utf8_collate_key (t, -1);
|
||||
g_free (t);
|
||||
}
|
||||
else
|
||||
{
|
||||
s = g_utf8_collate_key (g_value_get_string (&value), -1);
|
||||
}
|
||||
|
||||
g_value_unset (&value);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
static GtkOrdering
|
||||
gtk_string_sorter_compare (GtkSorter *sorter,
|
||||
gpointer item1,
|
||||
gpointer item2)
|
||||
{
|
||||
GtkStringSorter *self = GTK_STRING_SORTER (sorter);
|
||||
GValue value1 = G_VALUE_INIT;
|
||||
GValue value2 = G_VALUE_INIT;
|
||||
const char *s1, *s2;
|
||||
gboolean res1, res2;
|
||||
char *s1, *s2;
|
||||
GtkOrdering result;
|
||||
|
||||
if (self->expression == NULL)
|
||||
return GTK_ORDERING_EQUAL;
|
||||
|
||||
res1 = gtk_expression_evaluate (self->expression, item1, &value1);
|
||||
res2 = gtk_expression_evaluate (self->expression, item2, &value2);
|
||||
s1 = gtk_string_sorter_get_key (self->expression, self->ignore_case, item1);
|
||||
s2 = gtk_string_sorter_get_key (self->expression, self->ignore_case, item2);
|
||||
|
||||
/* If items don't evaluate, order them at the end, so they aren't
|
||||
* in the way. */
|
||||
if (!res1)
|
||||
{
|
||||
result = res2 ? GTK_ORDERING_LARGER : GTK_ORDERING_EQUAL;
|
||||
goto out;
|
||||
}
|
||||
else if (!res2)
|
||||
{
|
||||
result = GTK_ORDERING_SMALLER;
|
||||
goto out;
|
||||
}
|
||||
result = gtk_ordering_from_cmpfunc (g_strcmp0 (s1, s2));
|
||||
|
||||
s1 = g_value_get_string (&value1);
|
||||
s2 = g_value_get_string (&value2);
|
||||
|
||||
/* If strings are NULL, order them before "". */
|
||||
if (s1 == NULL)
|
||||
{
|
||||
result = s2 == NULL ? GTK_ORDERING_EQUAL : GTK_ORDERING_SMALLER;
|
||||
goto out;
|
||||
}
|
||||
else if (s2 == NULL)
|
||||
{
|
||||
result = GTK_ORDERING_LARGER;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (self->ignore_case)
|
||||
{
|
||||
char *t1, *t2;
|
||||
|
||||
t1 = g_utf8_casefold (s1, -1);
|
||||
t2 = g_utf8_casefold (s2, -1);
|
||||
|
||||
result = gtk_ordering_from_cmpfunc (g_utf8_collate (t1, t2));
|
||||
|
||||
g_free (t1);
|
||||
g_free (t2);
|
||||
}
|
||||
else
|
||||
result = gtk_ordering_from_cmpfunc (g_utf8_collate (s1, s2));
|
||||
|
||||
out:
|
||||
g_value_unset (&value1);
|
||||
g_value_unset (&value2);
|
||||
g_free (s1);
|
||||
g_free (s2);
|
||||
|
||||
return result;
|
||||
}
|
||||
@ -137,6 +126,95 @@ gtk_string_sorter_get_order (GtkSorter *sorter)
|
||||
return GTK_SORTER_ORDER_PARTIAL;
|
||||
}
|
||||
|
||||
typedef struct _GtkStringSortKeys GtkStringSortKeys;
|
||||
struct _GtkStringSortKeys
|
||||
{
|
||||
GtkSortKeys keys;
|
||||
|
||||
GtkExpression *expression;
|
||||
gboolean ignore_case;
|
||||
};
|
||||
|
||||
static void
|
||||
gtk_string_sort_keys_free (GtkSortKeys *keys)
|
||||
{
|
||||
GtkStringSortKeys *self = (GtkStringSortKeys *) keys;
|
||||
|
||||
gtk_expression_unref (self->expression);
|
||||
g_slice_free (GtkStringSortKeys, self);
|
||||
}
|
||||
|
||||
static int
|
||||
gtk_string_sort_keys_compare (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer unused)
|
||||
{
|
||||
const char *sa = *(const char **) a;
|
||||
const char *sb = *(const char **) b;
|
||||
|
||||
if (sa == NULL)
|
||||
return sb == NULL ? GTK_ORDERING_EQUAL : GTK_ORDERING_LARGER;
|
||||
else if (sb == NULL)
|
||||
return GTK_ORDERING_SMALLER;
|
||||
|
||||
return gtk_ordering_from_cmpfunc (strcmp (sa, sb));
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gtk_string_sort_keys_is_compatible (GtkSortKeys *keys,
|
||||
GtkSortKeys *other)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_string_sort_keys_init_key (GtkSortKeys *keys,
|
||||
gpointer item,
|
||||
gpointer key_memory)
|
||||
{
|
||||
GtkStringSortKeys *self = (GtkStringSortKeys *) keys;
|
||||
char **key = (char **) key_memory;
|
||||
|
||||
*key = gtk_string_sorter_get_key (self->expression, self->ignore_case, item);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_string_sort_keys_clear_key (GtkSortKeys *keys,
|
||||
gpointer key_memory)
|
||||
{
|
||||
char **key = (char **) key_memory;
|
||||
|
||||
g_free (*key);
|
||||
}
|
||||
|
||||
static const GtkSortKeysClass GTK_STRING_SORT_KEYS_CLASS =
|
||||
{
|
||||
gtk_string_sort_keys_free,
|
||||
gtk_string_sort_keys_compare,
|
||||
gtk_string_sort_keys_is_compatible,
|
||||
gtk_string_sort_keys_init_key,
|
||||
gtk_string_sort_keys_clear_key,
|
||||
};
|
||||
|
||||
static GtkSortKeys *
|
||||
gtk_string_sort_keys_new (GtkStringSorter *self)
|
||||
{
|
||||
GtkStringSortKeys *result;
|
||||
|
||||
if (self->expression == NULL)
|
||||
return gtk_sort_keys_new_equal ();
|
||||
|
||||
result = gtk_sort_keys_new (GtkStringSortKeys,
|
||||
>K_STRING_SORT_KEYS_CLASS,
|
||||
sizeof (char *),
|
||||
sizeof (char *));
|
||||
|
||||
result->expression = gtk_expression_ref (self->expression);
|
||||
result->ignore_case = self->ignore_case;
|
||||
|
||||
return (GtkSortKeys *) result;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_string_sorter_set_property (GObject *object,
|
||||
guint prop_id,
|
||||
@ -239,6 +317,10 @@ static void
|
||||
gtk_string_sorter_init (GtkStringSorter *self)
|
||||
{
|
||||
self->ignore_case = TRUE;
|
||||
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_DIFFERENT,
|
||||
gtk_string_sort_keys_new (self));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -306,7 +388,9 @@ gtk_string_sorter_set_expression (GtkStringSorter *self,
|
||||
if (expression)
|
||||
self->expression = gtk_expression_ref (expression);
|
||||
|
||||
gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_DIFFERENT);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_DIFFERENT,
|
||||
gtk_string_sort_keys_new (self));
|
||||
|
||||
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_EXPRESSION]);
|
||||
}
|
||||
@ -345,7 +429,9 @@ gtk_string_sorter_set_ignore_case (GtkStringSorter *self,
|
||||
|
||||
self->ignore_case = ignore_case;
|
||||
|
||||
gtk_sorter_changed (GTK_SORTER (self), ignore_case ? GTK_SORTER_CHANGE_LESS_STRICT : GTK_SORTER_CHANGE_MORE_STRICT);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
ignore_case ? GTK_SORTER_CHANGE_LESS_STRICT : GTK_SORTER_CHANGE_MORE_STRICT,
|
||||
gtk_string_sort_keys_new (self));
|
||||
|
||||
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_IGNORE_CASE]);
|
||||
}
|
||||
|
943
gtk/gtktimsort-impl.c
Normal file
943
gtk/gtktimsort-impl.c
Normal file
@ -0,0 +1,943 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Benjamin Otte
|
||||
* Copyright (C) 2011 Patrick O. Perry
|
||||
* Copyright (C) 2008 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef NAME
|
||||
#define NAME WIDTH
|
||||
#endif
|
||||
|
||||
#define DEFINE_TEMP(temp) gpointer temp = g_alloca (WIDTH)
|
||||
#define ASSIGN(x, y) memcpy (x, y, WIDTH)
|
||||
#define INCPTR(x) ((gpointer) ((char *) (x) + WIDTH))
|
||||
#define DECPTR(x) ((gpointer) ((char *) (x) - WIDTH))
|
||||
#define ELEM(a, i) ((char *) (a) + (i) * WIDTH)
|
||||
#define LEN(n) ((n) * WIDTH)
|
||||
|
||||
#define CONCAT(x, y) gtk_tim_sort_ ## x ## _ ## y
|
||||
#define MAKE_STR(x, y) CONCAT (x, y)
|
||||
#define gtk_tim_sort(x) MAKE_STR (x, NAME)
|
||||
|
||||
/*
|
||||
* Reverse the specified range of the specified array.
|
||||
*
|
||||
* @param a the array in which a range is to be reversed
|
||||
* @param hi the index after the last element in the range to be reversed
|
||||
*/
|
||||
static void gtk_tim_sort(reverse_range) (GtkTimSort *self,
|
||||
gpointer a,
|
||||
gsize hi)
|
||||
{
|
||||
DEFINE_TEMP (t);
|
||||
char *front = a;
|
||||
char *back = ELEM (a, hi - 1);
|
||||
|
||||
g_assert (hi > 0);
|
||||
|
||||
while (front < back)
|
||||
{
|
||||
ASSIGN (t, front);
|
||||
ASSIGN (front, back);
|
||||
ASSIGN (back, t);
|
||||
front = INCPTR (front);
|
||||
back = DECPTR (back);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the length of the run beginning at the specified position in
|
||||
* the specified array and reverses the run if it is descending (ensuring
|
||||
* that the run will always be ascending when the method returns).
|
||||
*
|
||||
* A run is the longest ascending sequence with:
|
||||
*
|
||||
* a[0] <= a[1] <= a[2] <= ...
|
||||
*
|
||||
* or the longest descending sequence with:
|
||||
*
|
||||
* a[0] > a[1] > a[2] > ...
|
||||
*
|
||||
* For its intended use in a stable mergesort, the strictness of the
|
||||
* definition of "descending" is needed so that the call can safely
|
||||
* reverse a descending sequence without violating stability.
|
||||
*
|
||||
* @param a the array in which a run is to be counted and possibly reversed
|
||||
* @param hi index after the last element that may be contained in the run.
|
||||
* It is required that {@code 0 < hi}.
|
||||
* @param compare the comparator to used for the sort
|
||||
* @return the length of the run beginning at the specified position in
|
||||
* the specified array
|
||||
*/
|
||||
static gsize
|
||||
gtk_tim_sort(prepare_run) (GtkTimSort *self,
|
||||
GtkTimSortRun *out_change)
|
||||
{
|
||||
gsize run_hi = 1;
|
||||
char *cur;
|
||||
char *next;
|
||||
|
||||
if (self->size <= run_hi)
|
||||
{
|
||||
gtk_tim_sort_set_change (out_change, NULL, 0);
|
||||
return self->size;
|
||||
}
|
||||
|
||||
cur = INCPTR (self->base);
|
||||
next = INCPTR (cur);
|
||||
run_hi++;
|
||||
|
||||
/* Find end of run, and reverse range if descending */
|
||||
if (gtk_tim_sort_compare (self, cur, self->base) < 0) /* Descending */
|
||||
{
|
||||
while (run_hi < self->size && gtk_tim_sort_compare (self, next, cur) < 0)
|
||||
{
|
||||
run_hi++;
|
||||
cur = next;
|
||||
next = INCPTR (next);
|
||||
}
|
||||
gtk_tim_sort(reverse_range) (self, self->base, run_hi);
|
||||
gtk_tim_sort_set_change (out_change, self->base, run_hi);
|
||||
}
|
||||
else /* Ascending */
|
||||
{
|
||||
while (run_hi < self->size && gtk_tim_sort_compare (self, next, cur) >= 0)
|
||||
{
|
||||
run_hi++;
|
||||
cur = next;
|
||||
next = INCPTR (next);
|
||||
}
|
||||
gtk_tim_sort_set_change (out_change, NULL, 0);
|
||||
}
|
||||
|
||||
return run_hi;
|
||||
}
|
||||
|
||||
/*
|
||||
* Sorts the specified portion of the specified array using a binary
|
||||
* insertion sort. This is the best method for sorting small numbers
|
||||
* of elements. It requires O(n log n) compares, but O(n^2) data
|
||||
* movement (worst case).
|
||||
*
|
||||
* If the initial part of the specified range is already sorted,
|
||||
* this method can take advantage of it: the method assumes that the
|
||||
* elements from index {@code lo}, inclusive, to {@code start},
|
||||
* exclusive are already sorted.
|
||||
*
|
||||
* @param a the array in which a range is to be sorted
|
||||
* @param hi the index after the last element in the range to be sorted
|
||||
* @param start the index of the first element in the range that is
|
||||
* not already known to be sorted ({@code lo <= start <= hi})
|
||||
*/
|
||||
static void gtk_tim_sort(binary_sort) (GtkTimSort *self,
|
||||
gpointer a,
|
||||
gsize hi,
|
||||
gsize start,
|
||||
GtkTimSortRun *inout_change)
|
||||
{
|
||||
DEFINE_TEMP (pivot);
|
||||
char *startp;
|
||||
char *change_min = ELEM (a, hi);
|
||||
char *change_max = a;
|
||||
|
||||
g_assert (start <= hi);
|
||||
|
||||
if (start == 0)
|
||||
start++;
|
||||
|
||||
startp = ELEM (a, start);
|
||||
|
||||
for (; start < hi; start++, startp = INCPTR (startp))
|
||||
{
|
||||
/* Set left (and right) to the index where a[start] (pivot) belongs */
|
||||
char *leftp = a;
|
||||
gsize right = start;
|
||||
gsize n;
|
||||
|
||||
/*
|
||||
* Invariants:
|
||||
* pivot >= all in [0, left).
|
||||
* pivot < all in [right, start).
|
||||
*/
|
||||
while (0 < right)
|
||||
{
|
||||
gsize mid = right >> 1;
|
||||
gpointer midp = ELEM (leftp, mid);
|
||||
if (gtk_tim_sort_compare (self, startp, midp) < 0)
|
||||
{
|
||||
right = mid;
|
||||
}
|
||||
else
|
||||
{
|
||||
leftp = INCPTR (midp);
|
||||
right -= (mid + 1);
|
||||
}
|
||||
}
|
||||
g_assert (0 == right);
|
||||
|
||||
/*
|
||||
* The invariants still hold: pivot >= all in [lo, left) and
|
||||
* pivot < all in [left, start), so pivot belongs at left. Note
|
||||
* that if there are elements equal to pivot, left points to the
|
||||
* first slot after them -- that's why this sort is stable.
|
||||
* Slide elements over to make room to make room for pivot.
|
||||
*/
|
||||
n = startp - leftp; /* The number of bytes to move */
|
||||
if (n == 0)
|
||||
continue;
|
||||
|
||||
ASSIGN (pivot, startp);
|
||||
memmove (INCPTR (leftp), leftp, n); /* POP: overlaps */
|
||||
|
||||
/* a[left] = pivot; */
|
||||
ASSIGN (leftp, pivot);
|
||||
|
||||
change_min = MIN (change_min, leftp);
|
||||
change_max = MAX (change_max, ELEM (startp, 1));
|
||||
}
|
||||
|
||||
if (change_max > (char *) a)
|
||||
{
|
||||
g_assert (change_min < ELEM (a, hi));
|
||||
if (inout_change && inout_change->len)
|
||||
{
|
||||
change_max = MAX (change_max, ELEM (inout_change->base, inout_change->len));
|
||||
change_min = MIN (change_min, (char *) inout_change->base);
|
||||
}
|
||||
gtk_tim_sort_set_change (inout_change, change_min, (change_max - change_min) / WIDTH);
|
||||
}
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gtk_tim_sort(merge_append) (GtkTimSort *self,
|
||||
GtkTimSortRun *out_change)
|
||||
{
|
||||
/* Identify next run */
|
||||
gsize run_len;
|
||||
|
||||
run_len = gtk_tim_sort(prepare_run) (self, out_change);
|
||||
if (run_len == 0)
|
||||
return FALSE;
|
||||
|
||||
/* If run is short, extend to min(self->min_run, self->size) */
|
||||
if (run_len < self->min_run)
|
||||
{
|
||||
gsize force = MIN (self->size, self->min_run);
|
||||
gtk_tim_sort(binary_sort) (self, self->base, force, run_len, out_change);
|
||||
run_len = force;
|
||||
}
|
||||
/* Push run onto pending-run stack, and maybe merge */
|
||||
gtk_tim_sort_push_run (self, self->base, run_len);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Locates the position at which to insert the specified key into the
|
||||
* specified sorted range; if the range contains an element equal to key,
|
||||
* returns the index of the leftmost equal element.
|
||||
*
|
||||
* @param key the key whose insertion point to search for
|
||||
* @param base the array in which to search
|
||||
* @param len the length of the range; must be > 0
|
||||
* @param hint the index at which to begin the search, 0 <= hint < n.
|
||||
* The closer hint is to the result, the faster this method will run.
|
||||
* @param c the comparator used to order the range, and to search
|
||||
* @return the int k, 0 <= k <= n such that a[b + k - 1] < key <= a[b + k],
|
||||
* pretending that a[b - 1] is minus infinity and a[b + n] is infinity.
|
||||
* In other words, key belongs at index b + k; or in other words,
|
||||
* the first k elements of a should precede key, and the last n - k
|
||||
* should follow it.
|
||||
*/
|
||||
static gsize
|
||||
gtk_tim_sort(gallop_left) (GtkTimSort *self,
|
||||
gpointer key,
|
||||
gpointer base,
|
||||
gsize len,
|
||||
gsize hint)
|
||||
{
|
||||
char *hintp = ELEM (base, hint);
|
||||
gsize last_ofs = 0;
|
||||
gsize ofs = 1;
|
||||
|
||||
g_assert (len > 0 && hint < len);
|
||||
if (gtk_tim_sort_compare (self, key, hintp) > 0)
|
||||
{
|
||||
/* Gallop right until a[hint+last_ofs] < key <= a[hint+ofs] */
|
||||
gsize max_ofs = len - hint;
|
||||
while (ofs < max_ofs
|
||||
&& gtk_tim_sort_compare (self, key, ELEM (hintp, ofs)) > 0)
|
||||
{
|
||||
last_ofs = ofs;
|
||||
ofs = (ofs << 1) + 1; /* eventually this becomes SIZE_MAX */
|
||||
}
|
||||
if (ofs > max_ofs)
|
||||
ofs = max_ofs;
|
||||
|
||||
/* Make offsets relative to base */
|
||||
last_ofs += hint + 1; /* POP: we add 1 here so last_ofs stays non-negative */
|
||||
ofs += hint;
|
||||
}
|
||||
else /* key <= a[hint] */
|
||||
/* Gallop left until a[hint-ofs] < key <= a[hint-last_ofs] */
|
||||
{
|
||||
const gsize max_ofs = hint + 1;
|
||||
gsize tmp;
|
||||
while (ofs < max_ofs
|
||||
&& gtk_tim_sort_compare (self, key, ELEM (hintp, -ofs)) <= 0)
|
||||
{
|
||||
last_ofs = ofs;
|
||||
ofs = (ofs << 1) + 1; /* no need to check for overflow */
|
||||
}
|
||||
if (ofs > max_ofs)
|
||||
ofs = max_ofs;
|
||||
|
||||
/* Make offsets relative to base */
|
||||
tmp = last_ofs;
|
||||
last_ofs = hint + 1 - ofs; /* POP: we add 1 here so last_ofs stays non-negative */
|
||||
ofs = hint - tmp;
|
||||
}
|
||||
g_assert (last_ofs <= ofs && ofs <= len);
|
||||
|
||||
/*
|
||||
* Now a[last_ofs-1] < key <= a[ofs], so key belongs somewhere
|
||||
* to the right of last_ofs but no farther right than ofs. Do a binary
|
||||
* search, with invariant a[last_ofs - 1] < key <= a[ofs].
|
||||
*/
|
||||
/* last_ofs++; POP: we added 1 above to keep last_ofs non-negative */
|
||||
while (last_ofs < ofs)
|
||||
{
|
||||
/*gsize m = last_ofs + ((ofs - last_ofs) >> 1); */
|
||||
/* http://stackoverflow.com/questions/4844165/safe-integer-middle-value-formula */
|
||||
gsize m = (last_ofs & ofs) + ((last_ofs ^ ofs) >> 1);
|
||||
|
||||
if (gtk_tim_sort_compare (self, key, ELEM (base, m)) > 0)
|
||||
last_ofs = m + 1; /* a[m] < key */
|
||||
else
|
||||
ofs = m; /* key <= a[m] */
|
||||
}
|
||||
g_assert (last_ofs == ofs); /* so a[ofs - 1] < key <= a[ofs] */
|
||||
return ofs;
|
||||
}
|
||||
|
||||
/*
|
||||
* Like gallop_left, except that if the range contains an element equal to
|
||||
* key, gallop_right returns the index after the rightmost equal element.
|
||||
*
|
||||
* @param key the key whose insertion point to search for
|
||||
* @param base the array in which to search
|
||||
* @param len the length of the range; must be > 0
|
||||
* @param hint the index at which to begin the search, 0 <= hint < n.
|
||||
* The closer hint is to the result, the faster this method will run.
|
||||
* @param c the comparator used to order the range, and to search
|
||||
* @return the int k, 0 <= k <= n such that a[b + k - 1] <= key < a[b + k]
|
||||
*/
|
||||
static gsize
|
||||
gtk_tim_sort(gallop_right) (GtkTimSort *self,
|
||||
gpointer key,
|
||||
gpointer base,
|
||||
gsize len,
|
||||
gsize hint)
|
||||
{
|
||||
char *hintp = ELEM (base, hint);
|
||||
gsize ofs = 1;
|
||||
gsize last_ofs = 0;
|
||||
|
||||
g_assert (len > 0 && hint < len);
|
||||
|
||||
if (gtk_tim_sort_compare (self, key, hintp) < 0)
|
||||
{
|
||||
/* Gallop left until a[hint - ofs] <= key < a[hint - last_ofs] */
|
||||
gsize max_ofs = hint + 1;
|
||||
gsize tmp;
|
||||
while (ofs < max_ofs
|
||||
&& gtk_tim_sort_compare (self, key, ELEM (hintp, -ofs)) < 0)
|
||||
{
|
||||
last_ofs = ofs;
|
||||
ofs = (ofs << 1) + 1; /* no need to check for overflow */
|
||||
}
|
||||
if (ofs > max_ofs)
|
||||
ofs = max_ofs;
|
||||
|
||||
/* Make offsets relative to base */
|
||||
tmp = last_ofs;
|
||||
last_ofs = hint + 1 - ofs;
|
||||
ofs = hint - tmp;
|
||||
}
|
||||
else /* a[hint] <= key */
|
||||
/* Gallop right until a[hint + last_ofs] <= key < a[hint + ofs] */
|
||||
{
|
||||
gsize max_ofs = len - hint;
|
||||
while (ofs < max_ofs
|
||||
&& gtk_tim_sort_compare (self, key, ELEM (hintp, ofs)) >= 0)
|
||||
{
|
||||
last_ofs = ofs;
|
||||
ofs = (ofs << 1) + 1; /* no need to check for overflow */
|
||||
}
|
||||
if (ofs > max_ofs)
|
||||
ofs = max_ofs;
|
||||
|
||||
/* Make offsets relative to base */
|
||||
last_ofs += hint + 1;
|
||||
ofs += hint;
|
||||
}
|
||||
g_assert (last_ofs <= ofs && ofs <= len);
|
||||
|
||||
/*
|
||||
* Now a[last_ofs - 1] <= key < a[ofs], so key belongs somewhere to
|
||||
* the right of last_ofs but no farther right than ofs. Do a binary
|
||||
* search, with invariant a[last_ofs - 1] <= key < a[ofs].
|
||||
*/
|
||||
while (last_ofs < ofs)
|
||||
{
|
||||
/* gsize m = last_ofs + ((ofs - last_ofs) >> 1); */
|
||||
gsize m = (last_ofs & ofs) + ((last_ofs ^ ofs) >> 1);
|
||||
|
||||
if (gtk_tim_sort_compare (self, key, ELEM (base, m)) < 0)
|
||||
ofs = m; /* key < a[m] */
|
||||
else
|
||||
last_ofs = m + 1; /* a[m] <= key */
|
||||
}
|
||||
g_assert (last_ofs == ofs); /* so a[ofs - 1] <= key < a[ofs] */
|
||||
return ofs;
|
||||
}
|
||||
|
||||
/*
|
||||
* Merges two adjacent runs in place, in a stable fashion. The first
|
||||
* element of the first run must be greater than the first element of the
|
||||
* second run (a[base1] > a[base2]), and the last element of the first run
|
||||
* (a[base1 + len1-1]) must be greater than all elements of the second run.
|
||||
*
|
||||
* For performance, this method should be called only when len1 <= len2;
|
||||
* its twin, merge_hi should be called if len1 >= len2. (Either method
|
||||
* may be called if len1 == len2.)
|
||||
*
|
||||
* @param base1 first element in first run to be merged
|
||||
* @param len1 length of first run to be merged (must be > 0)
|
||||
* @param base2 first element in second run to be merged
|
||||
* (must be aBase + aLen)
|
||||
* @param len2 length of second run to be merged (must be > 0)
|
||||
*/
|
||||
static void
|
||||
gtk_tim_sort(merge_lo) (GtkTimSort *self,
|
||||
gpointer base1,
|
||||
gsize len1,
|
||||
gpointer base2,
|
||||
gsize len2)
|
||||
{
|
||||
/* Copy first run into temp array */
|
||||
gpointer tmp = gtk_tim_sort_ensure_capacity (self, len1);
|
||||
char *cursor1;
|
||||
char *cursor2;
|
||||
char *dest;
|
||||
gsize min_gallop;
|
||||
|
||||
g_assert (len1 > 0 && len2 > 0 && ELEM (base1, len1) == base2);
|
||||
|
||||
/* System.arraycopy(a, base1, tmp, 0, len1); */
|
||||
memcpy (tmp, base1, LEN (len1)); /* POP: can't overlap */
|
||||
|
||||
cursor1 = tmp; /* Indexes into tmp array */
|
||||
cursor2 = base2; /* Indexes int a */
|
||||
dest = base1; /* Indexes int a */
|
||||
|
||||
/* Move first element of second run and deal with degenerate cases */
|
||||
/* a[dest++] = a[cursor2++]; */
|
||||
ASSIGN (dest, cursor2);
|
||||
dest = INCPTR (dest);
|
||||
cursor2 = INCPTR (cursor2);
|
||||
|
||||
if (--len2 == 0)
|
||||
{
|
||||
memcpy (dest, cursor1, LEN (len1)); /* POP: can't overlap */
|
||||
return;
|
||||
}
|
||||
if (len1 == 1)
|
||||
{
|
||||
memmove (dest, cursor2, LEN (len2)); /* POP: overlaps */
|
||||
|
||||
/* a[dest + len2] = tmp[cursor1]; // Last elt of run 1 to end of merge */
|
||||
ASSIGN (ELEM (dest, len2), cursor1);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Use local variable for performance */
|
||||
min_gallop = self->min_gallop;
|
||||
|
||||
while (TRUE)
|
||||
{
|
||||
gsize count1 = 0; /* Number of times in a row that first run won */
|
||||
gsize count2 = 0; /* Number of times in a row that second run won */
|
||||
|
||||
/*
|
||||
* Do the straightforward thing until (if ever) one run starts
|
||||
* winning consistently.
|
||||
*/
|
||||
do
|
||||
{
|
||||
g_assert (len1 > 1 && len2 > 0);
|
||||
if (gtk_tim_sort_compare (self, cursor2, cursor1) < 0)
|
||||
{
|
||||
ASSIGN (dest, cursor2);
|
||||
dest = INCPTR (dest);
|
||||
cursor2 = INCPTR (cursor2);
|
||||
count2++;
|
||||
count1 = 0;
|
||||
if (--len2 == 0)
|
||||
goto outer;
|
||||
if (count2 >= min_gallop)
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSIGN (dest, cursor1);
|
||||
dest = INCPTR (dest);
|
||||
cursor1 = INCPTR (cursor1);
|
||||
count1++;
|
||||
count2 = 0;
|
||||
if (--len1 == 1)
|
||||
goto outer;
|
||||
if (count1 >= min_gallop)
|
||||
break;
|
||||
}
|
||||
}
|
||||
while (TRUE); /* (count1 | count2) < min_gallop); */
|
||||
|
||||
/*
|
||||
* One run is winning so consistently that galloping may be a
|
||||
* huge win. So try that, and continue galloping until (if ever)
|
||||
* neither run appears to be winning consistently anymore.
|
||||
*/
|
||||
do
|
||||
{
|
||||
g_assert (len1 > 1 && len2 > 0);
|
||||
count1 = gtk_tim_sort(gallop_right) (self, cursor2, cursor1, len1, 0);
|
||||
if (count1 != 0)
|
||||
{
|
||||
memcpy (dest, cursor1, LEN (count1)); /* POP: can't overlap */
|
||||
dest = ELEM (dest, count1);
|
||||
cursor1 = ELEM (cursor1, count1);
|
||||
len1 -= count1;
|
||||
if (len1 <= 1) /* len1 == 1 || len1 == 0 */
|
||||
goto outer;
|
||||
}
|
||||
ASSIGN (dest, cursor2);
|
||||
dest = INCPTR (dest);
|
||||
cursor2 = INCPTR (cursor2);
|
||||
if (--len2 == 0)
|
||||
goto outer;
|
||||
|
||||
count2 = gtk_tim_sort(gallop_left) (self, cursor1, cursor2, len2, 0);
|
||||
if (count2 != 0)
|
||||
{
|
||||
memmove (dest, cursor2, LEN (count2)); /* POP: might overlap */
|
||||
dest = ELEM (dest, count2);
|
||||
cursor2 = ELEM (cursor2, count2);
|
||||
len2 -= count2;
|
||||
if (len2 == 0)
|
||||
goto outer;
|
||||
}
|
||||
ASSIGN (dest, cursor1);
|
||||
dest = INCPTR (dest);
|
||||
cursor1 = INCPTR (cursor1);
|
||||
if (--len1 == 1)
|
||||
goto outer;
|
||||
if (min_gallop > 0)
|
||||
min_gallop--;
|
||||
}
|
||||
while (count1 >= MIN_GALLOP || count2 >= MIN_GALLOP);
|
||||
min_gallop += 2; /* Penalize for leaving gallop mode */
|
||||
} /* End of "outer" loop */
|
||||
outer:
|
||||
self->min_gallop = min_gallop < 1 ? 1 : min_gallop; /* Write back to field */
|
||||
|
||||
if (len1 == 1)
|
||||
{
|
||||
g_assert (len2 > 0);
|
||||
memmove (dest, cursor2, LEN (len2)); /* POP: might overlap */
|
||||
ASSIGN (ELEM (dest, len2), cursor1); /* Last elt of run 1 to end of merge */
|
||||
}
|
||||
else if (len1 == 0)
|
||||
{
|
||||
g_critical ("Comparison method violates its general contract");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_assert (len2 == 0);
|
||||
g_assert (len1 > 1);
|
||||
memcpy (dest, cursor1, LEN (len1)); /* POP: can't overlap */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Like merge_lo, except that this method should be called only if
|
||||
* len1 >= len2; merge_lo should be called if len1 <= len2. (Either method
|
||||
* may be called if len1 == len2.)
|
||||
*
|
||||
* @param base1 first element in first run to be merged
|
||||
* @param len1 length of first run to be merged (must be > 0)
|
||||
* @param base2 first element in second run to be merged
|
||||
* (must be aBase + aLen)
|
||||
* @param len2 length of second run to be merged (must be > 0)
|
||||
*/
|
||||
static void
|
||||
gtk_tim_sort(merge_hi) (GtkTimSort *self,
|
||||
gpointer base1,
|
||||
gsize len1,
|
||||
gpointer base2,
|
||||
gsize len2)
|
||||
{
|
||||
/* Copy second run into temp array */
|
||||
gpointer tmp = gtk_tim_sort_ensure_capacity (self, len2);
|
||||
char *cursor1; /* Indexes into a */
|
||||
char *cursor2; /* Indexes into tmp array */
|
||||
char *dest; /* Indexes into a */
|
||||
gsize min_gallop;
|
||||
|
||||
g_assert (len1 > 0 && len2 > 0 && ELEM (base1, len1) == base2);
|
||||
|
||||
memcpy (tmp, base2, LEN (len2)); /* POP: can't overlap */
|
||||
|
||||
cursor1 = ELEM (base1, len1 - 1); /* Indexes into a */
|
||||
cursor2 = ELEM (tmp, len2 - 1); /* Indexes into tmp array */
|
||||
dest = ELEM (base2, len2 - 1); /* Indexes into a */
|
||||
|
||||
/* Move last element of first run and deal with degenerate cases */
|
||||
/* a[dest--] = a[cursor1--]; */
|
||||
ASSIGN (dest, cursor1);
|
||||
dest = DECPTR (dest);
|
||||
cursor1 = DECPTR (cursor1);
|
||||
if (--len1 == 0)
|
||||
{
|
||||
memcpy (ELEM (dest, -(len2 - 1)), tmp, LEN (len2)); /* POP: can't overlap */
|
||||
return;
|
||||
}
|
||||
if (len2 == 1)
|
||||
{
|
||||
dest = ELEM (dest, -len1);
|
||||
cursor1 = ELEM (cursor1, -len1);
|
||||
memmove (ELEM (dest, 1), ELEM (cursor1, 1), LEN (len1)); /* POP: overlaps */
|
||||
/* a[dest] = tmp[cursor2]; */
|
||||
ASSIGN (dest, cursor2);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Use local variable for performance */
|
||||
min_gallop = self->min_gallop;
|
||||
|
||||
while (TRUE)
|
||||
{
|
||||
gsize count1 = 0; /* Number of times in a row that first run won */
|
||||
gsize count2 = 0; /* Number of times in a row that second run won */
|
||||
|
||||
/*
|
||||
* Do the straightforward thing until (if ever) one run
|
||||
* appears to win consistently.
|
||||
*/
|
||||
do
|
||||
{
|
||||
g_assert (len1 > 0 && len2 > 1);
|
||||
if (gtk_tim_sort_compare (self, cursor2, cursor1) < 0)
|
||||
{
|
||||
ASSIGN (dest, cursor1);
|
||||
dest = DECPTR (dest);
|
||||
cursor1 = DECPTR (cursor1);
|
||||
count1++;
|
||||
count2 = 0;
|
||||
if (--len1 == 0)
|
||||
goto outer;
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSIGN (dest, cursor2);
|
||||
dest = DECPTR (dest);
|
||||
cursor2 = DECPTR (cursor2);
|
||||
count2++;
|
||||
count1 = 0;
|
||||
if (--len2 == 1)
|
||||
goto outer;
|
||||
}
|
||||
}
|
||||
while ((count1 | count2) < min_gallop);
|
||||
|
||||
/*
|
||||
* One run is winning so consistently that galloping may be a
|
||||
* huge win. So try that, and continue galloping until (if ever)
|
||||
* neither run appears to be winning consistently anymore.
|
||||
*/
|
||||
do
|
||||
{
|
||||
g_assert (len1 > 0 && len2 > 1);
|
||||
count1 = len1 - gtk_tim_sort(gallop_right) (self, cursor2, base1, len1, len1 - 1);
|
||||
if (count1 != 0)
|
||||
{
|
||||
dest = ELEM (dest, -count1);
|
||||
cursor1 = ELEM (cursor1, -count1);
|
||||
len1 -= count1;
|
||||
memmove (INCPTR (dest), INCPTR (cursor1),
|
||||
LEN (count1)); /* POP: might overlap */
|
||||
if (len1 == 0)
|
||||
goto outer;
|
||||
}
|
||||
ASSIGN (dest, cursor2);
|
||||
dest = DECPTR (dest);
|
||||
cursor2 = DECPTR (cursor2);
|
||||
if (--len2 == 1)
|
||||
goto outer;
|
||||
|
||||
count2 = len2 - gtk_tim_sort(gallop_left) (self, cursor1, tmp, len2, len2 - 1);
|
||||
if (count2 != 0)
|
||||
{
|
||||
dest = ELEM (dest, -count2);
|
||||
cursor2 = ELEM (cursor2, -count2);
|
||||
len2 -= count2;
|
||||
memcpy (INCPTR (dest), INCPTR (cursor2), LEN (count2)); /* POP: can't overlap */
|
||||
if (len2 <= 1) /* len2 == 1 || len2 == 0 */
|
||||
goto outer;
|
||||
}
|
||||
ASSIGN (dest, cursor1);
|
||||
dest = DECPTR (dest);
|
||||
cursor1 = DECPTR (cursor1);
|
||||
if (--len1 == 0)
|
||||
goto outer;
|
||||
if (min_gallop > 0)
|
||||
min_gallop--;
|
||||
}
|
||||
while (count1 >= MIN_GALLOP || count2 >= MIN_GALLOP);
|
||||
min_gallop += 2; /* Penalize for leaving gallop mode */
|
||||
} /* End of "outer" loop */
|
||||
outer:
|
||||
self->min_gallop = min_gallop < 1 ? 1 : min_gallop; /* Write back to field */
|
||||
|
||||
if (len2 == 1)
|
||||
{
|
||||
g_assert (len1 > 0);
|
||||
dest = ELEM (dest, -len1);
|
||||
cursor1 = ELEM (cursor1, -len1);
|
||||
memmove (INCPTR (dest), INCPTR (cursor1), LEN (len1)); /* POP: might overlap */
|
||||
/* a[dest] = tmp[cursor2]; // Move first elt of run2 to front of merge */
|
||||
ASSIGN (dest, cursor2);
|
||||
}
|
||||
else if (len2 == 0)
|
||||
{
|
||||
g_critical ("Comparison method violates its general contract");
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_assert (len1 == 0);
|
||||
g_assert (len2 > 0);
|
||||
memcpy (ELEM (dest, -(len2 - 1)), tmp, LEN (len2)); /* POP: can't overlap */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Merges the two runs at stack indices i and i+1. Run i must be
|
||||
* the penultimate or antepenultimate run on the stack. In other words,
|
||||
* i must be equal to pending_runs-2 or pending_runs-3.
|
||||
*
|
||||
* @param i stack index of the first of the two runs to merge
|
||||
*/
|
||||
static void
|
||||
gtk_tim_sort(merge_at) (GtkTimSort *self,
|
||||
gsize i,
|
||||
GtkTimSortRun *out_change)
|
||||
{
|
||||
gpointer base1 = self->run[i].base;
|
||||
gsize len1 = self->run[i].len;
|
||||
gpointer base2 = self->run[i + 1].base;
|
||||
gsize len2 = self->run[i + 1].len;
|
||||
gsize k;
|
||||
|
||||
g_assert (self->pending_runs >= 2);
|
||||
g_assert (i == self->pending_runs - 2 || i == self->pending_runs - 3);
|
||||
g_assert (len1 > 0 && len2 > 0);
|
||||
g_assert (ELEM (base1, len1) == base2);
|
||||
|
||||
/*
|
||||
* Find where the first element of run2 goes in run1. Prior elements
|
||||
* in run1 can be ignored (because they're already in place).
|
||||
*/
|
||||
k = gtk_tim_sort(gallop_right) (self, base2, base1, len1, 0);
|
||||
base1 = ELEM (base1, k);
|
||||
len1 -= k;
|
||||
if (len1 == 0)
|
||||
{
|
||||
gtk_tim_sort_set_change (out_change, NULL, 0);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find where the last element of run1 goes in run2. Subsequent elements
|
||||
* in run2 can be ignored (because they're already in place).
|
||||
*/
|
||||
len2 = gtk_tim_sort(gallop_left) (self,
|
||||
ELEM (base1, len1 - 1),
|
||||
base2, len2, len2 - 1);
|
||||
if (len2 == 0)
|
||||
{
|
||||
gtk_tim_sort_set_change (out_change, NULL, 0);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Merge remaining runs, using tmp array with min(len1, len2) elements */
|
||||
if (len1 <= len2)
|
||||
{
|
||||
if (len1 > self->max_merge_size)
|
||||
{
|
||||
base1 = ELEM (self->run[i].base, self->run[i].len - self->max_merge_size);
|
||||
gtk_tim_sort(merge_lo) (self, base1, self->max_merge_size, base2, len2);
|
||||
gtk_tim_sort_set_change (out_change, base1, self->max_merge_size + len2);
|
||||
self->run[i].len -= self->max_merge_size;
|
||||
self->run[i + 1].base = ELEM (self->run[i + 1].base, - self->max_merge_size);
|
||||
self->run[i + 1].len += self->max_merge_size;
|
||||
g_assert (ELEM (self->run[i].base, self->run[i].len) == self->run[i + 1].base);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
gtk_tim_sort(merge_lo) (self, base1, len1, base2, len2);
|
||||
gtk_tim_sort_set_change (out_change, base1, len1 + len2);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (len2 > self->max_merge_size)
|
||||
{
|
||||
gtk_tim_sort(merge_hi) (self, base1, len1, base2, self->max_merge_size);
|
||||
gtk_tim_sort_set_change (out_change, base1, len1 + self->max_merge_size);
|
||||
self->run[i].len += self->max_merge_size;
|
||||
self->run[i + 1].base = ELEM (self->run[i + 1].base, self->max_merge_size);
|
||||
self->run[i + 1].len -= self->max_merge_size;
|
||||
g_assert (ELEM (self->run[i].base, self->run[i].len) == self->run[i + 1].base);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
gtk_tim_sort(merge_hi) (self, base1, len1, base2, len2);
|
||||
gtk_tim_sort_set_change (out_change, base1, len1 + len2);
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
/*
|
||||
* Record the length of the combined runs; if i is the 3rd-last
|
||||
* run now, also slide over the last run (which isn't involved
|
||||
* in this merge). The current run (i+1) goes away in any case.
|
||||
*/
|
||||
self->run[i].len += self->run[i + 1].len;
|
||||
if (i == self->pending_runs - 3)
|
||||
self->run[i + 1] = self->run[i + 2];
|
||||
self->pending_runs--;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Examines the stack of runs waiting to be merged and merges adjacent runs
|
||||
* until the stack invariants are reestablished:
|
||||
*
|
||||
* 1. run_len[i - 3] > run_len[i - 2] + run_len[i - 1]
|
||||
* 2. run_len[i - 2] > run_len[i - 1]
|
||||
*
|
||||
* This method is called each time a new run is pushed onto the stack,
|
||||
* so the invariants are guaranteed to hold for i < pending_runs upon
|
||||
* entry to the method.
|
||||
*
|
||||
* POP:
|
||||
* Modified according to http://envisage-project.eu/wp-content/uploads/2015/02/sorting.pdf
|
||||
*
|
||||
* and
|
||||
*
|
||||
* https://bugs.openjdk.java.net/browse/JDK-8072909 (suggestion 2)
|
||||
*
|
||||
*/
|
||||
static gboolean
|
||||
gtk_tim_sort(merge_collapse) (GtkTimSort *self,
|
||||
GtkTimSortRun *out_change)
|
||||
{
|
||||
GtkTimSortRun *run = self->run;
|
||||
gsize n;
|
||||
|
||||
if (self->pending_runs <= 1)
|
||||
return FALSE;
|
||||
|
||||
n = self->pending_runs - 2;
|
||||
if ((n > 0 && run[n - 1].len <= run[n].len + run[n + 1].len) ||
|
||||
(n > 1 && run[n - 2].len <= run[n].len + run[n - 1].len))
|
||||
{
|
||||
if (run[n - 1].len < run[n + 1].len)
|
||||
n--;
|
||||
}
|
||||
else if (run[n].len > run[n + 1].len)
|
||||
{
|
||||
return FALSE; /* Invariant is established */
|
||||
}
|
||||
|
||||
gtk_tim_sort(merge_at) (self, n, out_change);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Merges all runs on the stack until only one remains. This method is
|
||||
* called once, to complete the sort.
|
||||
*/
|
||||
static gboolean
|
||||
gtk_tim_sort(merge_force_collapse) (GtkTimSort *self,
|
||||
GtkTimSortRun *out_change)
|
||||
{
|
||||
gsize n;
|
||||
|
||||
if (self->pending_runs <= 1)
|
||||
return FALSE;
|
||||
|
||||
n = self->pending_runs - 2;
|
||||
if (n > 0 && self->run[n - 1].len < self->run[n + 1].len)
|
||||
n--;
|
||||
gtk_tim_sort(merge_at) (self, n, out_change);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gtk_tim_sort(step) (GtkTimSort *self,
|
||||
GtkTimSortRun *out_change)
|
||||
{
|
||||
g_assert (self);
|
||||
|
||||
if (gtk_tim_sort(merge_collapse) (self, out_change))
|
||||
return TRUE;
|
||||
|
||||
if (gtk_tim_sort(merge_append) (self, out_change))
|
||||
return TRUE;
|
||||
|
||||
if (gtk_tim_sort(merge_force_collapse) (self, out_change))
|
||||
return TRUE;
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
#undef DEFINE_TEMP
|
||||
#undef ASSIGN
|
||||
#undef INCPTR
|
||||
#undef DECPTR
|
||||
#undef ELEM
|
||||
#undef LEN
|
||||
|
||||
#undef CONCAT
|
||||
#undef MAKE_STR
|
||||
#undef gtk_tim_sort
|
||||
|
||||
#undef WIDTH
|
||||
#undef NAME
|
373
gtk/gtktimsort.c
Normal file
373
gtk/gtktimsort.c
Normal file
@ -0,0 +1,373 @@
|
||||
/* Lots of code for an adaptive, stable, natural mergesort. There are many
|
||||
* pieces to this algorithm; read listsort.txt for overviews and details.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gtktimsortprivate.h"
|
||||
|
||||
/*
|
||||
* This is the minimum sized sequence that will be merged. Shorter
|
||||
* sequences will be lengthened by calling binarySort. If the entire
|
||||
* array is less than this length, no merges will be performed.
|
||||
*
|
||||
* This constant should be a power of two. It was 64 in Tim Peter's C
|
||||
* implementation, but 32 was empirically determined to work better in
|
||||
* [Android's Java] implementation. In the unlikely event that you set
|
||||
* this constant to be a number that's not a power of two, you'll need
|
||||
* to change the compute_min_run() computation.
|
||||
*
|
||||
* If you decrease this constant, you must change the
|
||||
* GTK_TIM_SORT_MAX_PENDING value, or you risk running out of space.
|
||||
* See Python's listsort.txt for a discussion of the minimum stack
|
||||
* length required as a function of the length of the array being sorted and
|
||||
* the minimum merge sequence length.
|
||||
*/
|
||||
#define MIN_MERGE 32
|
||||
|
||||
/*
|
||||
* When we get into galloping mode, we stay there until both runs win less
|
||||
* often than MIN_GALLOP consecutive times.
|
||||
*/
|
||||
#define MIN_GALLOP 7
|
||||
|
||||
/*
|
||||
* Returns the minimum acceptable run length for an array of the specified
|
||||
* length. Natural runs shorter than this will be extended with binary sort.
|
||||
*
|
||||
* Roughly speaking, the computation is:
|
||||
*
|
||||
* If n < MIN_MERGE, return n (it's too small to bother with fancy stuff).
|
||||
* Else if n is an exact power of 2, return MIN_MERGE/2.
|
||||
* Else return an int k, MIN_MERGE/2 <= k <= MIN_MERGE, such that n/k
|
||||
* is close to, but strictly less than, an exact power of 2.
|
||||
*
|
||||
* For the rationale, see listsort.txt.
|
||||
*
|
||||
* @param n the length of the array to be sorted
|
||||
* @return the length of the minimum run to be merged
|
||||
*/
|
||||
static gsize
|
||||
compute_min_run (gsize n)
|
||||
{
|
||||
gsize r = 0; // Becomes 1 if any 1 bits are shifted off
|
||||
|
||||
while (n >= MIN_MERGE) {
|
||||
r |= (n & 1);
|
||||
n >>= 1;
|
||||
}
|
||||
return n + r;
|
||||
}
|
||||
|
||||
void
|
||||
gtk_tim_sort_init (GtkTimSort *self,
|
||||
gpointer base,
|
||||
gsize size,
|
||||
gsize element_size,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer data)
|
||||
{
|
||||
self->element_size = element_size;
|
||||
self->base = base;
|
||||
self->size = size;
|
||||
self->compare_func = compare_func;
|
||||
self->data = data;
|
||||
|
||||
self->min_gallop = MIN_GALLOP;
|
||||
self->max_merge_size = G_MAXSIZE;
|
||||
self->min_run = compute_min_run (size);
|
||||
|
||||
self->tmp = NULL;
|
||||
self->tmp_length = 0;
|
||||
self->pending_runs = 0;
|
||||
}
|
||||
|
||||
void
|
||||
gtk_tim_sort_finish (GtkTimSort *self)
|
||||
{
|
||||
g_free (self->tmp);
|
||||
}
|
||||
|
||||
void
|
||||
gtk_tim_sort (gpointer base,
|
||||
gsize size,
|
||||
gsize element_size,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer user_data)
|
||||
{
|
||||
GtkTimSort self;
|
||||
|
||||
gtk_tim_sort_init (&self, base, size, element_size, compare_func, user_data);
|
||||
|
||||
while (gtk_tim_sort_step (&self, NULL));
|
||||
|
||||
gtk_tim_sort_finish (&self);
|
||||
}
|
||||
|
||||
static inline int
|
||||
gtk_tim_sort_compare (GtkTimSort *self,
|
||||
gpointer a,
|
||||
gpointer b)
|
||||
{
|
||||
return self->compare_func (a, b, self->data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Pushes the specified run onto the pending-run stack.
|
||||
*
|
||||
* @param runBase index of the first element in the run
|
||||
* @param runLen the number of elements in the run
|
||||
*/
|
||||
static void
|
||||
gtk_tim_sort_push_run (GtkTimSort *self,
|
||||
void *base,
|
||||
gsize len)
|
||||
{
|
||||
g_assert (self->pending_runs < GTK_TIM_SORT_MAX_PENDING);
|
||||
g_assert (len <= self->size);
|
||||
|
||||
self->run[self->pending_runs].base = base;
|
||||
self->run[self->pending_runs].len = len;
|
||||
self->pending_runs++;
|
||||
|
||||
/* Advance to find next run */
|
||||
self->base = ((char *) self->base) + len * self->element_size;
|
||||
self->size -= len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the external array tmp has at least the specified
|
||||
* number of elements, increasing its size if necessary. The size
|
||||
* increases exponentially to ensure amortized linear time complexity.
|
||||
*
|
||||
* @param min_capacity the minimum required capacity of the tmp array
|
||||
* @return tmp, whether or not it grew
|
||||
*/
|
||||
static gpointer
|
||||
gtk_tim_sort_ensure_capacity (GtkTimSort *self,
|
||||
gsize min_capacity)
|
||||
{
|
||||
if (self->tmp_length < min_capacity)
|
||||
{
|
||||
/* Compute smallest power of 2 > min_capacity */
|
||||
gsize new_size = min_capacity;
|
||||
new_size |= new_size >> 1;
|
||||
new_size |= new_size >> 2;
|
||||
new_size |= new_size >> 4;
|
||||
new_size |= new_size >> 8;
|
||||
new_size |= new_size >> 16;
|
||||
if (sizeof(new_size) > 4)
|
||||
new_size |= new_size >> 32;
|
||||
|
||||
new_size++;
|
||||
if (new_size == 0) /* (overflow) Not bloody likely! */
|
||||
new_size = min_capacity;
|
||||
|
||||
g_free (self->tmp);
|
||||
self->tmp_length = new_size;
|
||||
self->tmp = g_malloc (self->tmp_length * self->element_size);
|
||||
}
|
||||
|
||||
return self->tmp;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_tim_sort_set_change (GtkTimSortRun *out_change,
|
||||
gpointer base,
|
||||
gsize len)
|
||||
{
|
||||
if (out_change)
|
||||
{
|
||||
out_change->base = base;
|
||||
out_change->len = len;
|
||||
}
|
||||
}
|
||||
|
||||
/*<private>
|
||||
* gtk_tim_sort_get_runs:
|
||||
* @self: a #GtkTimSort
|
||||
* @runs: (out) (caller-allocates): Place to store the 0-terminated list of
|
||||
* runs
|
||||
*
|
||||
* Stores the already presorted list of runs - ranges of items that are
|
||||
* known to be sorted among themselves.
|
||||
*
|
||||
* This can be used with gtk_tim_sort_set_runs() when resuming a sort later.
|
||||
**/
|
||||
void
|
||||
gtk_tim_sort_get_runs (GtkTimSort *self,
|
||||
gsize runs[GTK_TIM_SORT_MAX_PENDING + 1])
|
||||
{
|
||||
gsize i;
|
||||
|
||||
g_return_if_fail (self);
|
||||
g_return_if_fail (runs);
|
||||
|
||||
for (i = 0; i < self->pending_runs; i++)
|
||||
runs[i] = self->run[i].len;
|
||||
}
|
||||
|
||||
/*<private>
|
||||
* gtk_tim_sort_set_runs:
|
||||
* @self: a freshly initialized #GtkTimSort
|
||||
* @runs: (array length=zero-terminated): a 0-terminated list of runs
|
||||
*
|
||||
* Sets the list of runs. A run is a range of items that are already
|
||||
* sorted correctly among themselves. Runs must appear at the beginning of
|
||||
* the array.
|
||||
*
|
||||
* Runs can only be set at the beginning of the sort operation.
|
||||
**/
|
||||
void
|
||||
gtk_tim_sort_set_runs (GtkTimSort *self,
|
||||
gsize *runs)
|
||||
{
|
||||
gsize i;
|
||||
|
||||
g_return_if_fail (self);
|
||||
g_return_if_fail (self->pending_runs == 0);
|
||||
|
||||
for (i = 0; runs[i] != 0; i++)
|
||||
gtk_tim_sort_push_run (self, self->base, runs[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
* gtk_tim_sort_set_max_merge_size:
|
||||
* @self: a #GtkTimSort
|
||||
* @max_merge_size: Maximum size of a merge step, 0 for unlimited
|
||||
*
|
||||
* Sets the maximum size of a merge step. Every time
|
||||
* gtk_tim_sort_step() is called and a merge operation has to be
|
||||
* done, the @max_merge_size will be used to limit the size of
|
||||
* the merge.
|
||||
*
|
||||
* The benefit is that merges happen faster, and if you're using
|
||||
* an incremental sorting algorithm in the main thread, this will
|
||||
* limit the runtime.
|
||||
*
|
||||
* The disadvantage is that setting up merges is expensive and that
|
||||
* various optimizations benefit from larger merges, so the total
|
||||
* runtime of the sorting will increase with the number of merges.
|
||||
*
|
||||
* A good estimate is to set a @max_merge_size to 1024 for around
|
||||
* 1ms runtimes, if your compare function is fast.
|
||||
*
|
||||
* By default, max_merge_size is set to unlimited.
|
||||
**/
|
||||
void
|
||||
gtk_tim_sort_set_max_merge_size (GtkTimSort *self,
|
||||
gsize max_merge_size)
|
||||
{
|
||||
g_return_if_fail (self != NULL);
|
||||
|
||||
if (max_merge_size == 0)
|
||||
max_merge_size = G_MAXSIZE;
|
||||
self->max_merge_size = max_merge_size;
|
||||
}
|
||||
|
||||
/**
|
||||
* gtk_tim_sort_get_progress:
|
||||
* @self: a #GtkTimSort
|
||||
*
|
||||
* Does a progress estimate about sort progress, estimates relative
|
||||
* to the number of items to sort.
|
||||
*
|
||||
* Note that this is entirely a progress estimate and does not have
|
||||
* a relationship with items put in their correct place.
|
||||
* It is also an estimate, so no guarantees are made about accuracy,
|
||||
* other than that it will only report 100% completion when it is
|
||||
* indeed done sorting.
|
||||
*
|
||||
* To get a percentage, you need to divide this number by the total
|
||||
* number of elements that are being sorted.
|
||||
*
|
||||
* Returns: Rough guess of sort progress
|
||||
**/
|
||||
gsize
|
||||
gtk_tim_sort_get_progress (GtkTimSort *self)
|
||||
{
|
||||
#define DEPTH 4
|
||||
gsize i;
|
||||
gsize last, progress;
|
||||
|
||||
g_return_val_if_fail (self != NULL, 0);
|
||||
|
||||
if (self->pending_runs == 0)
|
||||
return 0;
|
||||
|
||||
last = self->run[0].len;
|
||||
progress = 0;
|
||||
|
||||
for (i = 1; i < DEPTH + 1 && i < self->pending_runs; i++)
|
||||
{
|
||||
progress += (DEPTH + 1 - i) * MAX (last, self->run[i].len);
|
||||
last = MIN (last, self->run[i].len);
|
||||
}
|
||||
if (i < DEPTH + 1)
|
||||
progress += (DEPTH + 1 - i) * last;
|
||||
|
||||
return progress / DEPTH;
|
||||
#undef DEPTH
|
||||
}
|
||||
|
||||
#if 1
|
||||
#define WIDTH 4
|
||||
#include "gtktimsort-impl.c"
|
||||
|
||||
#define WIDTH 8
|
||||
#include "gtktimsort-impl.c"
|
||||
|
||||
#define WIDTH 16
|
||||
#include "gtktimsort-impl.c"
|
||||
#endif
|
||||
|
||||
#define NAME default
|
||||
#define WIDTH (self->element_size)
|
||||
#include "gtktimsort-impl.c"
|
||||
|
||||
/*
|
||||
* gtk_tim_sort_step:
|
||||
* @self: a #GtkTimSort
|
||||
* @out_change: (optional): Return location for changed
|
||||
* area. If a change did not cause any changes (for example,
|
||||
* if an already sorted array gets sorted), out_change
|
||||
* will be set to %NULL and 0.
|
||||
*
|
||||
* Performs another step in the sorting process. If a
|
||||
* step was performed, %TRUE is returned and @out_change is
|
||||
* set to the smallest area that contains all changes while
|
||||
* sorting.
|
||||
*
|
||||
* If the data is completely sorted, %FALSE will be
|
||||
* returned.
|
||||
*
|
||||
* Returns: %TRUE if an action was performed
|
||||
**/
|
||||
gboolean
|
||||
gtk_tim_sort_step (GtkTimSort *self,
|
||||
GtkTimSortRun *out_change)
|
||||
{
|
||||
gboolean result;
|
||||
|
||||
g_assert (self);
|
||||
|
||||
switch (self->element_size)
|
||||
{
|
||||
case 4:
|
||||
result = gtk_tim_sort_step_4 (self, out_change);
|
||||
break;
|
||||
case 8:
|
||||
result = gtk_tim_sort_step_8 (self, out_change);
|
||||
break;
|
||||
case 16:
|
||||
result = gtk_tim_sort_step_16 (self, out_change);
|
||||
break;
|
||||
default:
|
||||
result = gtk_tim_sort_step_default (self, out_change);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
122
gtk/gtktimsortprivate.h
Normal file
122
gtk/gtktimsortprivate.h
Normal file
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright © 2020 Benjamin Otte
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __GTK_TIMSORT_PRIVATE_H__
|
||||
#define __GTK_TIMSORT_PRIVATE_H__
|
||||
|
||||
#include <gdk/gdk.h>
|
||||
|
||||
/* The maximum number of entries in a GtkTimState's pending-runs stack.
|
||||
* This is enough to sort arrays of size up to about
|
||||
* 32 * phi ** GTK_TIM_SORT_MAX_PENDING
|
||||
* where phi ~= 1.618. 85 is ridiculously large enough, good for an array
|
||||
* with 2**64 elements.
|
||||
*/
|
||||
#define GTK_TIM_SORT_MAX_PENDING 86
|
||||
|
||||
typedef struct _GtkTimSort GtkTimSort;
|
||||
typedef struct _GtkTimSortRun GtkTimSortRun;
|
||||
|
||||
struct _GtkTimSortRun
|
||||
{
|
||||
void *base;
|
||||
gsize len;
|
||||
};
|
||||
|
||||
struct _GtkTimSort
|
||||
{
|
||||
/*
|
||||
* Size of elements. Used to decide on fast paths.
|
||||
*/
|
||||
gsize element_size;
|
||||
|
||||
/* The comparator for this sort.
|
||||
*/
|
||||
GCompareDataFunc compare_func;
|
||||
gpointer data;
|
||||
|
||||
/*
|
||||
* The array being sorted.
|
||||
*/
|
||||
gpointer base;
|
||||
gsize size;
|
||||
|
||||
/*
|
||||
* The maximum size of a merge. It's guaranteed >0 and user-provided.
|
||||
* See the comments for gtk_tim_sort_set_max_merge_size() for details.
|
||||
*/
|
||||
gsize max_merge_size;
|
||||
|
||||
/*
|
||||
* This controls when we get *into* galloping mode. It is initialized
|
||||
* to MIN_GALLOP. The mergeLo and mergeHi methods nudge it higher for
|
||||
* random data, and lower for highly structured data.
|
||||
*/
|
||||
gsize min_gallop;
|
||||
|
||||
/*
|
||||
* The minimum run length. See compute_min_run() for details.
|
||||
*/
|
||||
gsize min_run;
|
||||
|
||||
/*
|
||||
* Temp storage for merges.
|
||||
*/
|
||||
void *tmp;
|
||||
gsize tmp_length;
|
||||
|
||||
/*
|
||||
* A stack of pending runs yet to be merged. Run i starts at
|
||||
* address base[i] and extends for len[i] elements. It's always
|
||||
* true (so long as the indices are in bounds) that:
|
||||
*
|
||||
* runBase[i] + runLen[i] == runBase[i + 1]
|
||||
*
|
||||
* so we could cut the storage for this, but it's a minor amount,
|
||||
* and keeping all the info explicit simplifies the code.
|
||||
*/
|
||||
gsize pending_runs; // Number of pending runs on stack
|
||||
GtkTimSortRun run[GTK_TIM_SORT_MAX_PENDING];
|
||||
};
|
||||
|
||||
void gtk_tim_sort_init (GtkTimSort *self,
|
||||
gpointer base,
|
||||
gsize size,
|
||||
gsize element_size,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer data);
|
||||
void gtk_tim_sort_finish (GtkTimSort *self);
|
||||
|
||||
void gtk_tim_sort_get_runs (GtkTimSort *self,
|
||||
gsize runs[GTK_TIM_SORT_MAX_PENDING + 1]);
|
||||
void gtk_tim_sort_set_runs (GtkTimSort *self,
|
||||
gsize *runs);
|
||||
void gtk_tim_sort_set_max_merge_size (GtkTimSort *self,
|
||||
gsize max_merge_size);
|
||||
|
||||
gsize gtk_tim_sort_get_progress (GtkTimSort *self);
|
||||
|
||||
gboolean gtk_tim_sort_step (GtkTimSort *self,
|
||||
GtkTimSortRun *out_change);
|
||||
|
||||
void gtk_tim_sort (gpointer base,
|
||||
gsize size,
|
||||
gsize element_size,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer user_data);
|
||||
|
||||
#endif /* __GTK_TIMSORT_PRIVATE_H__ */
|
@ -24,6 +24,7 @@
|
||||
#include "gtktreelistmodel.h"
|
||||
|
||||
#include "gtkintl.h"
|
||||
#include "gtksorterprivate.h"
|
||||
#include "gtktypebuiltins.h"
|
||||
|
||||
/**
|
||||
@ -64,6 +65,301 @@ static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
|
||||
|
||||
G_DEFINE_TYPE (GtkTreeListRowSorter, gtk_tree_list_row_sorter, GTK_TYPE_SORTER)
|
||||
|
||||
#define MAX_KEY_DEPTH (8)
|
||||
|
||||
/* our key is a gpointer[MAX_KEY_DEPTH] and contains:
|
||||
*
|
||||
* key[0] != NULL:
|
||||
* The depth of the item is <= MAX_KEY_DEPTH so we can put the keys
|
||||
* inline. This is the key for the ancestor at depth 0.
|
||||
*
|
||||
* key[0] == NULL && key[1] != NULL:
|
||||
* The depth of the item is > MAX_KEY_DEPTH so it had to be allocated.
|
||||
* key[1] contains this allocated and NULL-terminated array.
|
||||
*
|
||||
* key[0] == NULL && key[1] == NULL:
|
||||
* The item is not a TreeListRow. To break ties, we put the item in key[2] to
|
||||
* allow a direct compare.
|
||||
*/
|
||||
typedef struct _GtkTreeListRowSortKeys GtkTreeListRowSortKeys;
|
||||
typedef struct _GtkTreeListRowCacheKey GtkTreeListRowCacheKey;
|
||||
struct _GtkTreeListRowSortKeys
|
||||
{
|
||||
GtkSortKeys keys;
|
||||
|
||||
GtkSortKeys *sort_keys;
|
||||
GHashTable *cached_keys;
|
||||
};
|
||||
|
||||
struct _GtkTreeListRowCacheKey
|
||||
{
|
||||
GtkTreeListRow *row;
|
||||
guint ref_count;
|
||||
};
|
||||
|
||||
static GtkTreeListRowCacheKey *
|
||||
cache_key_from_key (GtkTreeListRowSortKeys *self,
|
||||
gpointer key)
|
||||
{
|
||||
if (self->sort_keys == NULL)
|
||||
return key;
|
||||
|
||||
return (GtkTreeListRowCacheKey *) ((char *) key + GTK_SORT_KEYS_ALIGN (gtk_sort_keys_get_key_size (self->sort_keys), G_ALIGNOF (GtkTreeListRowCacheKey)));
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_tree_list_row_sort_keys_free (GtkSortKeys *keys)
|
||||
{
|
||||
GtkTreeListRowSortKeys *self = (GtkTreeListRowSortKeys *) keys;
|
||||
|
||||
g_assert (g_hash_table_size (self->cached_keys) == 0);
|
||||
g_hash_table_unref (self->cached_keys);
|
||||
if (self->sort_keys)
|
||||
gtk_sort_keys_unref (self->sort_keys);
|
||||
g_slice_free (GtkTreeListRowSortKeys, self);
|
||||
}
|
||||
|
||||
static inline gboolean
|
||||
unpack (gpointer *key,
|
||||
gpointer **out_keys,
|
||||
gsize *out_max_size)
|
||||
{
|
||||
if (key[0])
|
||||
{
|
||||
*out_keys = key;
|
||||
*out_max_size = MAX_KEY_DEPTH;
|
||||
return TRUE;
|
||||
}
|
||||
else if (key[1])
|
||||
{
|
||||
*out_keys = (gpointer *) key[1];
|
||||
*out_max_size = G_MAXSIZE;
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
gtk_tree_list_row_sort_keys_compare (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer data)
|
||||
{
|
||||
GtkTreeListRowSortKeys *self = (GtkTreeListRowSortKeys *) data;
|
||||
gpointer *keysa = (gpointer *) a;
|
||||
gpointer *keysb = (gpointer *) b;
|
||||
gsize sizea, sizeb;
|
||||
gboolean resa, resb;
|
||||
gsize i;
|
||||
GtkOrdering result;
|
||||
|
||||
resa = unpack (keysa, &keysa, &sizea);
|
||||
resb = unpack (keysb, &keysb, &sizeb);
|
||||
if (!resa)
|
||||
return resb ? GTK_ORDERING_LARGER : (keysa[2] < keysb[2] ? GTK_ORDERING_SMALLER :
|
||||
(keysb[2] > keysa[2] ? GTK_ORDERING_LARGER : GTK_ORDERING_EQUAL));
|
||||
else if (!resb)
|
||||
return GTK_ORDERING_SMALLER;
|
||||
|
||||
for (i = 0; i < MIN (sizea, sizeb); i++)
|
||||
{
|
||||
if (keysa[i] == keysb[i])
|
||||
{
|
||||
if (keysa[i] == NULL)
|
||||
return GTK_ORDERING_EQUAL;
|
||||
continue;
|
||||
}
|
||||
else if (keysa[i] == NULL)
|
||||
return GTK_ORDERING_SMALLER;
|
||||
else if (keysb[i] == NULL)
|
||||
return GTK_ORDERING_LARGER;
|
||||
|
||||
if (self->sort_keys)
|
||||
result = gtk_sort_keys_compare (self->sort_keys, keysa[i], keysb[i]);
|
||||
else
|
||||
result = GTK_ORDERING_EQUAL;
|
||||
|
||||
if (result == GTK_ORDERING_EQUAL)
|
||||
{
|
||||
/* We must break ties here because if a ever gets a child,
|
||||
* it would need to go right inbetween a and b. */
|
||||
GtkTreeListRowCacheKey *cachea = cache_key_from_key (self, keysa[i]);
|
||||
GtkTreeListRowCacheKey *cacheb = cache_key_from_key (self, keysb[i]);
|
||||
if (gtk_tree_list_row_get_position (cachea->row) < gtk_tree_list_row_get_position (cacheb->row))
|
||||
result = GTK_ORDERING_SMALLER;
|
||||
else
|
||||
result = GTK_ORDERING_LARGER;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
if (sizea < sizeb)
|
||||
return GTK_ORDERING_SMALLER;
|
||||
else if (sizea > sizeb)
|
||||
return GTK_ORDERING_LARGER;
|
||||
else
|
||||
return GTK_ORDERING_EQUAL;
|
||||
}
|
||||
|
||||
static gboolean
|
||||
gtk_tree_list_row_sort_keys_is_compatible (GtkSortKeys *keys,
|
||||
GtkSortKeys *other)
|
||||
{
|
||||
GtkTreeListRowSortKeys *self = (GtkTreeListRowSortKeys *) keys;
|
||||
GtkTreeListRowSortKeys *compare;
|
||||
|
||||
if (keys->klass != other->klass)
|
||||
return FALSE;
|
||||
|
||||
compare = (GtkTreeListRowSortKeys *) other;
|
||||
|
||||
if (self->sort_keys && compare->sort_keys)
|
||||
return gtk_sort_keys_is_compatible (self->sort_keys, compare->sort_keys);
|
||||
else
|
||||
return self->sort_keys == compare->sort_keys;
|
||||
}
|
||||
|
||||
static gpointer
|
||||
gtk_tree_list_row_sort_keys_ref_key (GtkTreeListRowSortKeys *self,
|
||||
GtkTreeListRow *row)
|
||||
{
|
||||
GtkTreeListRowCacheKey *cache_key;
|
||||
gpointer key;
|
||||
|
||||
key = g_hash_table_lookup (self->cached_keys, row);
|
||||
if (key)
|
||||
{
|
||||
cache_key = cache_key_from_key (self, key);
|
||||
cache_key->ref_count++;
|
||||
return key;
|
||||
}
|
||||
|
||||
if (self->sort_keys)
|
||||
key = g_malloc (GTK_SORT_KEYS_ALIGN (gtk_sort_keys_get_key_size (self->sort_keys), G_ALIGNOF (GtkTreeListRowCacheKey))
|
||||
+ sizeof (GtkTreeListRowCacheKey));
|
||||
else
|
||||
key = g_malloc (sizeof (GtkTreeListRowCacheKey));
|
||||
cache_key = cache_key_from_key (self, key);
|
||||
cache_key->row = g_object_ref (row);
|
||||
cache_key->ref_count = 1;
|
||||
if (self->sort_keys)
|
||||
{
|
||||
gpointer item = gtk_tree_list_row_get_item (row);
|
||||
gtk_sort_keys_init_key (self->sort_keys, item, key);
|
||||
g_object_unref (item);
|
||||
}
|
||||
|
||||
g_hash_table_insert (self->cached_keys, row, key);
|
||||
return key;
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_tree_list_row_sort_keys_unref_key (GtkTreeListRowSortKeys *self,
|
||||
gpointer key)
|
||||
{
|
||||
GtkTreeListRowCacheKey *cache_key = cache_key_from_key (self, key);
|
||||
|
||||
cache_key->ref_count--;
|
||||
if (cache_key->ref_count > 0)
|
||||
return;
|
||||
|
||||
if (self->sort_keys)
|
||||
gtk_sort_keys_clear_key (self->sort_keys, key);
|
||||
|
||||
g_hash_table_remove (self->cached_keys, cache_key->row);
|
||||
g_object_unref (cache_key->row);
|
||||
g_free (key);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_tree_list_row_sort_keys_init_key (GtkSortKeys *keys,
|
||||
gpointer item,
|
||||
gpointer key_memory)
|
||||
{
|
||||
GtkTreeListRowSortKeys *self = (GtkTreeListRowSortKeys *) keys;
|
||||
gpointer *key = (gpointer *) key_memory;
|
||||
GtkTreeListRow *row, *parent;
|
||||
guint i, depth;
|
||||
|
||||
if (!GTK_IS_TREE_LIST_ROW (item))
|
||||
{
|
||||
key[0] = NULL;
|
||||
key[1] = NULL;
|
||||
key[2] = item;
|
||||
return;
|
||||
}
|
||||
|
||||
row = GTK_TREE_LIST_ROW (item);
|
||||
depth = gtk_tree_list_row_get_depth (row) + 1;
|
||||
if (depth > MAX_KEY_DEPTH)
|
||||
{
|
||||
key[0] = NULL;
|
||||
key[1] = g_new (gpointer, depth + 1);
|
||||
key = key[1];
|
||||
key[depth] = NULL;
|
||||
}
|
||||
else if (depth < MAX_KEY_DEPTH)
|
||||
{
|
||||
key[depth] = NULL;
|
||||
}
|
||||
|
||||
g_object_ref (row);
|
||||
for (i = depth; i-- > 0; )
|
||||
{
|
||||
key[i] = gtk_tree_list_row_sort_keys_ref_key (self, row);
|
||||
parent = gtk_tree_list_row_get_parent (row);
|
||||
g_object_unref (row);
|
||||
row = parent;
|
||||
}
|
||||
g_assert (row == NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
gtk_tree_list_row_sort_keys_clear_key (GtkSortKeys *keys,
|
||||
gpointer key_memory)
|
||||
{
|
||||
GtkTreeListRowSortKeys *self = (GtkTreeListRowSortKeys *) keys;
|
||||
gpointer *key = (gpointer *) key_memory;
|
||||
gsize i, max;
|
||||
|
||||
if (!unpack (key, &key, &max))
|
||||
return;
|
||||
|
||||
for (i = 0; i < max && key[i] != NULL; i++)
|
||||
gtk_tree_list_row_sort_keys_unref_key (self, key[i]);
|
||||
|
||||
if (key[0] == NULL)
|
||||
g_free (key[1]);
|
||||
}
|
||||
|
||||
static const GtkSortKeysClass GTK_TREE_LIST_ROW_SORT_KEYS_CLASS =
|
||||
{
|
||||
gtk_tree_list_row_sort_keys_free,
|
||||
gtk_tree_list_row_sort_keys_compare,
|
||||
gtk_tree_list_row_sort_keys_is_compatible,
|
||||
gtk_tree_list_row_sort_keys_init_key,
|
||||
gtk_tree_list_row_sort_keys_clear_key,
|
||||
};
|
||||
|
||||
static GtkSortKeys *
|
||||
gtk_tree_list_row_sort_keys_new (GtkTreeListRowSorter *self)
|
||||
{
|
||||
GtkTreeListRowSortKeys *result;
|
||||
|
||||
result = gtk_sort_keys_new (GtkTreeListRowSortKeys,
|
||||
>K_TREE_LIST_ROW_SORT_KEYS_CLASS,
|
||||
sizeof (gpointer[MAX_KEY_DEPTH]),
|
||||
sizeof (gpointer[MAX_KEY_DEPTH]));
|
||||
|
||||
if (self->sorter)
|
||||
result->sort_keys = gtk_sort_keys_ref (gtk_sorter_get_keys (self->sorter));
|
||||
result->cached_keys = g_hash_table_new (NULL, NULL);
|
||||
|
||||
return (GtkSortKeys *) result;
|
||||
}
|
||||
|
||||
static GtkOrdering
|
||||
gtk_tree_list_row_sorter_compare (GtkSorter *sorter,
|
||||
gpointer item1,
|
||||
@ -164,9 +460,13 @@ gtk_tree_list_row_sorter_get_order (GtkSorter *sorter)
|
||||
}
|
||||
|
||||
static void
|
||||
propagate_changed (GtkSorter *sorter, GtkSorterChange change, gpointer data)
|
||||
propagate_changed (GtkSorter *sorter,
|
||||
GtkSorterChange change,
|
||||
GtkTreeListRowSorter *self)
|
||||
{
|
||||
gtk_sorter_changed (GTK_SORTER (data), change);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
change,
|
||||
gtk_tree_list_row_sort_keys_new (self));
|
||||
}
|
||||
|
||||
static void
|
||||
@ -252,6 +552,9 @@ gtk_tree_list_row_sorter_class_init (GtkTreeListRowSorterClass *class)
|
||||
static void
|
||||
gtk_tree_list_row_sorter_init (GtkTreeListRowSorter *self)
|
||||
{
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_DIFFERENT,
|
||||
gtk_tree_list_row_sort_keys_new (self));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -308,7 +611,9 @@ gtk_tree_list_row_sorter_set_sorter (GtkTreeListRowSorter *self,
|
||||
if (self->sorter)
|
||||
g_signal_connect (sorter, "changed", G_CALLBACK (propagate_changed), self);
|
||||
|
||||
gtk_sorter_changed (GTK_SORTER (self), GTK_SORTER_CHANGE_DIFFERENT);
|
||||
gtk_sorter_changed_with_keys (GTK_SORTER (self),
|
||||
GTK_SORTER_CHANGE_DIFFERENT,
|
||||
gtk_tree_list_row_sort_keys_new (self));
|
||||
|
||||
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SORTER]);
|
||||
}
|
||||
|
@ -133,12 +133,14 @@ gtk_private_sources = files([
|
||||
'gtksearchengine.c',
|
||||
'gtksearchenginemodel.c',
|
||||
'gtksizerequestcache.c',
|
||||
'gtksortkeys.c',
|
||||
'gtkstyleanimation.c',
|
||||
'gtkstylecascade.c',
|
||||
'gtkstyleproperty.c',
|
||||
'gtktextbtree.c',
|
||||
'gtktexthistory.c',
|
||||
'gtktextviewchild.c',
|
||||
'gtktimsort.c',
|
||||
'gtktrashmonitor.c',
|
||||
'gtktreedatalist.c',
|
||||
])
|
||||
|
@ -424,6 +424,7 @@ test_model_changes (gconstpointer model_id)
|
||||
position,
|
||||
remove ? 1 : 0,
|
||||
(gpointer *) &source, 1);
|
||||
g_object_unref (model1);
|
||||
g_object_unref (source);
|
||||
}
|
||||
else if (remove)
|
||||
|
@ -96,12 +96,17 @@ tests = [
|
||||
{ 'name': 'slicelistmodel' },
|
||||
{ 'name': 'sorter' },
|
||||
{ 'name': 'sortlistmodel' },
|
||||
{ 'name': 'sortlistmodel-exhaustive' },
|
||||
{ 'name': 'spinbutton' },
|
||||
{ 'name': 'stringlist' },
|
||||
{ 'name': 'templates' },
|
||||
{ 'name': 'textbuffer' },
|
||||
{ 'name': 'textiter' },
|
||||
{ 'name': 'theme-validate' },
|
||||
{
|
||||
'name': 'timsort',
|
||||
'sources': ['timsort.c', '../../gtk/gtktimsort.c'],
|
||||
},
|
||||
{ 'name': 'tooltips' },
|
||||
{ 'name': 'treelistmodel' },
|
||||
{
|
||||
|
440
testsuite/gtk/sortlistmodel-exhaustive.c
Normal file
440
testsuite/gtk/sortlistmodel-exhaustive.c
Normal file
@ -0,0 +1,440 @@
|
||||
/*
|
||||
* Copyright © 2020 Benjamin Otte
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <locale.h>
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#define ensure_updated() G_STMT_START{ \
|
||||
while (g_main_context_pending (NULL)) \
|
||||
g_main_context_iteration (NULL, TRUE); \
|
||||
}G_STMT_END
|
||||
|
||||
#define assert_model_equal(model1, model2) G_STMT_START{ \
|
||||
guint _i, _n; \
|
||||
g_assert_cmpint (g_list_model_get_n_items (model1), ==, g_list_model_get_n_items (model2)); \
|
||||
_n = g_list_model_get_n_items (model1); \
|
||||
for (_i = 0; _i < _n; _i++) \
|
||||
{ \
|
||||
gpointer o1 = g_list_model_get_item (model1, _i); \
|
||||
gpointer o2 = g_list_model_get_item (model2, _i); \
|
||||
\
|
||||
if (o1 != o2) \
|
||||
{ \
|
||||
char *_s = g_strdup_printf ("Objects differ at index %u out of %u", _i, _n); \
|
||||
g_assertion_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, _s); \
|
||||
g_free (_s); \
|
||||
} \
|
||||
\
|
||||
g_object_unref (o1); \
|
||||
g_object_unref (o2); \
|
||||
} \
|
||||
}G_STMT_END
|
||||
|
||||
G_GNUC_UNUSED static char *
|
||||
model_to_string (GListModel *model)
|
||||
{
|
||||
GString *string;
|
||||
guint i, n;
|
||||
|
||||
n = g_list_model_get_n_items (model);
|
||||
string = g_string_new (NULL);
|
||||
|
||||
/* Check that all unchanged items are indeed unchanged */
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
gpointer item, model_item = g_list_model_get_item (model, i);
|
||||
if (GTK_IS_TREE_LIST_ROW (model_item))
|
||||
item = gtk_tree_list_row_get_item (model_item);
|
||||
else
|
||||
item = model_item;
|
||||
|
||||
if (i > 0)
|
||||
g_string_append (string, ", ");
|
||||
if (G_IS_LIST_MODEL (item))
|
||||
g_string_append (string, "*");
|
||||
else
|
||||
g_string_append (string, gtk_string_object_get_string (item));
|
||||
g_object_unref (model_item);
|
||||
}
|
||||
|
||||
return g_string_free (string, FALSE);
|
||||
}
|
||||
|
||||
static void
|
||||
assert_items_changed_correctly (GListModel *model,
|
||||
guint position,
|
||||
guint removed,
|
||||
guint added,
|
||||
GListModel *compare)
|
||||
{
|
||||
guint i, n_items;
|
||||
|
||||
//g_print ("%s => %u -%u +%u => %s\n", model_to_string (compare), position, removed, added, model_to_string (model));
|
||||
|
||||
g_assert_cmpint (g_list_model_get_n_items (model), ==, g_list_model_get_n_items (compare) - removed + added);
|
||||
n_items = g_list_model_get_n_items (model);
|
||||
|
||||
if (position != 0 || removed != n_items)
|
||||
{
|
||||
/* Check that all unchanged items are indeed unchanged */
|
||||
for (i = 0; i < position; i++)
|
||||
{
|
||||
gpointer o1 = g_list_model_get_item (model, i);
|
||||
gpointer o2 = g_list_model_get_item (compare, i);
|
||||
g_assert_cmphex (GPOINTER_TO_SIZE (o1), ==, GPOINTER_TO_SIZE (o2));
|
||||
g_object_unref (o1);
|
||||
g_object_unref (o2);
|
||||
}
|
||||
for (i = position + added; i < n_items; i++)
|
||||
{
|
||||
gpointer o1 = g_list_model_get_item (model, i);
|
||||
gpointer o2 = g_list_model_get_item (compare, i - added + removed);
|
||||
g_assert_cmphex (GPOINTER_TO_SIZE (o1), ==, GPOINTER_TO_SIZE (o2));
|
||||
g_object_unref (o1);
|
||||
g_object_unref (o2);
|
||||
}
|
||||
|
||||
/* Check that the first and last added item are different from
|
||||
* first and last removed item.
|
||||
* Otherwise we could have kept them as-is
|
||||
*/
|
||||
if (removed > 0 && added > 0)
|
||||
{
|
||||
gpointer o1 = g_list_model_get_item (model, position);
|
||||
gpointer o2 = g_list_model_get_item (compare, position);
|
||||
g_assert_cmphex (GPOINTER_TO_SIZE (o1), !=, GPOINTER_TO_SIZE (o2));
|
||||
g_object_unref (o1);
|
||||
g_object_unref (o2);
|
||||
|
||||
o1 = g_list_model_get_item (model, position + added - 1);
|
||||
o2 = g_list_model_get_item (compare, position + removed - 1);
|
||||
g_assert_cmphex (GPOINTER_TO_SIZE (o1), !=, GPOINTER_TO_SIZE (o2));
|
||||
g_object_unref (o1);
|
||||
g_object_unref (o2);
|
||||
}
|
||||
}
|
||||
|
||||
/* Finally, perform the same change as the signal indicates */
|
||||
g_list_store_splice (G_LIST_STORE (compare), position, removed, NULL, 0);
|
||||
for (i = position; i < position + added; i++)
|
||||
{
|
||||
gpointer item = g_list_model_get_item (G_LIST_MODEL (model), i);
|
||||
g_list_store_insert (G_LIST_STORE (compare), i, item);
|
||||
g_object_unref (item);
|
||||
}
|
||||
}
|
||||
|
||||
static GtkSortListModel *
|
||||
sort_list_model_new (GListModel *source,
|
||||
GtkSorter *sorter)
|
||||
{
|
||||
GtkSortListModel *model;
|
||||
GListStore *check;
|
||||
guint i;
|
||||
|
||||
model = gtk_sort_list_model_new (source, sorter);
|
||||
check = g_list_store_new (G_TYPE_OBJECT);
|
||||
for (i = 0; i < g_list_model_get_n_items (G_LIST_MODEL (model)); i++)
|
||||
{
|
||||
gpointer item = g_list_model_get_item (G_LIST_MODEL (model), i);
|
||||
g_list_store_append (check, item);
|
||||
g_object_unref (item);
|
||||
}
|
||||
g_signal_connect_data (model,
|
||||
"items-changed",
|
||||
G_CALLBACK (assert_items_changed_correctly),
|
||||
check,
|
||||
(GClosureNotify) g_object_unref,
|
||||
0);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
#define N_MODELS 8
|
||||
|
||||
static GtkSortListModel *
|
||||
create_sort_list_model (gconstpointer model_id,
|
||||
gboolean track_changes,
|
||||
GListModel *source,
|
||||
GtkSorter *sorter)
|
||||
{
|
||||
GtkSortListModel *model;
|
||||
guint id = GPOINTER_TO_UINT (model_id);
|
||||
|
||||
if (track_changes)
|
||||
model = sort_list_model_new (id & 1 ? NULL : source, id & 2 ? NULL : sorter);
|
||||
else
|
||||
model = gtk_sort_list_model_new (id & 1 ? NULL : source, id & 2 ? NULL : sorter);
|
||||
|
||||
switch (id >> 2)
|
||||
{
|
||||
case 0:
|
||||
break;
|
||||
|
||||
case 1:
|
||||
//gtk_sort_list_model_set_incremental (model, TRUE);
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
break;
|
||||
}
|
||||
|
||||
if (id & 1)
|
||||
gtk_sort_list_model_set_model (model, source);
|
||||
if (id & 2)
|
||||
gtk_sort_list_model_set_sorter (model, sorter);
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
static GListModel *
|
||||
create_source_model (guint min_size, guint max_size)
|
||||
{
|
||||
const char *strings[] = { "A", "a", "B", "b" };
|
||||
GtkStringList *list;
|
||||
guint i, size;
|
||||
|
||||
size = g_test_rand_int_range (min_size, max_size + 1);
|
||||
list = gtk_string_list_new (NULL);
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
gtk_string_list_append (list, strings[g_test_rand_int_range (0, G_N_ELEMENTS (strings))]);
|
||||
|
||||
return G_LIST_MODEL (list);
|
||||
}
|
||||
|
||||
#define N_SORTERS 3
|
||||
|
||||
static GtkSorter *
|
||||
create_sorter (gsize id)
|
||||
{
|
||||
GtkSorter *sorter;
|
||||
|
||||
switch (id)
|
||||
{
|
||||
case 0:
|
||||
return gtk_string_sorter_new (NULL);
|
||||
|
||||
case 1:
|
||||
case 2:
|
||||
/* match all As, Bs and nothing */
|
||||
sorter = gtk_string_sorter_new (gtk_property_expression_new (GTK_TYPE_STRING_OBJECT, NULL, "string"));
|
||||
if (id == 1)
|
||||
gtk_string_sorter_set_ignore_case (GTK_STRING_SORTER (sorter), TRUE);
|
||||
return sorter;
|
||||
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static GtkSorter *
|
||||
create_random_sorter (gboolean allow_null)
|
||||
{
|
||||
guint n;
|
||||
|
||||
if (allow_null)
|
||||
n = g_test_rand_int_range (0, N_SORTERS + 1);
|
||||
else
|
||||
n = g_test_rand_int_range (0, N_SORTERS);
|
||||
|
||||
if (n >= N_SORTERS)
|
||||
return NULL;
|
||||
|
||||
return create_sorter (n);
|
||||
}
|
||||
|
||||
/* Compare this:
|
||||
* source => sorter1 => sorter2
|
||||
* with:
|
||||
* source => multisorter(sorter1, sorter2)
|
||||
* and randomly change the source and sorters and see if the
|
||||
* two continue agreeing.
|
||||
*/
|
||||
static void
|
||||
test_two_sorters (gconstpointer model_id)
|
||||
{
|
||||
GtkSortListModel *compare;
|
||||
GtkSortListModel *model1, *model2;
|
||||
GListModel *source;
|
||||
GtkSorter *every, *sorter;
|
||||
guint i, j, k;
|
||||
|
||||
source = create_source_model (10, 10);
|
||||
model2 = create_sort_list_model (model_id, TRUE, source, NULL);
|
||||
/* can't track changes from a sortmodel, where the same items get reordered */
|
||||
model1 = create_sort_list_model (model_id, FALSE, G_LIST_MODEL (model2), NULL);
|
||||
every = gtk_multi_sorter_new ();
|
||||
compare = create_sort_list_model (model_id, TRUE, source, every);
|
||||
g_object_unref (every);
|
||||
g_object_unref (source);
|
||||
|
||||
for (i = 0; i < N_SORTERS; i++)
|
||||
{
|
||||
sorter = create_sorter (i);
|
||||
gtk_sort_list_model_set_sorter (model1, sorter);
|
||||
gtk_multi_sorter_append (GTK_MULTI_SORTER (every), sorter);
|
||||
|
||||
for (j = 0; j < N_SORTERS; j++)
|
||||
{
|
||||
sorter = create_sorter (i);
|
||||
gtk_sort_list_model_set_sorter (model2, sorter);
|
||||
gtk_multi_sorter_append (GTK_MULTI_SORTER (every), sorter);
|
||||
|
||||
ensure_updated ();
|
||||
assert_model_equal (G_LIST_MODEL (model2), G_LIST_MODEL (compare));
|
||||
|
||||
for (k = 0; k < 10; k++)
|
||||
{
|
||||
source = create_source_model (0, 1000);
|
||||
gtk_sort_list_model_set_model (compare, source);
|
||||
gtk_sort_list_model_set_model (model2, source);
|
||||
g_object_unref (source);
|
||||
|
||||
ensure_updated ();
|
||||
assert_model_equal (G_LIST_MODEL (model1), G_LIST_MODEL (compare));
|
||||
}
|
||||
|
||||
gtk_multi_sorter_remove (GTK_MULTI_SORTER (every), 1);
|
||||
}
|
||||
|
||||
gtk_multi_sorter_remove (GTK_MULTI_SORTER (every), 0);
|
||||
}
|
||||
|
||||
g_object_unref (compare);
|
||||
g_object_unref (model2);
|
||||
g_object_unref (model1);
|
||||
}
|
||||
|
||||
/* Run:
|
||||
* source => sorter1 => sorter2
|
||||
* and randomly add/remove sources and change the sorters and
|
||||
* see if the two sorters stay identical
|
||||
*/
|
||||
static void
|
||||
test_stability (gconstpointer model_id)
|
||||
{
|
||||
GListStore *store;
|
||||
GtkFlattenListModel *flatten;
|
||||
GtkSortListModel *sort1, *sort2;
|
||||
GtkSorter *sorter;
|
||||
gsize i;
|
||||
|
||||
sorter = create_random_sorter (TRUE);
|
||||
|
||||
store = g_list_store_new (G_TYPE_OBJECT);
|
||||
flatten = gtk_flatten_list_model_new (G_LIST_MODEL (store));
|
||||
sort1 = create_sort_list_model (model_id, TRUE, G_LIST_MODEL (flatten), sorter);
|
||||
sort2 = create_sort_list_model (model_id, FALSE, G_LIST_MODEL (sort1), sorter);
|
||||
g_clear_object (&sorter);
|
||||
|
||||
for (i = 0; i < 500; i++)
|
||||
{
|
||||
gboolean add = FALSE, remove = FALSE;
|
||||
guint position;
|
||||
|
||||
switch (g_test_rand_int_range (0, 4))
|
||||
{
|
||||
case 0:
|
||||
/* change the sorter */
|
||||
sorter = create_random_sorter (TRUE);
|
||||
gtk_sort_list_model_set_sorter (sort1, sorter);
|
||||
gtk_sort_list_model_set_sorter (sort2, sorter);
|
||||
g_clear_object (&sorter);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
/* remove a model */
|
||||
remove = TRUE;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
/* add a model */
|
||||
add = TRUE;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
/* replace a model */
|
||||
remove = TRUE;
|
||||
add = TRUE;
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached ();
|
||||
break;
|
||||
}
|
||||
|
||||
position = g_test_rand_int_range (0, g_list_model_get_n_items (G_LIST_MODEL (store)) + 1);
|
||||
if (g_list_model_get_n_items (G_LIST_MODEL (store)) == position)
|
||||
remove = FALSE;
|
||||
|
||||
if (add)
|
||||
{
|
||||
/* We want at least one element, otherwise the sorters will see no changes */
|
||||
GListModel *source = create_source_model (1, 50);
|
||||
g_list_store_splice (store,
|
||||
position,
|
||||
remove ? 1 : 0,
|
||||
(gpointer *) &source, 1);
|
||||
g_object_unref (source);
|
||||
}
|
||||
else if (remove)
|
||||
{
|
||||
g_list_store_remove (store, position);
|
||||
}
|
||||
|
||||
if (g_test_rand_bit ())
|
||||
{
|
||||
ensure_updated ();
|
||||
assert_model_equal (G_LIST_MODEL (sort1), G_LIST_MODEL (sort2));
|
||||
}
|
||||
}
|
||||
|
||||
g_object_unref (sort2);
|
||||
g_object_unref (sort1);
|
||||
g_object_unref (flatten);
|
||||
g_object_unref (store);
|
||||
}
|
||||
|
||||
static void
|
||||
add_test_for_all_models (const char *name,
|
||||
GTestDataFunc test_func)
|
||||
{
|
||||
guint i;
|
||||
|
||||
for (i = 0; i < N_MODELS; i++)
|
||||
{
|
||||
char *path = g_strdup_printf ("/sorterlistmodel/model%u/%s", i, name);
|
||||
g_test_add_data_func (path, GUINT_TO_POINTER (i), test_func);
|
||||
g_free (path);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
setlocale (LC_ALL, "C");
|
||||
|
||||
add_test_for_all_models ("two-sorters", test_two_sorters);
|
||||
add_test_for_all_models ("stability", test_stability);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
@ -306,9 +306,7 @@ test_set_sorter (void)
|
||||
gtk_sort_list_model_set_sorter (sort, sorter);
|
||||
g_object_unref (sorter);
|
||||
assert_model (sort, "2 4 6 8 10");
|
||||
/* Technically, this is correct, but we shortcut setting the sort func:
|
||||
* assert_changes (sort, "0-4+4"); */
|
||||
assert_changes (sort, "0-5+5");
|
||||
assert_changes (sort, "0-4+4");
|
||||
|
||||
g_object_unref (store);
|
||||
g_object_unref (sort);
|
||||
@ -394,6 +392,28 @@ test_remove_items (void)
|
||||
g_object_unref (sort);
|
||||
}
|
||||
|
||||
static void
|
||||
test_stability (void)
|
||||
{
|
||||
GtkSortListModel *sort;
|
||||
GListStore *store;
|
||||
GtkSorter *sorter;
|
||||
|
||||
store = new_store ((guint[]) { 11, 31, 21, 1, 0 });
|
||||
sort = new_model (store);
|
||||
assert_model (sort, "1 11 21 31");
|
||||
assert_changes (sort, "");
|
||||
|
||||
sorter = gtk_custom_sorter_new (compare_modulo, GUINT_TO_POINTER (5), NULL);
|
||||
gtk_sort_list_model_set_sorter (sort, sorter);
|
||||
g_object_unref (sorter);
|
||||
assert_model (sort, "11 31 21 1");
|
||||
assert_changes (sort, "0-4+4");
|
||||
|
||||
g_object_unref (store);
|
||||
g_object_unref (sort);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
@ -411,6 +431,7 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/sortlistmodel/add_items", test_add_items);
|
||||
g_test_add_func ("/sortlistmodel/remove_items", test_remove_items);
|
||||
#endif
|
||||
g_test_add_func ("/sortlistmodel/stability", test_stability);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
253
testsuite/gtk/timsort.c
Normal file
253
testsuite/gtk/timsort.c
Normal file
@ -0,0 +1,253 @@
|
||||
/*
|
||||
* Copyright © 2020 Benjamin Otte
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <locale.h>
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#include "gtk/gtktimsortprivate.h"
|
||||
|
||||
#define assert_sort_equal(a, b, size, n) \
|
||||
g_assert_cmpmem (a, sizeof (size) * n, b, sizeof (size) * n)
|
||||
|
||||
static int
|
||||
compare_int (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer unused)
|
||||
{
|
||||
int ia = *(const int *) a;
|
||||
int ib = *(const int *) b;
|
||||
|
||||
return ia < ib ? -1 : (ia > ib);
|
||||
}
|
||||
|
||||
static int
|
||||
compare_pointer (gconstpointer a,
|
||||
gconstpointer b,
|
||||
gpointer unused)
|
||||
{
|
||||
gpointer pa = *(const gpointer *) a;
|
||||
gpointer pb = *(const gpointer *) b;
|
||||
|
||||
return pa < pb ? -1 : (pa > pb);
|
||||
}
|
||||
G_GNUC_UNUSED static void
|
||||
dump (int *a,
|
||||
gsize n)
|
||||
{
|
||||
gsize i;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
if (i)
|
||||
g_print(", ");
|
||||
g_print ("%d", a[i]);
|
||||
}
|
||||
g_print ("\n");
|
||||
}
|
||||
|
||||
static void
|
||||
run_comparison (gpointer a,
|
||||
gsize n,
|
||||
gsize element_size,
|
||||
GCompareDataFunc compare_func,
|
||||
gpointer data)
|
||||
{
|
||||
gint64 start, mid, end;
|
||||
gpointer b;
|
||||
|
||||
b = g_memdup (a, element_size * n);
|
||||
|
||||
start = g_get_monotonic_time ();
|
||||
gtk_tim_sort (a, n, element_size, compare_func, data);
|
||||
mid = g_get_monotonic_time ();
|
||||
g_qsort_with_data (b, n, element_size, compare_func, data);
|
||||
end = g_get_monotonic_time ();
|
||||
|
||||
g_test_message ("%zu items in %uus vs %uus (%u%%)",
|
||||
n,
|
||||
(guint) (mid - start),
|
||||
(guint) (end - mid),
|
||||
(guint) (100 * (mid - start) / MAX (1, end - mid)));
|
||||
assert_sort_equal (a, b, int, n);
|
||||
|
||||
g_free (b);
|
||||
}
|
||||
|
||||
static void
|
||||
test_integers (void)
|
||||
{
|
||||
int *a;
|
||||
gsize i, n, run;
|
||||
|
||||
a = g_new (int, 1000);
|
||||
|
||||
for (run = 0; run < 10; run++)
|
||||
{
|
||||
n = g_test_rand_int_range (0, 1000);
|
||||
for (i = 0; i < n; i++)
|
||||
a[i] = g_test_rand_int ();
|
||||
|
||||
run_comparison (a, n, sizeof (int), compare_int, NULL);
|
||||
}
|
||||
|
||||
g_free (a);
|
||||
}
|
||||
|
||||
static void
|
||||
test_integers_runs (void)
|
||||
{
|
||||
int *a;
|
||||
gsize i, j, n, run;
|
||||
|
||||
a = g_new (int, 1000);
|
||||
|
||||
for (run = 0; run < 10; run++)
|
||||
{
|
||||
n = g_test_rand_int_range (0, 1000);
|
||||
for (i = 0; i < n; i++)
|
||||
{
|
||||
a[i] = g_test_rand_int ();
|
||||
j = i + g_test_rand_int_range (0, 20);
|
||||
j = MIN (n, j);
|
||||
if (g_test_rand_bit ())
|
||||
{
|
||||
for (i++; i < j; i++)
|
||||
a[i] = a[i - 1] + 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i++; i < j; i++)
|
||||
a[i] = a[i - 1] - 1;
|
||||
}
|
||||
}
|
||||
|
||||
run_comparison (a, n, sizeof (int), compare_int, NULL);
|
||||
}
|
||||
|
||||
g_free (a);
|
||||
}
|
||||
|
||||
static void
|
||||
test_integers_huge (void)
|
||||
{
|
||||
int *a;
|
||||
gsize i, n;
|
||||
|
||||
n = g_test_rand_int_range (2 * 1000 * 1000, 5 * 1000 * 1000);
|
||||
|
||||
a = g_new (int, n);
|
||||
for (i = 0; i < n; i++)
|
||||
a[i] = g_test_rand_int ();
|
||||
|
||||
run_comparison (a, n, sizeof (int), compare_int, NULL);
|
||||
|
||||
g_free (a);
|
||||
}
|
||||
|
||||
static void
|
||||
test_pointers (void)
|
||||
{
|
||||
gpointer *a;
|
||||
gsize i, n, run;
|
||||
|
||||
a = g_new (gpointer, 1000);
|
||||
|
||||
for (run = 0; run < 10; run++)
|
||||
{
|
||||
n = g_test_rand_int_range (0, 1000);
|
||||
for (i = 0; i < n; i++)
|
||||
a[i] = GINT_TO_POINTER (g_test_rand_int ());
|
||||
|
||||
run_comparison (a, n, sizeof (gpointer), compare_pointer, NULL);
|
||||
}
|
||||
|
||||
g_free (a);
|
||||
}
|
||||
|
||||
static void
|
||||
test_pointers_huge (void)
|
||||
{
|
||||
gpointer *a;
|
||||
gsize i, n;
|
||||
|
||||
n = g_test_rand_int_range (2 * 1000 * 1000, 5 * 1000 * 1000);
|
||||
|
||||
a = g_new (gpointer, n);
|
||||
for (i = 0; i < n; i++)
|
||||
a[i] = GINT_TO_POINTER (g_test_rand_int ());
|
||||
|
||||
run_comparison (a, n, sizeof (gpointer), compare_pointer, NULL);
|
||||
|
||||
g_free (a);
|
||||
}
|
||||
|
||||
static void
|
||||
test_steps (void)
|
||||
{
|
||||
GtkTimSortRun change;
|
||||
GtkTimSort sort;
|
||||
int *a, *b;
|
||||
gsize i, n;
|
||||
|
||||
n = g_test_rand_int_range (20 * 1000, 50 * 1000);
|
||||
|
||||
a = g_new (int, n);
|
||||
for (i = 0; i < n; i++)
|
||||
a[i] = g_test_rand_int ();
|
||||
b = g_memdup (a, sizeof (int) * n);
|
||||
|
||||
gtk_tim_sort_init (&sort, a, n, sizeof (int), compare_int, NULL);
|
||||
gtk_tim_sort_set_max_merge_size (&sort, g_test_rand_int_range (512, 2048));
|
||||
|
||||
while (gtk_tim_sort_step (&sort, &change))
|
||||
{
|
||||
if (change.len)
|
||||
{
|
||||
int *a_start = change.base;
|
||||
int *b_start = b + ((int *) change.base - a);
|
||||
g_assert_cmpint (a_start[0], !=, b_start[0]);
|
||||
g_assert_cmpint (a_start[change.len - 1], !=, b_start[change.len - 1]);
|
||||
memcpy (b_start, a_start, change.len * sizeof (int));
|
||||
}
|
||||
|
||||
assert_sort_equal (a, b, int, n);
|
||||
}
|
||||
|
||||
g_qsort_with_data (b, n, sizeof (int), compare_int, NULL);
|
||||
assert_sort_equal (a, b, int, n);
|
||||
|
||||
gtk_tim_sort_finish (&sort);
|
||||
g_free (b);
|
||||
g_free (a);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
setlocale (LC_ALL, "C");
|
||||
|
||||
g_test_add_func ("/timsort/integers", test_integers);
|
||||
g_test_add_func ("/timsort/integers/runs", test_integers_runs);
|
||||
g_test_add_func ("/timsort/integers/huge", test_integers_huge);
|
||||
g_test_add_func ("/timsort/pointers", test_pointers);
|
||||
g_test_add_func ("/timsort/pointers/huge", test_pointers_huge);
|
||||
g_test_add_func ("/timsort/steps", test_steps);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
Loading…
Reference in New Issue
Block a user