Skip to content

Commit c6c3987

Browse files
committed
ringidx minor cleanup
1 parent c0ffdab commit c6c3987

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

ringidx/ringidx.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,37 @@
55
/*
66
Package ringidx provides circular indexing logic for writing a given
77
length of data into a fixed-sized buffer and wrapping around this
8-
buffer, overwriting the oldest data. No copying is required so
8+
buffer, overwriting the oldest data. No copying is required so
99
it is highly efficient
1010
*/
1111
package ringidx
1212

1313
//go:generate core generate -add-types
1414

15-
// Index is the ring index structure, maintaining starting index and length
16-
// into a ring-buffer with maximum length Max. Max must be > 0 and Len <= Max.
15+
// Index is the ring index structure for a dynamically-sized ring buffer,
16+
// maintaining starting index and length into a ring-buffer with maximum
17+
// length Max. Max must be > 0 and Len <= Max.
1718
// When adding new items would overflow Max, starting index is shifted over
18-
// to overwrite the oldest items with the new ones. No moving is ever
19-
// required -- just a fixed-length buffer of size Max.
19+
// to overwrite the oldest items with the new ones. No moving is ever
20+
// required: just a fixed-length buffer of size Max.
2021
type Index struct {
2122

22-
// the starting index where current data starts -- the oldest data is at this index, and continues for Len items, wrapping around at Max, coming back up at most to StIndex-1
23-
StIndex int
23+
// Start the starting index where current data starts.
24+
// The oldest data is at this index, and continues for Len items,
25+
// wrapping around at Max, coming back up at most to Start-1.
26+
Start int
2427

25-
// the number of items stored starting at StIndex. Capped at Max
28+
// Len is the number of items stored starting at Start. Capped at Max.
2629
Len int
2730

28-
// the maximum number of items that can be stored in this ring
31+
// Max is the maximum number of items that can be stored in this ring.
2932
Max int
3033
}
3134

32-
// Index returns the index of the i'th item starting from StIndex.
35+
// Index returns the index of the i'th item starting from Start.
3336
// i must be < Len.
3437
func (ri *Index) Index(i int) int {
35-
i += ri.StIndex
38+
i += ri.Start
3639
if i >= ri.Max {
3740
i -= ri.Max
3841
}
@@ -63,12 +66,12 @@ func (ri *Index) Add(n int) {
6366
// Shift moves the starting index up by n, and decrements the Len by n as well.
6467
// This is called prior to adding new items if doing so would exceed Max length.
6568
func (ri *Index) Shift(n int) {
66-
ri.StIndex = ri.Index(n)
69+
ri.Start = ri.Index(n)
6770
ri.Len -= n
6871
}
6972

7073
// Reset initializes start index and length to 0
7174
func (ri *Index) Reset() {
72-
ri.StIndex = 0
75+
ri.Start = 0
7376
ri.Len = 0
7477
}

0 commit comments

Comments
 (0)