package unionfind

import "unsafe"

// UnionFind is a disjoint-set forest over elements 0 .. n-1.
//
// Each entry data[i] holds either:
//   - a negative integer -size (for a root, where size >= 1), or
//   - a non-negative parent index in 0 .. n-1 (for a non-root).
//
// Because n < 2^31, both element indices and negated set sizes fit in int32
// without overflow.
type UnionFind struct {
	data []int32
	sets uint32
}

// New creates a UnionFind with n singleton sets (0 .. n-1). n may be 0.
func New(n uint32) *UnionFind {
	if n == 0 {
		return &UnionFind{}
	}
	data := make([]int32, n)
	limit := len(data)
	if limit > 64 {
		limit = 64
	}
	for i := 0; i < limit; i++ {
		data[i] = -1
	}
	for bp := 64; bp < len(data); bp <<= 1 {
		copy(data[bp:], data[:bp])
	}
	return &UnionFind{
		data: data,
		sets: n,
	}
}

// Find returns the representative (root) of x's set, shortening the path by
// path splitting.
func (u *UnionFind) Find(x uint32) uint32 {
	d := (*[1 << 32]int32)(unsafe.Pointer(unsafe.SliceData(u.data)))
	p := d[x]
	r := x
	if p >= 0 {
		r = uint32(p)
	}
	s := d[r]
	for s >= 0 {
		d[x] = s
		x = r
		r = uint32(s)
		s = d[r]
	}
	return r
}

// Union merges the sets containing a and b using union by size (on a tie,
// b's root is attached under a's root) and path splitting. Returns true if
// two distinct sets were merged.
func (u *UnionFind) Union(a, b uint32) bool {
	d := (*[1 << 32]int32)(unsafe.Pointer(unsafe.SliceData(u.data)))
	pa := d[a]
	pb := d[b]
	ra := a
	if pa >= 0 {
		ra = uint32(pa)
	}
	rb := b
	if pb >= 0 {
		rb = uint32(pb)
	}
	sa := d[ra]
	sb := d[rb]
	if (sa & sb) >= 0 {
		for sa >= 0 {
			d[a] = sa
			a = ra
			ra = uint32(sa)
			sa = d[ra]
		}
		for sb >= 0 {
			d[b] = sb
			b = rb
			rb = uint32(sb)
			sb = d[rb]
		}
	}
	if ra == rb {
		return false
	}
	if sa > sb {
		ra, rb = rb, ra
	}
	d[ra] = sa + sb
	d[rb] = int32(ra)
	u.sets--
	return true
}

// Connected reports whether a and b belong to the same set, shortening paths
// by path splitting.
func (u *UnionFind) Connected(a, b uint32) bool {
	d := (*[1 << 32]int32)(unsafe.Pointer(unsafe.SliceData(u.data)))
	pa := d[a]
	pb := d[b]
	ra := a
	if pa >= 0 {
		ra = uint32(pa)
	}
	rb := b
	if pb >= 0 {
		rb = uint32(pb)
	}
	sa := d[ra]
	sb := d[rb]
	if (sa & sb) >= 0 {
		for sa >= 0 {
			d[a] = sa
			a = ra
			ra = uint32(sa)
			sa = d[ra]
		}
		for sb >= 0 {
			d[b] = sb
			b = rb
			rb = uint32(sb)
			sb = d[rb]
		}
	}
	return ra == rb
}

// SetSize returns the number of elements in x's set, shortening the path by
// path splitting.
func (u *UnionFind) SetSize(x uint32) uint32 {
	d := (*[1 << 32]int32)(unsafe.Pointer(unsafe.SliceData(u.data)))
	p := d[x]
	r := x
	if p >= 0 {
		r = uint32(p)
	}
	s := d[r]
	for s >= 0 {
		d[x] = s
		x = r
		r = uint32(s)
		s = d[r]
	}
	return uint32(-s)
}

// Sets returns the current number of disjoint sets.
func (u *UnionFind) Sets() uint32 {
	return u.sets
}

// Len returns the number of elements in the structure.
func (u *UnionFind) Len() uint32 {
	return uint32(len(u.data))
}

// Parent returns x's parent link, or x itself if x is a root. It does not
// modify the forest.
func (u *UnionFind) Parent(x uint32) uint32 {
	p := u.data[x]
	if p < 0 {
		return x
	}
	return uint32(p)
}
