tiny_ring 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/tiny_ring.rb +35 -0
- metadata +57 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0310e9d9d45217d00a3c355cd4858c8643ee3e35461c97d61e55a71afd70f66f
|
4
|
+
data.tar.gz: a0cd6337aa725c55a617bc9e39a44ab1d8e411359824f87cf5eae09db8553f99
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 98a113680b76baab7dbc4f2c2273aa142c12ef6c69a29de626e99d913e3fbc7a5b1df1970ed497b812b4da47972fd539c9ef14d94dffc29e14dc284dd5c5a0f3
|
7
|
+
data.tar.gz: feb8d20566eb55c86fa194c35577b17db3f9407288b42dd7164f2707d63c2f88545dc3e921d8a2ad8db4385c1c819d6b4288de2afea2a2ff218df8d7593d87b3
|
data/lib/tiny_ring.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# memory efficient ring buffer implementation
|
2
|
+
#
|
3
|
+
# usage:
|
4
|
+
# > tr = TinyRing.new(3)
|
5
|
+
# > tr << 1 # => [1, nil, nil]
|
6
|
+
# > tr << 2 # => [1, 2, nil]
|
7
|
+
# > tr << 3 # => [1, 2, 3]
|
8
|
+
# > tr << 4 # => [4, 2, 3]
|
9
|
+
# > puts tr.to_a.inspect # => [2, 3, 4]
|
10
|
+
class TinyRing
|
11
|
+
def initialize(size)
|
12
|
+
@size = size
|
13
|
+
@buffer = Array.new(size)
|
14
|
+
@current_index = 0
|
15
|
+
end
|
16
|
+
|
17
|
+
def push(item)
|
18
|
+
@buffer[@current_index] = item
|
19
|
+
@current_index = (@current_index + 1) % @size
|
20
|
+
end
|
21
|
+
alias << push
|
22
|
+
|
23
|
+
# returns the items most recently inserted into the ring buffer the most
|
24
|
+
# recently added item item added to the buffer.
|
25
|
+
# returns nil if the buffer is empty
|
26
|
+
def last
|
27
|
+
previous_index = (@current_index - 1) % @size
|
28
|
+
@buffer[previous_index]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns the contents of the buffer as an Array, ordered from oldest to newest.
|
32
|
+
def to_a
|
33
|
+
(@buffer[@current_index..-1] + @buffer[0...@current_index]).compact
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiny_ring
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Lunt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: minitest
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: a tiny ring buffer implementation
|
28
|
+
email: jefflunt@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/tiny_ring.rb
|
34
|
+
homepage: https://github.com/jefflunt/tiny_ring
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubygems_version: 3.3.7
|
54
|
+
signing_key:
|
55
|
+
specification_version: 4
|
56
|
+
summary: a tiny ring buffer implementation that is memory efficient
|
57
|
+
test_files: []
|