package dlist

import "unsafe"

// dnode is a single 16-byte, pointer-free list node.
// Eight nodes pack cleanly into each 128-byte cache line with no line splits,
// and being pointer-free (noscan) eliminates GC scanning and write barriers.
type dnode struct {
	next uint32
	prev uint32
	val  int64
}

// Compile-time check that unsafe.Sizeof(dnode{}) == 16 (shift by 4).
const (
	_ = uint(16 - unsafe.Sizeof(dnode{}))
	_ = uint(unsafe.Sizeof(dnode{}) - 16)
)

const initialCap = 64

// DList is an arena-backed doubly linked list using a circular sentinel at
// index 0: nodes[0].next is the front handle, nodes[0].prev is the back handle,
// and 0 marks both ends of the list so linking and unlinking are branchless.
type DList struct {
	nodes  []dnode
	used   uint32
	free   uint32
	length int
}

// New creates an empty doubly linked list.
func New() *DList {
	return &DList{
		nodes: make([]dnode, initialCap),
		used:  1,
	}
}

// PushFront inserts value as the new first element and returns its handle.
func (l *DList) PushFront(value int64) uint64 {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	idx := l.free
	if idx != 0 {
		l.free = (*dnode)(unsafe.Add(base, uintptr(idx)<<4)).next
	} else {
		idx = l.used
		cap := len(l.nodes)
		if int(idx) == cap {
			n := make([]dnode, cap*2)
			copy(n, l.nodes)
			l.nodes = n
			base = unsafe.Pointer(unsafe.SliceData(n))
		}
		l.used = idx + 1
	}
	s := (*dnode)(base)
	head := s.next
	*(*dnode)(unsafe.Add(base, uintptr(idx)<<4)) = dnode{head, 0, value}
	(*dnode)(unsafe.Add(base, uintptr(head)<<4)).prev = idx
	s.next = idx
	l.length++
	return uint64(idx)
}

// PushBack inserts value as the new last element and returns its handle.
func (l *DList) PushBack(value int64) uint64 {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	idx := l.free
	if idx != 0 {
		l.free = (*dnode)(unsafe.Add(base, uintptr(idx)<<4)).next
	} else {
		idx = l.used
		cap := len(l.nodes)
		if int(idx) == cap {
			n := make([]dnode, cap*2)
			copy(n, l.nodes)
			l.nodes = n
			base = unsafe.Pointer(unsafe.SliceData(n))
		}
		l.used = idx + 1
	}
	s := (*dnode)(base)
	tail := s.prev
	*(*dnode)(unsafe.Add(base, uintptr(idx)<<4)) = dnode{0, tail, value}
	(*dnode)(unsafe.Add(base, uintptr(tail)<<4)).next = idx
	s.prev = idx
	l.length++
	return uint64(idx)
}

// InsertBefore inserts value immediately before node and returns its handle.
func (l *DList) InsertBefore(node uint64, value int64) uint64 {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	idx := l.free
	if idx != 0 {
		l.free = (*dnode)(unsafe.Add(base, uintptr(idx)<<4)).next
	} else {
		idx = l.used
		cap := len(l.nodes)
		if int(idx) == cap {
			n := make([]dnode, cap*2)
			copy(n, l.nodes)
			l.nodes = n
			base = unsafe.Pointer(unsafe.SliceData(n))
		}
		l.used = idx + 1
	}
	p := (*dnode)(unsafe.Add(base, uintptr(node)<<4))
	prev := p.prev
	*(*dnode)(unsafe.Add(base, uintptr(idx)<<4)) = dnode{uint32(node), prev, value}
	(*dnode)(unsafe.Add(base, uintptr(prev)<<4)).next = idx
	p.prev = idx
	l.length++
	return uint64(idx)
}

