Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
slice.h
Go to the documentation of this file.
1
5#pragma once
6#include "buffer.h"
7
11template<typename T>
12struct Slice {
14 const Buffer<T> *buf;
16 size_t len;
17 size_t start;
18
20 T& operator[](size_t ix) const {
21 assert(ix < len);
22 return buf->buf[start+ix];
23 }
25 T* begin() const { return &buf->buf[start]; }
27 T* end() const { return &buf->buf[start+len]; }
28
31 buf = nullptr; len = 0;
32 }
33 Slice(const Buffer<T> &b, size_t start, size_t end=0) : buf{&b}, start(start) {
34 len = end == 0 ? b.len - start : end - start;
35 start = start;
36 assert(end <= b.len);
37 }
38 Slice(Slice<T> slc, size_t start, size_t end=0) : buf{slc.buf}, start(slc.start + start) {
39 len = end == 0 ? slc.buf->len - slc.start - start : end - start;
40 assert(end <= slc.buf->len - slc.start);
41 }
42};
Copyright (c) 2023 IACE.
dynamically allocated, but fixed-size buffer template
Definition buffer.h:18
size_t len
number of items stored in buffer
Definition buffer.h:21
slice of existing Buffer.
Definition slice.h:12
size_t len
number of items in slice
Definition slice.h:16
~Slice()
destructor
Definition slice.h:30
T * begin() const
begin method for range-based for loops
Definition slice.h:25
const Buffer< T > * buf
pointer to owning Buffer
Definition slice.h:14
T * end() const
end method for range-based for loops
Definition slice.h:27
T & operator[](size_t ix) const
access into known size of buffer
Definition slice.h:20