105 lines
2.2 KiB
C
105 lines
2.2 KiB
C
/*
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
*/
|
|
#ifndef OCARINA_CORE_CONTAINERS_SET_H
|
|
#define OCARINA_CORE_CONTAINERS_SET_H
|
|
|
|
#include <core/file.h>
|
|
#include <glib.h>
|
|
#include <stdbool.h>
|
|
|
|
|
|
struct set {
|
|
GHashTable *s_set;
|
|
};
|
|
|
|
struct set_iter {
|
|
unsigned int it_val;
|
|
GHashTableIter it_iter;
|
|
};
|
|
|
|
|
|
#define SET_INIT() \
|
|
{ \
|
|
.s_set = g_hash_table_new(g_direct_hash, g_direct_equal), \
|
|
}
|
|
|
|
static inline void set_init(struct set *set)
|
|
{
|
|
set->s_set = g_hash_table_new(g_direct_hash, g_direct_equal);
|
|
}
|
|
|
|
static inline void set_deinit(struct set *set)
|
|
{
|
|
g_hash_table_destroy(set->s_set);
|
|
}
|
|
|
|
static inline struct set *set_alloc()
|
|
{
|
|
struct set *set = g_malloc(sizeof(struct set));
|
|
set_init(set);
|
|
return set;
|
|
}
|
|
|
|
static inline void set_free(void *set)
|
|
{
|
|
set_deinit((struct set *)set);
|
|
g_free(set);
|
|
}
|
|
|
|
static inline bool set_insert(struct set *set, unsigned int value)
|
|
{
|
|
return g_hash_table_add(set->s_set, GUINT_TO_POINTER(value));
|
|
}
|
|
|
|
static inline bool set_remove(struct set *set, unsigned int value)
|
|
{
|
|
return g_hash_table_remove(set->s_set, GUINT_TO_POINTER(value));
|
|
}
|
|
|
|
static inline void set_clear(struct set *set)
|
|
{
|
|
g_hash_table_remove_all(set->s_set);
|
|
}
|
|
|
|
static inline bool set_has(const struct set *set, unsigned int value)
|
|
{
|
|
return g_hash_table_contains(set->s_set, GUINT_TO_POINTER(value));
|
|
}
|
|
|
|
static inline unsigned int set_size(struct set *set)
|
|
{
|
|
return g_hash_table_size(set->s_set);
|
|
}
|
|
|
|
/* Copy values from set1 into set2. */
|
|
void set_copy(const struct set *, struct set *);
|
|
|
|
/* Remove values from set2 that are not also in set1. */
|
|
void set_inline_intersect(const struct set *, struct set *);
|
|
|
|
/* Read values from file. */
|
|
void set_read(struct file *, struct set *);
|
|
|
|
/* Write values to file. */
|
|
void set_write(struct file *, struct set *);
|
|
|
|
|
|
static inline bool set_iter_next(struct set_iter *it)
|
|
{
|
|
gpointer key;
|
|
bool ret = g_hash_table_iter_next(&it->it_iter, &key, NULL);
|
|
it->it_val = GPOINTER_TO_INT(key);
|
|
return ret;
|
|
}
|
|
|
|
static inline void set_iter_init(const struct set *set, struct set_iter *it)
|
|
{
|
|
g_hash_table_iter_init(&it->it_iter, set->s_set);
|
|
}
|
|
|
|
#define set_for_each(set, it) \
|
|
for (set_iter_init(set, it); set_iter_next(it); )
|
|
|
|
#endif /* OCARINA_CORE_CONTAINERS_SET_H */
|