aboutsummaryrefslogtreecommitdiffstats
path: root/src/bookmarks/ephy-nodes-cover.c
blob: 6d33b08e061e8ba15e7f8d0cf9282031176d0c54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 *  Copyright © 2004 Peter Harvey <pah06@uow.edu.au>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "config.h"

#include "ephy-nodes-cover.h"

/* Count the number of node entries which are children of parent. */
gint
ephy_nodes_count_covered (EphyNode *parent, const GPtrArray *children)
{
    guint i, len = 0;
    EphyNode *child;
    
    for(i = 0; i < children->len; i++)
    {
        child = g_ptr_array_index (children, i);
        if (ephy_node_has_child (parent, child))
        {
            len++;
        }
    }
    return len;
}

/* Removes from the array of children those which are children of the given parent. */
gint
ephy_nodes_remove_covered (EphyNode *parent, GPtrArray *children)
{
    guint i, len = children->len;
    EphyNode *child;
    
    for(i = 0; i < children->len; i++)
    {
        child = g_ptr_array_index (children, i);
        if (ephy_node_has_child (parent, child))
        {
            g_ptr_array_remove_index_fast (children, i);
            i--;
        }
    }
    return len - children->len;
}

/* Removes from the array of children those which are children of the given parent. */
gint
ephy_nodes_remove_not_covered (EphyNode *parent, GPtrArray *children)
{
    guint i, len = children->len;
    EphyNode *child;
    
    for(i = 0; i < children->len; i++)
    {
        child = g_ptr_array_index (children, i);
        if (!ephy_node_has_child (parent, child))
        {
            g_ptr_array_remove_index_fast (children, i);
            i--;
        }
    }
    return len - children->len;
}

/* Returns the subset of children which are childs of the given parent.
 * Stores the result in the given _covered array if non-null. */
GPtrArray *
ephy_nodes_get_covered (EphyNode *parent, const GPtrArray *children, GPtrArray *_covered)
{
    GPtrArray *covered = _covered?_covered:g_ptr_array_sized_new (children->len);
    EphyNode *child;
    guint i;

    covered->len = 0;
    for (i = 0; i < children->len; i++)
    {
        child = g_ptr_array_index (children, i);
        if (ephy_node_has_child (parent, child))
        {
            g_ptr_array_add (covered, child);
        }
    }
    
    return covered;
}

/* Returns true if the parent covers all the children. */
gboolean
ephy_nodes_covered (EphyNode *parent, const GPtrArray *children)
{
    EphyNode *child;
    guint i;

    for (i = 0; i < children->len; i++)
    {
        child = g_ptr_array_index (children, i);
        if (!ephy_node_has_child (parent, child))
        {
            return FALSE;
        }
    }
    
    return TRUE;
}

static gint
compare_chosen (const guint *a, const guint *b, guint *count_c)
{
    return (count_c[*b] - count_c[*a]);
}

/* Returns the subset of parents which provide a covering of children.
 * Arguments other than parents and children arguments are only used if non-null.
 * Uses the _covering array to store the subset of parents.
 * Uses the _uncovered array to store those children which couldn't be covered.
 * Uses the _sizes array to store the number of children covered by each parent. */
GPtrArray *
ephy_nodes_get_covering (const GPtrArray *parents, const GPtrArray *children,
             GPtrArray *_covering, GPtrArray *_uncovered, GArray *_sizes)
{
    GPtrArray *uncovered = _uncovered?_uncovered:g_ptr_array_sized_new (children->len);
    GPtrArray *covering = _covering?_covering:g_ptr_array_sized_new (parents->len);
    GArray *chosen = g_array_sized_new (FALSE, FALSE, sizeof(guint), parents->len);
    GArray *sizes = _sizes;

    /* Create arrays to store the number of children each parent has which
     * are currently not covered, and the number of children it has total. */
    guint *count_u = g_malloc (sizeof(guint) * parents->len);
    guint *count_c = g_malloc (sizeof(guint) * parents->len);
    
    EphyNode *parent;
    guint i, p;

    /* Empty all the returning arrays. */
    uncovered->len = 0;
    covering->len = 0;
    if (sizes) sizes->len = 0;
    
    /* Initialise the array of uncovered bookmarks. */
    for (i = 0; i < children->len; i++)
    {
        g_ptr_array_add (uncovered, g_ptr_array_index (children, i));
    }
    
    /* Initialise the count_u and count_c arrays.
     * NB: count_u[0] is set to 0 if the parent node
       covers the entire set of children. */
    for (i = 0, p = 0; i < parents->len; i++)
    {
        parent = g_ptr_array_index (parents, i);
        count_c[i] = ephy_nodes_count_covered (parent, children);
        count_u[i] = (count_c[i]<children->len) ? count_c[i] : 0;
        if (count_u[i] > count_u[p]) p = i;
    }
    
    /* While there are more suitable topics... */
    while (p < parents->len && count_u[p])
    {
        /* Update the arrays of uncovered bookmarks and covering topics. */
        parent = g_ptr_array_index (parents, p);
        ephy_nodes_remove_covered (parent, uncovered);
        g_array_append_val (chosen, p);
        
        /* Find the next most suitable topic. */
        count_u[p] = 0;
        for (i = 0; i < parents->len; i++)
        {
            /* Lazy update the count_u[i] array. */
            if (count_u[i] > count_u[p] || (count_u[i] == count_u[p] && count_c[i] < count_c[p]))
            {
                parent = g_ptr_array_index (parents, i);
                count_u[i] = ephy_nodes_count_covered (parent, uncovered);
            }

            if (count_u[i] > count_u[p] || (count_u[i] == count_u[p] && count_c[i] < count_c[p]))
            {
                p = i;
            }
        }
    }

    g_array_sort_with_data (chosen, (GCompareDataFunc)compare_chosen, count_c);
    
    for (i = 0; i < chosen->len; i++)
    {
        p = g_array_index (chosen, guint, i);
        g_ptr_array_add (covering, g_ptr_array_index (parents, p));
        if (sizes) g_array_append_val (sizes, count_c[p]);
    }

    if (_uncovered != uncovered) g_ptr_array_free (uncovered, TRUE);
    g_array_free (chosen, TRUE);
    g_free (count_u);
    g_free (count_c);
    
    return covering;
}