summaryrefslogtreecommitdiff
path: root/osinfo/osinfo_list.c
blob: c829a624b98a7f94db00f32eeae639188ad7eafb (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
 * libosinfo:
 *
 * Copyright (C) 2009-2012, 2014 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *   Arjun Roy <arroy@redhat.com>
 *   Christophe Fergeau <cfergeau@redhat.com>
 *   Daniel P. Berrange <berrange@redhat.com>
 */

#include <config.h>

#include <osinfo/osinfo.h>
#include <glib/gi18n-lib.h>

G_DEFINE_ABSTRACT_TYPE(OsinfoList, osinfo_list, G_TYPE_OBJECT);

#define OSINFO_LIST_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), OSINFO_TYPE_LIST, OsinfoListPrivate))

/**
 * SECTION:osinfo_list
 * @short_description: Abstract base class for entity lists
 * @see_also: #OsinfoEntity
 *
 * #OsinfoList provides a way to maintain a list of #OsinfoEntity objects.
 *
 */

struct _OsinfoListPrivate
{
    GPtrArray *array;
    GHashTable *entities;

    GType elementType;
};

enum {
    PROP_O,

    PROP_ELEMENT_TYPE
};


static void
osinfo_list_set_property(GObject      *object,
                         guint         property_id,
                         const GValue *value,
                         GParamSpec   *pspec)
{
    OsinfoList *list = OSINFO_LIST(object);

    switch (property_id) {
    case PROP_ELEMENT_TYPE:
        list->priv->elementType = g_value_get_gtype(value);
        break;

    default:
        /* We don't have any other property... */
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        break;
    }
}

static void
osinfo_list_get_property(GObject    *object,
                         guint       property_id,
                         GValue     *value,
                         GParamSpec *pspec)
{
    OsinfoList *list = OSINFO_LIST(object);

    switch (property_id) {
    case PROP_ELEMENT_TYPE:
        g_value_set_gtype(value, list->priv->elementType);
        break;

    default:
        /* We don't have any other property... */
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
        break;
    }
}


static void
osinfo_list_finalize(GObject *object)
{
    OsinfoList *list = OSINFO_LIST(object);

    g_ptr_array_free(list->priv->array, TRUE);
    g_hash_table_unref(list->priv->entities);

    /* Chain up to the parent class */
    G_OBJECT_CLASS(osinfo_list_parent_class)->finalize(object);
}

/* Init functions */
static void
osinfo_list_class_init(OsinfoListClass *klass)
{
    GObjectClass *g_klass = G_OBJECT_CLASS(klass);
    GParamSpec *pspec;

    g_klass->set_property = osinfo_list_set_property;
    g_klass->get_property = osinfo_list_get_property;
    g_klass->finalize = osinfo_list_finalize;

    /**
     * OsinfoList:element-type:
     *
     * The specialization of the list. The list will be
     * restricted to storing #OsinfoEntity objects of
     * the specified type.
     */
    pspec = g_param_spec_gtype("element-type",
                               "Element type",
                               _("List element type"),
                               OSINFO_TYPE_ENTITY,
                               G_PARAM_CONSTRUCT_ONLY |
                               G_PARAM_READWRITE |
                               G_PARAM_STATIC_STRINGS);

    g_object_class_install_property(g_klass,
                                    PROP_ELEMENT_TYPE,
                                    pspec);

    g_type_class_add_private(klass, sizeof(OsinfoListPrivate));
}

static void
osinfo_list_init(OsinfoList *list)
{
    list->priv = OSINFO_LIST_GET_PRIVATE(list);
    list->priv->array = g_ptr_array_new_with_free_func(NULL);
    list->priv->entities = g_hash_table_new_full(g_str_hash,
                                                 g_str_equal,
                                                 NULL,
                                                 g_object_unref);
}

/**
 * osinfo_list_get_element_type:
 * @list: the entity list
 *
 * Retrieves the type of the subclass of #OsinfoEntity
 * that may be stored in the list
 *
 * Returns: the type of entity stored
 */
GType osinfo_list_get_element_type(OsinfoList *list)
{
    return list->priv->elementType;
}


/**
 * osinfo_list_get_length:
 * @list: the entity list
 *
 * Retrieves the number of elements currently stored
 * in the list
 *
 * Returns: the list length
 */
gint osinfo_list_get_length(OsinfoList *list)
{
    return list->priv->array->len;
}

/**
 * osinfo_list_get_nth:
 * @list: the entity list
 * @idx: the list position to fetch
 *
 * Retrieves the element in the list at position @idx. If
 * @idx is less than zero, or greater than the number of
 * elements in the list, the results are undefined.
 *
 * Returns: (transfer none): the list element
 */
OsinfoEntity *osinfo_list_get_nth(OsinfoList *list, gint idx)
{
    return g_ptr_array_index(list->priv->array, idx);
}


/**
 * osinfo_list_get_elements:
 * @list: the entity list
 *
 * Retrieve a linked list of all elements in the list.
 *
 * Returns: (transfer container) (element-type OsinfoEntity): the list elements
 */
GList *osinfo_list_get_elements(OsinfoList *list)
{
    return g_hash_table_get_values(list->priv->entities);
}

/**
 * osinfo_list_find_by_id:
 * @list: the entity list
 * @id: the unique identifier
 *
 * Search the list looking for the entity with a matching
 * unique identifier.
 *
 * Returns: (transfer none): the matching entity, or NULL
 */
OsinfoEntity *osinfo_list_find_by_id(OsinfoList *list, const gchar *id)
{
    return g_hash_table_lookup(list->priv->entities, id);
}


/**
 * osinfo_list_add:
 * @list: the entity list
 * @entity: (transfer none): the entity to add to the list
 *
 * Adds a new entity to the list.
 */
void osinfo_list_add(OsinfoList *list, OsinfoEntity *entity)
{
    OsinfoEntity *preexisting;
    g_return_if_fail(G_TYPE_CHECK_INSTANCE_TYPE(entity, list->priv->elementType));

    g_object_ref(entity);
    preexisting = osinfo_list_find_by_id(list, osinfo_entity_get_id(entity));
    if (preexisting != NULL) {
        g_ptr_array_remove(list->priv->array, preexisting);
    }
    g_ptr_array_add(list->priv->array, entity);
    g_hash_table_replace(list->priv->entities,
                         (gchar *)osinfo_entity_get_id(entity), entity);
}


/**
 * osinfo_list_add_filtered:
 * @list: the entity list
 * @source: (transfer none): the source for elements
 * @filter: (transfer none): filter to process the source with
 *
 * Adds all entities from @source which are matched by @filter. Using one
 * of the constructors in a subclass is preferrable
 * to this method.
 */
void osinfo_list_add_filtered(OsinfoList *list, OsinfoList *source, OsinfoFilter *filter)
{
    int i, len;
    g_return_if_fail(list->priv->elementType == source->priv->elementType);

    len = osinfo_list_get_length(source);
    for (i = 0; i < len; i++) {
        OsinfoEntity *entity = osinfo_list_get_nth(source, i);
        if (osinfo_filter_matches(filter, entity))
            osinfo_list_add(list, entity);
    }
}


/**
 * osinfo_list_add_intersection:
 * @list: the entity list
 * @sourceOne: (transfer none): the first list to add
 * @sourceTwo: (transfer none): the second list to add
 *
 * Computes the intersection between @sourceOne and @sourceTwo and
 * adds the resulting list of entities to the @list. Using one
 * of the constructors in a subclass is preferrable
 * to this method.
 */
void osinfo_list_add_intersection(OsinfoList *list, OsinfoList *sourceOne, OsinfoList *sourceTwo)
{
    int i, len;
    g_return_if_fail(list->priv->elementType == sourceOne->priv->elementType);
    g_return_if_fail(list->priv->elementType == sourceTwo->priv->elementType);

    // Make set representation of otherList and newList
    GHashTable *otherSet = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
    GHashTable *newSet = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

    // Add all from otherList to otherSet
    len = osinfo_list_get_length(sourceTwo);
    for (i = 0; i < len; i++) {
        OsinfoEntity *entity = osinfo_list_get_nth(sourceTwo, i);
        g_hash_table_insert(otherSet, g_strdup(osinfo_entity_get_id(entity)), entity);
    }

    // If other contains entity, and new list does not, add to new list
    len = osinfo_list_get_length(sourceOne);
    for (i = 0; i < len; i++) {
        OsinfoEntity *entity = osinfo_list_get_nth(sourceOne, i);

        if (g_hash_table_lookup(otherSet, osinfo_entity_get_id(entity)) &&
            !g_hash_table_lookup(newSet, osinfo_entity_get_id(entity))) {
            g_hash_table_insert(newSet, g_strdup(osinfo_entity_get_id(entity)), entity);
            osinfo_list_add(list, entity);
        }
    }

    g_hash_table_destroy(otherSet);
    g_hash_table_destroy(newSet);
}


/**
 * osinfo_list_add_union:
 * @list: the entity list
 * @sourceOne: (transfer none): the first list to add
 * @sourceTwo: (transfer none): the second list to add
 *
 * Computes the union between @sourceOne and @sourceTwo and
 * adds the resulting list of entities to the @list. Using one
 * of the constructors in a subclass is preferrable
 * to this method.
 */
void osinfo_list_add_union(OsinfoList *list, OsinfoList *sourceOne, OsinfoList *sourceTwo)
{
    // Make set version of new list
    GHashTable *newSet;
    g_return_if_fail(list->priv->elementType == sourceOne->priv->elementType);
    g_return_if_fail(list->priv->elementType == sourceTwo->priv->elementType);

    newSet = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);

    // Add all from first list to new list
    int i, len;
    len = osinfo_list_get_length(sourceOne);
    for (i = 0; i < len; i++) {
        OsinfoEntity *entity = osinfo_list_get_nth(sourceOne, i);
        osinfo_list_add(list, entity);
        g_hash_table_insert(newSet, g_strdup(osinfo_entity_get_id(entity)), entity);
    }

    // Add remaining elements from this list to new list
    len = osinfo_list_get_length(sourceTwo);
    for (i = 0; i < len; i++) {
        OsinfoEntity *entity = osinfo_list_get_nth(sourceTwo, i);
        // If new list does not contain element, add to new list
        if (!g_hash_table_lookup(newSet, osinfo_entity_get_id(entity))) {
            osinfo_list_add(list, entity);
            g_hash_table_insert(newSet, g_strdup(osinfo_entity_get_id(entity)), entity);
        }
    }

    g_hash_table_unref(newSet);
}

