Remove core/containers/queue

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-04-03 09:53:35 -04:00 committed by Anna Schumaker
parent db01ed3208
commit 378ff72307
6 changed files with 0 additions and 295 deletions

View File

@ -1,29 +0,0 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/containers/queue.h>
guint _q_add_sorted(struct _queue *queue, gpointer data,
GCompareDataFunc func, gpointer user_data)
{
struct _q_iter it;
_q_for_each(queue, &it) {
if (func(_q_iter_val(&it), data, user_data) > 0) {
g_queue_insert_before(&queue->_queue, it.it_iter, data);
return it.it_pos;
}
}
return _q_add_tail(queue, data);
}
gpointer _q_remove_it(struct _queue *queue, struct _q_iter *it)
{
gpointer ret = _q_iter_val(it);
GList *link = it->it_iter;
_q_iter_prev(it);
g_queue_delete_link(&queue->_queue, link);
return ret;
}

View File

@ -1,108 +0,0 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#ifndef OCARINA_CORE_CONTAINERS_QUEUE_H
#define OCARINA_CORE_CONTAINERS_QUEUE_H
#include <glib.h>
struct _queue {
GQueue _queue;
};
struct _q_iter {
guint it_pos;
GList *it_iter;
};
/* Called to initialize a queue iterator. */
static inline void _q_iter_init(const struct _queue *queue, struct _q_iter *it)
{
it->it_iter = g_list_first(queue->_queue.head);
it->it_pos = 0;
}
/* Called to advance a queue iterator by one step. */
static inline void _q_iter_next(struct _q_iter *it)
{
it->it_iter = g_list_next(it->it_iter);
it->it_pos++;
}
/* Called to rewind a queue iterator by one step. */
static inline void _q_iter_prev(struct _q_iter *it)
{
it->it_iter = g_list_previous(it->it_iter);
it->it_pos--;
}
/* Called to set a queue iterator to a specific position. */
static inline void _q_iter_set(struct _queue *queue, struct _q_iter *it,
unsigned int pos)
{
it->it_iter = g_queue_peek_nth_link(&queue->_queue, pos);
it->it_pos = pos;
}
/* Called to access the value of a queue iterator. */
static inline gpointer _q_iter_val(struct _q_iter *it)
{
return (it->it_iter) ? it->it_iter->data : NULL;
}
#define _q_for_each(queue, it) \
for (_q_iter_init(queue, it); (it)->it_iter; _q_iter_next(it))
#define _Q_INIT() \
{ \
._queue = G_QUEUE_INIT, \
}
/* Called to initialize a queue. */
static inline void _q_init(struct _queue *queue)
{
g_queue_init(&queue->_queue);
}
/* Called to find the size of a queue. */
static inline guint _q_size(struct _queue *queue)
{
return g_queue_get_length(&queue->_queue);
}
/* Called to add an item to the head of a queue. */
static inline guint _q_add_head(struct _queue *queue, gpointer data)
{
g_queue_push_head(&queue->_queue, data);
return 0;
}
/* Called to add an item to the tail of a queue. */
static inline guint _q_add_tail(struct _queue *queue, gpointer data)
{
g_queue_push_tail(&queue->_queue, data);
return _q_size(queue) - 1;
}
/* Called to add an item to a sorted queue. */
guint _q_add_sorted(struct _queue *, gpointer, GCompareDataFunc, gpointer);
/* Called to remove all items from a queue */
static inline void _q_clear(struct _queue *queue)
{
g_queue_clear(&queue->_queue);
}
/* Called to remove an item by iterator. */
gpointer _q_remove_it(struct _queue *, struct _q_iter *);
/* Called to sort the queue. */
static inline void _q_sort(struct _queue *queue, GCompareDataFunc func,
gpointer user_data)
{
g_queue_sort(&queue->_queue, func, user_data);
}
#endif /* OCARINA_CORE_CONTAINERS_QUEUE_H */

View File

@ -8,7 +8,6 @@
#ifndef OCARINA_CORE_QUEUE_H
#define OCARINA_CORE_QUEUE_H
#include <core/containers/queue.h>
#include <core/file.h>
#include <core/tags/track.h>

View File

@ -6,7 +6,6 @@ idle
containers/set
containers/database
containers/index
containers/queue
filter
tags/artist
tags/album

View File

@ -13,6 +13,5 @@ def ContainerTest(name):
res += [ ContainerTest("set") ]
res += [ ContainerTest("database") ]
res += [ ContainerTest("index") ]
res += [ ContainerTest("queue") ]
Return("res")

View File

