forked from basilTeam/basil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.h
166 lines (134 loc) · 4.2 KB
/
slice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2021, the Basil authors
* All rights reserved.
*
* This source code is licensed under the 3-Clause BSD License, the full text
* of which can be found in the LICENSE file in the root directory
* of this project.
*/
#ifndef BASIL_SLICE_H
#define BASIL_SLICE_H
#include "defs.h"
#include "panic.h"
template<typename T, typename U>
struct pair {
T first;
U second;
pair() {}
pair(const T& _first, const U& _second):
first(_first), second(_second) {}
bool operator==(const pair& other) const {
return first == other.first && second == other.second;
}
};
template<typename T>
class const_slice {
u32 _size;
const T* _data;
public:
const_slice(u32 size, const T* data):
_size(size), _data(data) {
//
}
const T& operator[](u32 i) const {
if (i >= _size) panic("Attempted out-of-bounds access of slice element!");
return _data[i];
}
const_slice operator[](pair<u32, u32> range) const {
if (range.first >= _size || range.second > _size) panic("Attempted out-of-bounds subslice of slice!");
if (range.second < range.first) panic("Attempted subslice with reversed bounds!");
return { range.second - range.first, _data + range.first };
}
u32 size() const {
return _size;
}
const T& front() const {
if (_size == 0) panic("Attempted to get first element of empty slice!");
return _data[0];
}
const T& back() const {
if (_size == 0) panic("Attempted to get last element of empty slice!");
return _data[_size - 1];
}
const T* begin() const {
return _data;
}
const T* end() const {
return _data + _size;
}
};
template<typename T>
class slice {
u32 _size;
T* _data;
public:
slice(u32 size, T* data):
_size(size), _data(data) {
//
}
const T& operator[](u32 i) const {
if (i >= _size) panic("Attempted out-of-bounds access of slice element!");
return _data[i];
}
T& operator[](u32 i) {
if (i >= _size) panic("Attempted out-of-bounds access of slice element!");
return _data[i];
}
const_slice<T> operator[](pair<u32, u32> range) const {
if (range.first >= _size || range.second > _size) panic("Attempted out-of-bounds subslice of slice!");
if (range.second < range.first) panic("Attempted subslice with reversed bounds!");
return { range.second - range.first, _data + range.first };
}
slice operator[](pair<u32, u32> range) {
if (range.first >= _size || range.second > _size) panic("Attempted out-of-bounds subslice of slice!");
if (range.second < range.first) panic("Attempted subslice with reversed bounds!");
return { range.second - range.first, _data + range.first };
}
u32 size() const {
return _size;
}
const T& front() const {
if (_size == 0) panic("Attempted to get first element of empty slice!");
return _data[0];
}
const T& back() const {
if (_size == 0) panic("Attempted to get last element of empty slice!");
return _data[_size - 1];
}
T& front() {
if (_size == 0) panic("Attempted to get first element of empty slice!");
return _data[0];
}
T& back() {
if (_size == 0) panic("Attempted to get last element of empty slice!");
return _data[_size - 1];
}
const T* begin() const {
return _data;
}
const T* end() const {
return _data + _size;
}
T* begin() {
return _data;
}
T* end() {
return _data + _size;
}
operator const_slice<T>() const {
return { _size, _data };
}
};
template<typename T>
const_slice<T> span(const const_slice<T>& a, const const_slice<T>& b) {
const const_slice<T>& from = a._data < b._data ? a : b;
const const_slice<T>& to = a._data > b._data ? a : b;
return { to._size + to._data - from._data, from._data };
}
template<typename T>
slice<T> span(const slice<T>& a, const slice<T>& b) {
const slice<T>& from = a._data < b._data ? a : b;
const slice<T>& to = a._data > b._data ? a : b;
return { to._size + to._data - from._data, from._data };
}
#endif