Switching ring.append to ring.push (#39)
Co-authored-by: Zachary Levy <zachary@sunforge.is> Reviewed-on: #39
This commit was merged in pull request #39.
This commit is contained in:
+23
-23
@@ -113,19 +113,19 @@ advance :: proc {
|
||||
advance_soa,
|
||||
}
|
||||
|
||||
append_aos :: #force_inline proc(ring: ^Ring($E), element: E) {
|
||||
push_aos :: #force_inline proc(ring: ^Ring($E), element: E) {
|
||||
ring.data[ring.next_write_index] = element
|
||||
advance(ring)
|
||||
}
|
||||
|
||||
append_soa :: #force_inline proc(ring: ^Ring_Soa($E), element: E) {
|
||||
push_soa :: #force_inline proc(ring: ^Ring_Soa($E), element: E) {
|
||||
ring.data[ring.next_write_index] = element
|
||||
advance(ring)
|
||||
}
|
||||
|
||||
append :: proc {
|
||||
append_aos,
|
||||
append_soa,
|
||||
push :: proc {
|
||||
push_aos,
|
||||
push_soa,
|
||||
}
|
||||
|
||||
get_aos :: #force_inline proc(ring: Ring($E), index: int) -> ^E {
|
||||
@@ -202,7 +202,7 @@ test_ring_aos :: proc(t: ^testing.T) {
|
||||
defer destroy(&ring)
|
||||
|
||||
for i in 1 ..= 5 {
|
||||
append(&ring, i)
|
||||
push(&ring, i)
|
||||
log.debug("Length:", ring.len)
|
||||
log.debug("Start index:", start_index_aos(ring))
|
||||
log.debug("Next write index:", ring.next_write_index)
|
||||
@@ -215,7 +215,7 @@ test_ring_aos :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_aos(ring), 0)
|
||||
|
||||
for i in 6 ..= 15 {
|
||||
append(&ring, i)
|
||||
push(&ring, i)
|
||||
log.debug("Length:", ring.len)
|
||||
log.debug("Start index:", start_index_aos(ring))
|
||||
log.debug("Next write index:", ring.next_write_index)
|
||||
@@ -230,7 +230,7 @@ test_ring_aos :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_aos(ring), 5)
|
||||
|
||||
for i in 15 ..= 25 {
|
||||
append(&ring, i)
|
||||
push(&ring, i)
|
||||
log.debug("Length:", ring.len)
|
||||
log.debug("Start index:", start_index_aos(ring))
|
||||
log.debug("Next write index:", ring.next_write_index)
|
||||
@@ -241,7 +241,7 @@ test_ring_aos :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, get_last(ring)^, 25)
|
||||
|
||||
clear(&ring)
|
||||
append(&ring, 1)
|
||||
push(&ring, 1)
|
||||
testing.expect_value(t, ring.len, 1)
|
||||
testing.expect_value(t, get(ring, 0)^, 1)
|
||||
}
|
||||
@@ -256,7 +256,7 @@ test_ring_soa :: proc(t: ^testing.T) {
|
||||
defer destroy(&ring)
|
||||
|
||||
for i in 1 ..= 5 {
|
||||
append(&ring, Ints{i, i})
|
||||
push(&ring, Ints{i, i})
|
||||
log.debug("Length:", ring.len)
|
||||
log.debug("Start index:", start_index_soa(ring))
|
||||
log.debug("Next write index:", ring.next_write_index)
|
||||
@@ -269,7 +269,7 @@ test_ring_soa :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_soa(ring), 0)
|
||||
|
||||
for i in 6 ..= 15 {
|
||||
append(&ring, Ints{i, i})
|
||||
push(&ring, Ints{i, i})
|
||||
log.debug("Length:", ring.len)
|
||||
log.debug("Start index:", start_index_soa(ring))
|
||||
log.debug("Next write index:", ring.next_write_index)
|
||||
@@ -284,7 +284,7 @@ test_ring_soa :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_soa(ring), 5)
|
||||
|
||||
for i in 15 ..= 25 {
|
||||
append(&ring, Ints{i, i})
|
||||
push(&ring, Ints{i, i})
|
||||
log.debug("Length:", ring.len)
|
||||
log.debug("Start index:", start_index_soa(ring))
|
||||
log.debug("Next write index:", ring.next_write_index)
|
||||
@@ -295,7 +295,7 @@ test_ring_soa :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, get_last(ring), Ints{25, 25})
|
||||
|
||||
clear(&ring)
|
||||
append(&ring, Ints{1, 1})
|
||||
push(&ring, Ints{1, 1})
|
||||
testing.expect_value(t, ring.len, 1)
|
||||
testing.expect_value(t, get(ring, 0), Ints{1, 1})
|
||||
}
|
||||
@@ -314,7 +314,7 @@ test_ring_aos_init_from_slice :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_aos(ring), 0)
|
||||
|
||||
// Partial fill (3 / 7).
|
||||
for i in 1 ..= 3 do append(&ring, i)
|
||||
for i in 1 ..= 3 do push(&ring, i)
|
||||
testing.expect_value(t, ring.len, 3)
|
||||
testing.expect_value(t, ring.next_write_index, 3)
|
||||
testing.expect_value(t, start_index_aos(ring), 0)
|
||||
@@ -324,7 +324,7 @@ test_ring_aos_init_from_slice :: proc(t: ^testing.T) {
|
||||
|
||||
// Fill exactly to capacity. Pushing element 7 must make len == cap
|
||||
// AND wrap next_write_index from 6 back to 0 in the same step.
|
||||
for i in 4 ..= 7 do append(&ring, i)
|
||||
for i in 4 ..= 7 do push(&ring, i)
|
||||
testing.expect_value(t, ring.len, 7)
|
||||
testing.expect_value(t, ring.next_write_index, 0)
|
||||
testing.expect_value(t, start_index_aos(ring), 0)
|
||||
@@ -333,7 +333,7 @@ test_ring_aos_init_from_slice :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, get_last(ring)^, 7)
|
||||
|
||||
// First overwrite — oldest element shifts by one.
|
||||
append(&ring, 8)
|
||||
push(&ring, 8)
|
||||
testing.expect_value(t, ring.len, 7)
|
||||
testing.expect_value(t, ring.next_write_index, 1)
|
||||
testing.expect_value(t, start_index_aos(ring), 1)
|
||||
@@ -344,7 +344,7 @@ test_ring_aos_init_from_slice :: proc(t: ^testing.T) {
|
||||
// Stress: 3 more complete wrap cycles (21 more pushes).
|
||||
// After 29 total pushes, ring contains the last 7 (23..=29),
|
||||
// and next_write_index = 29 mod 7 = 1.
|
||||
for i in 9 ..= 29 do append(&ring, i)
|
||||
for i in 9 ..= 29 do push(&ring, i)
|
||||
testing.expect_value(t, ring.len, 7)
|
||||
testing.expect_value(t, ring.next_write_index, 1)
|
||||
testing.expect_value(t, start_index_aos(ring), 1)
|
||||
@@ -360,7 +360,7 @@ test_ring_aos_init_from_slice :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_aos(ring), 0)
|
||||
|
||||
// Single-element edge case: get_last(len==1) routes through get(ring, 0).
|
||||
append(&ring, 42)
|
||||
push(&ring, 42)
|
||||
testing.expect_value(t, ring.len, 1)
|
||||
testing.expect_value(t, ring.next_write_index, 1)
|
||||
testing.expect_value(t, get(ring, 0)^, 42)
|
||||
@@ -385,7 +385,7 @@ test_ring_soa_init_from_slice :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_soa(ring), 0)
|
||||
|
||||
// Partial fill (3 / 7).
|
||||
for i in 1 ..= 3 do append(&ring, Ints{i, i})
|
||||
for i in 1 ..= 3 do push(&ring, Ints{i, i})
|
||||
testing.expect_value(t, ring.len, 3)
|
||||
testing.expect_value(t, ring.next_write_index, 3)
|
||||
testing.expect_value(t, start_index_soa(ring), 0)
|
||||
@@ -395,7 +395,7 @@ test_ring_soa_init_from_slice :: proc(t: ^testing.T) {
|
||||
|
||||
// Fill exactly to capacity. Pushing element 7 must make len == cap
|
||||
// AND wrap next_write_index from 6 back to 0 in the same step.
|
||||
for i in 4 ..= 7 do append(&ring, Ints{i, i})
|
||||
for i in 4 ..= 7 do push(&ring, Ints{i, i})
|
||||
testing.expect_value(t, ring.len, 7)
|
||||
testing.expect_value(t, ring.next_write_index, 0)
|
||||
testing.expect_value(t, start_index_soa(ring), 0)
|
||||
@@ -404,7 +404,7 @@ test_ring_soa_init_from_slice :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, get_last(ring), Ints{7, 7})
|
||||
|
||||
// First overwrite — oldest element shifts by one.
|
||||
append(&ring, Ints{8, 8})
|
||||
push(&ring, Ints{8, 8})
|
||||
testing.expect_value(t, ring.len, 7)
|
||||
testing.expect_value(t, ring.next_write_index, 1)
|
||||
testing.expect_value(t, start_index_soa(ring), 1)
|
||||
@@ -415,7 +415,7 @@ test_ring_soa_init_from_slice :: proc(t: ^testing.T) {
|
||||
// Stress: 3 more complete wrap cycles (21 more pushes).
|
||||
// After 29 total pushes, ring contains the last 7 (23..=29),
|
||||
// and next_write_index = 29 mod 7 = 1.
|
||||
for i in 9 ..= 29 do append(&ring, Ints{i, i})
|
||||
for i in 9 ..= 29 do push(&ring, Ints{i, i})
|
||||
testing.expect_value(t, ring.len, 7)
|
||||
testing.expect_value(t, ring.next_write_index, 1)
|
||||
testing.expect_value(t, start_index_soa(ring), 1)
|
||||
@@ -431,7 +431,7 @@ test_ring_soa_init_from_slice :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, start_index_soa(ring), 0)
|
||||
|
||||
// Single-element edge case: get_last(len==1) routes through get(ring, 0).
|
||||
append(&ring, Ints{42, 42})
|
||||
push(&ring, Ints{42, 42})
|
||||
testing.expect_value(t, ring.len, 1)
|
||||
testing.expect_value(t, ring.next_write_index, 1)
|
||||
testing.expect_value(t, get(ring, 0), Ints{42, 42})
|
||||
|
||||
Reference in New Issue
Block a user