Skip to content

Commit

Permalink
Sort endids written out by fsm_endid_get().
Browse files Browse the repository at this point in the history
This is for caller convenience, I keep calling qsort for various reasons whenever I call this function, for example to use memcmp on ids.

But the final motivation here was that I wanted to memcmp a const-qualified pointer to an id set stored elsewhere, and I can't do that without allocating for a local copy. And that seems silly when we can just qsort here.
  • Loading branch information
katef committed Jul 1, 2024
1 parent 14bbc0f commit d756f9e
Show file tree
Hide file tree
Showing 24 changed files with 33 additions and 206 deletions.
3 changes: 1 addition & 2 deletions include/fsm/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ fsm_endid_set(struct fsm *fsm, fsm_state_t end_state, fsm_end_id_t id);
* id_buf is expected to have enough cells (according to id_buf_count)
* to store all the end IDs. You can find this with fsm_endid_count().
*
* The end IDs in the buffer may appear in any order,
* but will not have duplicates.
* The end IDs in the buffer are sorted and do not have duplicates.
*
* A state with no end IDs set is considered equivalent to a state
* that has the empty set, this API does not distinguish these cases.
Expand Down
4 changes: 3 additions & 1 deletion src/libfsm/endids.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,9 @@ fsm_endid_get(const struct fsm *fsm, fsm_state_t end_state,
ids[id_i] = b->ids->ids[id_i];
}

/* todo: could sort them here, if it matters. */
/* sorting for caller convenience */
qsort(ids, count, sizeof *ids, cmp_endids);

return 1;
} else { /* collision */
#if LOG_ENDIDS > 4
Expand Down
12 changes: 0 additions & 12 deletions src/libfsm/minimise.c
Original file line number Diff line number Diff line change
Expand Up @@ -939,14 +939,6 @@ split_ecs_by_end_metadata(struct min_env *env, const struct fsm *fsm)
return res;
}

static int
cmp_end_ids(const void *pa, const void *pb)
{
const fsm_end_id_t a = *(fsm_end_id_t *)pa;
const fsm_end_id_t b = *(fsm_end_id_t *)pb;
return a < b ? -1 : a > b ? 1 : 0;
}

