2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "filecluster.h"
23 static gboolean check_list_contains_sublist(GList *haystack, GList *needle)
25 // TODO(xsdg): Optimize this! Sort, then scan?
26 GList *h_work, *n_work;
27 for (n_work = needle; n_work; n_work = n_work->next)
29 gboolean found = FALSE;
30 for (h_work = haystack; h_work; h_work = h_work->next)
39 if (!found) return FALSE;
45 FileClusterList *fileclusterlist_new()
47 FileClusterList *fcl = g_new0(FileClusterList, 1);
48 fcl->clusters = g_hash_table_new(&g_direct_hash, &g_direct_equal);
51 FileCluster *filecluster_new()
53 FileCluster *fc = g_new0(FileCluster, 1);
56 void fileclusterlist_free(FileClusterList *fcl)
58 if (fcl->fd_list) g_list_free_full(fcl->fd_list, (GDestroyNotify)&filecluster_free);
59 g_hash_table_destroy(fcl->clusters);
63 void filecluster_free(FileCluster *fc)
68 FileCluster *fileclusterlist_create_cluster(FileClusterList *fcl, GList *fd_items)
72 // Check preconditions.
73 if (!fd_items) return NULL;
74 // TODO(xsdg): Is this actually necessary?
75 // if (!check_list_contains_sublist(fcl->fd_list, fd_items)) return NULL;
76 for (work = fd_items; work; work = work->next)
78 FileData *fd = work->data;
79 if (g_hash_table_contains(fcl->clusters, fd))
81 // TODO(xsdg): Show this warning in the UI.
82 g_warning("Tried to create a cluster with a file that is already clustered.");
87 FileCluster *new_fc = filecluster_new();
88 new_fc->items = g_list_copy(fd_items);
89 new_fc->head = new_fc->items;
91 for (GList *item = new_fc->items; item; item = item->next)
93 FileData *fd = item->data;
94 g_hash_table_insert(fcl->clusters, fd, new_fc);
100 gboolean fileclusterlist_has_head(FileClusterList *fcl, FileData *fd)
102 FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
103 if (!fc) return FALSE;
104 return fc->head->data == fd;
107 gboolean fileclusterlist_has_child(FileClusterList *fcl, FileData *fd)
109 FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
110 if (!fc) return FALSE;
111 return fc->head->data != fd;
114 GList *filecluster_remove_children_from_list(FileClusterList *fcl, GList *list)
120 FileData *fd = work->data;
122 // Advance early in case link needs to be removed/freed.
125 if (fileclusterlist_has_child(fcl, fd))
127 list = g_list_remove_link(list, link);