feat: add union-find-delete data structure
This commit is contained in:
parent
8e2e802c9f
commit
4c3463d3fe
61
src/utils/ufd.c
Normal file
61
src/utils/ufd.c
Normal file
@ -0,0 +1,61 @@
|
||||
#include "ufd.h"
|
||||
#include "stdlib.h"
|
||||
#include <sys/queue.h>
|
||||
|
||||
struct ufd_t ufd__make_set(struct context_entry_t* thread)
|
||||
{
|
||||
struct ufd_children_t children = TAILQ_HEAD_INITIALIZER(children);
|
||||
|
||||
return (struct ufd_t) {
|
||||
.thread = thread,
|
||||
.parent = NULL,
|
||||
.repr = NULL,
|
||||
.children = children,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
void ufd__init(struct ufd_t* ufd, struct context_entry_t* thread)
|
||||
{
|
||||
TAILQ_INIT(&ufd->children);
|
||||
ufd->thread = thread;
|
||||
ufd->repr = NULL,
|
||||
ufd->parent = NULL;
|
||||
}
|
||||
|
||||
|
||||
void ufd__join(struct ufd_t* th1, struct ufd_t* th2)
|
||||
{
|
||||
struct ufd_t* a = ufd__find(th1);
|
||||
struct ufd_t* b = ufd__find(th2);
|
||||
|
||||
if (a != b)
|
||||
{
|
||||
a->parent = b;
|
||||
a->repr = b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct ufd_t* ufd__find(struct ufd_t* th)
|
||||
{
|
||||
if (th->parent == NULL)
|
||||
return th;
|
||||
|
||||
// If parent is null, repr can't be null
|
||||
if (th->repr->parent == NULL)
|
||||
return th->repr;
|
||||
|
||||
struct ufd_t* nrepr = ufd__find(th->repr);
|
||||
th->repr = nrepr;
|
||||
return nrepr;
|
||||
}
|
||||
|
||||
|
||||
void ufd__delete(struct ufd_t* th)
|
||||
{
|
||||
struct ufd_t* child;
|
||||
TAILQ_FOREACH(child, &th->children, link) {
|
||||
child->parent = NULL;
|
||||
}
|
||||
}
|
35
src/utils/ufd.h
Normal file
35
src/utils/ufd.h
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef _UNION_FIND_H_
|
||||
#define _UNION_FIND_H_
|
||||
#include <sys/queue.h>
|
||||
|
||||
TAILQ_HEAD(ufd_children_t, ufd_t);
|
||||
|
||||
// union find delete data structure
|
||||
struct ufd_t {
|
||||
struct ufd_t* parent;
|
||||
struct ufd_t* repr;
|
||||
struct context_entry_t* thread;
|
||||
struct ufd_children_t children;
|
||||
TAILQ_ENTRY(ufd_t) link;
|
||||
};
|
||||
|
||||
|
||||
struct ufd_t ufd__make_set(struct context_entry_t* thread);
|
||||
|
||||
void ufd__init(struct ufd_t* ufd, struct context_entry_t* thread);
|
||||
|
||||
/*
|
||||
* Join th1 set to th2 set
|
||||
*/
|
||||
void ufd__join(struct ufd_t* th1, struct ufd_t* th2);
|
||||
|
||||
/*
|
||||
* Find th's representent
|
||||
*/
|
||||
struct ufd_t* ufd__find(struct ufd_t* th);
|
||||
|
||||
/*
|
||||
* Deletes th from its set
|
||||
*/
|
||||
void ufd__delete(struct ufd_t* th);
|
||||
#endif // _UNION_FIND_H_
|
Loading…
x
Reference in New Issue
Block a user