MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Slice< T > Struct Template Reference

A Slice which is just a pointer + length Accessing elements through the array operator will do bounds checks Accessing out of bounds will terminate the program instead of throw Can also get sub slices. More...

#include <slice.hpp>

Inheritance diagram for mtcore::Slice< T >:

Public Types

using Elem = T
 

Public Member Functions

decltype(auto) ptr_iter () noexcept
 
decltype(auto) ptr_iter () const noexcept
 
decltype(auto) iter () const noexcept
 
constexpr Slice< std::add_const_t< T > > to_const () const noexcept
 Converts to a const Slice.
 
constexpr void init (T *head, size_t len)
 Initializes a Slice Using init instead of a constructor so that slices allocated with malloc or an arena can be initialized after the fact.
 
constexpr const T & operator[] (size_t i) const noexcept
 Access element at a specific index Const operator, returns a const reference.
 
T & operator[] (size_t i) noexcept
 Access element at a specific index Non-const operator, returns a mutable reference.
 
T & at (size_t i) noexcept
 Access element at a specific index Non-const operator, returns a mutable reference.
 
constexpr const T & at (size_t i) const noexcept
 Access element at a specific index Const operator, returns a const reference.
 
constexpr size_t size () const noexcept
 Gets the size of a Slice.
 
constexpr bool empty () const noexcept
 Checks if a Slice is empty.
 
constexpr Slice sub_rng (size_t start, size_t end) const noexcept
 Gets a sub slice from start to end index.
 
constexpr Slice sub (size_t start) const noexcept
 Gets a sub Slice from start.
 
constexpr Slice sub (size_t start, size_t len) const noexcept
 Gets a sub Slice from start up to a length.
 
std::strong_ordering operator<=> (const Slice< std::remove_const_t< T > > &other) const noexcept
 Compares against another Slice.
 
std::strong_ordering operator<=> (const Slice< std::add_const_t< T > > &other) const noexcept
 Compares against another Slice.
 
bool operator== (const Slice< std::remove_const_t< T > > &o) const noexcept
 
bool operator!= (const Slice< std::remove_const_t< T > > &o) const noexcept
 
bool operator< (const Slice< std::remove_const_t< T > > &o) const noexcept
 
bool operator> (const Slice< std::remove_const_t< T > > &o) const noexcept
 
bool operator<= (const Slice< std::remove_const_t< T > > &o) const noexcept
 
bool operator>= (const Slice< std::remove_const_t< T > > &o) const noexcept
 
bool operator== (const Slice< std::add_const_t< T > > &o) const noexcept
 
bool operator!= (const Slice< std::add_const_t< T > > &o) const noexcept
 
bool operator< (const Slice< std::add_const_t< T > > &o) const noexcept
 
bool operator> (const Slice< std::add_const_t< T > > &o) const noexcept
 
bool operator<= (const Slice< std::add_const_t< T > > &o) const noexcept
 
bool operator>= (const Slice< std::add_const_t< T > > &o) const noexcept
 

Public Attributes

T * head = nullptr
 
size_t len = 0
 

Detailed Description

template<class T>
struct mtcore::Slice< T >

A Slice which is just a pointer + length Accessing elements through the array operator will do bounds checks Accessing out of bounds will terminate the program instead of throw Can also get sub slices.

Accessing elements is \(O(1)\) Getting a sub-slice is \(O(1)\) Cannot remove/add arbitrary elements

Template Parameters
TType of element pointed to by the Slice

Definition at line 46 of file colls/slice.hpp.

Member Typedef Documentation

◆ Elem

template<class T>
using mtcore::Slice< T >::Elem = T

Definition at line 47 of file colls/slice.hpp.

Member Function Documentation

◆ at() [1/2]

template<class T>
const T & mtcore::Slice< T >::at ( size_t i) const
inlinenodiscardconstexprnoexcept

Access element at a specific index Const operator, returns a const reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 108 of file colls/slice.hpp.

108{ return (*this)[i]; }
A Slice which is just a pointer + length Accessing elements through the array operator will do bounds...

◆ at() [2/2]

template<class T>
T & mtcore::Slice< T >::at ( size_t i)
inlinenodiscardnoexcept

Access element at a specific index Non-const operator, returns a mutable reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 101 of file colls/slice.hpp.

101{ return (*this)[i]; }

◆ empty()

template<class T>
bool mtcore::Slice< T >::empty ( ) const
inlinenodiscardconstexprnoexcept

Checks if a Slice is empty.

Definition at line 118 of file colls/slice.hpp.

118{ return head == nullptr || len == 0; }
Here is the caller graph for this function:

◆ init()

template<class T>
void mtcore::Slice< T >::init ( T * head,
size_t len )
inlineconstexpr

Initializes a Slice Using init instead of a constructor so that slices allocated with malloc or an arena can be initialized after the fact.

Parameters
headPointer to the starting memory address
lenNumber of elements of type T in the Slice

