123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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
- }
|