summaryrefslogtreecommitdiff
path: root/set_bitsets.c
blob: 3e4c257a7b76300437170ddf4b498a62388646b5 (plain) (blame)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
	Projet d'algorithmique et programmation 2013-2014
	(cours de C.Matthieu et J.Stern)
	Alex AUVOLAT, Mendes OULAMARA

	Implémentation des ensembles d'entiers sous forme de bitsets.
	Code sous la juridiction de Mendes OULAMARA.
*/

#include "sets.h"
#include <stdlib.h>
#include <stdio.h>
#define SCOD 8*sizeof(unsigned long long)

int nbCells(int n){
	if(n == (n/SCOD)*SCOD)
		return n/SCOD;
	else
		return n/SCOD+1;
}

int set_size(const set s){
    return s.size;
}

set empty_set(int size){
	set res;
	int i;
	res.size = size;
	int N = nbCells(size);
	res.tab = malloc(SCOD*N/8);
	for(i = 0; i < N; i++)
	    res.tab[i] = 0;
	return res;
}

/*
set singleton(int size, int x){
	set res = empty_set(size);
	
	if(x >= size){
		printf("Index %d out of bound %d in singleton creation\n", x, size);
		exit 1;
	}

	res.tab.tab[x/SCOD] = 1<<(x%SCOD);
	return res;
}*/

set copy_set(const set s){
	set res = empty_set(s.size);
	int N = nbCells(s.size), i;
	for(i = 0; i < N; i++) 
		res.tab[i] = s.tab[i];
	return res;
}

void delete_set(set a){
	free(a.tab);
}

void set_union_ip(set a, const set b){
	if(a.size != b.size){
		printf("Union failed : the sets don't have the same size\n");
		exit(1);
	}
	int N = nbCells(a.size), i;
	for(i = 0; i < N; i++) 
		a.tab[i] = a.tab[i]|b.tab[i];
}

void set_inter_ip(set a, const set b){
	if(a.size != b.size){
		printf("Intersection failed : the sets don't have the same size\n");
		exit(1);
	}
	int N = nbCells(a.size), i;
	for(i = 0; i < N; i++) 
		a.tab[i] = a.tab[i]&b.tab[i];
}


void set_diff_ip(set a, const set b){
	if(a.size != b.size){
		printf("Difference failed : the sets don't have the same size\n");
		exit(1);
	}
	int N = nbCells(a.size), i;
	for(i = 0; i < N; i++) 
		a.tab[i] = a.tab[i] & (~b.tab[i]);
}

/*
set set_union(const set a, const set b){
	set res = copy_set(a);
	set_union_ip(res, b);
}

set set_inter(const set a, const set b){
	set res = copy_set(a);
	set_inter_ip(res, b);
}

set set_diff(const set a, const set b){
	set res = copy_set(a);
	set_diff_ip(res, b);
}
*/

bool is_set_empty(const set s){
	int N = nbCells(s.size),i;
	for(i = 0; i < N; i++) 
		if(s.tab[i] != 0)
			return false;
	return true;
}

inline bool set_mem(int x, const set s){
	return  (s.tab[x/SCOD]>>(x%SCOD))&1;
}

int dyadic_val(unsigned long long x){
	if(x == 0){
		printf("Infinite valuation\n");
		exit (1);
	}
	int i;
	for(i=0; ((x>>i)&1) == 0; i++);
	return i;
}

int elt_of_set(const set s){
	int N=nbCells(s.size), i;
	for(i=0; i<N; i++)
		if(s.tab[i] != 0)

	printf("No element in an empty set!\n");
	exit(1);
}

//set set_add(int x, const set s);

void set_add_ip(int x, set s){
	s.tab[x/SCOD] = s.tab[x/SCOD] | 1<<(x%SCOD);
}

//set set_remove(int x, const set s);

void set_remove_ip(int x, set s){
	s.tab[x/SCOD] = s.tab[x/SCOD] & (~(1<<(x%SCOD)));
}

void dump_set(const set s){
    int i=0;
    printf("[");
    for(i=0; i < s.size; i++)
	if(set_mem(i, s))
	    printf(" %d ", i);
    printf("] (%d)\n", s.size);
}

set full_set(int n) {
    set r = empty_set(n);
    int N=nbCells(r.size), i;
    for(i=0; i< N; i++)
	r.tab[i]=-1;
    return r;
}