/**
 * osinfo_list_add_all:
 * @list: the entity list
 * @source: (transfer none): the list to add
 *
 * Adds all entities from @source to @list. Using one
 * of the constructors in a subclass is preferrable
 * to this method.
 */
void osinfo_list_add_all(OsinfoList *list, OsinfoList *source)
{
    int i, len;
    g_return_if_fail(list->priv->elementType == source->priv->elementType);

    len = osinfo_list_get_length(source);
    for (i = 0; i < len; i++) {
        OsinfoEntity *entity = osinfo_list_get_nth(source, i);
        osinfo_list_add(list, entity);
    }
}

/*
 * Creates a list of the same type as sourceOne and sourceTwo after
 * checking they are the same type. The created list elements are
 * of the same type as the elements of sourceOne and sourceTwo
 */
static OsinfoList *osinfo_list_new_same(OsinfoList *sourceOne,
                                        OsinfoList *sourceTwo)
{
    GType typeOne = G_OBJECT_TYPE(sourceOne);

    if (sourceTwo != NULL) {
        GType typeTwo = G_OBJECT_TYPE(sourceTwo);

        g_return_val_if_fail(typeOne == typeTwo, NULL);
        g_return_val_if_fail(OSINFO_IS_LIST(sourceTwo), NULL);
    }

    g_return_val_if_fail(OSINFO_IS_LIST(sourceOne), NULL);

    return g_object_new(typeOne,
                        "element-type",
                        sourceOne->priv->elementType,
                        NULL);
}

