github.com/MetalBlockchain/metalgo@v1.11.9/utils/heap/set.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package heap
     5  
     6  // NewSet returns a heap without duplicates ordered by its values
     7  func NewSet[T comparable](less func(a, b T) bool) Set[T] {
     8  	return Set[T]{
     9  		set: NewMap[T, T](less),
    10  	}
    11  }
    12  
    13  type Set[T comparable] struct {
    14  	set Map[T, T]
    15  }
    16  
    17  // Push returns if the entry was added
    18  func (s Set[T]) Push(t T) bool {
    19  	_, hadValue := s.set.Push(t, t)
    20  	return !hadValue
    21  }
    22  
    23  func (s Set[T]) Pop() (T, bool) {
    24  	pop, _, ok := s.set.Pop()
    25  	return pop, ok
    26  }
    27  
    28  func (s Set[T]) Peek() (T, bool) {
    29  	peek, _, ok := s.set.Peek()
    30  	return peek, ok
    31  }
    32  
    33  func (s Set[T]) Len() int {
    34  	return s.set.Len()
    35  }
    36  
    37  func (s Set[T]) Remove(t T) bool {
    38  	_, existed := s.set.Remove(t)
    39  	return existed
    40  }
    41  
    42  func (s Set[T]) Fix(t T) {
    43  	s.set.Fix(t)
    44  }
    45  
    46  func (s Set[T]) Contains(t T) bool {
    47  	return s.set.Contains(t)
    48  }