From d9213c8f253decc10ea788569bd6365f8ad5d932 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Thu, 26 Jan 2017 10:12:12 +0100 Subject: [PATCH] WIP: still converting to clibs/selist --- clibs | 2 +- src/donations.c | 24 ++++++++++-------------- src/kernel/curse.c | 2 +- src/kernel/spell.c | 2 +- src/quicklist.c | 30 ++++++++++++++++++++++++++++-- src/quicklist.h | 5 +++++ src/travelthru.c | 28 ++++++++++++++++++++-------- src/util/message.c | 2 +- 8 files changed, 67 insertions(+), 28 deletions(-) diff --git a/clibs b/clibs index 339ac5680..daa9de083 160000 --- a/clibs +++ b/clibs @@ -1 +1 @@ -Subproject commit 339ac5680ec9027c88c4516c72b9da8233b6f3e3 +Subproject commit daa9de08314a75a4d77ef4e08c713088a96a5ca5 diff --git a/src/donations.c b/src/donations.c index c6e4308ac..0b1413a5f 100644 --- a/src/donations.c +++ b/src/donations.c @@ -59,22 +59,18 @@ void free_donations(void) { transfers = 0; } -static void report_transfer(faction *f1, faction *f2, region *r, int amount) { - struct message *msg = msg_message("donation", - "from to amount", f1, f2, amount); - r_addmessage(r, f1, msg); - r_addmessage(r, f2, msg); - msg_release(msg); +static void report_transfer(void *data) { + transfer *tf = (transfer *)data; + if (tf->amount > 0) { + struct message *msg = msg_message("donation", + "from to amount", tf->f1, tf->f2, tf->amount); + r_addmessage(tf->r, tf->f1, msg); + r_addmessage(tf->r, tf->f2, msg); + msg_release(msg); + } } void report_donations(void) { - ql_iter qli = qli_init(&transfers); - - while (qli_more(qli)) { - transfer *tf = (transfer *)qli_next(&qli); - if (tf->amount > 0) { - report_transfer(tf->f1, tf->f2, tf->r, tf->amount); - } - } + selist_foreach(transfers, report_transfer); } diff --git a/src/kernel/curse.c b/src/kernel/curse.c index 123f858f5..5e2086476 100644 --- a/src/kernel/curse.c +++ b/src/kernel/curse.c @@ -308,7 +308,7 @@ void ct_register(const curse_type * ct) unsigned int hash = tolower(ct->cname[0]) & 0xFF; quicklist **ctlp = cursetypes + hash; - ql_set_insert(ctlp, (void *)ct); + selist_set_insert(ctlp, (void *)ct, NULL); ++ct_changes; } diff --git a/src/kernel/spell.c b/src/kernel/spell.c index 8e14ec2c0..2644f341f 100644 --- a/src/kernel/spell.c +++ b/src/kernel/spell.c @@ -57,7 +57,7 @@ void free_spells(void) { void add_spell(struct quicklist **slistp, spell * sp) { - if (!ql_set_insert(slistp, sp)) { + if (!selist_set_insert(slistp, sp, NULL)) { log_error("add_spell: the list already contains the spell '%s'.\n", sp->sname); } } diff --git a/src/quicklist.c b/src/quicklist.c index 75d7b8641..2b760019c 100644 --- a/src/quicklist.c +++ b/src/quicklist.c @@ -1,4 +1,30 @@ #include "quicklist.h" +#include -bool ql_set_insert_ex(struct quicklist **qlp, void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); -bool ql_set_find_ex(struct quicklist **qlp, int *qip, const void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); +bool ql_set_remove(struct quicklist **qlp, const void *data) +{ + int qi, qn; + quicklist *ql = *qlp; + + if (!ql) + return false; + + qn = selist_length(ql); + for (qi = 0; qi != qn; ++qi) { + void *qd = selist_get(ql, qi); + if (qd == data) { + return selist_delete(qlp, qi) == 0; + } + } + return false; +} + +bool ql_set_insert(struct quicklist **qlp, void *data) +{ + return selist_set_insert(qlp, data, NULL); +} + +bool ql_set_find(struct quicklist **qlp, int *qip, const void *data) +{ + return selist_set_find(qlp, qip, data, NULL); +} diff --git a/src/quicklist.h b/src/quicklist.h index fe2f26991..c0446b471 100644 --- a/src/quicklist.h +++ b/src/quicklist.h @@ -2,7 +2,11 @@ #define selist quicklist #define selist_free ql_free +#define selist_delete ql_delete +#define selist_foreach ql_foreach #define selist_get ql_get +#define selist_replace ql_replace +#define selist_length ql_length #define selist_push ql_push #define selist_empty ql_empty #define selist_advance ql_advance @@ -12,3 +16,4 @@ typedef void(*ql_cb)(void *); bool ql_set_insert_ex(struct quicklist **qlp, void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); bool ql_set_find_ex(struct quicklist **qlp, int *qip, const void *data, int (*cmp_cb)(const void *lhs, const void *rhs)); + diff --git a/src/travelthru.c b/src/travelthru.c index 5e6cac41b..04ca6cee5 100644 --- a/src/travelthru.c +++ b/src/travelthru.c @@ -99,18 +99,30 @@ bool travelthru_cansee(const struct region *r, const struct faction *f, const st return false; } -void travelthru_map(region * r, void(*cb)(region *, struct unit *, void *), void *cbdata) +struct cb_data { + void(*call)(region *, struct unit *, void *); + void *data; + struct region *r; +}; + +void cb_map(void *data, void *ex) { + struct cb_data *cb = (struct cb_data *)ex; + struct unit *u = (struct unit *)data; + cb->call(cb->r, u, cb->data); +} + +void travelthru_map(region * r, void(*cb)(region *, struct unit *, void *), void *data) { attrib *a; + struct cb_data cbdata; + assert(r); + cbdata.call = cb; + cbdata.data = data; + cbdata.r = r; a = a_find(r->attribs, &at_travelunit); if (a) { - quicklist *ql; - ql_iter qi; - ql = (quicklist *)a->data.v; - for (qi = qli_init(&ql); qli_more(qi);) { - unit *u = (unit *)qli_next(&qi); - cb(r, u, cbdata); - } + quicklist *ql = (quicklist *)a->data.v; + selist_foreach_ex(ql, cb_map, &cbdata); } } diff --git a/src/util/message.c b/src/util/message.c index f19fc3313..46bfec969 100644 --- a/src/util/message.c +++ b/src/util/message.c @@ -215,7 +215,7 @@ const message_type *mt_register(message_type * type) unsigned int hash = hashstring(type->name) % MT_MAXHASH; quicklist **qlp = messagetypes + hash; - if (ql_set_insert(qlp, type)) { + if (selist_set_insert(qlp, type, NULL)) { type->key = mt_id(type); } return type;