diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-21 16:08:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-21 16:08:57 +0100 |
commit | e26a605c93d8085dfff8e440838fd3c43b63cff6 (patch) | |
tree | 7022e74d0fa7a3ab542eaa2df851e7f625e8f1ff /plumbing/cache/queue.go | |
parent | 73855d0a5f617bcda1f33e730f3bc7cf8afbef6c (diff) | |
parent | 6e15f9cb0dbac68992eb242282e725784fe72b32 (diff) | |
download | go-git-e26a605c93d8085dfff8e440838fd3c43b63cff6.tar.gz |
Merge pull request #278 from ajnavarro/improvement/move-cache-to-plumbing
cache: move package to plumbing
Diffstat (limited to 'plumbing/cache/queue.go')
-rw-r--r-- | plumbing/cache/queue.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/plumbing/cache/queue.go b/plumbing/cache/queue.go new file mode 100644 index 0000000..8c6d7d3 --- /dev/null +++ b/plumbing/cache/queue.go @@ -0,0 +1,46 @@ +package cache + +import "srcd.works/go-git.v4/plumbing" + +// queue is a basic FIFO queue based on a circular list that resize as needed. +type queue struct { + elements []plumbing.Hash + size int + head int + tail int + count int +} + +// newQueue returns a queue with the specified initial size +func newQueue(size int) *queue { + return &queue{ + elements: make([]plumbing.Hash, size), + size: size, + } +} + +// Push adds a node to the queue. +func (q *queue) Push(h plumbing.Hash) { + if q.head == q.tail && q.count > 0 { + elements := make([]plumbing.Hash, len(q.elements)+q.size) + copy(elements, q.elements[q.head:]) + copy(elements[len(q.elements)-q.head:], q.elements[:q.head]) + q.head = 0 + q.tail = len(q.elements) + q.elements = elements + } + q.elements[q.tail] = h + q.tail = (q.tail + 1) % len(q.elements) + q.count++ +} + +// Pop removes and returns a Hash from the queue in first to last order. +func (q *queue) Pop() plumbing.Hash { + if q.count == 0 { + return plumbing.ZeroHash + } + node := q.elements[q.head] + q.head = (q.head + 1) % len(q.elements) + q.count-- + return node +} |