diff options
author | Wim <wim@42.be> | 2021-04-03 19:16:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-03 19:16:46 +0200 |
commit | 21eb37e471c338a90f2e23c86106f7e49e2d1196 (patch) | |
tree | e7d1cfa89f31fcf0578edae7727f2230bba744a2 /vendor/github.com/zfjagann | |
parent | d3b60cc445e5871971b543fde9483dba3924bf68 (diff) | |
download | matterbridge-msglm-21eb37e471c338a90f2e23c86106f7e49e2d1196.tar.gz matterbridge-msglm-21eb37e471c338a90f2e23c86106f7e49e2d1196.tar.bz2 matterbridge-msglm-21eb37e471c338a90f2e23c86106f7e49e2d1196.zip |
Update vendor (#1446)
* Update vendor
* Use upstream emoji lib again
Diffstat (limited to 'vendor/github.com/zfjagann')
-rw-r--r-- | vendor/github.com/zfjagann/golang-ring/README.md | 17 | ||||
-rw-r--r-- | vendor/github.com/zfjagann/golang-ring/ring.go | 53 |
2 files changed, 60 insertions, 10 deletions
diff --git a/vendor/github.com/zfjagann/golang-ring/README.md b/vendor/github.com/zfjagann/golang-ring/README.md index 1da6cf73..56988e1b 100644 --- a/vendor/github.com/zfjagann/golang-ring/README.md +++ b/vendor/github.com/zfjagann/golang-ring/README.md @@ -1,9 +1,6 @@ # ring - -[![GoDoc](https://godoc.org/github.com/zfjagann/golang-ring?status.svg)](https://godoc.org/github.com/zfjagann/golang-ring) - -- - import "github.com/zfjagann/golang-ring" + import "github.com/zealws/golang-ring" Package ring provides a simple implementation of a ring buffer. @@ -20,19 +17,27 @@ Changing this value only affects ring buffers created after it is changed. ```go type Ring struct { + sync.Mutex } ``` Type Ring implements a Circular Buffer. The default value of the Ring struct is a valid (empty) Ring buffer with capacity DefaultCapacify. -#### func (Ring) Capacity +#### func (*Ring) Capacity ```go -func (r Ring) Capacity() int +func (r *Ring) Capacity() int ``` Capacity returns the current capacity of the ring buffer. +#### func (*Ring) ContentSize + +```go +func (r *Ring) ContentSize() int +``` +ContentSize returns the current number of elements inside the ring buffer. + #### func (*Ring) Dequeue ```go diff --git a/vendor/github.com/zfjagann/golang-ring/ring.go b/vendor/github.com/zfjagann/golang-ring/ring.go index 345ee8cd..308c97cc 100644 --- a/vendor/github.com/zfjagann/golang-ring/ring.go +++ b/vendor/github.com/zfjagann/golang-ring/ring.go @@ -160,7 +160,11 @@ func (r *Ring) get(p int) interface{} { // returns the modified index of an unmodified index func (r *Ring) mod(p int) int { - return p % len(r.buff) + v := p % len(r.buff) + for v < 0 { // this bit fixes negative indices + v += len(r.buff) + } + return v } func (r *Ring) checkInit() { @@ -178,12 +182,53 @@ func (r *Ring) checkInit() { func (r *Ring) extend(size int) { if size == len(r.buff) { return - } else if size < len(r.buff) { - r.buff = r.buff[0:size] } + + if size < len(r.buff) { + // shrink the buffer + if r.head == -1 { + // nothing in the buffer, so just shrink it directly + r.buff = r.buff[0:size] + } else { + newb := make([]interface{}, 0, size) + // buffer has stuff in it, so save the most recent stuff... + // start at HEAD-SIZE-1 and walk forwards + for i := size - 1; i >= 0; i-- { + idx := r.mod(r.head - i) + newb = append(newb, r.buff[idx]) + } + // reset head and tail to proper values + r.head = len(newb) - 1 + r.tail = 0 + r.buff = newb + } + return + } + + // grow the buffer newb := make([]interface{}, size-len(r.buff)) for i := range newb { newb[i] = nil } - r.buff = append(r.buff, newb...) + if r.head == -1 { + // nothing in the buffer + r.buff = append(r.buff, newb...) + } else if r.head >= r.tail { + // growing at the end is safe + r.buff = append(r.buff, newb...) + } else { + // buffer has stuff that wraps around the end + // have to rearrange the buffer so the contents are still in order + part1 := make([]interface{}, len(r.buff[:r.head+1])) + copy(part1, r.buff[:r.head+1]) + part2 := make([]interface{}, len(r.buff[r.tail:])) + copy(part2, r.buff[r.tail:]) + r.buff = append(r.buff, newb...) + newTail := r.mod(r.tail + len(newb)) + r.tail = newTail + copy(r.buff[:r.head+1], part1) + copy(r.buff[r.head+1:r.tail], newb) + copy(r.buff[r.tail:], part2) + + } } |