package plist

import (
	"github.com/emirpasic/gods/trees/avltree"
	"github.com/emirpasic/gods/utils"
)

// PriorityQueue 优先队列 适合数据量不大, 加索引
type PriorityQueue struct {
	avl        *avltree.Tree
	size       int
	comparator utils.Comparator
	splitlimit int
}

func NewWithIntComparator() *PriorityQueue {
	pq := new(PriorityQueue)
	pq.comparator = func(v1, v2 interface{}) int {
		if v1.(int) > v2.(int) {
			return 1
		} else if v1.(int) < v2.(int) {
			return -1
		}
		return 0
	}
	pq.avl = avltree.NewWith(pq.comparator)
	pq.size = 0
	return pq
}

func (pq *PriorityQueue) Push(value interface{}) {
	var pl *PriorityList
	pq.size++

	floor, ok := pq.avl.Floor(value)
	if ok {

		pl = floor.Value.(*PriorityList)
		cur := pl.head
		pl.size++

		if pq.comparator(value, cur.value) > 0 {

			temp := pl.head
			pl.head = &Node{value: value}
			pl.head.next = temp

			return
		}

		for cur.next != nil {
			if pq.comparator(value, cur.next.value) >= 0 {
				temp := cur.next
				cur.next = &Node{value: value}
				cur.next.next = temp
				return
			}
			cur = cur.next
		}
		// next == nil
		cur.next = &Node{value: value}
		return

	}

	pl = &PriorityList{}
	pl.head = &Node{value: value}
	pl.size++

	pq.avl.Put(pl.head.value, pl)

	return

}
