| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 5 |
// Use of this source code is governed by a BSD-style |
| 6 |
// license that can be found in the LICENSE file. |
| 7 |
|
| 8 |
package log // import "go.opentelemetry.io/otel/sdk/log" |
| 9 |
|
| 10 |
// A ring is an element of a circular list, or ring. Rings do not have a |
| 11 |
// beginning or end; a pointer to any ring element serves as reference to the |
| 12 |
// entire ring. Empty rings are represented as nil ring pointers. The zero |
| 13 |
// value for a ring is a one-element ring with a nil Value. |
| 14 |
// |
| 15 |
// This is copied from the "container/ring" package. It uses a Record type for |
| 16 |
// Value instead of any to avoid allocations. |
| 17 |
type ring struct { |
| 18 |
next, prev *ring |
| 19 |
Value Record |
| 20 |
} |
| 21 |
|
| 22 |
func (r *ring) init() *ring { |
| 23 |
r.next = r |
| 24 |
r.prev = r |
| 25 |
return r |
| 26 |
} |
| 27 |
|
| 28 |
// Next returns the next ring element. r must not be empty. |
| 29 |
func (r *ring) Next() *ring { |
| 30 |
if r.next == nil { |
| 31 |
return r.init() |
| 32 |
} |
| 33 |
return r.next |
| 34 |
} |
| 35 |
|
| 36 |
// Prev returns the previous ring element. r must not be empty. |
| 37 |
func (r *ring) Prev() *ring { |
| 38 |
if r.next == nil { |
| 39 |
return r.init() |
| 40 |
} |
| 41 |
return r.prev |
| 42 |
} |
| 43 |
|
| 44 |
// newRing creates a ring of n elements. |
| 45 |
func newRing(n int) *ring { |
| 46 |
if n <= 0 { |
| 47 |
return nil |
| 48 |
} |
| 49 |
r := new(ring) |
| 50 |
p := r |
| 51 |
for i := 1; i < n; i++ { |
| 52 |
p.next = &ring{prev: p} |
| 53 |
p = p.next |
| 54 |
} |
| 55 |
p.next = r |
| 56 |
r.prev = p |
| 57 |
return r |
| 58 |
} |
| 59 |
|
| 60 |
// Len computes the number of elements in ring r. It executes in time |
| 61 |
// proportional to the number of elements. |
| 62 |
func (r *ring) Len() int { |
| 63 |
n := 0 |
| 64 |
if r != nil { |
| 65 |
n = 1 |
| 66 |
for p := r.Next(); p != r; p = p.next { |
| 67 |
n++ |
| 68 |
} |
| 69 |
} |
| 70 |
return n |
| 71 |
} |
| 72 |
|
| 73 |
// Do calls function f on each element of the ring, in forward order. The |
| 74 |
// behavior of Do is undefined if f changes *r. |
| 75 |
func (r *ring) Do(f func(Record)) { |
| 76 |
if r != nil { |
| 77 |
f(r.Value) |
| 78 |
for p := r.Next(); p != r; p = p.next { |
| 79 |
f(p.Value) |
| 80 |
} |
| 81 |
} |
| 82 |
} |