core/containers: Remove unused set class

Implements #48: Remove set code
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-04-29 10:46:16 -04:00 committed by Anna Schumaker
parent 98fe6d3bbb
commit fa0dee5921
6 changed files with 1 additions and 308 deletions

View File

@ -1,43 +0,0 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/containers/set.h>
void set_copy(const struct set *lhs, struct set *rhs)
{
struct set_iter it_lhs;
set_for_each(lhs, &it_lhs)
set_insert(rhs, it_lhs.it_val);
}
void set_inline_intersect(const struct set *lhs, struct set *rhs)
{
struct set_iter it_rhs;
set_for_each(rhs, &it_rhs) {
if (!set_has(lhs, it_rhs.it_val))
g_hash_table_iter_remove(&it_rhs.it_iter);
}
}
void set_read(struct file *file, struct set *set)
{
unsigned int num, val, i;
file_readf(file, "%u ", &num);
for (i = 0; i < num; i++) {
file_readf(file, "%u ", &val);
set_insert(set, val);
}
}
void set_write(struct file *file, struct set *set)
{
struct set_iter it;
file_writef(file, "%u ", set_size(set));
set_for_each(set, &it)
file_writef(file, "%u ", it.it_val);
}

View File

@ -1,104 +0,0 @@
/*
* 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 */

View File

@ -3,7 +3,6 @@ string
file
date
idle
containers/set
containers/database
filter
tags/artist

View File

@ -10,7 +10,6 @@ def ContainerTest(name):
Depends(run, res[-1])
return run
res += [ ContainerTest("set") ]
res += [ ContainerTest("database") ]
Return("res")

View File

@ -1,159 +0,0 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/containers/set.h>
#include <tests/test.h>
void test_set()
{
struct file f = FILE_INIT("set", 0, 0);
struct set *set = set_alloc();
unsigned int i, N = 10;
struct set_iter it;
/* Insert N items. */
for (i = 0; i < N; i++) {
test_loop_equal(set_insert(set, i), (bool)true, i);
test_loop_equal(set_insert(set, i), (bool)false, i);
test_loop_equal(set_size(set), i + 1, i);
test_loop_equal(set_has(set, i), (bool)true, i);
} test_loop_passed();
set_insert(set, 0);
test_equal(set_size(set), N);
/* Remove even items. */
for (i = 0; i < N; i += 2) {
test_loop_equal(set_remove(set, i), (bool)true, i);
test_loop_equal(set_remove(set, i), (bool)false, i);
test_loop_equal(set_has(set, i), (bool)false, i);
} test_loop_passed();
test_equal(set_size(set), N / 2);
/* Test iterating. */
i = 1;
set_for_each(set, &it) {
test_loop_equal(it.it_val, i, i);
i += 2;
} test_loop_passed();
file_open(&f, OPEN_WRITE);
set_write(&f, set);
file_close(&f);
set_clear(set);
test_equal(set_size(set), 0);
file_open(&f, OPEN_READ);
set_read(&f, set);
test_equal(set_size(set), N / 2);
file_close(&f);
/* Check values. */
for (i = 0; i < N; i++) {
if (i % 2) { /* Odd values. */
test_loop_equal(set_has(set, i), (bool)true, i);
} else { /* Even values. */
test_loop_equal(set_has(set, i), (bool)false, i);
}
} test_loop_passed();
set_free(set);
}
void test_copy()
{
struct set set1 = SET_INIT();
struct set set2 = SET_INIT();
struct set set3 = SET_INIT();
unsigned int i;
/* Test copy between two empty sets. */
set_copy(&set1, &set2);
test_equal(set_size(&set1), 0);
test_equal(set_size(&set2), 0);
/* Add values 0 - 9 to set1, but keep set2 empty. */
for (i = 0; i < 10; i++)
set_insert(&set1, i);
set_copy(&set1, &set2);
test_equal(set_size(&set1), 10);
test_equal(set_size(&set2), 10);
/* Set2 should now contain values 0 - 9. */
for (i = 0; i < 10; i++) {
test_loop_equal(set_has(&set2, i), (bool)true, i);
} test_loop_passed();
/* Add values 10 - 19 to set3, copy into set2. */
for (i = 10; i < 20; i++)
set_insert(&set3, i);
set_copy(&set3, &set2);
test_equal(set_size(&set3), 10);
test_equal(set_size(&set2), 20);
/* Set2 should now contain values 0 - 19. */
for (i = 0; i < 20; i++) {
test_loop_equal(set_has(&set2, i), (bool)true, i);
} test_loop_passed();
set_deinit(&set1);
set_deinit(&set2);
set_deinit(&set3);
}
void test_intersection()
{
struct set set1 = SET_INIT();
struct set set2 = SET_INIT();
unsigned int i;
/* Test intersection between two empty sets. */
set_inline_intersect(&set1, &set2);
test_equal(set_size(&set1), 0);
test_equal(set_size(&set2), 0);
/* Add values 5 - 14 to set2, but keep set1 empty. */
for (i = 5; i < 15; i++)
set_insert(&set2, i);
set_inline_intersect(&set1, &set2);
test_equal(set_size(&set1), 0);
test_equal(set_size(&set2), 0);
/* Add values 0 - 9 to set1, but keep set2 empty. */
for (i = 0; i < 10; i++)
set_insert(&set1, i);
set_inline_intersect(&set1, &set2);
test_equal(set_size(&set1), 10);
test_equal(set_size(&set2), 0);
/* Add values 5 - 14 back into set2. */
for (i = 5; i < 15; i++)
set_insert(&set2, i);
set_inline_intersect(&set1, &set2);
test_equal(set_size(&set1), 10);
test_equal(set_size(&set2), 5);
/* Set1 should now contain values 5 - 9. */
for (i = 0; i < 15; i++) {
if (i >= 5 && i < 10) {
test_loop_equal(set_has(&set2, i), (bool)true, i);
} else {
test_loop_equal(set_has(&set2, i), (bool)false, i);
}
} test_loop_passed();
set_deinit(&set1);
set_deinit(&set2);
}
DECLARE_UNIT_TESTS(
UNIT_TEST("Set", test_set),
UNIT_TEST("Set Copy", test_copy),
UNIT_TEST("Set Intersection", test_intersection),
);

View File

@ -189,6 +189,7 @@ static void test_tracks()
test_equal(gtk_label_get_text(runtime), "");
core_deinit();
gui_builder_deinit();
}
DECLARE_UNIT_TESTS(