Definition at line 69 of file colls/slice.hpp.

69 {
70 this->head = head;
71 this->len = len;
72 }
Here is the caller graph for this function:

◆ iter()

template<class T>
decltype(auto) mtcore::Slice< T >::iter ( ) const
inlinenodiscardnoexcept

Definition at line 54 of file colls/slice.hpp.

54{ return iter::val(*this); }
ValIter< T > val(const T &r)
Generic value iterator that uses the operator[] and incrementing indexes to iterate over a collection...
Definition iter.hpp:114
Here is the caller graph for this function:

◆ operator!=() [1/2]

template<class T>
bool mtcore::Slice< T >::operator!= ( const Slice< std::add_const_t< T > > & o) const
inlinenoexcept

Definition at line 240 of file colls/slice.hpp.

240 {
242 }

◆ operator!=() [2/2]

template<class T>
bool mtcore::Slice< T >::operator!= ( const Slice< std::remove_const_t< T > > & o) const
inlinenoexcept

Definition at line 214 of file colls/slice.hpp.

214 {
216 }

◆ operator<() [1/2]

template<class T>
bool mtcore::Slice< T >::operator< ( const Slice< std::add_const_t< T > > & o) const
inlinenoexcept

Definition at line 244 of file colls/slice.hpp.

244 {
246 }

◆ operator<() [2/2]

template<class T>
bool mtcore::Slice< T >::operator< ( const Slice< std::remove_const_t< T > > & o) const
inlinenoexcept

Definition at line 218 of file colls/slice.hpp.

218 {
220 }

◆ operator<=() [1/2]

template<class T>
bool mtcore::Slice< T >::operator<= ( const Slice< std::add_const_t< T > > & o) const
inlinenoexcept

Definition at line 252 of file colls/slice.hpp.

252 {
253 const auto cmp = *this <=> o;
255 }

◆ operator<=() [2/2]

template<class T>
bool mtcore::Slice< T >::operator<= ( const Slice< std::remove_const_t< T > > & o) const
inlinenoexcept

Definition at line 226 of file colls/slice.hpp.

226 {
227 const auto cmp = *this <=> o;
229 }

◆ operator<=>() [1/2]

template<class T>
std::strong_ordering mtcore::Slice< T >::operator<=> ( const Slice< std::add_const_t< T > > & other) const
inlinenoexcept

Compares against another Slice.

Parameters
otherConstant Slice to compare to
Returns
Relative ordering

Definition at line 177 of file colls/slice.hpp.

177 {
178 if (empty() && other.empty()) {
180 }
181
182 if (empty()) {
184 }
185
186 if (other.empty()) {
188 }
189
190 for (size_t i = 0; i < len && i < other.len; ++i) {
191 if (head[i] > other.head[i]) {
193 }
194 if (head[i] < other.head[i]) {
196 }
197 }
198
199 if (len == other.len) {
201 }
202 else if (len < other.len) {
204 }
205 else {
207 }
208 }
constexpr bool empty() const noexcept
Checks if a Slice is empty.

◆ operator<=>() [2/2]

template<class T>
std::strong_ordering mtcore::Slice< T >::operator<=> ( const Slice< std::remove_const_t< T > > & other) const
inlinenoexcept

Compares against another Slice.

Parameters
otherMutable Slice to compare to
Returns
Relative ordering

Definition at line 168 of file colls/slice.hpp.

168 {
169 return *this <=> other.to_const();
170 }
constexpr Slice< std::add_const_t< T > > to_const() const noexcept
Converts to a const Slice.

◆ operator==() [1/2]

template<class T>
bool mtcore::Slice< T >::operator== ( const Slice< std::add_const_t< T > > & o) const
inlinenoexcept

Definition at line 236 of file colls/slice.hpp.

236 {
238 }

◆ operator==() [2/2]

template<class T>
bool mtcore::Slice< T >::operator== ( const Slice< std::remove_const_t< T > > & o) const
inlinenoexcept

Definition at line 210 of file colls/slice.hpp.

210 {
212 }

◆ operator>() [1/2]

template<class T>
bool mtcore::Slice< T >::operator> ( const Slice< std::add_const_t< T > > & o) const
inlinenoexcept

Definition at line 248 of file colls/slice.hpp.

248 {
250 }

◆ operator>() [2/2]

template<class T>
bool mtcore::Slice< T >::operator> ( const Slice< std::remove_const_t< T > > & o) const
inlinenoexcept

Definition at line 222 of file colls/slice.hpp.

222 {
224 }

◆ operator>=() [1/2]

template<class T>
bool mtcore::Slice< T >::operator>= ( const Slice< std::add_const_t< T > > & o) const
inlinenoexcept

Definition at line 257 of file colls/slice.hpp.

257 {
258 const auto cmp = *this <=> o;
260 }

◆ operator>=() [2/2]

