songs.go 751B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package main
  2. import (
  3. "time"
  4. )
  5. // From https://golang.org/pkg/container/heap/
  6. type Song struct {
  7. time time.Time
  8. song string
  9. week int
  10. sync bool
  11. index int
  12. }
  13. type SongPriorityQueue []*Song
  14. func (pq SongPriorityQueue) Len() int {
  15. return len(pq)
  16. }
  17. func (pq SongPriorityQueue) Less(i, j int) bool {
  18. return pq[i].time.Before(pq[j].time)
  19. }
  20. func (pq SongPriorityQueue) Swap(i, j int) {
  21. pq[i], pq[j] = pq[j], pq[i]
  22. pq[i].index = i
  23. pq[j].index = j
  24. }
  25. func (pq *SongPriorityQueue) Push(x interface{}) {
  26. n := len(*pq)
  27. item := x.(*Song)
  28. item.index = n
  29. *pq = append(*pq, item)
  30. }
  31. func (pq *SongPriorityQueue) Pop() interface{} {
  32. old := *pq
  33. n := len(old)
  34. item := old[n-1]
  35. item.index = -1 // for safety
  36. *pq = old[0 : n-1]
  37. return item
  38. }