Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

window

Returns an iterator with a sliding window of slices for buffer. The sliding window has length size and on every iteration moves forward by advance.

Extract data for moving average with: window(u8, "abcdefg", 3, 1) will return slices "abc", "bcd", "cde", "def", "efg", null, in that order.

Chunk or split every N items with: window(u8, "abcdefg", 3, 3) will return slices "abc", "def", "g", null, in that order.

Pick every even index with: window(u8, "abcdefg", 1, 2) will return slices "a", "c", "e", "g" null, in that order.

The size and advance must be not be zero.

mem.window
pub fn window(comptime T: type, buffer: []const T, size: usize, advance: usize) WindowIterator(T)

File

lib/std/mem.zig:3008

Code

pub fn window(comptime T: type, buffer: []const T, size: usize, advance: usize) WindowIterator(T) {
    assert(size != 0);
    assert(advance != 0);
    return .{
        .index = if (buffer.len > 0) 0 else null,
        .buffer = buffer,
        .size = size,
        .advance = advance,
    };
}