Start building a class to enable clustering of files.
[geeqie.git] / src / filecluster.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "filecluster.h"
20
21 static gboolean check_list_contains_sublist(GList *haystack, GList *needle)
22 {
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)
26         {
27                 gboolean found = FALSE;
28                 for (h_work = haystack; h_work; h_work = h_work->next)
29                 {
30                         if (n_work == h_work)
31                                 {
32                                 found = TRUE;
33                                 break;
34                                 }
35                 }
36
37                 if (!found) return FALSE;
38         }
39
40         return TRUE;
41 }
42
43 FileClusterList *fileclusterlist_new()
44 {
45         FileClusterList *fcl = g_new0(FileClusterList, 1);
46         fcl->clusters = g_hash_table_new(&g_direct_hash, &g_direct_equal);
47 }
48
49 FileCluster *filecluster_new()
50 {
51         FileCluster *fc = g_new0(FileCluster, 1);
52 }
53
54 void fileclusterlist_free(FileClusterList *fcl)
55 {
56         if (fcl->fd_list) g_list_free_full(fcl->fd_list, (GDestroyNotify)&filecluster_free);
57         g_hash_table_destroy(fcl->clusters);
58         g_free(fcl);
59 }
60
61 void filecluster_free(FileCluster *fc)
62 {
63         g_free(fc);
64 }
65
66 FileCluster *fileclusterlist_create_cluster(FileClusterList *fcl, GList *fd_items)
67 {
68         GList *work;
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)
72         {
73                 FileData *fd = work->data;
74                 if (g_hash_table_contains(fcl->clusters, fd)) return NULL;
75         }
76
77         FileCluster *new_fc = filecluster_new();
78         new_fc->items = g_list_copy(fd_items);
79         new_fc->head = new_fc->items;
80
81         return new_fc;
82 }
83
84 gboolean fileclusterlist_has_head(FileClusterList *fcl, FileData *fd)
85 {
86         FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
87         if (!fc) return FALSE;
88         return fc->head->data == fd;
89 }
90
91 gboolean fileclusterlist_has_child(FileClusterList *fcl, FileData *fd)
92 {
93         FileCluster *fc = g_hash_table_lookup(fcl->clusters, fd);
94         if (!fc) return FALSE;
95         return fc->head->data != fd;
96 }