@ -1,155 +0,0 @@
/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <core/containers/queue.h>
#include <tests/test.h>
static unsigned int data_val = 0;
static inline unsigned int test_q_first(struct _queue *queue)
{
return GPOINTER_TO_INT(g_queue_peek_head(&queue->_queue));
}
static inline unsigned int test_q_last(struct _queue *queue)
{
return GPOINTER_TO_INT(g_queue_peek_tail(&queue->_queue));
}
static inline unsigned int test_q_iter_val(struct _q_iter *it)
{
return GPOINTER_TO_INT(_q_iter_val(it));
}
static int test_sort_int(gconstpointer a, gconstpointer b, gpointer data)
{
data_val = GPOINTER_TO_INT(data);
return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
}
static void test_stress(unsigned int N)
{
struct _queue queue = _Q_INIT();
struct _q_iter it;
unsigned int i;
/* _q_init() */
test_equal((void *)queue._queue.head, NULL);
test_equal((void *)queue._queue.tail, NULL);
test_equal(_q_size(&queue), 0);
/* _q_add_head() */
for (i = 0; i < N; i++) {
test_loop_equal(_q_add_head(&queue, GINT_TO_POINTER(i)), 0, i);
test_loop_equal(_q_size(&queue), i + 1, i);
test_loop_equal(test_q_first(&queue), i, i);
} test_loop_passed();
test_equal(_q_size(&queue), N);
/* _q_add_tail() */
for (i = 0; i < N; i++) {
test_loop_equal(_q_add_tail(&queue, GINT_TO_POINTER(i)), N + i, i);
test_loop_equal(_q_size(&queue), N + i + 1, i);
test_loop_equal(test_q_last(&queue), i, i);
} test_loop_passed();
test_equal(_q_size(&queue), 2 * N);
/* _q_iter_set() */
for (i = 0; i < (2 * N); i += N) {
_q_iter_set(&queue, &it, i);
test_loop_not_equal((void *)it.it_iter, NULL, i);
test_loop_equal(it.it_pos, i, i);
if (i == N) {
test_loop_equal(test_q_iter_val(&it), 0, i);
} else {
test_loop_equal(test_q_iter_val(&it), N - 1, i);
}
} test_loop_passed();
/* _q_for_each() */
i = 0;
_q_for_each(&queue, &it) {
test_loop_not_equal((void *)it.it_iter, NULL, i);
test_loop_equal(it.it_pos, i, i);
if (i < N) {
test_loop_equal(test_q_iter_val(&it), N - (i + 1), i);
} else {
test_loop_equal(test_q_iter_val(&it), i - N, i);
}
i++;
} test_loop_passed();
/* _q_remove_it() (last N items) */
_q_iter_set(&queue, &it, 2 * N - 1);
for (i = 0; i < N; i++) {
test_loop_not_equal((void *)it.it_iter, NULL, i);
test_loop_equal(it.it_pos, (2 * N) - (i + 1), i);
test_loop_equal(GPOINTER_TO_INT(_q_remove_it(&queue, &it)), N - (i + 1), i);
} test_loop_passed();
test_equal(_q_size(&queue), N);
/* _q_remove_it() (half of remaining items) */
for (i = 0; i < (N / 2); i++) {
_q_iter_set(&queue, &it, 0);
test_loop_not_equal((void *)it.it_iter, NULL, i);
test_loop_equal(it.it_pos, 0, i);
test_loop_equal(GPOINTER_TO_INT(_q_remove_it(&queue, &it)), N - (i + 1), i);
} test_loop_passed();
test_equal(_q_size(&queue), N / 2);
/* _q_clear() (remaining items) */
_q_clear(&queue);
test_equal(_q_size(&queue), 0);
test_equal((void *)queue._queue.head, NULL);
test_equal((void *)queue._queue.tail, NULL);
test_equal(queue._queue.length, 0);
}
static void test_basics() { test_stress(10); }
static void test_stress_0() { test_stress(0); }
static void test_stress_100K() { test_stress(100000); }
void test_sort()
{
struct _queue queue = _Q_INIT();
unsigned int i, N = 10;
struct _q_iter it;
/* _q_add_sorted() (10 .. 19) */
for (i = 0; i < N; i++) {
test_loop_equal(_q_add_sorted(&queue, GINT_TO_POINTER(i + 10),
test_sort_int,
GINT_TO_POINTER(42)), i, i);
} test_loop_passed();
test_equal(_q_size(&queue), N);
test_equal(data_val, 42);
/* _q_add_sorted() (0 .. 9) */
for (i = 0; i < N; i++) {
test_loop_equal(_q_add_sorted(&queue, GINT_TO_POINTER(i),
test_sort_int,
GINT_TO_POINTER(43)), i, i);
} test_loop_passed();
test_equal(_q_size(&queue), 2 * N);
test_equal(data_val, 43);
/* _q_sort(), after reversing the queue. */
g_queue_reverse(&queue._queue);
_q_sort(&queue, test_sort_int, GINT_TO_POINTER(44));
i = 0;
_q_for_each(&queue, &it) {
test_loop_equal(test_q_iter_val(&it), i, i);
i++;
} test_loop_passed();
test_equal(data_val, 44);
g_queue_clear(&queue._queue);
}
DECLARE_UNIT_TESTS(
UNIT_TEST("Queue Basics", test_basics),
UNIT_TEST("Queue Stress (N = 0)", test_stress_0),
UNIT_TEST("Queue Stress (N = 100,000)", test_stress_100K),
UNIT_TEST("Queue Sorting", test_sort),
);