forked from github/server
removing quicklist, importing it from a submodule
This commit is contained in:
parent
6e106c9fc0
commit
db094810fd
35 changed files with 30 additions and 537 deletions
|
@ -27,7 +27,7 @@ without prior permission by the authors of Eressea.
|
|||
|
||||
#include <util/language.h>
|
||||
#include <util/log.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <tolua.h>
|
||||
|
|
|
@ -44,7 +44,7 @@ without prior permission by the authors of Eressea.
|
|||
#include <util/event.h>
|
||||
#include <util/lists.h>
|
||||
#include <util/log.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
|
||||
#include <lua.h>
|
||||
#include <tolua.h>
|
||||
|
|
|
@ -57,7 +57,7 @@ without prior permission by the authors of Eressea.
|
|||
#include <util/language.h>
|
||||
#include <util/lists.h>
|
||||
#include <util/log.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/storage.h>
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include <util/nrmessage.c>
|
||||
#include <util/os.c>
|
||||
#include <util/parser.c>
|
||||
#include <util/quicklist.c>
|
||||
#include <util/rand.c>
|
||||
#include <util/resolve.c>
|
||||
#include <util/sql.c>
|
||||
|
|
|
@ -64,7 +64,7 @@ without prior permission by the authors of Eressea.
|
|||
#include <util/log.h>
|
||||
#include <util/message.h>
|
||||
#include <util/nrmessage.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
|
||||
#include <libxml/encoding.h>
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/lists.h>
|
||||
#include <util/log.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/sql.h>
|
||||
|
|
|
@ -17,7 +17,7 @@ without prior permission by the authors of Eressea.
|
|||
#include <assert.h>
|
||||
|
||||
#include <util/attrib.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rng.h>
|
||||
|
||||
#include <kernel/building.h>
|
||||
|
|
|
@ -77,7 +77,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/log.h>
|
||||
#include <util/message.h>
|
||||
#include <util/nrmessage.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rng.h>
|
||||
|
||||
#include <libxml/encoding.h>
|
||||
|
|
|
@ -43,7 +43,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/attrib.h>
|
||||
#include <util/base36.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/rng.h>
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ without prior permission by the authors of Eressea.
|
|||
#include <util/goodies.h>
|
||||
#include <util/language.h>
|
||||
#include <util/message.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/unicode.h>
|
||||
#include <util/xml.h>
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ without prior permission by the authors of Eressea.
|
|||
#include <util/bsdstring.h>
|
||||
#include <util/language.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/umlaut.h>
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/log.h>
|
||||
#include <util/os.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/rng.h>
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/functions.h>
|
||||
#include <util/language.h>
|
||||
#include <util/log.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/resolve.h>
|
||||
#include <util/storage.h>
|
||||
#include <util/umlaut.h>
|
||||
|
|
|
@ -62,7 +62,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/log.h>
|
||||
#include <util/lists.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/sql.h>
|
||||
|
|
|
@ -29,7 +29,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/attrib.h>
|
||||
#include <util/language.h>
|
||||
#include <util/log.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/storage.h>
|
||||
|
||||
|
|
|
@ -284,7 +284,7 @@ attrib_type at_curse = {
|
|||
/* Spruch identifizieren */
|
||||
|
||||
#include <util/umlaut.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
|
||||
static quicklist *cursetypes[256];
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include "spellbook.h"
|
||||
|
||||
/* util includes */
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/rng.h>
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <kernel/skill.h>
|
||||
#include <kernel/spell.h>
|
||||
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
|
||||
#include <CuTest.h>
|
||||
#include <tests.h>
|
||||
|
|
|
@ -41,7 +41,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/lists.h>
|
||||
#include <util/language.h>
|
||||
#include <util/log.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/resolve.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/storage.h>
|
||||
|
|
|
@ -57,7 +57,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/lists.h>
|
||||
#include <util/log.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/resolve.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/rng.h>
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
#include <kernel/spellbook.h>
|
||||
#include <kernel/unit.h>
|
||||
#include <kernel/pool.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/language.h>
|
||||
|
||||
#include <CuTest.h>
|
||||
|
|
|
@ -18,7 +18,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
|
||||
#include <platform.h>
|
||||
#include <kernel/config.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include "pathfinder.h"
|
||||
|
||||
#include "region.h"
|
||||
|
|
|
@ -53,7 +53,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/lists.h>
|
||||
#include <util/log.h>
|
||||
#include <util/os.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
|
||||
/* libc includes */
|
||||
#include <sys/stat.h>
|
||||
|
|
|
@ -65,7 +65,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/log.h>
|
||||
#include <util/os.h>
|
||||
#include <util/parser.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rand.h>
|
||||
#include <util/resolve.h>
|
||||
#include <util/rng.h>
|
||||
|
|
|
@ -36,7 +36,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/language.h>
|
||||
#include <util/lists.h>
|
||||
#include <util/umlaut.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/storage.h>
|
||||
#include <util/xml.h>
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|||
#include <util/language.h>
|
||||
#include <util/log.h>
|
||||
#include <util/umlaut.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
|
||||
/* libc includes */
|
||||
#include <assert.h>
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <platform.h>
|
||||
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <kernel/types.h>
|
||||
#include <kernel/spell.h>
|
||||
#include <kernel/magic.h>
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
#include <kernel/config.h>
|
||||
#include <kernel/spell.h>
|
||||
#include <kernel/magic.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/log.h>
|
||||
|
||||
#include "spellbook.h"
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <kernel/magic.h>
|
||||
#include <kernel/spell.h>
|
||||
#include <kernel/spellbook.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/language.h>
|
||||
|
||||
#include <CuTest.h>
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
#include <util/language.h>
|
||||
#include <util/lists.h>
|
||||
#include <util/log.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/sql.h>
|
||||
#include <util/unicode.h>
|
||||
|
|
|
@ -30,7 +30,7 @@
|
|||
#include <util/language.h>
|
||||
#include <util/resolve.h>
|
||||
#include <util/rng.h>
|
||||
#include <util/quicklist.h>
|
||||
#include <quicklist.h>
|
||||
#include <util/storage.h>
|
||||
|
||||
/* libc includes */
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
#include <util/base36_test.c>
|
||||
#include <util/bsdstring_test.c>
|
||||
#include <util/functions_test.c>
|
||||
#include <util/quicklist_test.c>
|
||||
#include <util/umlaut_test.c>
|
||||
#include <kernel/magic_test.c>
|
||||
#include <kernel/move_test.c>
|
||||
|
@ -51,7 +50,6 @@ int RunAllTests(void)
|
|||
/* util */
|
||||
CuSuiteAddSuite(suite, get_base36_suite());
|
||||
CuSuiteAddSuite(suite, get_bsdstring_suite());
|
||||
CuSuiteAddSuite(suite, get_quicklist_suite());
|
||||
CuSuiteAddSuite(suite, get_functions_suite());
|
||||
CuSuiteAddSuite(suite, get_umlaut_suite());
|
||||
/* kernel */
|
||||
|
|
|
@ -1,256 +0,0 @@
|
|||
/*
|
||||
Copyright (c) 2010-2011, Enno Rehling <enno@eressea.de>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
**/
|
||||
|
||||
#include "quicklist.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define QL_MAXSIZE 14 /* max. number of elements unrolled into one node */
|
||||
#define QL_LIMIT 7 /* this many or fewer number in a node => attempt merge */
|
||||
|
||||
/* The total size of this struct is 64 bytes on a 32-bit system with
|
||||
* normal alignment. YMMV, so on a 64-bit system, twiddle the
|
||||
* constants above */
|
||||
struct quicklist {
|
||||
struct quicklist *next;
|
||||
int num_elements;
|
||||
void *elements[QL_MAXSIZE];
|
||||
};
|
||||
|
||||
void *ql_get(const quicklist * ql, int index)
|
||||
{
|
||||
return (ql
|
||||
&& index < ql->num_elements) ? ql->elements[index] : ql_get(ql->next,
|
||||
index - ql->num_elements);
|
||||
}
|
||||
|
||||
void *ql_replace(quicklist * ql, int index, void *data)
|
||||
{
|
||||
if (ql && index < ql->num_elements) {
|
||||
void *orig = ql->elements[index];
|
||||
ql->elements[index] = data;
|
||||
return orig;
|
||||
} else {
|
||||
return ql_replace(ql->next, index - ql->num_elements, data);
|
||||
}
|
||||
}
|
||||
|
||||
int ql_length(const quicklist * ql)
|
||||
{
|
||||
return ql ? ql->num_elements + ql_length(ql->next) : 0;
|
||||
}
|
||||
|
||||
int ql_empty(const quicklist * ql)
|
||||
{
|
||||
return !ql;
|
||||
}
|
||||
|
||||
void ql_push(quicklist ** qlp, void *data)
|
||||
{
|
||||
quicklist *ql = 0;
|
||||
while (*qlp && ((*qlp)->next || (*qlp)->num_elements == QL_MAXSIZE)) {
|
||||
qlp = &(*qlp)->next;
|
||||
}
|
||||
if (!*qlp) {
|
||||
ql = (quicklist *) malloc(sizeof(quicklist));
|
||||
ql->num_elements = 0;
|
||||
ql->next = 0;
|
||||
*qlp = ql;
|
||||
} else {
|
||||
ql = *qlp;
|
||||
}
|
||||
ql->elements[ql->num_elements++] = data;
|
||||
}
|
||||
|
||||
int ql_delete(quicklist ** qlp, int index)
|
||||
{
|
||||
quicklist *ql = *qlp;
|
||||
if (index < 0)
|
||||
return EINVAL;
|
||||
if (ql && index >= ql->num_elements) {
|
||||
return ql_delete(&ql->next, index - ql->num_elements);
|
||||
} else if (ql) {
|
||||
if (index + 1 < ql->num_elements) {
|
||||
memmove(ql->elements + index, ql->elements + index + 1,
|
||||
(ql->num_elements - index - 1) * sizeof(void *));
|
||||
}
|
||||
--ql->num_elements;
|
||||
if (ql->num_elements == 0) {
|
||||
*qlp = ql->next;
|
||||
free(ql);
|
||||
} else if (ql->next && ql->num_elements <= QL_LIMIT) {
|
||||
quicklist *qn = ql->next;
|
||||
if (ql->num_elements + qn->num_elements > QL_MAXSIZE) {
|
||||
memcpy(ql->elements + ql->num_elements, qn->elements, sizeof(void *));
|
||||
--qn->num_elements;
|
||||
++ql->num_elements;
|
||||
memmove(qn->elements, qn->elements + 1,
|
||||
qn->num_elements * sizeof(void *));
|
||||
} else {
|
||||
memcpy(ql->elements + ql->num_elements, qn->elements,
|
||||
qn->num_elements * sizeof(void *));
|
||||
ql->num_elements += qn->num_elements;
|
||||
ql->next = qn->next;
|
||||
free(qn);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ql_insert(quicklist ** qlp, int index, void *data)
|
||||
{
|
||||
quicklist *ql = *qlp;
|
||||
if (ql) {
|
||||
if (index >= QL_MAXSIZE) {
|
||||
return ql_insert(&ql->next, index - ql->num_elements, data);
|
||||
} else if (ql->num_elements < QL_MAXSIZE) {
|
||||
memmove(ql->elements + index + 1, ql->elements + index,
|
||||
(ql->num_elements - index) * sizeof(void *));
|
||||
ql->elements[index] = data;
|
||||
++ql->num_elements;
|
||||
} else {
|
||||
quicklist *qn = (quicklist *) malloc(sizeof(quicklist));
|
||||
qn->next = ql->next;
|
||||
ql->next = qn;
|
||||
qn->num_elements = ql->num_elements-QL_LIMIT;
|
||||
ql->num_elements = QL_LIMIT;
|
||||
memcpy(qn->elements, ql->elements + ql->num_elements,
|
||||
qn->num_elements * sizeof(void *));
|
||||
if (index <= ql->num_elements) {
|
||||
return ql_insert(qlp, index, data);
|
||||
} else {
|
||||
return ql_insert(&ql->next, index - ql->num_elements, data);
|
||||
}
|
||||
}
|
||||
} else if (index == 0) {
|
||||
ql_push(qlp, data);
|
||||
} else {
|
||||
return EINVAL;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ql_foreach(struct quicklist *ql, void (*cb) (void *))
|
||||
{
|
||||
for (; ql; ql = ql->next) {
|
||||
int i;
|
||||
for (i = 0; i != ql->num_elements; ++i) {
|
||||
cb(ql->elements[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int ql_advance(struct quicklist **iterator, int *index, int stride)
|
||||
{
|
||||
quicklist *ql = *iterator;
|
||||
int i = *index;
|
||||
if (!ql || i < 0 || stride < 0) {
|
||||
return ERANGE;
|
||||
} else if (i + stride < ql->num_elements) {
|
||||
*index = i + stride;
|
||||
return 0;
|
||||
} else {
|
||||
*index = i - ql->num_elements + stride;
|
||||
*iterator = ql->next;
|
||||
return ql_advance(iterator, index, 0);
|
||||
}
|
||||
}
|
||||
|
||||
void ql_free(struct quicklist *ql)
|
||||
{
|
||||
while (ql) {
|
||||
quicklist * qn = ql;
|
||||
ql = ql->next;
|
||||
free(qn);
|
||||
}
|
||||
}
|
||||
|
||||
int ql_set_remove(struct quicklist **qlp, void *data)
|
||||
{
|
||||
int qi;
|
||||
quicklist *ql = *qlp;
|
||||
|
||||
if (!ql)
|
||||
return 0;
|
||||
|
||||
for (qi = 0; qi != ql->num_elements; ++qi) {
|
||||
void *qd = ql_get(ql, qi);
|
||||
if (qd == data) {
|
||||
return ql_delete(qlp, qi) == 0;
|
||||
}
|
||||
}
|
||||
return ql_set_remove(&ql->next, data);
|
||||
}
|
||||
|
||||
int ql_set_insert(struct quicklist **qlp, void *data)
|
||||
{
|
||||
if (*qlp) {
|
||||
quicklist *ql = *qlp;
|
||||
if (ql->num_elements > 0 && ql->elements[ql->num_elements - 1] < data) {
|
||||
if (ql->num_elements == QL_MAXSIZE || (ql->next
|
||||
&& ql->next->elements[0] <= data)) {
|
||||
return ql_set_insert(&ql->next, data);
|
||||
} else {
|
||||
ql->elements[ql->num_elements++] = data;
|
||||
}
|
||||
return 0;
|
||||
} else {
|
||||
int i;
|
||||
/* TODO: OPT | binary search */
|
||||
for (i = 0; i != ql->num_elements; ++i) {
|
||||
if (data < ql->elements[i]) {
|
||||
ql_insert(qlp, i, data);
|
||||
return 0;
|
||||
}
|
||||
if (data == ql->elements[i]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ql_push(qlp, data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ql_set_find(struct quicklist **qlp, int *qip, const void *data)
|
||||
{
|
||||
quicklist *ql = *qlp;
|
||||
int qi;
|
||||
|
||||
while (ql && ql->elements[ql->num_elements - 1] < data) {
|
||||
ql = ql->next;
|
||||
}
|
||||
|
||||
if (!ql)
|
||||
return 0;
|
||||
|
||||
/* TODO: OPT | binary search */
|
||||
for (qi = 0; qi != ql->num_elements; ++qi) {
|
||||
if (ql->elements[qi] > data) {
|
||||
return 0;
|
||||
}
|
||||
if (ql->elements[qi] == data) {
|
||||
if (qip) {
|
||||
*qip = qi;
|
||||
*qlp = ql;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
/*
|
||||
Copyright (c) 2010-2011, Enno Rehling <enno@eressea.de>
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
**/
|
||||
|
||||
#ifndef _QUICKLIST_H
|
||||
#define _QUICKLIST_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* see http://en.wikipedia.org/wiki/Unrolled_linked_list */
|
||||
typedef struct quicklist quicklist;
|
||||
void *ql_get(const struct quicklist *ql, int index);
|
||||
int ql_length(const struct quicklist *ql);
|
||||
int ql_empty(const struct quicklist *ql);
|
||||
void ql_push(struct quicklist **qlp, void *data);
|
||||
int ql_delete(struct quicklist **qlp, int index);
|
||||
int ql_insert(struct quicklist **qlp, int index, void *data);
|
||||
void ql_foreach(struct quicklist *ql, void (*cb) (void *));
|
||||
int ql_advance(struct quicklist **iterator, int *index, int stride);
|
||||
void ql_free(struct quicklist *ql);
|
||||
void *ql_replace(struct quicklist *ql, int index, void *data);
|
||||
|
||||
/* you can use it as a set (sorted pointers)*/
|
||||
int ql_set_insert(struct quicklist **qlp, void *data);
|
||||
int ql_set_find(struct quicklist **qlp, int *qip, const void *data);
|
||||
int ql_set_remove(struct quicklist **qlp, void *data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
|
@ -1,203 +0,0 @@
|
|||
#include <CuTest.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "quicklist.h"
|
||||
|
||||
static const char *hello = "Hello World";
|
||||
|
||||
static void test_insert(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
int i;
|
||||
for (i = 0; i != 32; ++i) {
|
||||
CuAssertIntEquals(tc, i, ql_length(ql));
|
||||
ql_insert(&ql, 0, (void *)i);
|
||||
}
|
||||
for (i = 0; i != 32; ++i) {
|
||||
CuAssertIntEquals(tc, 31 - i, (int)ql_get(ql, i));
|
||||
}
|
||||
}
|
||||
|
||||
static void test_insert_delete_gives_null(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
CuAssertIntEquals(tc, 1, ql_empty(ql));
|
||||
ql_push(&ql, (void *)42);
|
||||
CuAssertIntEquals(tc, 0, ql_empty(ql));
|
||||
ql_delete(&ql, 0);
|
||||
CuAssertPtrEquals(tc, 0, ql);
|
||||
}
|
||||
|
||||
static void test_replace(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
int a;
|
||||
ql_push(&ql, (void *)42);
|
||||
a = (int)ql_replace(ql, 0, (void *)43);
|
||||
CuAssertIntEquals(tc, 42, a);
|
||||
CuAssertIntEquals(tc, 43, (int)ql_get(ql, 0));
|
||||
}
|
||||
|
||||
static void test_set_insert(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
int a, qi;
|
||||
a = ql_set_insert(&ql, (void *)42);
|
||||
CuAssertIntEquals(tc, 1, ql_length(ql));
|
||||
CuAssertIntEquals(tc, 0, a);
|
||||
a = ql_set_insert(&ql, (void *)43);
|
||||
CuAssertIntEquals(tc, 2, ql_length(ql));
|
||||
CuAssertIntEquals(tc, 0, a);
|
||||
a = ql_set_insert(&ql, (void *)42);
|
||||
CuAssertIntEquals(tc, 2, ql_length(ql));
|
||||
CuAssertIntEquals(tc, 1, a);
|
||||
a = ql_set_insert(&ql, (void *)41);
|
||||
CuAssertIntEquals(tc, 0, a);
|
||||
CuAssertIntEquals(tc, 3, ql_length(ql));
|
||||
CuAssertIntEquals(tc, 41, (int)ql_get(ql, 0));
|
||||
CuAssertIntEquals(tc, 42, (int)ql_get(ql, 1));
|
||||
CuAssertIntEquals(tc, 43, (int)ql_get(ql, 2));
|
||||
|
||||
a = ql_set_find(&ql, &qi, (void *)49);
|
||||
CuAssertIntEquals(tc, 0, a);
|
||||
a = ql_set_find(&ql, &qi, (void *)42);
|
||||
CuAssertIntEquals(tc, 1, a);
|
||||
CuAssertIntEquals(tc, 42, (int)ql_get(ql, qi));
|
||||
}
|
||||
|
||||
static void test_set_remove(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL, *q2;
|
||||
int a;
|
||||
|
||||
ql_set_insert(&ql, (void *)41);
|
||||
ql_set_insert(&ql, (void *)42);
|
||||
ql_set_insert(&ql, (void *)43);
|
||||
|
||||
q2 = ql;
|
||||
|
||||
a = ql_set_remove(&ql, (void *)42);
|
||||
CuAssertPtrEquals(tc, q2, ql);
|
||||
CuAssertIntEquals(tc, 1, a);
|
||||
CuAssertIntEquals(tc, 41, (int)ql_get(ql, 0));
|
||||
CuAssertIntEquals(tc, 43, (int)ql_get(ql, 1));
|
||||
CuAssertIntEquals(tc, 2, ql_length(ql));
|
||||
|
||||
a = ql_set_remove(&ql, (void *)42);
|
||||
CuAssertPtrEquals(tc, q2, ql);
|
||||
CuAssertIntEquals(tc, 0, a);
|
||||
|
||||
ql_set_remove(&ql, (void *)41);
|
||||
ql_set_remove(&ql, (void *)43);
|
||||
CuAssertPtrEquals(tc, 0, ql);
|
||||
}
|
||||
|
||||
static void test_set_find(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL, *q2;
|
||||
int a, qi;
|
||||
|
||||
for (a = 0; a != 32; ++a) {
|
||||
ql_set_insert(&ql, (void *)a);
|
||||
}
|
||||
|
||||
q2 = ql;
|
||||
a = ql_set_find(&q2, 0, (void *)31);
|
||||
CuAssertIntEquals(tc, 1, a);
|
||||
CuAssertPtrEquals(tc, ql, q2);
|
||||
|
||||
q2 = ql;
|
||||
a = ql_set_find(&ql, &qi, (void *)0);
|
||||
CuAssertIntEquals(tc, 1, a);
|
||||
CuAssertIntEquals(tc, 0, qi);
|
||||
CuAssertPtrEquals(tc, ql, q2);
|
||||
|
||||
q2 = ql;
|
||||
a = ql_set_find(&ql, &qi, (void *)31);
|
||||
CuAssertIntEquals(tc, 1, a);
|
||||
CuAssertIntEquals(tc, 31, (int)ql_get(ql, qi));
|
||||
CuAssertTrue(tc, ql != q2);
|
||||
}
|
||||
|
||||
static void test_advance(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL, *qli;
|
||||
int i, n = 31;
|
||||
for (i = 0; i != 32; ++i) {
|
||||
ql_insert(&ql, 0, (void *)i);
|
||||
}
|
||||
for (i = 0, qli = ql; qli; ql_advance(&qli, &i, 1), n--) {
|
||||
int g = (int)ql_get(qli, i);
|
||||
CuAssertIntEquals(tc, n, g);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_push(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
CuAssertIntEquals(tc, 0, ql_length(ql));
|
||||
ql_push(&ql, (void *)hello);
|
||||
CuAssertIntEquals(tc, 1, ql_length(ql));
|
||||
CuAssertStrEquals(tc, "Hello World", (const char *)ql_get(ql, 0));
|
||||
ql_delete(&ql, 0);
|
||||
CuAssertPtrEquals(tc, 0, ql);
|
||||
}
|
||||
|
||||
static void test_delete_edgecases(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
ql_delete(&ql, 0);
|
||||
CuAssertPtrEquals(tc, 0, ql);
|
||||
ql_push(&ql, (void *)hello);
|
||||
ql_delete(&ql, -1);
|
||||
ql_delete(&ql, 32);
|
||||
CuAssertIntEquals(tc, 1, ql_length(ql));
|
||||
}
|
||||
|
||||
static void test_insert_many(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
int i;
|
||||
for (i = 0; i != 32; ++i) {
|
||||
ql_push(&ql, (void *)i);
|
||||
}
|
||||
for (i = 0; i != 32; ++i) {
|
||||
CuAssertIntEquals(tc, 32 - i, ql_length(ql));
|
||||
CuAssertIntEquals(tc, i, (int)ql_get(ql, 0));
|
||||
CuAssertIntEquals(tc, 31, (int)ql_get(ql, ql_length(ql) - 1));
|
||||
ql_delete(&ql, 0);
|
||||
}
|
||||
CuAssertPtrEquals(tc, 0, ql);
|
||||
}
|
||||
|
||||
static void test_delete_rand(CuTest * tc)
|
||||
{
|
||||
struct quicklist *ql = NULL;
|
||||
int i;
|
||||
for (i = 0; i != 32; ++i) {
|
||||
ql_push(&ql, (void *)i);
|
||||
}
|
||||
CuAssertIntEquals(tc, 32, ql_length(ql));
|
||||
ql_delete(&ql, 0);
|
||||
CuAssertIntEquals(tc, 1, (int)ql_get(ql, 0));
|
||||
CuAssertIntEquals(tc, 31, ql_length(ql));
|
||||
ql_delete(&ql, 30);
|
||||
CuAssertIntEquals(tc, 30, ql_length(ql));
|
||||
}
|
||||
|
||||
CuSuite *get_quicklist_suite(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
SUITE_ADD_TEST(suite, test_advance);
|
||||
SUITE_ADD_TEST(suite, test_replace);
|
||||
SUITE_ADD_TEST(suite, test_push);
|
||||
SUITE_ADD_TEST(suite, test_insert);
|
||||
SUITE_ADD_TEST(suite, test_set_remove);
|
||||
SUITE_ADD_TEST(suite, test_set_find);
|
||||
SUITE_ADD_TEST(suite, test_insert_delete_gives_null);
|
||||
SUITE_ADD_TEST(suite, test_insert_many);
|
||||
SUITE_ADD_TEST(suite, test_delete_rand);
|
||||
SUITE_ADD_TEST(suite, test_delete_edgecases);
|
||||
SUITE_ADD_TEST(suite, test_set_insert);
|
||||
return suite;
|
||||
}
|
Loading…
Reference in a new issue