|
5 | 5 | /* |
6 | 6 | Package ringidx provides circular indexing logic for writing a given |
7 | 7 | 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 |
9 | 9 | it is highly efficient |
10 | 10 | */ |
11 | 11 | package ringidx |
12 | 12 |
|
13 | 13 | //go:generate core generate -add-types |
14 | 14 |
|
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. |
17 | 18 | // 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. |
20 | 21 | type Index struct { |
21 | 22 |
|
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 |
24 | 27 |
|
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. |
26 | 29 | Len int |
27 | 30 |
|
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. |
29 | 32 | Max int |
30 | 33 | } |
31 | 34 |
|
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. |
33 | 36 | // i must be < Len. |
34 | 37 | func (ri *Index) Index(i int) int { |
35 | | - i += ri.StIndex |
| 38 | + i += ri.Start |
36 | 39 | if i >= ri.Max { |
37 | 40 | i -= ri.Max |
38 | 41 | } |
@@ -63,12 +66,12 @@ func (ri *Index) Add(n int) { |
63 | 66 | // Shift moves the starting index up by n, and decrements the Len by n as well. |
64 | 67 | // This is called prior to adding new items if doing so would exceed Max length. |
65 | 68 | func (ri *Index) Shift(n int) { |
66 | | - ri.StIndex = ri.Index(n) |
| 69 | + ri.Start = ri.Index(n) |
67 | 70 | ri.Len -= n |
68 | 71 | } |
69 | 72 |
|
70 | 73 | // Reset initializes start index and length to 0 |
71 | 74 | func (ri *Index) Reset() { |
72 | | - ri.StIndex = 0 |
| 75 | + ri.Start = 0 |
73 | 76 | ri.Len = 0 |
74 | 77 | } |
0 commit comments