/**
 * osinfo_list_new_copy:
 * @source: the list to copy
 *
 * Construct a new list that is filled with elements from @source
 *
 * Returns: (transfer full): a copy of the list
 */
OsinfoList *osinfo_list_new_copy(OsinfoList *source)
{
    OsinfoList *newList = osinfo_list_new_same(source, NULL);
    g_return_val_if_fail(newList != NULL, NULL);
    osinfo_list_add_all(OSINFO_LIST(newList),
                        OSINFO_LIST(source));
    return newList;
}

/**
 * osinfo_list_new_filtered:
 * @source: the list to copy
 * @filter: the filter to apply
 *
 * Construct a new list that is filled with elements from @source that
 * match @filter
 *
 * Returns: (transfer full): a filtered copy of the list
 */
OsinfoList *osinfo_list_new_filtered(OsinfoList *source, OsinfoFilter *filter)
{
    OsinfoList *newList = osinfo_list_new_same(source, NULL);
    g_return_val_if_fail(newList != NULL, NULL);
    osinfo_list_add_filtered(OSINFO_LIST(newList),
                             OSINFO_LIST(source),
                             filter);
    return newList;
}

/**
 * osinfo_list_new_intersection:
 * @sourceOne: the first list to copy
 * @sourceTwo: the second list to copy
 *
 * Construct a new list that is filled with only the elements
 * that are present in both @sourceOne and @sourceTwo.
 *
 * Returns: (transfer full): an intersection of the two lists
 */
OsinfoList *osinfo_list_new_intersection(OsinfoList *sourceOne,
                                         OsinfoList *sourceTwo)
{
    OsinfoList *newList = osinfo_list_new_same(sourceOne, sourceTwo);
    g_return_val_if_fail(newList != NULL, NULL);
    osinfo_list_add_intersection(OSINFO_LIST(newList),
                                 OSINFO_LIST(sourceOne),
                                 OSINFO_LIST(sourceTwo));
    return newList;
}

/**
 * osinfo_list_new_union:
 * @sourceOne: the first list to copy
 * @sourceTwo: the second list to copy
 *
 * Construct a new list that is filled with all the that are present in
 * either @sourceOne and @sourceTwo. @sourceOne and @sourceTwo must be of
 * the same type.
 *
 * Returns: (transfer full): a union of the two lists
 */
OsinfoList *osinfo_list_new_union(OsinfoList *sourceOne,
                                  OsinfoList *sourceTwo)
{
    OsinfoList *newList = osinfo_list_new_same(sourceOne, sourceTwo);
    g_return_val_if_fail(newList != NULL, NULL);
    osinfo_list_add_union(OSINFO_LIST(newList),
                          OSINFO_LIST(sourceOne),
                          OSINFO_LIST(sourceTwo));
    return newList;
}


/*
 * Local variables:
 *  indent-tabs-mode: nil
 *  c-indent-level: 4
 *  c-basic-offset: 4
 * End:
 */