Switched from queue to Golang slice for SongQueue

This commit is contained in:
Matthieu Grieger 2014-12-31 14:36:53 -08:00
parent cfcd7eae30
commit f6c86f8566
3 changed files with 9 additions and 125 deletions

View file

@ -90,6 +90,5 @@ THE SOFTWARE.
* [Tim Cooper](https://github.com/bontibon) for [gumble](https://github.com/layeh/gumble).
* [Ricardo Garcia](https://github.com/rg3) for [youtube-dl](https://github.com/rg3/youtube-dl).
* [ScalingData](https://github.com/scalingdata) for [gcfg](https://github.com/scalingdata/gcfg).
* [Hicham Bouabdallah](https://github.com/hishboy) for [Golang queue implementation](https://github.com/hishboy/gocommons/blob/master/lang/queue.go).
* [kennygrant](https://github.com/kennygrant) for [sanitize](https://github.com/kennygrant/sanitize).
* [Jason Moiron](https://github.com/jmoiron) for [jsonq](https://github.com/jmoiron/jsonq).

117
queue.go
View file

@ -1,117 +0,0 @@
//
// queue.go
//
// Created by Hicham Bouabdallah
// Copyright (c) 2012 SimpleRocket LLC
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
package main
import "sync"
type queuenode struct {
data interface{}
next *queuenode
}
// A go-routine safe FIFO (first in first out) data stucture.
type Queue struct {
head *queuenode
tail *queuenode
count int
lock *sync.Mutex
}
// Creates a new pointer to a new queue.
func NewQueue() *Queue {
q := &Queue{}
q.lock = &sync.Mutex{}
return q
}
// Returns the number of elements in the queue (i.e. size/length)
// go-routine safe.
func (q *Queue) Len() int {
q.lock.Lock()
defer q.lock.Unlock()
return q.count
}
// Pushes/inserts a value at the end/tail of the queue.
// Note: this function does mutate the queue.
// go-routine safe.
func (q *Queue) Push(item interface{}) {
q.lock.Lock()
defer q.lock.Unlock()
n := &queuenode{data: item}
if q.tail == nil {
q.tail = n
q.head = n
} else {
q.tail.next = n
q.tail = n
}
q.count++
}
// Returns the value at the front of the queue.
// i.e. the oldest value in the queue.
// Note: this function does mutate the queue.
// go-routine safe.
func (q *Queue) Poll() interface{} {
q.lock.Lock()
defer q.lock.Unlock()
if q.head == nil {
return nil
}
n := q.head
q.head = n.next
if q.head == nil {
q.tail = nil
}
q.count--
return n.data
}
// Returns a read value at the front of the queue.
// i.e. the oldest value in the queue.
// Note: this function does NOT mutate the queue.
// go-routine safe.
func (q *Queue) Peek() interface{} {
q.lock.Lock()
defer q.lock.Unlock()
n := q.head
if n == nil || n.data == nil {
return nil
}
return n.data
}

View file

@ -13,21 +13,21 @@ import (
// SongQueue type declaration. Serves as a wrapper around the queue structure defined in queue.go.
type SongQueue struct {
queue *Queue
queue []*Song
}
// Initializes a new queue and returns the new SongQueue.
func NewSongQueue() *SongQueue {
return &SongQueue{
queue: NewQueue(),
queue: make([]*Song, 0),
}
}
// Adds a song to the SongQueue.
func (q *SongQueue) AddSong(s *Song) error {
beforeLen := q.queue.Len()
q.queue.Push(s)
if q.queue.Len() == beforeLen+1 {
beforeLen := len(q.queue)
q.queue = append(q.queue, s)
if len(q.queue) == beforeLen+1 {
return nil
} else {
return errors.New("Could not add Song to the SongQueue.")
@ -37,10 +37,12 @@ func (q *SongQueue) AddSong(s *Song) error {
// Moves to the next song in SongQueue. NextSong() pops the first value of the queue, and is stored
// in dj.currentSong.
func (q *SongQueue) NextSong() *Song {
return q.queue.Poll().(*Song)
s, queue := q.queue[0], q.queue[1:]
q.queue = queue
return s
}
// Returns the length of the SongQueue.
func (q *SongQueue) Len() int {
return q.queue.Len()
return len(q.queue)
}