philiprehberger-ring_buffer 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +20 -0
- data/README.md +22 -0
- data/lib/philiprehberger/ring_buffer/version.rb +1 -1
- data/lib/philiprehberger/ring_buffer.rb +47 -6
- metadata +5 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8c2b316f686802f756d5af662c80a995759a3f0ed2700696e709cc35bc1e96a0
|
|
4
|
+
data.tar.gz: bded60fa9e0033419322ebb5e740dfbb7ee5b8a6c3d12daf1673c56de4fab56f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c7337e138c271f3293407174a9cd5ee5aaceace3bf597a122df6bcb53fdedf1d9a14331863b1fcd0939e66b54d8acfb070dd939fed37bda83513a8254703af7a
|
|
7
|
+
data.tar.gz: 838464c7e8c0904e288967ada4656abc5cd376b6a02d77b45cc16d5ffc5c201d015815e5b370cde1c2fce66ff1a7b7699e07dd302304234368459a34cfe27737
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,16 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.3.0] - 2026-04-09
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `#shift` removes and returns the oldest element
|
|
14
|
+
- `#pop` removes and returns the newest element
|
|
15
|
+
- `#oldest` and `#newest` peek without mutating
|
|
16
|
+
|
|
17
|
+
### Changed
|
|
18
|
+
- Internal `to_a` now derives the start index from `@head` and `@count`, supporting consumption via `shift`/`pop` while preserving all existing semantics
|
|
19
|
+
|
|
10
20
|
## [0.2.0] - 2026-04-03
|
|
11
21
|
|
|
12
22
|
### Added
|
|
@@ -57,3 +67,13 @@ and this gem adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
57
67
|
- Statistics: average, sum, min, max
|
|
58
68
|
- Last-n element retrieval
|
|
59
69
|
- Enumerable support
|
|
70
|
+
|
|
71
|
+
[0.3.0]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.3.0
|
|
72
|
+
[0.2.0]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.2.0
|
|
73
|
+
[0.1.6]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.1.6
|
|
74
|
+
[0.1.5]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.1.5
|
|
75
|
+
[0.1.4]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.1.4
|
|
76
|
+
[0.1.3]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.1.3
|
|
77
|
+
[0.1.2]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.1.2
|
|
78
|
+
[0.1.1]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.1.1
|
|
79
|
+
[0.1.0]: https://github.com/philiprehberger/rb-ring-buffer/releases/tag/v0.1.0
|
data/README.md
CHANGED
|
@@ -70,6 +70,24 @@ buf.size # => 0
|
|
|
70
70
|
buf.empty? # => true
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
+
### Consuming Elements
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
buf = Philiprehberger::RingBuffer.new(3)
|
|
77
|
+
[1, 2, 3].each { |v| buf.push(v) }
|
|
78
|
+
|
|
79
|
+
buf.shift # => 1 (removes oldest)
|
|
80
|
+
buf.pop # => 3 (removes newest)
|
|
81
|
+
buf.to_a # => [2]
|
|
82
|
+
|
|
83
|
+
buf.push(4)
|
|
84
|
+
buf.push(5)
|
|
85
|
+
buf.oldest # => 2 (peek, no mutation)
|
|
86
|
+
buf.newest # => 5
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
`shift`, `pop`, `oldest`, and `newest` all return `nil` when the buffer is empty.
|
|
90
|
+
|
|
73
91
|
### Statistics
|
|
74
92
|
|
|
75
93
|
```ruby
|
|
@@ -100,6 +118,10 @@ buf.select(&:odd?) # => [1, 3]
|
|
|
100
118
|
|--------|-------------|
|
|
101
119
|
| `RingBuffer.new(capacity)` | Create a buffer with fixed capacity |
|
|
102
120
|
| `#push(value)` | Add a value, overwriting oldest if full |
|
|
121
|
+
| `#shift` | Remove and return the oldest element (or `nil`) |
|
|
122
|
+
| `#pop` | Remove and return the newest element (or `nil`) |
|
|
123
|
+
| `#oldest` | Peek the oldest element without removing (or `nil`) |
|
|
124
|
+
| `#newest` | Peek the newest element without removing (or `nil`) |
|
|
103
125
|
| `#[](index)` | Access by index (0 = oldest, -1 = newest) |
|
|
104
126
|
| `#to_a` | Convert to array (oldest first) |
|
|
105
127
|
| `#size` | Number of elements in the buffer |
|
|
@@ -39,12 +39,53 @@ module Philiprehberger
|
|
|
39
39
|
def to_a
|
|
40
40
|
return [] if @count.zero?
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
start = (@head - @count) % @capacity
|
|
43
|
+
Array.new(@count) { |i| @buffer[(start + i) % @capacity] }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Remove and return the oldest element
|
|
47
|
+
#
|
|
48
|
+
# @return [Object, nil]
|
|
49
|
+
def shift
|
|
50
|
+
return nil if empty?
|
|
51
|
+
|
|
52
|
+
start = (@head - @count) % @capacity
|
|
53
|
+
value = @buffer[start]
|
|
54
|
+
@buffer[start] = nil
|
|
55
|
+
@count -= 1
|
|
56
|
+
value
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Remove and return the newest element
|
|
60
|
+
#
|
|
61
|
+
# @return [Object, nil]
|
|
62
|
+
def pop
|
|
63
|
+
return nil if empty?
|
|
64
|
+
|
|
65
|
+
newest_idx = (@head - 1) % @capacity
|
|
66
|
+
value = @buffer[newest_idx]
|
|
67
|
+
@buffer[newest_idx] = nil
|
|
68
|
+
@head = newest_idx
|
|
69
|
+
@count -= 1
|
|
70
|
+
value
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Return the oldest element without removing it
|
|
74
|
+
#
|
|
75
|
+
# @return [Object, nil]
|
|
76
|
+
def oldest
|
|
77
|
+
return nil if empty?
|
|
78
|
+
|
|
79
|
+
@buffer[(@head - @count) % @capacity]
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Return the newest element without removing it
|
|
83
|
+
#
|
|
84
|
+
# @return [Object, nil]
|
|
85
|
+
def newest
|
|
86
|
+
return nil if empty?
|
|
87
|
+
|
|
88
|
+
@buffer[(@head - 1) % @capacity]
|
|
48
89
|
end
|
|
49
90
|
|
|
50
91
|
# Number of elements currently in the buffer
|
metadata
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: philiprehberger-ring_buffer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Philip Rehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-04-
|
|
11
|
+
date: 2026-04-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Fixed-capacity ring buffer that overwrites oldest entries on overflow,
|
|
14
|
-
with index access,
|
|
15
|
-
median), first/last-n retrieval, and
|
|
14
|
+
with index access, push/pop/shift mutation, oldest/newest peek, built-in statistics
|
|
15
|
+
(average, sum, min, max, variance, stddev, median), first/last-n retrieval, and
|
|
16
|
+
Enumerable support.
|
|
16
17
|
email:
|
|
17
18
|
- me@philiprehberger.com
|
|
18
19
|
executables: []
|