package heap import ( "bytes" "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama" ) type MinPairingHeap struct { head *phNode size int } type phNode struct { val *pilorama.NodeInfo key []byte child *phNode sibling *phNode } func NewPairing() *MinPairingHeap { return &MinPairingHeap{} } func (m *MinPairingHeap) Insert(infos ...pilorama.NodeInfo) { for i := range infos { tmp := &phNode{key: infos[i].Meta.GetAttr(pilorama.AttributeFilename), val: &infos[i]} m.head = meld(tmp, m.head) m.size++ } } func (m *MinPairingHeap) IsEmpty() bool { return m.head == nil } func (m *MinPairingHeap) ExtractMin() pilorama.NodeInfo { val := m.head.val m.head = mergePairs(m.head.child) m.size-- return *val } func meld(m1, m2 *phNode) *phNode { if m1 == nil { return m2 } if m2 == nil { return m1 } if bytes.Compare(m1.key, m2.key) == -1 { m1.child, m2.sibling = m2, m1.child return m1 } m2.child, m1.sibling = m1, m2.child return m2 } func mergePairs(n *phNode) *phNode { if n == nil { return nil } if n.sibling == nil { return n } tmp := n.sibling.sibling return meld(meld(n, n.sibling), mergePairs(tmp)) }