datatypes: adding ringbuffer
Rewritten ringbuffer-feature moved to vlib/datatypes. Implementing head and tail pointers, as well as fixed sized array
Example:
module main
import datatypes
fn main() {
// create a new ringbuffer with size 4
// the generic argument sets the type
mut r := datatypes.new_ringbuffer<int>(4)
// push elements
r.push(3)
r.push(4)
// get the oldest element. It will be removed from the buffer when calling `pop()`
oldest_value := r.pop()
println(oldest_value) // Output: 3
}
Done.
I added errors now and implemented the suggested changes.
Thank you. I just need ringbuffer.
Can you provide a method to push more elements at a time? push(elements []T)
This is very useful for reading and writing socket buffers. Reading and writing one by one a byte cumbersome and slow
Thank you. I just need ringbuffer.
Can you provide a method to push more elements at a time?
push(elements []T)This is very useful for reading and writing socket buffers. Reading and writing one by one a byte cumbersome and slow
Won't push_many(elements []T) method be better?
Good idea, will work on it.
It may be even better to simply implement push(elements ...T). Then you can supply 1 or more elements. If you want to only push 1, it would be push(one). To push 2, push(one, two). If you want to push an array, it would be push(...arr).
You can even go crazy with something like push(one, two, ...arr, three, four).
It may be even better to simply implement
push(elements ...T). Then you can supply 1 or more elements. If you want to only push 1, it would bepush(one). To push 2,push(one, two). If you want to push an array, it would bepush(...arr).You can even go crazy with something like
push(one, two, ...arr, three, four).
Strongly agree
It may be even better to simply implement
push(elements ...T). Then you can supply 1 or more elements. If you want to only push 1, it would bepush(one). To push 2,push(one, two). If you want to push an array, it would bepush(...arr).You can even go crazy with something like
push(one, two, ...arr, three, four).
I also agree. Have to change it now, first I did push_many. Will change it.
I see you also have pop and pop_many. Maybe just pop that always returns an array? Not sure about that one. Less convenient if you only want to pop one thing, but consistent...
I see you also have
popandpop_many. Maybe justpopthat always returns an array? Not sure about that one. Less convenient if you only want to pop one thing, but consistent...
Yes, I have also been thinking about it. I could do pop(n ...u64) allowing only one n element in the array to be given. If n.len equals 0 or the value of the first element equals 1, it would return a single element, otherwise it would return an array... What do you think?
edit
… but that wouldn’t be possible since I have to choose whether a method returns T or []T…
I tried to do it like push(elements ...T), but I get a builder error when trying it out... I don't know why. See below...
/tmp/v_501/ring.15083904507890574193.tmp.c:12164:131: error: initializing 'int' with an expression of incompatible type 'Array_int' (aka 'struct array')
_option_void _t1 = ringbuffer__RingBuffer_T_int_push_T_int(&ring, new_array_from_c_array_noscan(1, 1, sizeof(int), _MOV((int[1]){_v_array})));
^~~~~~~~
1 warning and 1 error generated.
...
==================
(Use `v -cg` to print the entire error message)
builder error:
==================
C error. This should never happen.
For now I will leave it like this with two separate methodspush(element T) and push_many(elements []T).
This solution works.
I tried to do it like
push(elements ...T), but I get a builder error when trying it out... I don't know why. See below...
When you receive it, it will look like an array. You have to do the for e in elements that you have in push_many.
I tried to do it like
push(elements ...T), but I get a builder error when trying it out... I don't know why. See below...When you receive it, it will look like an array. You have to do the
for e in elementsthat you have inpush_many.
Yes, this is what I did. It gives me the error above...
Good work @floscodes
Good work @floscodes
Thank you 🙏
The failing macos-cross job is because of network errors. I'll try and re-run it edit Looks like we're all green :)
The failing macos-cross job is because of network errors.
I'll try and re-run it
edit
Looks like we're all green :)
.... oh that's why 😅 thank you!
Good work.
Thank you 🙏