golang中用slice实现queue

实现一

最直观的实现,用一个slice。

type Queue struct {
  s []*valueType
}

func (q *Queue) pushBack(v *valueType) {
  s = append(s, v)
}

func (q *Queue) popFront() *valueType {
  if len(s) == 0 {
    return nil
  }
  
  v := s[0]
  s[0] = nil
  s = s[1:]
  return v
}

这有个问题,Queue中的slice所占的内存并不会随着popFront操作而释放,已经出队的元素所占的内存仍在slice底层的数组中保留,直到slice扩容才会释放原来的内存,这样导致频繁的申请和释放内存。

实现二

用两个slice构造一个queue,在go net.http.transport包中有个wantConnQueue的实现,看注释是参考了Okasaki's purely functional queue.

// A wantConnQueue is a queue of wantConns.
type wantConnQueue struct {
    // This is a queue, not a deque.
    // It is split into two stages - head[headPos:] and tail.
    // popFront is trivial (headPos++) on the first stage, and
    // pushBack is trivial (append) on the second stage.
    // If the first stage is empty, popFront can swap the
    // first and second stages to remedy the situation.
    //
    // This two-stage split is analogous to the use of two lists
    // in Okasaki's purely functional queue but without the
    // overhead of reversing the list when swapping stages.
    head    []*wantConn
    headPos int
    tail    []*wantConn
}

// len returns the number of items in the queue.
func (q *wantConnQueue) len() int {
    return len(q.head) - q.headPos + len(q.tail)
}

// pushBack adds w to the back of the queue.
func (q *wantConnQueue) pushBack(w *wantConn) {
    q.tail = append(q.tail, w)
}

// popFront removes and returns the wantConn at the front of the queue.
func (q *wantConnQueue) popFront() *wantConn {
    if q.headPos >= len(q.head) {
        if len(q.tail) == 0 {
            return nil
        }
        // Pick up tail as new head, clear tail.
        q.head, q.headPos, q.tail = q.tail, 0, q.head[:0]
    }
    w := q.head[q.headPos]
    q.head[q.headPos] = nil
    q.headPos++
    return w
}

// peekFront returns the wantConn at the front of the queue without removing it.
func (q *wantConnQueue) peekFront() *wantConn {
    if q.headPos < len(q.head) {
        return q.head[q.headPos]
    }
    if len(q.tail) > 0 {
        return q.tail[0]
    }
    return nil
}

// cleanFront pops any wantConns that are no longer waiting from the head of the
// queue, reporting whether any were popped.
func (q *wantConnQueue) cleanFront() (cleaned bool) {
    for {
        w := q.peekFront()
        if w == nil || w.waiting() {
            return cleaned
        }
        q.popFront()
        cleaned = true
    }
}

入队的时候,append到tail中。出队的时候,从head[headPos]中取第一个元素,如果head空了,就交换head和tail。这样head slice为空的时候,与tail slice交换,底层的数组空间可以重用,从而节省内存空间。

reference

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,144评论 1 32
  • 今天开始分析YYCache 包含的文件类 YYCache YYMemoryCache YYDiskCache YY...
    充满活力的早晨阅读 815评论 4 1
  • 一些概念 数据结构就是研究数据的逻辑结构和物理结构以及它们之间相互关系,并对这种结构定义相应的运算,而且确保经过这...
    Winterfell_Z阅读 5,994评论 0 13
  • ArrayDeque Deque接口的大小可变数组的实现。 特性: 底层实现时循环数组 没有容量限制,在数组元素装...
    navyd阅读 1,687评论 0 2
  • 低树讵胜叶,轻香增自通。发萼初攒此,余采尚霏红。 新花对白日,故蕊逐行风。参差不俱曜,谁肯盼微丛? ――〖咏蔷薇〗...
    郑灵悦阅读 246评论 0 2