// Union-find: disjoint-set forest, union by size, path halving.
//
// Layout: a single array of n int32_t entries, packed into the same
// allocation as the header. For element x:
//   p[x] >= 0  : x is not a root; p[x] is its parent.
//   p[x] <  0  : x is a root; -p[x] is the size of its set.
// n < 2^31, so every parent index and every set size fits.

#include "unionfind.h"

#include <stdlib.h>
#include <string.h>

struct unionfind {
    uint32_t n;
    uint32_t sets;
    int32_t p[];
};

#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)

unionfind *uf_new(uint32_t n) {
    size_t bytes = sizeof(unionfind) + (size_t)n * sizeof(int32_t);
    unionfind *uf = malloc(bytes);
    if (!uf) abort();
    uf->n = n;
    uf->sets = n;
    // Every element a root of size 1: all entries -1 (all bytes 0xFF).
    // Touching the pages here keeps page faults out of later operations.
    memset(uf->p, 0xFF, (size_t)n * sizeof(int32_t));
    return uf;
}

void uf_free(unionfind *uf) { free(uf); }

// Path halving from x, where v = p[x] >= 0 on entry. Returns the root and
// stores its (negative) entry in *rv.
static inline uint32_t halve_from(int32_t *restrict p, uint32_t x, int32_t v, int32_t *rv) {
    while (v >= 0) {
        int32_t g = p[v];
        if (g < 0) {
            *rv = g;
            return (uint32_t)v;
        }
        p[x] = g;
        x = (uint32_t)g;
        v = p[g];
    }
    *rv = v;
    return x;
}

static inline uint32_t find_root(int32_t *restrict p, uint32_t x, int32_t *rv) {
    int32_t v = p[x];
    if (v < 0) {
        *rv = v;
        return x;
    }
    return halve_from(p, x, v, rv);
}

// Find the roots of a and b together, interleaving the two walks so that
// their cache misses overlap. Both walks use path halving.
static inline void find_two(int32_t *restrict p, uint32_t *a, int32_t *av, uint32_t *b,
                            int32_t *bv) {
    uint32_t x = *a, y = *b;
    int32_t vx = p[x], vy = p[y];
    while (vx >= 0 && vy >= 0) {
        int32_t gx = p[vx];
        int32_t gy = p[vy];
        if (gx < 0) {
            x = (uint32_t)vx;
            vx = gx;
        } else {
            p[x] = gx;
            x = (uint32_t)gx;
            vx = p[gx];
        }
        if (gy < 0) {
            y = (uint32_t)vy;
            vy = gy;
        } else {
            p[y] = gy;
            y = (uint32_t)gy;
            vy = p[gy];
        }
    }
    if (vx >= 0) x = halve_from(p, x, vx, &vx);
    if (vy >= 0) y = halve_from(p, y, vy, &vy);
    *a = x;
    *av = vx;
    *b = y;
    *bv = vy;
}

uint32_t uf_find(unionfind *uf, uint32_t x) {
    int32_t rv;
    return find_root(uf->p, x, &rv);
}

bool uf_union(unionfind *uf, uint32_t a, uint32_t b) {
    int32_t *restrict p = uf->p;
    int32_t va, vb;
    find_two(p, &a, &va, &b, &vb);
    if (UNLIKELY(a == b)) return false;
    // Union by size: the smaller set (less negative entry) goes below.
    if (va > vb) {
        uint32_t t = a;
        a = b;
        b = t;
    }
    p[a] = va + vb;
    p[b] = (int32_t)a;
    uf->sets--;
    return true;
}

bool uf_connected(unionfind *uf, uint32_t a, uint32_t b) {
    int32_t va, vb;
    find_two(uf->p, &a, &va, &b, &vb);
    return a == b;
}

uint32_t uf_set_size(unionfind *uf, uint32_t x) {
    int32_t rv;
    find_root(uf->p, x, &rv);
    return (uint32_t)(-rv);
}

uint32_t uf_sets(const unionfind *uf) { return uf->sets; }

uint32_t uf_len(const unionfind *uf) { return uf->n; }

uint32_t uf_parent(const unionfind *uf, uint32_t x) {
    int32_t v = uf->p[x];
    return v < 0 ? x : (uint32_t)v;
}
