mirror of
https://github.com/cubixle/playground.git
synced 2026-04-24 22:44:46 +01:00
sorted queue
This commit is contained in:
69
sorted_queue/main.go
Normal file
69
sorted_queue/main.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package queue
|
||||
|
||||
import "sync"
|
||||
|
||||
type Item struct {
|
||||
ID string
|
||||
Data string
|
||||
Priority int
|
||||
}
|
||||
|
||||
type queue struct {
|
||||
m map[string]struct{}
|
||||
queue []*Item
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
func NewQueue() *queue {
|
||||
return &queue{
|
||||
m: map[string]struct{}{},
|
||||
mu: &sync.Mutex{},
|
||||
queue: []*Item{},
|
||||
}
|
||||
}
|
||||
|
||||
// put on the queue with no duplicates
|
||||
func (q *queue) Put(r *Item) bool {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
_, ok := q.m[r.ID]
|
||||
if ok {
|
||||
return false
|
||||
}
|
||||
|
||||
q.m[r.ID] = struct{}{}
|
||||
|
||||
q.queue = append(q.queue, r)
|
||||
|
||||
if len(q.queue) > 0 {
|
||||
// sort the queue
|
||||
for i := len(q.queue); i > 0; i-- {
|
||||
if q.queue[i].Priority > q.queue[i-1].Priority {
|
||||
q.queue[i] = q.queue[i-1]
|
||||
q.queue[i-1] = q.queue[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (q *queue) GetNext() *Item {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
if len(q.m) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
r := q.queue[0]
|
||||
|
||||
// delete the id from the map
|
||||
delete(q.m, r.ID)
|
||||
|
||||
// delete from the front of the list
|
||||
q.queue = q.queue[1:]
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user