Commit f2f7b2fa authored by Jens Gustedt's avatar Jens Gustedt
Browse files

constness and some boundary specifications

parent 7d6e5a0e
......@@ -63,7 +63,7 @@ p99_uf* p99_uf_init(p99_uf* uf, uint64_t size) {
}
p99_inline
void p99_uf_destroy(p99_uf volatile* uf) {
void p99_uf_destroy(p99_uf const volatile* uf) {
// empty
}
......@@ -82,87 +82,87 @@ p99_uf* p99_uf_alloc(uint64_t size) {
** @brief Free a UF data structure
**/
p99_inline
void p99_uf_free(p99_uf volatile* uf) {
void p99_uf_free(p99_uf const volatile* uf) {
p99_uf_destroy(uf);
free((void*)uf);
}
p99_inline
bool p00_uf_root(int64_t tab[static 1], int64_t pos) {
bool p00_uf_root(int64_t pos, int64_t const tab[static pos+1]) {
return tab[pos] < 0;
}
p99_inline
uint64_t p00_uf_size(int64_t tab[static 1], int64_t pos) {
return p00_uf_root(tab, pos) ? -tab[pos] : 0;
uint64_t p00_uf_size(int64_t pos, int64_t const tab[static pos+1]) {
return p00_uf_root(pos, tab) ? -tab[pos] : 0;
}
p99_inline
int64_t p00_uf_find(int64_t tab[static 1], uint64_t pos) {
while (!p00_uf_root(tab, pos)) pos = tab[pos];
int64_t p00_uf_find(int64_t pos, int64_t const tab[static pos+1]) {
while (!p00_uf_root(pos, tab)) pos = tab[pos];
return pos;
}
p99_inline
int64_t p99_uf_find(p99_uf* uf, uint64_t pos) {
int64_t p99_uf_find(p99_uf const* uf, uint64_t pos) {
if (!uf || (uf->size <= pos)) return -1;
else return p00_uf_find(uf->tab, pos);
else return p00_uf_find(pos, uf->tab);
}
p99_inline
int64_t p00_uf_exchange(int64_t tab[static 1], int64_t pos, int64_t val) {
int64_t p00_uf_exchange(int64_t pos, int64_t tab[static pos+1], int64_t val) {
int64_t ret = tab[pos];
tab[pos] = val;
return ret;
}
p99_inline
void p00_uf_compress(int64_t tab[static 1], uint64_t pos, uint64_t root) {
while (!p00_uf_root(tab, pos)) {
pos = p00_uf_exchange(tab, pos, root);
void p00_uf_compress(uint64_t pos, int64_t tab[static pos+1], uint64_t root) {
while (!p00_uf_root(pos, tab)) {
pos = p00_uf_exchange(pos, tab, root);
}
}
p99_inline
int64_t p00_uf_findCompress(int64_t tab[static 1], int64_t pos) {
int64_t root = p00_uf_find(tab, pos);
p00_uf_compress(tab, pos, root);
int64_t p00_uf_findCompress(int64_t pos, int64_t tab[static pos+1]) {
int64_t root = p00_uf_find(pos, tab);
p00_uf_compress(pos, tab, root);
return root;
}
p99_inline
int64_t p99_uf_findCompress(p99_uf* uf, uint64_t pos) {
if (!uf || (uf->size <= pos)) return -1;
return p00_uf_findCompress(uf->tab, pos);
return p00_uf_findCompress(pos, uf->tab);
}
p99_inline
uint64_t p99_uf_size(p99_uf* uf, uint64_t pos) {
if (uf || (uf->size > pos)) {
int64_t root = p00_uf_findCompress(uf->tab, pos);
if (root >= 0 && p00_uf_root(uf->tab, pos))
int64_t root = p00_uf_findCompress(pos, uf->tab);
if (root >= 0 && p00_uf_root(pos, uf->tab))
return -uf->tab[pos];
}
return 0;
}
p99_inline
void p00_uf_union(int64_t* tab, int64_t left, int64_t right) {
tab[left] += p00_uf_exchange(tab, right, left);
void p00_uf_union(int64_t left, int64_t right, int64_t tab[static (left < right ? right : left)+1]) {
tab[left] += p00_uf_exchange(right, tab, left);
}
p99_inline
int64_t p99_uf_union(p99_uf* uf, uint64_t left, uint64_t right) {
int64_t root = -1;
if (uf && (uf->size > left) && (uf->size > right)) {
root = p00_uf_findCompress(uf->tab, left);
root = p00_uf_findCompress(left, uf->tab);
if (root >= 0) {
int64_t rright = p00_uf_find(uf->tab, right);
int64_t rright = p00_uf_find(right, uf->tab);
if (rright >= 0) {
// use the new root to mark all elements of the right
p00_uf_compress(uf->tab, right, root);
p00_uf_compress(right, uf->tab, root);
// now also link the old root on the right to the new one
p00_uf_union(uf->tab, root, rright);
p00_uf_union(root, rright, uf->tab);
} else
root = rright;
}
......@@ -174,7 +174,7 @@ p99_inline
void p99_uf_flatten(p99_uf* uf, uint64_t pos, uint64_t length) {
if (uf && (uf->size > pos) && ((uf->size-pos) >= length)) {
for (uint64_t stop = pos+length; pos < stop; ++pos) {
p00_uf_findCompress(uf->tab, pos);
p00_uf_findCompress(pos, uf->tab);
}
}
}
......
......@@ -26,7 +26,7 @@ uint64_t position(size_t n, uint64_t i, uint64_t j) {
** upward in the UF chain to the root.
**/
static
void printBoard(size_t n, p99_uf* uf) {
void printBoard(size_t n, p99_uf const* uf) {
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
int64_t region = uf->tab[position(n, i, j)];
......@@ -59,7 +59,7 @@ int main(void) {
// diagonals of width two.
for (size_t i = 0; i < n-1; ++i) {
for (size_t j = !(i%2); j < n; j += 2) {
p99_uf_union(uf, position(n, i, j), position(n, i+1, j));
p99_uf_union(uf, position(n, i+1, j), position(n, i, j));
}
}
printBoard(n, uf);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment