Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
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 standard library. Public Function

window

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

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.

Params

  • T: type
  • buffer: []const T
  • size: usize
  • advance: usize

File

mem.zig:3042
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
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,
    };
}