// InsertAfter inserts value immediately after node and returns its handle.
func (l *DList) InsertAfter(node uint64, value int64) uint64 {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	idx := l.free
	if idx != 0 {
		l.free = (*dnode)(unsafe.Add(base, uintptr(idx)<<4)).next
	} else {
		idx = l.used
		cap := len(l.nodes)
		if int(idx) == cap {
			n := make([]dnode, cap*2)
			copy(n, l.nodes)
			l.nodes = n
			base = unsafe.Pointer(unsafe.SliceData(n))
		}
		l.used = idx + 1
	}
	p := (*dnode)(unsafe.Add(base, uintptr(node)<<4))
	next := p.next
	*(*dnode)(unsafe.Add(base, uintptr(idx)<<4)) = dnode{next, uint32(node), value}
	(*dnode)(unsafe.Add(base, uintptr(next)<<4)).prev = idx
	p.next = idx
	l.length++
	return uint64(idx)
}

// Remove unlinks node, pushes its slot onto the free list, and returns its value.
func (l *DList) Remove(node uint64) int64 {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	p := (*dnode)(unsafe.Add(base, uintptr(node)<<4))
	prev := p.prev
	next := p.next
	val := p.val
	(*dnode)(unsafe.Add(base, uintptr(prev)<<4)).next = next
	(*dnode)(unsafe.Add(base, uintptr(next)<<4)).prev = prev
	p.next = l.free
	l.free = uint32(node)
	l.length--
	return val
}

// PopFront removes and returns the first element, or (0, false) if empty.
func (l *DList) PopFront() (value int64, ok bool) {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	s := (*dnode)(base)
	head := s.next
	if head == 0 {
		return 0, false
	}
	p := (*dnode)(unsafe.Add(base, uintptr(head)<<4))
	next := p.next
	val := p.val
	s.next = next
	(*dnode)(unsafe.Add(base, uintptr(next)<<4)).prev = 0
	p.next = l.free
	l.free = head
	l.length--
	return val, true
}

// PopBack removes and returns the last element, or (0, false) if empty.
func (l *DList) PopBack() (value int64, ok bool) {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	s := (*dnode)(base)
	tail := s.prev
	if tail == 0 {
		return 0, false
	}
	p := (*dnode)(unsafe.Add(base, uintptr(tail)<<4))
	prev := p.prev
	val := p.val
	s.prev = prev
	(*dnode)(unsafe.Add(base, uintptr(prev)<<4)).next = 0
	p.next = l.free
	l.free = tail
	l.length--
	return val, true
}

// MoveToFront moves node to the front of the list while preserving its handle and value.
func (l *DList) MoveToFront(node uint64) {
	base := unsafe.Pointer(unsafe.SliceData(l.nodes))
	p := (*dnode)(unsafe.Add(base, uintptr(node)<<4))
	prev := p.prev
	if prev == 0 {
		return
	}
	next := p.next
	(*dnode)(unsafe.Add(base, uintptr(prev)<<4)).next = next
	(*dnode)(unsafe.Add(base, uintptr(next)<<4)).prev = prev
	s := (*dnode)(base)
	head := s.next
	id := uint32(node)
	p.prev = 0
	p.next = head
	(*dnode)(unsafe.Add(base, uintptr(head)<<4)).prev = id
	s.next = id
}

// Front returns the handle of the first node, or 0 if the list is empty.
func (l *DList) Front() uint64 {
	return uint64((*dnode)(unsafe.Pointer(unsafe.SliceData(l.nodes))).next)
}

// Back returns the handle of the last node, or 0 if the list is empty.
func (l *DList) Back() uint64 {
	return uint64((*dnode)(unsafe.Pointer(unsafe.SliceData(l.nodes))).prev)
}

// Next returns the handle of the node after node, or 0 if node is the last.
func (l *DList) Next(node uint64) uint64 {
	return uint64((*dnode)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(l.nodes)), uintptr(node)<<4)).next)
}

// Prev returns the handle of the node before node, or 0 if node is the first.
func (l *DList) Prev(node uint64) uint64 {
	return uint64((*dnode)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(l.nodes)), uintptr(node)<<4)).prev)
}

// Get returns the value stored in node.
func (l *DList) Get(node uint64) int64 {
	return (*dnode)(unsafe.Add(unsafe.Pointer(unsafe.SliceData(l.nodes)), uintptr(node)<<4)).val
}

// Len returns the number of elements currently in the list.
func (l *DList) Len() int {
	return l.length
}