static int
collect_end_ids(const struct fsm *fsm, fsm_state_t s,
struct end_metadata_end *e)
Expand All @@ -965,10 +957,6 @@ collect_end_ids(const struct fsm *fsm, fsm_state_t s,
int res = fsm_endid_get(fsm, s, e->count, e->ids);
assert(res == 1);

/* sort, to make comparison easier later */
qsort(e->ids, e->count,
sizeof(e->ids[0]), cmp_end_ids);

#if LOG_ECS
fprintf(stderr, "%d:", s);
for (size_t i = 0; i < written; i++) {
Expand Down
3 changes: 1 addition & 2 deletions src/libfsm/minimise_test_oracle.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ fsm_minimise_test_oracle(const struct fsm *fsm)
continue;
}

int eres = fsm_endid_get(fsm, i,
count_a, ids_a);
int eres = fsm_endid_get(fsm, i, count_a, ids_a);
assert(eres == 1);

bool found = false;
Expand Down
15 changes: 0 additions & 15 deletions src/libfsm/print/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@
#include <fsm/print.h>
#include <fsm/options.h>

/* TODO: centralise */
static int
comp_end_id(const void *a, const void *b)
{
assert(a != NULL);
assert(b != NULL);

if (* (fsm_end_id_t *) a < * (fsm_end_id_t *) b) { return -1; }
if (* (fsm_end_id_t *) a > * (fsm_end_id_t *) b) { return +1; }

return 0;
}

static int
rangeclass(unsigned char x, unsigned char y)
{
Expand Down Expand Up @@ -161,8 +148,6 @@ fsm_print_api(FILE *f, const struct fsm *fsm_orig)
res = fsm_endid_get(fsm, end, count, ids);
assert(res == 1);

qsort(ids, count, sizeof *ids, comp_end_id);

fprintf(f, "\tif (fsm_isend(fsm, s[%u])) {\n", end);
for (size_t id = 0; id < count; id++) {
fprintf(f, "\t\tif (!fsm_endid_set(fsm, s[%u], %zu)) { return 0; }\n", end, id);
Expand Down
15 changes: 0 additions & 15 deletions src/libfsm/print/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@
#include <fsm/print.h>
#include <fsm/options.h>

/* TODO: centralise */
static int
comp_end_id(const void *a, const void *b)
{
assert(a != NULL);
assert(b != NULL);

if (* (fsm_end_id_t *) a < * (fsm_end_id_t *) b) { return -1; }
if (* (fsm_end_id_t *) a > * (fsm_end_id_t *) b) { return +1; }

return 0;
}

/* TODO: centralise */
static int
findany(const struct fsm *fsm, fsm_state_t state, fsm_state_t *a)
Expand Down Expand Up @@ -325,8 +312,6 @@ fsm_print_fsm(FILE *f, const struct fsm *fsm)
}

if (count > 0) {
qsort(ids, count, sizeof *ids, comp_end_id);

fprintf(f, " = [");

for (size_t id = 0; id < count; id++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/capture/capture4.c
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ check(const struct fsm *fsm, const char *string,
int gres;
fsm_end_id_t ids[2];

gres = fsm_endid_get(fsm, end, 2, ids);
gres = fsm_endid_get(fsm, end, fsm_endid_count(fsm, end), ids);
if (gres != 1) {
assert(!"fsm_getendids failed");
}
Expand Down
7 changes: 1 addition & 6 deletions tests/endids/endids0.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,7 @@ int main(void)

assert( fsm_endid_count(fsm, state_ind) == 1);

ret = fsm_endid_get(
fsm,
state_ind,
1,
&id);

ret = fsm_endid_get(fsm, state_ind, 1, &id);
assert(ret == 1);
assert(id == 1);

Expand Down
13 changes: 2 additions & 11 deletions tests/endids/endids0_many_endids.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,14 @@ int main(void)

assert(ret == 1);

/* sort endids and compare */
qsort(&endids[0],
sizeof endids / sizeof endids[0], sizeof endids[0],
cmp_endids);
for (i=0; i < fsm_endid_count(fsm, state_ind); i++) {
for (i=0; i < sizeof all_ids / sizeof all_ids[0]; i++) {
assert(endids[i] == sorted_all_ids[i]);
}

#if 0
/* endids should be sorted */
for (i=0; i < nwritten; i++) {
for (i=0; i < sizeof all_ids / sizeof all_ids[0]; i++) {
assert(endids[i] == sorted_all_ids[i]);
}
#endif /* 0 */

nend++;
}
Expand Down Expand Up @@ -160,9 +154,6 @@ int main(void)
assert( ids[0] == matches[i].endid );
#endif /* 0 */

/* sort endids and compare */
qsort(&ids[0], count, sizeof ids[0], cmp_endids);

for (j=0; j < count; j++) {
assert(ids[j] == sorted_all_ids[j]);
}
Expand Down
7 changes: 1 addition & 6 deletions tests/endids/endids1_determinise.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ int main(void)

assert( fsm_endid_count(fsm, state_ind) == 1);

ret = fsm_endid_get(
fsm,
state_ind,
1,
&endid);

ret = fsm_endid_get(fsm, state_ind, 1, &endid);
assert(ret == 1);
assert( endid == 1 );
}
Expand Down
7 changes: 1 addition & 6 deletions tests/endids/endids1_determinise_and_minimise.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ int main(void)

assert(fsm_endid_count(fsm, state_ind) == 1);

ret = fsm_endid_get(
fsm,
state_ind,
1,
&endid);

ret = fsm_endid_get(fsm, state_ind, 1, &endid);
assert(ret == 1);
assert( endid == 1 );
}
Expand Down
12 changes: 2 additions & 10 deletions tests/endids/endids2_union.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,9 @@ int main(void)

count = fsm_endid_count(comb, state_ind);
// fprintf(stderr, "state %u, count = %zu\n", state_ind, count);
assert(count > 0 && count <= 2);

assert( count > 0 && count <= 2);

ret = fsm_endid_get(
comb,
state_ind,
2,
&endids[0]);

ret = fsm_endid_get( comb, state_ind, count, &endids[0]);
assert(ret == 1);

if (count == 1) {
Expand Down Expand Up @@ -158,8 +152,6 @@ int main(void)

assert( count == matches[i].count );

qsort(&ids[0], count, sizeof ids[0], cmp_endids);

for (j=0; j < count; j++) {
assert( ids[j] == matches[i].endid[j] );
}
Expand Down
16 changes: 2 additions & 14 deletions tests/endids/endids2_union_many_endids.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,9 @@ int main(void)
memset(&ids[0], 0, sizeof ids);

count = fsm_endid_count(fsm, state_ind);

assert(count > 0 && count <= sizeof ids/sizeof ids[0]);

ret = fsm_endid_get(
fsm,
state_ind,
sizeof ids/sizeof ids[0],
&ids[0]);

ret = fsm_endid_get(fsm, state_ind, count, &ids[0]);
assert(ret == 1);

memset(&tested_pattern[0], 0, sizeof tested_pattern);
Expand Down Expand Up @@ -253,15 +247,9 @@ int main(void)
memset(&ids[0], 0, sizeof ids);

count = fsm_endid_count(fsm, state_ind);

assert(count <= NUM_ENDIDS_TOTAL);

ret = fsm_endid_get(
fsm,
state_ind,
sizeof ids/sizeof ids[0],
&ids[0]);

ret = fsm_endid_get(fsm, state_ind, count, &ids[0]);
assert(ret == 1);
}
}
Expand Down
12 changes: 1 addition & 11 deletions tests/endids/endids4.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,12 @@ int main(void)
int ret;

count = fsm_endid_count(comb, state_ind);

printf("state %u count = %zu\n", state_ind, count);

assert(count == 2);

ret = fsm_endid_get(
comb,
state_ind,
2,
&endids[0]);

ret = fsm_endid_get(comb, state_ind, count, &endids[0]);
assert(ret == 1);

qsort(&endids[0], count, sizeof endids[0], cmp_endids);
assert(endids[0] == 1 && endids[1] == 2);
}
}
Expand Down Expand Up @@ -149,8 +141,6 @@ int main(void)

assert( count == matches[i].count );

qsort(&ids[0], count, sizeof ids[0], cmp_endids);

for (j=0; j < count; j++) {
assert( ids[j] == matches[i].endid[j] );
}
Expand Down
11 changes: 1 addition & 10 deletions tests/endids/endids5.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,10 @@ int main(void)
int ret;

count = fsm_endid_count(comb, state_ind);

printf("state %u count = %zu\n", state_ind, count);

assert(count == 1);

ret = fsm_endid_get(
comb,
state_ind,
2,
&endids[0]);

ret = fsm_endid_get(comb, state_ind, count, &endids[0]);
assert(ret == 1);

assert(endids[0] == 1);
Expand Down Expand Up @@ -153,8 +146,6 @@ int main(void)

assert( count == matches[i].count );

qsort(&ids[0], count, sizeof ids[0], cmp_endids);

for (j=0; j < count; j++) {
assert( ids[j] == matches[i].endid[j] );
}
Expand Down
25 changes: 4 additions & 21 deletions tests/endids/endids6.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,9 @@ int main(void)
int ret;

count = fsm_endid_count(fsm, state_ind);
assert(count > 0 && count <= 2);

assert( count > 0 && count <= 2);

ret = fsm_endid_get(
fsm,
state_ind,
sizeof ids / sizeof ids[0],
&ids[0]);

ret = fsm_endid_get(fsm, state_ind, count, &ids[0]);
assert(ret == 1);

info[ninfo].state = state_ind;
Expand All @@ -114,7 +108,6 @@ int main(void)
assert(ids[0] == 1 || ids[0] == 2);
info[ninfo].ids[0] = ids[0];
} else if (count == 2) {
qsort(&ids[0], count, sizeof ids[0], cmp_endids);
assert(ids[0] == 1 && ids[1] == 2);
info[ninfo].ids[0] = ids[0];
info[ninfo].ids[1] = ids[1];
Expand All @@ -139,22 +132,12 @@ int main(void)
assert( state_ind == info[info_ind].state );

count = fsm_endid_count(fsm, state_ind);

assert(count > 0 && count <= 2);
assert(count == info[info_ind].count);

assert( count == info[info_ind].count );
ret = fsm_endid_get(
fsm,
state_ind,
sizeof ids / sizeof ids[0],
&ids[0]);

ret = fsm_endid_get(fsm, state_ind, count, &ids[0]);
assert(ret == 1);

if (count > 1) {
qsort(&ids[0], count, sizeof ids[0], cmp_endids);
}

for (ind=0; ind < count; ind++) {
fsm_end_id_t expected = info[info_ind].ids[ind];
switch (info[info_ind].ids[ind]) {
Expand Down
Loading

0 comments on commit d756f9e

Please sign in to comment.