aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/table/e-table-sorting-utils.c
diff options
context:
space:
mode:
authorChristopher James Lahey <clahey@ximian.com>2001-03-20 12:43:42 +0800
committerChris Lahey <clahey@src.gnome.org>2001-03-20 12:43:42 +0800
commit1510304c2de206313c637d9269b4fb451cb50adb (patch)
tree391fc87bab4413e5eec82de476d6ca08db2ce27d /widgets/table/e-table-sorting-utils.c
parent0629bbb778bf9634c380067b65a7f62f2f05a676 (diff)
downloadgsoc2013-evolution-1510304c2de206313c637d9269b4fb451cb50adb.tar
gsoc2013-evolution-1510304c2de206313c637d9269b4fb451cb50adb.tar.gz
gsoc2013-evolution-1510304c2de206313c637d9269b4fb451cb50adb.tar.bz2
gsoc2013-evolution-1510304c2de206313c637d9269b4fb451cb50adb.tar.lz
gsoc2013-evolution-1510304c2de206313c637d9269b4fb451cb50adb.tar.xz
gsoc2013-evolution-1510304c2de206313c637d9269b4fb451cb50adb.tar.zst
gsoc2013-evolution-1510304c2de206313c637d9269b4fb451cb50adb.zip
Deal with proxy_node_changed being called on a different root node than
2001-03-19 Christopher James Lahey <clahey@ximian.com> * e-tree-sorted.c (ets_proxy_node_changed): Deal with proxy_node_changed being called on a different root node than the one we have in our tree. * e-tree-table-adapter.c: Did some general clean up here. * Merged branch: 2001-03-19 Christopher James Lahey <clahey@ximian.com> * e-tree-sorted.c (find_child_path): Added this function to allow us to find paths that have been removed from the source. (ets_proxy_node_removed): Fixed the memmove here a bit. Call find_child_path. * e-tree-table-adapter.c (find_node): Check that the passed in path isn't NULL. 2001-03-19 Christopher James Lahey <clahey@ximian.com> * e-table-item.c (eti_reflow): Get width from header object instead of calculating it ourselves. * e-table-selection-model.c: Turn off selection saving since it's so slow. * e-table.c (e_table_set_state_object): Set the width of the newly created header object. * e-tree.c (e_tree_set_state_object): Set the width of the newly created header object. (tree_canvas_size_allocate): Don't bother setting the dimensions of the white background twice. 2001-03-18 Christopher James Lahey <clahey@ximian.com> * e-table-selection-model.c, e-table-selection-model.h: Made ETableSelectionModel save the cursor properly across changed signals if has_save_id is true. 2001-03-18 Christopher James Lahey <clahey@ximian.com> * e-table-selection-model.c, e-table-selection-model.h: Made ETableSelectionModel save selection properly across changed signals if has_save_id is true. * e-tree-memory.c: A couple of typos. 2001-03-18 Christopher James Lahey <clahey@ximian.com> * e-tree-memory.c, e-tree-sorted.c: Send pre_changes properly. 2001-03-18 Christopher James Lahey <clahey@ximian.com> * e-tree-table-adapter.c: Send pre_changes when performing set_expanded or root_node_set_visible. 2001-03-18 Christopher James Lahey <clahey@ximian.com> * e-tree-sorted.c (ets_is_expandable): When the API requests whether the object is expandable and it isn't, make sure to send a signal when it becomes expandable. * e-tree-table-adapter.c: Made it so that in a number of cases where it doesn't need to create an empty hash table node if the current tree node has no children, it doesn't. 2001-03-18 Christopher James Lahey <clahey@ximian.com> * e-tree-memory-callbacks.c, e-tree-memory-callbacks.h (etmc_has_save_id, etmc_get_save_id): Added has_save_id and get_save_id to the list of methods supported by e_tree_memory_callbacks. * e-tree-table-adapter.c, e-tree-table-adapter.h: Added saving of expanded nodes. 2001-03-18 Christopher James Lahey <clahey@ximian.com> * e-table-model.c, e-table-model.h (e_table_model_get_save_id): Changed row_save_id to get_save_id to be consistent with ETree. * e-tree-model.c, e-tree-model.h: Added "pre_change" signal. Added has_save_id and get_save_id methods. * e-tree-sorted.c: Proxy pre_change signal. Implemented has_save_id and get_save_id. If the base model doesn't provide has_save_id then we g_strdup_printf the pointer of the base model ETreePath as the save_id. * e-tree-table-adapter.c: Proxy pre_change signal. If base model has_save_id, then use the results of get_save_id as the key for the hash table of node attributes. Otherwise use the pointer as before. 2001-03-17 Christopher James Lahey <clahey@ximian.com> * e-tree-sorted.c (ets_sort_idle): Fixed it so that all nodes get sorted properly instead of just the top node. 2001-03-17 Christopher James Lahey <clahey@ximian.com> * e-table-sorting-utils.c (e_table_sorting_utils_tree_sort): Made tree sorting faster by using a cache. * e-tree-sorted.c: Added commented out debugging g_prints. 2001-03-17 Christopher James Lahey <clahey@ximian.com> * e-tree-sorted.c: Switched to using GMemChunks. 2001-03-17 Christopher James Lahey <clahey@ximian.com> * e-tree-sorted.c (resort_node): Made it so that children of a node that's being sorted don't send changed signals. 2001-03-17 Christopher James Lahey <clahey@ximian.com> * e-table-sorting-utils.c, e-table-sorting-utils.h: Switched to using e_sort and e_search instead of qsort and a linear search. Added the tree functions e_table_sorting_utils_tree_sort, e_table_sorting_utils_tree_check_position, and e_table_sorting_utils_tree_insert. * e-tree-sorted.c: Made this actually do sorting. * e-tree-table-adapter.c (etta_proxy_node_changed): The old_size needs to be the number of visible children + 1 to include the top node. * e-tree.c (e_tree_set_state_object): Set the sort_info on the ETreeSorted when you get a new sort_info. 2001-03-16 Christopher James Lahey <clahey@ximian.com> * Makefile.am: Added e-tree-sorted.c and e-tree-sorted.h. * e-table-item.c (eti_realize_cell_views): Only realize the cells if they're not realized already and if the canvas is realized. * e-table-sorted.c (ets_proxy_model_cell_changed): Matched the change to the signature of e_table_sorting_utils_affects_sort. * e-table-sorting-utils.c, e-table-sorting-utils.h (e_table_sorting_utils_affects_sort): Changed the signature of this function to not take the ETableModel source since it doesn't use it and we need to use this function for ETreeSorted which doesn't have an ETableModel. * e-tree-memory.c (etmm_get_expanded_default): Actually implement the get_expanded_default for this tree. * e-tree-model.h: Cleaned up the indentation here. * e-tree-sorted.c, e-tree-sorted.h: New class meant to be used for sorting trees. It doesn't actually sort yet. It simply acts as an ETreeProxy which is the hardest part of making ETreeSorted. * e-tree.c, e-tree.h: Made this use an ETreeSorted. 2001-03-14 Christopher James Lahey <clahey@ximian.com> * e-table-item.c, e-table-item.h, e-table-selection-model.c, e-table-selection-model.h, e-table-sorted.c, e-table-sorted.h, e-table-subset.c, e-table-subset.h, e-table.c, e-table.h: Switch to handling e_table_model_rows_inserted instead of e_table_model_row_inserted and e_table_model_rows_deleted instead of e_table_model_row_deleted. * e-table-model.c, e-table-model.h: Replaced the signals "model_row_inserted" and "model_row_deleted" with "model_rows_inserted" and "model_rows_deleted" so that when multiple rows are inserted or deleted at the same time they can be handled properly. * e-tree-table-adapter.c: Call "model_rows_inserted" and "model_rows_deleted" instead of "model_changed" when inserting or deleting multiple rows. 2001-03-14 Christopher James Lahey <clahey@ximian.com> * e-table-item.c (e_table_item_row_diff): Made this not count the pixel between rows if it isn't there. 2001-03-14 Christopher James Lahey <clahey@ximian.com> * e-table-item.c (eti_header_structure_changed): Properly attach & realize cell views here. 2001-03-13 Christopher James Lahey <clahey@ximian.com> * e-tree-table-adapter.c (etta_proxy_node_removed): Check that parent_node and parent aren't NULL before making function calls on them. 2001-03-13 Christopher James Lahey <clahey@ximian.com> * e-table-item.c (confirm_height_cache): Fixed a height cache miscalculation. * e-tree-table-adapter.c (find_first_child_node): Made the semantics of this mean that find_first_child_node(adapter, -1) means return the first node in the tree. 2001-03-13 Christopher James Lahey <clahey@ximian.com> * e-table-extras.c: Added a "string-integer" comparison function to the default %ETableExtras so you can do comparisons based on integer value even if you using strings for the data (this lets you do editable numbers, for instance.) * e-table-item.c: Rearranged it a bit so that if you have draw_grid off it doesn't add space for the horizontal lines, nor leave them the background color. * e-table-model.c, e-table-model.h: Added the row_save_id and has_save_id methods to %ETableModel. * e-tree.c, e-tree.h: Replaced e_tree_compute_location with e_tree_get_cell_at. 2001-03-08 Christopher James Lahey <clahey@ximian.com> * Makefile.am: Added e-table/e-table-utils.c, e-table/e-tree-memory-callbacks.c, e-table/e-tree-memory.c, e-table/e-tree-scrolled.c, e-table/e-tree-table-adapter.c, and e-table/e-tree.c. Removed e-table/e-tree-simple.c. Added e-table/e-table-utils.h, e-table/e-tree-memory-callbacks.h, e-table/e-tree-memory.h, e-table/e-tree-scrolled.h, e-table/e-tree-table-adapter.h, and e-table/e-tree.h. Removed e-table/e-tree-simple.h. * e-cell-tree.c: Updated this for the new tree. * e-table-item.c: Added some redraw requests where appropriate. * e-table-item.h: Fixed an incorrect class method declaration. * e-table-model.c, e-table-model.h: Removed e_table_model_has_sort_group and e_table_model_row_sort_group. * e-table-scrolled.h: Removed unused headers. * e-table-simple.c, e-table-simple.h: Rearranged this a bit. * e-table-sorter.c, e-table-sorting-utils.c, e-table-sorting-utils.h: Removed sort group stuff. Added the function e_table_sorting_utils_check_position. * e-table-utils.c, e-table-utils.h: Utility functions for ETable and ETree. * e-table.c: Moved some of the functionality from here to e-table-utils.c so that it can be reused by ETree. * e-tree-memory-callbacks.c, e-tree-memory-callbacks.h: Class to implement an ETreeMemory as callbacks instead of overriding the class. * e-tree-memory.c, e-tree-memory.h: ETreeModel that stores a tree of physical nodes. * e-tree-model.c, e-tree-model.h: Removed most of the functionality from here to the classes ETreeMemory and ETreeTableAdapter. This is now just a simple virtualized tree class. * e-tree-scrolled.c, e-tree-scrolled.h: New class. An ETree in an EScrollFrame. * e-tree-simple.c: Small change. This is no longer used. * e-tree-table-adapter.c, e-tree-table-adapter.h: ETableModel that represents an ETreeModel as a table. * e-tree.c, e-tree.h: New super class kind of like ETable but for trees. End of branch svn path=/trunk/; revision=8837
Diffstat (limited to 'widgets/table/e-table-sorting-utils.c')
-rw-r--r--widgets/table/e-table-sorting-utils.c562
1 files changed, 181 insertions, 381 deletions
diff --git a/widgets/table/e-table-sorting-utils.c b/widgets/table/e-table-sorting-utils.c
index 364397d17f..4b2d581206 100644
--- a/widgets/table/e-table-sorting-utils.c
+++ b/widgets/table/e-table-sorting-utils.c
@@ -1,7 +1,9 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+#include <config.h>
#include <string.h>
#include <e-table-sorting-utils.h>
+#include <gal/util/e-util.h>
#define d(x)
@@ -37,64 +39,34 @@ etsu_compare(ETableModel *source, ETableSortInfo *sort_info, ETableHeader *full_
return comp_val;
}
-static ETableSortInfo *sort_info_closure;
+typedef struct {
+ int cols;
+ void **vals;
+ int *ascending;
+ GCompareFunc *compare;
+} ETableSortClosure;
-static void **vals_closure;
-static int cols_closure;
-static int *ascending_closure;
-static GCompareFunc *compare_closure;
+typedef struct {
+ ETreeModel *tree;
+ ETableSortInfo *sort_info;
+ ETableHeader *full_header;
+} ETreeSortClosure;
/* FIXME: Make it not cache the second and later columns (as if anyone cares.) */
static int
-qsort_callback(const void *data1, const void *data2)
+e_sort_callback(const void *data1, const void *data2, gpointer user_data)
{
gint row1 = *(int *)data1;
gint row2 = *(int *)data2;
+ ETableSortClosure *closure = user_data;
int j;
- int sort_count = e_table_sort_info_sorting_get_count(sort_info_closure);
- int comp_val = 0;
- int ascending = 1;
- for (j = 0; j < sort_count; j++) {
- comp_val = (*(compare_closure[j]))(vals_closure[cols_closure * row1 + j], vals_closure[cols_closure * row2 + j]);
- ascending = ascending_closure[j];
- if (comp_val != 0)
- break;
- }
- if (comp_val == 0) {
- if (row1 < row2)
- comp_val = -1;
- if (row1 > row2)
- comp_val = 1;
- }
- if (!ascending)
- comp_val = -comp_val;
- return comp_val;
-}
-
-struct _subinfo {
- int start;
- GArray *rowsort; /* an array of row info's */
-};
-
-struct _rowinfo {
- int row;
- struct _subinfo *subinfo;
- struct _group_info *groupinfo;
-};
-
-static int
-qsort_callback_complex(const void *data1, const void *data2)
-{
- gint row1 = ((struct _rowinfo *)data1)->row;
- gint row2 = ((struct _rowinfo *)data2)->row;
- int j;
- int sort_count = e_table_sort_info_sorting_get_count(sort_info_closure);
+ int sort_count = closure->cols;
int comp_val = 0;
int ascending = 1;
for (j = 0; j < sort_count; j++) {
- comp_val = (*(compare_closure[j]))(vals_closure[cols_closure * row1 + j], vals_closure[cols_closure * row2 + j]);
- ascending = ascending_closure[j];
+ comp_val = (*(closure->compare[j]))(closure->vals[closure->cols * row1 + j], closure->vals[closure->cols * row2 + j]);
+ ascending = closure->ascending[j];
if (comp_val != 0)
break;
}
@@ -109,219 +81,6 @@ qsort_callback_complex(const void *data1, const void *data2)
return comp_val;
}
-/* if sortgroup is like:
-0 1 1 1
-1 1 2 2
-2 2 3 2
-3 2 4 3
-4 3 5 3
-5 2 6 1
-6 1 0 1
-
- Want to sort the 1's first
- Then sort each group of 2's, offsetting into the output by the new root 1 location
- ... Recursively ...
-*/
-
-struct _group_info {
- char *group;
- int row;
-};
-
-#ifdef DEBUG
-#undef DEBUG
-#endif
-/*#define DEBUG*/
-
-#ifdef DEBUG
-static int total=0;
-static int total_sorted=0;
-#endif
-
-/* builds the info needed to sort everything */
-static struct _subinfo *
-etsu_sort_build_subset(int rows, struct _group_info *groupinfo, int start, int *end)
-{
- int i, lastinsert;
- GArray *rowsort = g_array_new(0, 0, sizeof(struct _rowinfo));
- struct _subinfo *subinfo, *newsub;
- char *id, *newid;
- int idlen, newidlen;
- int cmp;
- int cmplen;
-
- subinfo = g_malloc0(sizeof(*subinfo));
- subinfo->rowsort = rowsort;
- subinfo->start = start;
- lastinsert = -1;
- id = groupinfo[start].group;
- newid = strrchr(id, '/');
- idlen = strlen(id);
- if (newid)
- cmplen = newid-id;
- else
- cmplen = idlen;
- d(printf("%d scanning level %s\n", start, id));
- for (i=start;i<rows;i++) {
- newid = groupinfo[i].group;
- newidlen = strlen(newid);
- d(printf("%d checking group %s\n", start, newid));
- cmp = strncmp(id, newid, cmplen);
- /* check for common parent */
- if (idlen == newidlen && cmp == 0) {
- struct _rowinfo rowinfo;
-
- d(printf("%d Same parent\n", start));
- rowinfo.row = groupinfo[i].row;
- rowinfo.subinfo = NULL;
- rowinfo.groupinfo = &groupinfo[i];
- lastinsert = rowsort->len;
- g_array_append_val(rowsort, rowinfo);
-#ifdef DEBUG
- total++;
-#endif
- } else if (newidlen > idlen) {
- /* must be a new subtree */
- d(printf("%d checking subtree instead\n", start));
- newsub = etsu_sort_build_subset(rows, groupinfo, i, &i);
- d(printf("found %d nodes in subtree\n", newsub->rowsort->len));
- g_array_index(rowsort, struct _rowinfo, lastinsert).subinfo = newsub;
- } else {
- i--;
- break;
- }
- }
- if (end)
- *end = i;
- d(printf("finished level %s start was %d end was %d\n", id, start, i));
- return subinfo;
-}
-
-/* sort each level, and then sort each level below that level (once we know
- where the sublevel will fit in the overall list) */
-static int
-etsu_sort_subset(int *map_table, struct _subinfo *subinfo, int startoffset)
-{
- GArray *rowsort = subinfo->rowsort;
- int offset, i;
-
- d(printf("sorting subset start %d rows %d\n", startoffset, rowsort->len));
-
- /* first, sort the actual data */
- qsort(rowsort->data, rowsort->len, sizeof(struct _rowinfo), qsort_callback_complex);
-
- /* then put it back in the map table, where appropriate */
- offset = startoffset;
- for (i=0;i<rowsort->len;i++) {
- struct _rowinfo *rowinfo;
-
- d(printf("setting offset %d\n", offset));
-
- rowinfo = &g_array_index(rowsort, struct _rowinfo, i);
- map_table[offset] = rowinfo->row;
- if (rowinfo->subinfo) {
- offset = etsu_sort_subset(map_table, rowinfo->subinfo, offset+1);
- } else
- offset += 1;
- }
- d(printf("end sort subset start %d\n", startoffset));
-
- return offset;
-}
-
-static void
-etsu_sort_free_subset(struct _subinfo *subinfo)
-{
- int i;
-
- for (i=0;i<subinfo->rowsort->len;i++) {
- struct _rowinfo *rowinfo;
-
- rowinfo = &g_array_index(subinfo->rowsort, struct _rowinfo, i);
- if (rowinfo->subinfo)
- etsu_sort_free_subset(rowinfo->subinfo);
- }
- g_array_free(subinfo->rowsort, TRUE);
- g_free(subinfo);
-}
-
-static int
-sort_groups_compare(const void *ap, const void *bp)
-{
- struct _group_info *a = (struct _group_info *)ap;
- struct _group_info *b = (struct _group_info *)bp;
-
- return strcmp(a->group, b->group);
-}
-
-#ifdef DEBUG
-static void
-print_id(int key, int val, void *data)
-{
- printf("gained id %d\n", key);
-}
-#endif
-
-/* use the sort group to select subsorts */
-static void
-etsu_sort_by_group(ETableModel *source, int *map_table, int rows)
-{
- struct _group_info *groups;
- struct _subinfo *subinfo;
- int i;
-#ifdef DEBUG
- GHashTable *members = g_hash_table_new(0, 0);
-
- total = 0;
- total_sorted = 0;
-#endif
-
- d(printf("sorting %d rows\n", rows));
-
- if (rows == 0)
- return;
-
- /* get the subset rows */
- groups = g_malloc(sizeof(struct _group_info) * rows);
- for (i=0;i<rows;i++) {
- groups[i].row = map_table[i];
- groups[i].group = g_strdup(e_table_model_row_sort_group(source, groups[i].row));
-#ifdef DEBUG
- g_hash_table_insert(members, map_table[i], 1);
- map_table[i] = 0;
-#endif
- }
-
- /* sort the group info */
- qsort(groups, rows, sizeof(struct _group_info), sort_groups_compare);
-
- d(printf("sorted groups:\n");
- for (i=0;i<rows;i++) {
- printf(" %s\n", groups[i].group);
- });
-
- /* now sort based on the group info */
- subinfo = etsu_sort_build_subset(rows, groups, 0, NULL);
- for (i=0;i<rows;i++) {
- g_free(groups[i].group);
- }
- g_free(groups);
- etsu_sort_subset(map_table, subinfo, 0);
- etsu_sort_free_subset(subinfo);
-#ifdef DEBUG
- for (i=0;i<rows;i++) {
- if (g_hash_table_lookup(members, map_table[i]) == 0) {
- printf("lost id %d\n", map_table[i]);
- }
- g_hash_table_remove(members, map_table[i]);
- }
- g_hash_table_foreach(members, print_id, 0);
-
- printf("total rows = %d, total processed = %d, total sorted = %d\n", rows, total, total_sorted);
-#endif
-
-}
-
void
e_table_sorting_utils_sort(ETableModel *source, ETableSortInfo *sort_info, ETableHeader *full_header, int *map_table, int rows)
{
@@ -329,6 +88,7 @@ e_table_sorting_utils_sort(ETableModel *source, ETableSortInfo *sort_info, ETabl
int i;
int j;
int cols;
+ ETableSortClosure closure;
g_return_if_fail(source != NULL);
g_return_if_fail(E_IS_TABLE_MODEL(source));
@@ -339,11 +99,12 @@ e_table_sorting_utils_sort(ETableModel *source, ETableSortInfo *sort_info, ETabl
total_rows = e_table_model_row_count(source);
cols = e_table_sort_info_sorting_get_count(sort_info);
- cols_closure = cols;
- vals_closure = g_new(void *, total_rows * cols);
- sort_info_closure = sort_info;
- ascending_closure = g_new(int, cols);
- compare_closure = g_new(GCompareFunc, cols);
+ closure.cols = cols;
+
+ closure.vals = g_new(void *, total_rows * cols);
+ closure.ascending = g_new(int, cols);
+ closure.compare = g_new(GCompareFunc, cols);
+
for (j = 0; j < cols; j++) {
ETableSortColumn column = e_table_sort_info_sorting_get_nth(sort_info, j);
ETableCol *col;
@@ -351,33 +112,27 @@ e_table_sorting_utils_sort(ETableModel *source, ETableSortInfo *sort_info, ETabl
if (col == NULL)
col = e_table_header_get_column (full_header, e_table_header_count (full_header) - 1);
for (i = 0; i < rows; i++) {
- vals_closure[map_table[i] * cols + j] = e_table_model_value_at (source, col->col_idx, map_table[i]);
+ closure.vals[map_table[i] * cols + j] = e_table_model_value_at (source, col->col_idx, map_table[i]);
}
- compare_closure[j] = col->compare;
- ascending_closure[j] = column.ascending;
+ closure.compare[j] = col->compare;
+ closure.ascending[j] = column.ascending;
}
- if (e_table_model_has_sort_group(source)) {
- etsu_sort_by_group(source, map_table, rows);
- } else {
- qsort(map_table, rows, sizeof(int), qsort_callback);
- }
- g_free(vals_closure);
- g_free(ascending_closure);
- g_free(compare_closure);
+ e_sort(map_table, rows, sizeof(int), e_sort_callback, &closure);
+
+ g_free(closure.vals);
+ g_free(closure.ascending);
+ g_free(closure.compare);
}
gboolean
-e_table_sorting_utils_affects_sort (ETableModel *source,
- ETableSortInfo *sort_info,
+e_table_sorting_utils_affects_sort (ETableSortInfo *sort_info,
ETableHeader *full_header,
int col)
{
int j;
int cols;
- g_return_val_if_fail(source != NULL, TRUE);
- g_return_val_if_fail(E_IS_TABLE_MODEL(source), TRUE);
g_return_val_if_fail(sort_info != NULL, TRUE);
g_return_val_if_fail(E_IS_TABLE_SORT_INFO(sort_info), TRUE);
g_return_val_if_fail(full_header != NULL, TRUE);
@@ -398,6 +153,7 @@ e_table_sorting_utils_affects_sort (ETableModel *source,
}
+/* FIXME: This could be done in time log n instead of time n with a binary search. */
int
e_table_sorting_utils_insert(ETableModel *source, ETableSortInfo *sort_info, ETableHeader *full_header, int *map_table, int rows, int row)
{
@@ -405,62 +161,13 @@ e_table_sorting_utils_insert(ETableModel *source, ETableSortInfo *sort_info, ETa
i = 0;
/* handle insertions when we have a 'sort group' */
- if (e_table_model_has_sort_group(source)) {
- /* find the row this row maps to */
- char *group = g_strdup(e_table_model_row_sort_group(source, row));
- const char *newgroup;
- int cmp, grouplen, newgrouplen;
-
- newgroup = strrchr(group, '/');
- grouplen = strlen(group);
- if (newgroup)
- cmp = newgroup-group;
- else
- cmp = grouplen;
-
- /* find first common parent */
- while (i < rows) {
- newgroup = e_table_model_row_sort_group(source, map_table[i]);
- if (strncmp(newgroup, group, cmp) == 0) {
- break;
- }
- i++;
- }
-
- /* check matching records */
- while (i<row) {
- newgroup = e_table_model_row_sort_group(source, map_table[i]);
- newgrouplen = strlen(newgroup);
- if (strncmp(newgroup, group, cmp) == 0) {
- /* common parent, check for same level */
- if (grouplen == newgrouplen) {
- if (etsu_compare(source, sort_info, full_header, map_table[i], row) >= 0)
- break;
- } else if (strncmp(newgroup + cmp, group + cmp, grouplen - cmp) == 0)
- /* Found a child of the inserted node. Insert here. */
- break;
- } else {
- /* ran out of common parents, insert here */
- break;
- }
- i++;
- }
- g_free(group);
- } else {
- while (i < rows && etsu_compare(source, sort_info, full_header, map_table[i], row) < 0)
- i++;
- }
+ while (i < rows && etsu_compare(source, sort_info, full_header, map_table[i], row) < 0)
+ i++;
return i;
}
-#if 0
-void *bsearch(const void *key, const void *base, size_t nmemb,
- size_t size, int (*compar)(const void *, const void *, void *), gpointer user_data)
-{
-
-}
-
+/* FIXME: This could be done in time log n instead of time n with a binary search. */
int
e_table_sorting_utils_check_position (ETableModel *source, ETableSortInfo *sort_info, ETableHeader *full_header, int *map_table, int rows, int view_row)
{
@@ -469,60 +176,153 @@ e_table_sorting_utils_check_position (ETableModel *source, ETableSortInfo *sort_
i = view_row;
row = map_table[i];
- /* handle insertions when we have a 'sort group' */
- if (e_table_model_has_sort_group(source)) {
- /* find the row this row maps to */
- char *group = g_strdup(e_table_model_row_sort_group(source, row));
- const char *newgroup;
- int cmp, grouplen, newgrouplen;
-
- newgroup = strrchr(group, '/');
- grouplen = strlen(group);
- if (newgroup)
- cmp = newgroup-group;
- else
- cmp = grouplen;
-
- /* find first common parent */
- while (i < rows) {
- newgroup = e_table_model_row_sort_group(source, map_table[i]);
- if (strncmp(newgroup, group, cmp) == 0) {
- break;
- }
- i++;
- }
- /* check matching records */
- while (i < row) {
- newgroup = e_table_model_row_sort_group(source, map_table[i]);
- newgrouplen = strlen(newgroup);
- if (strncmp(newgroup, group, cmp) == 0) {
- /* common parent, check for same level */
- if (grouplen == newgrouplen) {
- if (etsu_compare(source, sort_info, full_header, map_table[i], row) >= 0)
- break;
- } else if (strncmp(newgroup + cmp, group + cmp, grouplen - cmp) == 0)
- /* Found a child of the inserted node. Insert here. */
- break;
- } else {
- /* ran out of common parents, insert here */
- break;
- }
- i++;
- }
- g_free(group);
- } else {
- i = view_row;
- if (i < rows && etsu_compare(source, sort_info, full_header, map_table[i + 1], row) < 0) {
+ i = view_row;
+ if (i < rows - 1 && etsu_compare(source, sort_info, full_header, map_table[i + 1], row) < 0) {
+ i ++;
+ while (i < rows - 1 && etsu_compare(source, sort_info, full_header, map_table[i], row) < 0)
i ++;
- while (i < rows - 1 && etsu_compare(source, sort_info, full_header, map_table[i], row) < 0)
- i ++;
- } else if (i > 0 && etsu_compare(source, sort_info, full_header, map_table[i - 1], row) > 0) {
+ } else if (i > 0 && etsu_compare(source, sort_info, full_header, map_table[i - 1], row) > 0) {
+ i --;
+ while (i > 0 && etsu_compare(source, sort_info, full_header, map_table[i], row) > 0)
i --;
- while (i > 0 && etsu_compare(source, sort_info, full_header, map_table[i], row) > 0)
- i --;
+ }
+ return i;
+}
+
+
+
+
+/* This takes source rows. */
+static int
+etsu_tree_compare(ETreeModel *source, ETableSortInfo *sort_info, ETableHeader *full_header, ETreePath path1, ETreePath path2)
+{
+ int j;
+ int sort_count = e_table_sort_info_sorting_get_count(sort_info);
+ int comp_val = 0;
+ int ascending = 1;
+
+ for (j = 0; j < sort_count; j++) {
+ ETableSortColumn column = e_table_sort_info_sorting_get_nth(sort_info, j);
+ ETableCol *col;
+ col = e_table_header_get_column_by_col_idx(full_header, column.column);
+ if (col == NULL)
+ col = e_table_header_get_column (full_header, e_table_header_count (full_header) - 1);
+ comp_val = (*col->compare)(e_tree_model_value_at (source, path1, col->col_idx),
+ e_tree_model_value_at (source, path2, col->col_idx));
+ ascending = column.ascending;
+ if (comp_val != 0)
+ break;
+ }
+ if (!ascending)
+ comp_val = -comp_val;
+ return comp_val;
+}
+
+static int
+e_sort_tree_callback(const void *data1, const void *data2, gpointer user_data)
+{
+ ETreePath *path1 = *(ETreePath *)data1;
+ ETreePath *path2 = *(ETreePath *)data2;
+ ETreeSortClosure *closure = user_data;
+
+ return etsu_tree_compare(closure->tree, closure->sort_info, closure->full_header, path1, path2);
+}
+
+void
+e_table_sorting_utils_tree_sort(ETreeModel *source, ETableSortInfo *sort_info, ETableHeader *full_header, ETreePath *map_table, int count)
+{
+ ETableSortClosure closure;
+ int cols;
+ int i, j;
+ int *map;
+ ETreePath *map_copy;
+ g_return_if_fail(source != NULL);
+ g_return_if_fail(E_IS_TREE_MODEL(source));
+ g_return_if_fail(sort_info != NULL);
+ g_return_if_fail(E_IS_TABLE_SORT_INFO(sort_info));
+ g_return_if_fail(full_header != NULL);
+ g_return_if_fail(E_IS_TABLE_HEADER(full_header));
+
+ cols = e_table_sort_info_sorting_get_count(sort_info);
+ closure.cols = cols;
+
+ closure.vals = g_new(void *, count * cols);
+ closure.ascending = g_new(int, cols);
+ closure.compare = g_new(GCompareFunc, cols);
+
+ for (j = 0; j < cols; j++) {
+ ETableSortColumn column = e_table_sort_info_sorting_get_nth(sort_info, j);
+ ETableCol *col;
+
+ col = e_table_header_get_column_by_col_idx(full_header, column.column);
+ if (col == NULL)
+ col = e_table_header_get_column (full_header, e_table_header_count (full_header) - 1);
+
+ for (i = 0; i < count; i++) {
+ closure.vals[i * cols + j] = e_tree_model_value_at (source, map_table[i], col->col_idx);
}
+ closure.ascending[j] = column.ascending;
+ closure.compare[j] = col->compare;
+ }
+
+ map = g_new(int, count);
+ for (i = 0; i < count; i++) {
+ map[i] = i;
+ }
+
+ e_sort(map, count, sizeof(int), e_sort_callback, &closure);
+
+ map_copy = g_new(ETreePath, count);
+ for (i = 0; i < count; i++) {
+ map_copy[i] = map_table[i];
+ }
+ for (i = 0; i < count; i++) {
+ map_table[i] = map_copy[map[i]];
+ }
+
+ g_free(map);
+ g_free(map_copy);
+
+ g_free(closure.vals);
+ g_free(closure.ascending);
+ g_free(closure.compare);
+}
+
+/* FIXME: This could be done in time log n instead of time n with a binary search. */
+int
+e_table_sorting_utils_tree_check_position (ETreeModel *source, ETableSortInfo *sort_info, ETableHeader *full_header, ETreePath *map_table, int count, int old_index)
+{
+ int i;
+ ETreePath path;
+
+ i = old_index;
+ path = map_table[i];
+
+ if (i < count - 1 && etsu_tree_compare(source, sort_info, full_header, map_table[i + 1], path) < 0) {
+ i ++;
+ while (i < count - 1 && etsu_tree_compare(source, sort_info, full_header, map_table[i], path) < 0)
+ i ++;
+ } else if (i > 0 && etsu_tree_compare(source, sort_info, full_header, map_table[i - 1], path) > 0) {
+ i --;
+ while (i > 0 && etsu_tree_compare(source, sort_info, full_header, map_table[i], path) > 0)
+ i --;
}
return i;
}
-#endif
+
+/* FIXME: This does not pay attention to making sure that it's a stable insert. This needs to be fixed. */
+int
+e_table_sorting_utils_tree_insert(ETreeModel *source, ETableSortInfo *sort_info, ETableHeader *full_header, ETreePath *map_table, int count, ETreePath path)
+{
+ int start;
+ int end;
+ ETreeSortClosure closure;
+
+ closure.tree = source;
+ closure.sort_info = sort_info;
+ closure.full_header = full_header;
+
+ e_bsearch(&path, map_table, count, sizeof(ETreePath), e_sort_tree_callback, &closure, &start, &end);
+ return end;
+}