template<class T>
bool mtcore::Slice< T >::operator>= ( const Slice< std::remove_const_t< T > > & o) const
inlinenoexcept

Definition at line 231 of file colls/slice.hpp.

231 {
232 const auto cmp = *this <=> o;
234 }

◆ operator[]() [1/2]

template<class T>
const T & mtcore::Slice< T >::operator[] ( size_t i) const
inlinenodiscardconstexprnoexcept

Access element at a specific index Const operator, returns a const reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 79 of file colls/slice.hpp.

79 {
80 ensure(i < len, "SLICE ACCESS OUT OF BOUNDS");
81 ensure(head, "NULL POINTER DEREFERENCE");
82 return head[i];
83 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...

◆ operator[]() [2/2]

template<class T>
T & mtcore::Slice< T >::operator[] ( size_t i)
inlinenoexcept

Access element at a specific index Non-const operator, returns a mutable reference.

Parameters
iIndex of the element in the Slice to access

Definition at line 90 of file colls/slice.hpp.

90 {
91 ensure(i < len, "SLICE ACCESS OUT OF BOUNDS");
92 ensure(head, "NULL POINTER DEREFERENCE");
93 return head[i];
94 }

◆ ptr_iter() [1/2]

template<class T>
decltype(auto) mtcore::Slice< T >::ptr_iter ( ) const
inlinenodiscardnoexcept

Definition at line 53 of file colls/slice.hpp.

53{ return iter::const_ptr(*this); }
ConstPtrIter< T > const_ptr(const T &r)
Generic constant pointer iterator that uses the operator[] and incrementing indexes to iterate over a...
Definition iter.hpp:128

◆ ptr_iter() [2/2]

template<class T>
decltype(auto) mtcore::Slice< T >::ptr_iter ( )
inlinenodiscardnoexcept

Definition at line 52 of file colls/slice.hpp.

52{ return iter::ptr(*this); }
PtrIter< T > ptr(T &r)
Generic pointer iterator that uses the operator[] and incrementing indexes to iterate over a collecti...
Definition iter.hpp:101

◆ size()

template<class T>
size_t mtcore::Slice< T >::size ( ) const
inlinenodiscardconstexprnoexcept

Gets the size of a Slice.

Definition at line 113 of file colls/slice.hpp.

113{ return head != nullptr ? len : 0; }
Here is the caller graph for this function:

◆ sub() [1/2]

template<class T>
Slice mtcore::Slice< T >::sub ( size_t start) const
inlinenodiscardconstexprnoexcept

Gets a sub Slice from start.

Parameters
startThe starting index to get a sub Slice from

Definition at line 138 of file colls/slice.hpp.

138 {
139 Slice res;
140 if (start >= len) {
141 res.init(nullptr, 0);
142 return res;
143 }
144
145 res.init(head + start, len - start);
146 ensure(res.len + start == len, "BAD LENGTH MATH");
147 ensure(head + len == res.head + res.len, "BAD END POINTER");
148 return res;
149 }
constexpr void init(T *head, size_t len)
Initializes a Slice Using init instead of a constructor so that slices allocated with malloc or an ar...
Here is the caller graph for this function:

◆ sub() [2/2]

template<class T>
Slice mtcore::Slice< T >::sub ( size_t start,
size_t len ) const
inlinenodiscardconstexprnoexcept

Gets a sub Slice from start up to a length.

Parameters
startThe starting index to get a sub Slice from
lenMaximum length of the Slice

Definition at line 156 of file colls/slice.hpp.

156 {
157 auto res = sub(start);
158 res.len = res.len > len ? len : res.len;
159 ensure(res.len <= len, "BAD LENGTH");
160 return res;
161 }
constexpr Slice sub(size_t start) const noexcept
Gets a sub Slice from start.

◆ sub_rng()

template<class T>
Slice mtcore::Slice< T >::sub_rng ( size_t start,
size_t end ) const
inlinenodiscardconstexprnoexcept

Gets a sub slice from start to end index.

Parameters
startStarting index
endEnding index

Definition at line 125 of file colls/slice.hpp.

125 {
126 if (start >= end || start >= len) {
127 Slice res;
128 res.init(nullptr, 0);
129 return res;
130 }
131 return sub(start, end - start);
132 }

◆ to_const()

template<class T>
Slice< std::add_const_t< T > > mtcore::Slice< T >::to_const ( ) const
inlinenodiscardconstexprnoexcept

Converts to a const Slice.

Definition at line 57 of file colls/slice.hpp.

57 {
58 return {static_cast<std::add_pointer_t<std::add_const_t<T>>>(head), len};
59 }
Here is the caller graph for this function:

Member Data Documentation

◆ head

template<class T>
T* mtcore::Slice< T >::head = nullptr

Definition at line 49 of file colls/slice.hpp.

◆ len

template<class T>
size_t mtcore::Slice< T >::len = 0

Definition at line 50 of file colls/slice.hpp.


The documentation for this struct was generated from the following file: