github.com/benz9527/toy-box/algo@v0.0.0-20240221120937-66c0c6bd5abd/queue/queue_intf.go (about)

     1  package queue
     2  
     3  import "context"
     4  
     5  // Reference:
     6  // https://github.com/nsqio/nsq/blob/master/internal/pqueue/pqueue.go
     7  
     8  // Here I defined the interfaces for priority queue and delay queue.
     9  // In my plan, I will implement the priority queue and delay queue with array and heap.
    10  
    11  type LessThan[E comparable] func(i, j PQItem[E]) bool
    12  
    13  type PQItem[E comparable] interface {
    14  	GetPriority() int64
    15  	SetPriority(priority int64)
    16  	GetLessThan() LessThan[E]
    17  	SetLessThan(comparator LessThan[E])
    18  	GetIndex() int64
    19  	SetIndex(index int64)
    20  	GetValue() E
    21  }
    22  
    23  type PriorityQueue[E comparable] interface {
    24  	Len() int64
    25  	Push(item PQItem[E])
    26  	Pop() PQItem[E]
    27  	Peek() PQItem[E]
    28  }
    29  
    30  type DQItem[E comparable] interface {
    31  	GetExpiration() int64
    32  	GetPQItem() PQItem[E]
    33  }
    34  
    35  type DelayQueue[E comparable] interface {
    36  	Offer(item E, expiration int64) error
    37  	Poll(ctx context.Context, nowFn func() int64) (<-chan E, error)          // Async function
    38  	PollToChannel(ctx context.Context, nowFn func() int64, C chan<- E) error // Async function
    39  }