| 1 |
/* |
| 2 |
* Copyright © 2020 Benjamin Otte |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#pragma once |
| 19 |
|
| 20 |
#include <glib-object.h> |
| 21 |
|
| 22 |
/* The maximum number of entries in a GtkTimState's pending-runs stack. |
| 23 |
* This is enough to sort arrays of size up to about |
| 24 |
* 32 * phi ** GTK_TIM_SORT_MAX_PENDING |
| 25 |
* where phi ~= 1.618. 85 is ridiculously large enough, good for an array |
| 26 |
* with 2**64 elements. |
| 27 |
*/ |
| 28 |
#define GTK_TIM_SORT_MAX_PENDING 86 |
| 29 |
|
| 30 |
typedef struct _GtkTimSort GtkTimSort; |
| 31 |
typedef struct _GtkTimSortRun GtkTimSortRun; |
| 32 |
|
| 33 |
struct _GtkTimSortRun |
| 34 |
{ |
| 35 |
void *base; |
| 36 |
gsize len; |
| 37 |
}; |
| 38 |
|
| 39 |
struct _GtkTimSort |
| 40 |
{ |
| 41 |
/* |
| 42 |
* Size of elements. Used to decide on fast paths. |
| 43 |
*/ |
| 44 |
gsize element_size; |
| 45 |
|
| 46 |
/* The comparator for this sort. |
| 47 |
*/ |
| 48 |
GCompareDataFunc compare_func; |
| 49 |
gpointer data; |
| 50 |
|
| 51 |
/* |
| 52 |
* The array being sorted. |
| 53 |
*/ |
| 54 |
gpointer base; |
| 55 |
gsize size; |
| 56 |
|
| 57 |
/* |
| 58 |
* The maximum size of a merge. It's guaranteed >0 and user-provided. |
| 59 |
* See the comments for gtk_tim_sort_set_max_merge_size() for details. |
| 60 |
*/ |
| 61 |
gsize max_merge_size; |
| 62 |
|
| 63 |
/* |
| 64 |
* This controls when we get *into* galloping mode. It is initialized |
| 65 |
* to MIN_GALLOP. The mergeLo and mergeHi methods nudge it higher for |
| 66 |
* random data, and lower for highly structured data. |
| 67 |
*/ |
| 68 |
gsize min_gallop; |
| 69 |
|
| 70 |
/* |
| 71 |
* The minimum run length. See compute_min_run() for details. |
| 72 |
*/ |
| 73 |
gsize min_run; |
| 74 |
|
| 75 |
/* |
| 76 |
* Temp storage for merges. |
| 77 |
*/ |
| 78 |
void *tmp; |
| 79 |
gsize tmp_length; |
| 80 |
|
| 81 |
/* |
| 82 |
* A stack of pending runs yet to be merged. Run i starts at |
| 83 |
* address base[i] and extends for len[i] elements. It's always |
| 84 |
* true (so long as the indices are in bounds) that: |
| 85 |
* |
| 86 |
* runBase[i] + runLen[i] == runBase[i + 1] |
| 87 |
* |
| 88 |
* so we could cut the storage for this, but it's a minor amount, |
| 89 |
* and keeping all the info explicit simplifies the code. |
| 90 |
*/ |
| 91 |
gsize pending_runs; // Number of pending runs on stack |
| 92 |
GtkTimSortRun run[GTK_TIM_SORT_MAX_PENDING]; |
| 93 |
}; |
| 94 |
|
| 95 |
void gtk_tim_sort_init (GtkTimSort *self, |
| 96 |
gpointer base, |
| 97 |
gsize size, |
| 98 |
gsize element_size, |
| 99 |
GCompareDataFunc compare_func, |
| 100 |
gpointer data); |
| 101 |
void gtk_tim_sort_finish (GtkTimSort *self); |
| 102 |
|
| 103 |
void gtk_tim_sort_get_runs (GtkTimSort *self, |
| 104 |
gsize runs[GTK_TIM_SORT_MAX_PENDING + 1]); |
| 105 |
void gtk_tim_sort_set_runs (GtkTimSort *self, |
| 106 |
gsize *runs); |
| 107 |
void gtk_tim_sort_set_max_merge_size (GtkTimSort *self, |
| 108 |
gsize max_merge_size); |
| 109 |
|
| 110 |
gsize gtk_tim_sort_get_progress (GtkTimSort *self); |
| 111 |
|
| 112 |
gboolean gtk_tim_sort_step (GtkTimSort *self, |
| 113 |
GtkTimSortRun *out_change); |
| 114 |
|
| 115 |
void gtk_tim_sort (gpointer base, |
| 116 |
gsize size, |
| 117 |
gsize element_size, |
| 118 |
GCompareDataFunc compare_func, |
| 119 |
gpointer user_data); |
| 120 |
|