| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package buffer |
| 18 |
|
| 19 |
// defaultRingSize defines the default ring size if not specified |
| 20 |
const defaultRingSize = 16 |
| 21 |
|
| 22 |
// RingGrowingOptions sets parameters for [RingGrowing] and |
| 23 |
// [TypedRingGrowing]. |
| 24 |
type RingGrowingOptions struct { |
| 25 |
// InitialSize is the number of pre-allocated elements in the |
| 26 |
// initial underlying storage buffer. |
| 27 |
InitialSize int |
| 28 |
} |
| 29 |
|
| 30 |
// RingGrowing is a growing ring buffer. |
| 31 |
// Not thread safe. |
| 32 |
// |
| 33 |
// Deprecated: Use TypedRingGrowing[any] instead. |
| 34 |
type RingGrowing = TypedRingGrowing[any] |
| 35 |
|
| 36 |
// NewRingGrowing constructs a new RingGrowing instance with provided parameters. |
| 37 |
// |
| 38 |
// Deprecated: Use NewTypedRingGrowing[any] instead. |
| 39 |
func NewRingGrowing(initialSize int) *RingGrowing { |
| 40 |
return NewTypedRingGrowing[any](RingGrowingOptions{InitialSize: initialSize}) |
| 41 |
} |
| 42 |
|
| 43 |
// TypedRingGrowing is a growing ring buffer. |
| 44 |
// The zero value has an initial size of 0 and is ready to use. |
| 45 |
// Not thread safe. |
| 46 |
type TypedRingGrowing[T any] struct { |
| 47 |
data []T |
| 48 |
n int // Size of Data |
| 49 |
beg int // First available element |
| 50 |
readable int // Number of data items available |
| 51 |
} |
| 52 |
|
| 53 |
// NewTypedRingGrowing constructs a new TypedRingGrowing instance with provided parameters. |
| 54 |
func NewTypedRingGrowing[T any](opts RingGrowingOptions) *TypedRingGrowing[T] { |
| 55 |
return &TypedRingGrowing[T]{ |
| 56 |
data: make([]T, opts.InitialSize), |
| 57 |
n: opts.InitialSize, |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
// ReadOne reads (consumes) first item from the buffer if it is available, otherwise returns false. |
| 62 |
func (r *TypedRingGrowing[T]) ReadOne() (data T, ok bool) { |
| 63 |
if r.readable == 0 { |
| 64 |
return |
| 65 |
} |
| 66 |
r.readable-- |
| 67 |
element := r.data[r.beg] |
| 68 |
var zero T |
| 69 |
r.data[r.beg] = zero // Remove reference to the object to help GC |
| 70 |
if r.beg == r.n-1 { |
| 71 |
// Was the last element |
| 72 |
r.beg = 0 |
| 73 |
} else { |
| 74 |
r.beg++ |
| 75 |
} |
| 76 |
return element, true |
| 77 |
} |
| 78 |
|
| 79 |
// WriteOne adds an item to the end of the buffer, growing it if it is full. |
| 80 |
func (r *TypedRingGrowing[T]) WriteOne(data T) { |
| 81 |
if r.readable == r.n { |
| 82 |
// Time to grow |
| 83 |
newN := r.n * 2 |
| 84 |
if newN == 0 { |
| 85 |
newN = defaultRingSize |
| 86 |
} |
| 87 |
newData := make([]T, newN) |
| 88 |
to := r.beg + r.readable |
| 89 |
if to <= r.n { |
| 90 |
copy(newData, r.data[r.beg:to]) |
| 91 |
} else { |
| 92 |
copied := copy(newData, r.data[r.beg:]) |
| 93 |
copy(newData[copied:], r.data[:(to%r.n)]) |
| 94 |
} |
| 95 |
r.beg = 0 |
| 96 |
r.data = newData |
| 97 |
r.n = newN |
| 98 |
} |
| 99 |
r.data[(r.readable+r.beg)%r.n] = data |
| 100 |
r.readable++ |
| 101 |
} |
| 102 |
|
| 103 |
// Len returns the number of items in the buffer. |
| 104 |
func (r *TypedRingGrowing[T]) Len() int { |
| 105 |
return r.readable |
| 106 |
} |
| 107 |
|
| 108 |
// Cap returns the capacity of the buffer. |
| 109 |
func (r *TypedRingGrowing[T]) Cap() int { |
| 110 |
return r.n |
| 111 |
} |
| 112 |
|
| 113 |
// RingOptions sets parameters for [Ring]. |
| 114 |
type RingOptions struct { |
| 115 |
// InitialSize is the number of pre-allocated elements in the |
| 116 |
// initial underlying storage buffer. |
| 117 |
InitialSize int |
| 118 |
// NormalSize is the number of elements to allocate for new storage |
| 119 |
// buffers once the Ring is consumed and |
| 120 |
// can shrink again. |
| 121 |
NormalSize int |
| 122 |
} |
| 123 |
|
| 124 |
// Ring is a dynamically-sized ring buffer which can grow and shrink as-needed. |
| 125 |
// The zero value has an initial size and normal size of 0 and is ready to use. |
| 126 |
// Not thread safe. |
| 127 |
type Ring[T any] struct { |
| 128 |
growing TypedRingGrowing[T] |
| 129 |
normalSize int // Limits the size of the buffer that is kept for reuse. Read-only. |
| 130 |
} |
| 131 |
|
| 132 |
// NewRing constructs a new Ring instance with provided parameters. |
| 133 |
func NewRing[T any](opts RingOptions) *Ring[T] { |
| 134 |
return &Ring[T]{ |
| 135 |
growing: *NewTypedRingGrowing[T](RingGrowingOptions{InitialSize: opts.InitialSize}), |
| 136 |
normalSize: opts.NormalSize, |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// ReadOne reads (consumes) first item from the buffer if it is available, |
| 141 |
// otherwise returns false. When the buffer has been totally consumed and has |
| 142 |
// grown in size beyond its normal size, it shrinks down to its normal size again. |
| 143 |
func (r *Ring[T]) ReadOne() (data T, ok bool) { |
| 144 |
element, ok := r.growing.ReadOne() |
| 145 |
|
| 146 |
if r.growing.readable == 0 && r.growing.n > r.normalSize { |
| 147 |
// The buffer is empty. Reallocate a new buffer so the old one can be |
| 148 |
// garbage collected. |
| 149 |
r.growing.data = make([]T, r.normalSize) |
| 150 |
r.growing.n = r.normalSize |
| 151 |
r.growing.beg = 0 |
| 152 |
} |
| 153 |
|
| 154 |
return element, ok |
| 155 |
} |
| 156 |
|
| 157 |
// WriteOne adds an item to the end of the buffer, growing it if it is full. |
| 158 |
func (r *Ring[T]) WriteOne(data T) { |
| 159 |
r.growing.WriteOne(data) |
| 160 |
} |
| 161 |
|
| 162 |
// Len returns the number of items in the buffer. |
| 163 |
func (r *Ring[T]) Len() int { |
| 164 |
return r.growing.Len() |
| 165 |
} |
| 166 |
|
| 167 |
// Cap returns the capacity of the buffer. |
| 168 |
func (r *Ring[T]) Cap() int { |
| 169 |
return r.growing.Cap() |
| 170 |
} |