aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hashset.c
blob: d98e3175f3a14271b4cc5c07290dca4ac9d45a2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define CONTAINER_IMPLEMENTATION
#include "../include/container.h"
#include <stdio.h>

struct user {
	int id;
	const char* name;
};

size_t user_hash(const struct user* u) {
	return u->id;
}

int user_eq(const struct user* a, const struct user* b) {
	return !(a->id == b->id && strcmp(a->name, b->name) == 0);
}

struct user user(int id, const char* name) {
	return (struct user) {.id = id, .name = name};
}

void printuser(struct user* u) {
	printf("(struct user) {.id=%i, .name=\"%s\"};\n", u->id, u->name);
}

int main(void) {
	struct hash_set set = hset_create(sizeof(struct user), (hset_equal_fn)user_eq, (hset_hash_fn)user_hash);
	struct user u0 = user(0, "Jhon");
	struct user u1 = user(1, "John");
	struct user u2 = user(2, "John");
	struct user uw = user(2, "Jnoh");
	struct user ud = user(2, "John");
	hset_insert_copy(&set, &u0);
	hset_insert_copy(&set, &u1);
	hset_insert_copy(&set, &u2);
	hset_insert_copy(&set, &uw);
	hset_insert_copy(&set, &ud);
	hset_insert_copy(&set, &ud);
	hset_insert_copy(&set, &ud);
	hset_insert_copy(&set, &ud);
	hset_insert_copy(&set, &ud);
	hset_insert_copy(&set, &ud);
	hset_insert_copy(&set, &ud);

	hset_iter iter;
	for (hseti_begin(&set, &iter); !hseti_end(&iter); hseti_next(&iter)) {
		struct user* u = hseti_get(&iter);
		printuser(u);
	}

	hset_free(&set);
	return 0;
}