package main import "sync" type queue struct { buf []interface{} length int mux *sync.RWMutex } func newQueue() *queue { return &queue{make([]interface{}, 0), 0, &sync.RWMutex{}} } func (q *queue) rPush(e interface{}) { q.mux.Lock() defer q.mux.Unlock() q.buf = append(q.buf, e) q.length += 1 } func (q *queue) rPushs(elements ...interface{}) { q.mux.Lock() defer q.mux.Unlock() q.buf = append(q.buf, elements...) q.length += len(elements) } func (q *queue) lPush(e interface{}) { q.mux.Lock() defer q.mux.Unlock() q.buf = append([]interface{}{e}, q.buf...) q.length += 1 } func (q *queue) rPop() interface{} { q.mux.Lock() if q.length == 0 { return nil } n := q.buf[q.length-1] q.buf = q.buf[:q.length-1] q.length -= 1 q.mux.Unlock() return n } func (q *queue) lPop() interface{} { q.mux.Lock() if q.length == 0 { return nil } n := q.buf[0] q.buf = q.buf[1:] q.length -= 1 q.mux.Unlock() return n } func (q *queue) lGet() interface{} { q.mux.RLock() defer q.mux.RUnlock() if q.length == 0 { return nil } return q.buf[0] } func (q *queue) rGet() interface{} { q.mux.RLock() defer q.mux.RUnlock() if q.length == 0 { return nil } return q.buf[q.length-1] } func (q *queue) getLen() int { q.mux.RLock() defer q.mux.RUnlock() return q.length }