-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset.c
35 lines (30 loc) · 774 Bytes
/
set.c
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
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "vec.h"
#include "set.h"
set_t *set_new(size_t size){
return vec_new(size);
}
bool set_contains(set_t *set, uintptr_t item){
for(int i = 0; i < vec_length(set); i++){
set_entry_t *e = vec_get(set, i);
if(e->item == item) return true;
}
return false;
}
bool set_add(set_t *set, uintptr_t item) {
if(set_contains(set, item)) return false;
set_entry_t *e = malloc(sizeof(set_entry_t));
e->item = item;
vec_push(set, e);
return true;
}
void set_extend(set_t *set, set_t *from) {
for(int i = 0; i < vec_length(from); i++) {
set_entry_t *e = vec_get(from, i);
set_add(set, e->item);
}
}