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"
21 static gboolean check_list_contains_sublist(GList *haystack, GList *needle)
23 // TODO(xsdg): Optimize this! Sort, then scan?
24 GList *h_work, *n_work;
25 for (n_work = needle; n_work; n_work = n_work->next)
27 gboolean found = FALSE;
28 for (h_work = haystack; h_work; h_work = h_work->next)
37 if (!found) return FALSE;
43 FileClusterList *fileclusterlist_new()
45 FileClusterList *fcl = g_new0(FileClusterList, 1);
46 fcl->clusters = g_hash_table_new(&g_direct_hash, &g_direct_equal);
49 FileCluster *filecluster_new()
51 FileCluster *fc = g_new0(FileCluster, 1);
54 void fileclusterlist_free(FileClusterList *fcl)
56 if (fcl->fd_list) g_list_free_full(fcl->fd_list, (GDestroyNotify)&filecluster_free);
57 g_hash_table_destroy(fcl->clusters);
61 void filecluster_free(FileCluster *fc)
66 FileCluster *fileclusterlist_create_cluster(FileClusterList *fcl, GList *fd_items)
69 if (!fd_items) return NULL;
70 if (!check_list_contains_sublist(fcl->fd_list, fd_items)) return NULL;
71 for (work = fd_items; work; work = work->next)
73 FileData *fd = work->data;
74 if (g_hash_table_contains(fcl->clusters, fd)) return NULL;
77 FileCluster *new_fc = filecluster_new();
78 new_fc->items = g_list_copy(fd_items);
79 new_fc->head = new_fc->items;
84 gboolean fileclusterlist_has_head(FileClusterList *fcl, FileData *fd)
86 FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
87 if (!fc) return FALSE;
88 return fc->head->data == fd;
91 gboolean fileclusterlist_has_child(FileClusterList *fcl, FileData *fd)
93 FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
94 if (!fc) return FALSE;
95 return fc->head->data != fd;