Skip to content

Commit

Permalink
Cruft.
Browse files Browse the repository at this point in the history
  • Loading branch information
katef committed Mar 20, 2021
1 parent d6187d3 commit a87df37
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 374 deletions.
216 changes: 0 additions & 216 deletions src/adt/set.inc
Original file line number Diff line number Diff line change
Expand Up @@ -109,55 +109,6 @@ set_create(const struct fsm_alloc *a,
return set;
}

static struct set *
set_create_singleton(const struct fsm_alloc *a,
int (*cmp)(const void *a, const void *b), item_t *item)
{
struct set *s;

assert(cmp != NULL);

s = f_malloc(a, sizeof *s);
if (s == NULL) {
return NULL;
}

s->a = f_malloc(a, sizeof *s->a);
if (s->a == NULL) {
f_free(a, s);
return NULL;
}

s->alloc = a;
s->a[0] = item;
s->i = s->n = 1;
s->cmp = cmp;

return s;
}

static struct set *
set_copy(const struct set *set)
{
struct set *s;
s = malloc(sizeof *s);
if (s == NULL) {
return NULL;
}

s->cmp = set->cmp;
s->a = malloc(set->i * sizeof s->a[0]);
if (s->a == NULL) {
free(s);
return NULL;
}

s->i = s->n = set->i;
memcpy(s->a, set->a, s->n * sizeof s->a[0]);

return s;
}

static item_t *
set_add(struct set *set, item_t *item)
{
Expand Down Expand Up @@ -213,31 +164,6 @@ set_add(struct set *set, item_t *item)
return item;
}

static void
set_remove(struct set *set, const item_t *item)
{
size_t i;

assert(set != NULL);
assert(set->cmp != NULL);
assert(item != NULL);

if (set_empty(set)) {
return;
}

i = set_search(set, item);
if (set->cmp(&item, &set->a[i]) == 0) {
if (i < set->i) {
memmove(&set->a[i], &set->a[i + 1], (set->i - i - 1) * (sizeof *set->a));
}

set->i--;
}

assert(!set_contains(set, item));
}

static void
set_free(struct set *set)
{
Expand All @@ -248,145 +174,3 @@ set_free(struct set *set)
free(set);
}

static size_t
set_count(const struct set *set)
{
assert(set != NULL);
assert(set->a != NULL);

return set->i;
}

static void
set_clear(struct set *set)
{
assert(set != NULL);
assert(set->a != NULL);

set->i = 0;
}

static int
set_cmp(const struct set *a, const struct set *b)
{
assert(a != NULL);
assert(a->a != NULL);
assert(b != NULL);
assert(b->a != NULL);

if (a->i != b->i) {
return a->i - b->i;
}

return memcmp(a->a, b->a, a->i * sizeof *a->a);
}

static int
set_equal(const struct set *a, const struct set *b)
{
assert(a != NULL);
assert(a->a != NULL);
assert(b != NULL);
assert(b->a != NULL);

if (a->i != b->i) {
return 0;
}

return 0 == memcmp(a->a, b->a, a->i * sizeof *a->a);
}

static item_t *
set_first(const struct set *set, struct set_iter *it)
{
assert(set != NULL);
assert(set->a != NULL);
assert(it != NULL);

if (set_empty(set)) {
it->set = NULL;
return NULL;
}

it->i = 0;
it->set = set;

return it->set->a[it->i];
}

static item_t *
set_firstafter(const struct set *set, struct set_iter *it, const item_t *item)
{
size_t i;
int r;

assert(set != NULL);
assert(set->cmp != NULL);
assert(set->a != NULL);
assert(it != NULL);

if (set_empty(set)) {
it->set = NULL;
return NULL;
}

i = set_search(set, item);
r = set->cmp(&item, &set->a[i]);
assert(i <= set->i - 1);

if (r >= 0 && i == set->i - 1) {
it->set = NULL;
return NULL;
}

it->i = i;
if (r >= 0) {
it->i++;
}

it->set = set;
return it->set->a[it->i];
}

static item_t *
set_next(struct set_iter *it)
{
assert(it != NULL);

it->i++;
if (it->i >= it->set->i) {
return NULL;
}

return it->set->a[it->i];
}

static item_t *
set_only(const struct set *set)
{
assert(set != NULL);
assert(set->n >= 1);
assert(set->i == 1);
assert(set->a[0] != NULL);

return set->a[0];
}

static int
set_hasnext(const struct set_iter *it)
{
assert(it != NULL);

return it->set && it->i + 1 < it->set->i;
}

static const item_t *
set_array(const struct set *set)
{
if (set == NULL) {
return NULL;
}

return (const item_t *) set->a;
}

3 changes: 0 additions & 3 deletions tests/set/set2.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ int main(void) {
assert(set_add(s, &a));
assert(set_add(s, &a));

set_remove(s, &a);
assert(!set_contains(s, &a));

set_free(s);

return 0;
Expand Down
41 changes: 28 additions & 13 deletions tests/set/set3.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,43 @@ cmp_int(const void *a, const void *b)
else return 0;
}

int *next_int(void) {
static int n = 0;
int *p = malloc(sizeof *p);
if (p == NULL) abort();
*p = n++;
return p;
}

enum { COUNT = 5000U };

int main(void) {
struct set *s = set_create(NULL, cmp_int);
struct set_iter iter;
int *p;
int a[3] = {1, 2, 3};
int seen[3] = {0, 0, 0};
int i;
size_t i;
int **plist;

int a[3] = {1200,2400,3600};
const unsigned num_a = sizeof a / sizeof *a;

assert(s != NULL);

assert(set_add(s, &a[0]));
assert(set_add(s, &a[1]));
assert(set_add(s, &a[2]));
plist = calloc(COUNT, sizeof *plist);
assert(plist != NULL);

for (i = 0; i < COUNT; i++) {
int *itm = next_int();
plist[i] = itm;
assert(set_add(s, itm));
}

for (p = set_first(s, &iter); p != NULL; p = set_next(&iter)) {
assert(*p == 1 || *p == 2 || *p == 3);
seen[*p - 1] = 1;
for (i = 0; i < num_a; i++) {
assert(set_contains(s, &a[i]));
}

for (i = 0; i < 3; i++) {
assert(seen[i]);
for (i=0; i < COUNT; i++) {
free(plist[i]);
}
free(plist);

set_free(s);
return 0;
Expand Down
1 change: 0 additions & 1 deletion tests/set/set4.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ int main(void) {
plist[i] = itm;
assert(set_add(s, itm));
}
assert(set_count(s) == COUNT);

for (i=0; i < COUNT; i++) {
free(plist[i]);
Expand Down
70 changes: 0 additions & 70 deletions tests/set/set5.c

This file was deleted.

Loading

0 comments on commit a87df37

Please sign in to comment.