@@ -11,92 +11,83 @@ namespace dsplib {
11
11
template <typename T>
12
12
class base_array ;
13
13
14
- // TODO: rename `slice_t` to `mut_slice_t`
15
14
template <typename T>
16
- class slice_t ;
15
+ class mut_slice_t ;
17
16
18
- // TODO: rename `const_slice_t` to `slice_t`
19
17
template <typename T>
20
- class const_slice_t ;
18
+ class slice_t ;
21
19
22
- // ----------------------------------------------------------------------------------------
23
- // TODO: support empty slices
24
20
class base_slice_t
25
21
{
26
22
public:
27
23
base_slice_t () = default ;
28
24
29
- explicit base_slice_t (int n, int i1, int i2, int m) {
30
- // is empty
31
- if (n == 0 ) {
25
+ explicit base_slice_t (int n, int i1, int i2, int m)
26
+ : _m{m}
27
+ , _n{n}
28
+ , _i1{(i1 < 0 ) ? (_n + i1) : (i1)}
29
+ , _i2{(i2 < 0 ) ? (_n + i2) : (i2)}
30
+ , _nc{_count ()} {
31
+ if (_n == 0 ) {
32
32
return ;
33
33
}
34
34
35
35
DSPLIB_ASSERT (m != 0 , " Slice stride cannot be zero" );
36
-
37
- _m = m;
38
- _n = n;
39
- _i1 = (i1 < 0 ) ? (_n + i1) : (i1);
40
- _i2 = (i2 < 0 ) ? (_n + i2) : (i2);
41
- const int d = std::abs (_i2 - _i1);
42
- const int tm = std::abs (_m);
43
- _nc = (d % tm != 0 ) ? (d / tm + 1 ) : (d / tm );
44
-
45
- if ((_i1 < 0 ) || (_i1 >= _n)) {
46
- DSPLIB_THROW (" Left slice index out of range" );
47
- }
48
-
49
- if ((_i2 < 0 ) || (_i2 > _n)) {
50
- DSPLIB_THROW (" Right slice index out of range" );
51
- }
52
-
53
- if ((_m < 0 ) && (_i1 < _i2)) {
54
- DSPLIB_THROW (" First index is smaller for negative step" );
55
- }
56
-
57
- if ((_m > 0 ) && (_i1 > _i2)) {
58
- DSPLIB_THROW (" First index is greater for positive step" );
59
- }
60
-
61
- if (_nc > _n) {
62
- DSPLIB_THROW (" Slice range is greater vector size" );
63
- }
36
+ DSPLIB_ASSERT ((_i1 >= 0 ) && (_i1 < _n), " Left slice index out of range" );
37
+ DSPLIB_ASSERT ((_i2 >= 0 ) && (_i2 <= _n), " Right slice index out of range" );
38
+ DSPLIB_ASSERT (!((_m < 0 ) && (_i1 < _i2)), " First index is smaller for negative step" );
39
+ DSPLIB_ASSERT (!((_m > 0 ) && (_i1 > _i2)), " First index is greater for positive step" );
40
+ DSPLIB_ASSERT (_nc <= _n, " Slice range is greater array size" );
64
41
}
65
42
66
43
bool empty () const noexcept {
67
44
return (_nc == 0 );
68
45
}
69
46
70
47
protected:
71
- int _i1{0 };
72
- int _i2{0 };
73
- int _m{0 };
74
- int _n{0 };
75
- int _nc{0 };
48
+ const int _m{1 };
49
+ const int _n{0 };
50
+ const int _i1{0 };
51
+ const int _i2{0 };
52
+ const int _nc{0 };
53
+
54
+ private:
55
+ int _count () const noexcept {
56
+ if (_n == 0 ) {
57
+ return 0 ;
58
+ }
59
+ const int d = std::abs (_i2 - _i1);
60
+ const int tm = std::abs (_m);
61
+ const int size = (d % tm != 0 ) ? (d / tm + 1 ) : (d / tm );
62
+ return size;
63
+ }
76
64
};
77
65
78
- // ----------------------------------------------------------------------------------------
79
- // TODO: add concatenate array = slice | array
66
+ /* *
67
+ * @brief Non-mutable slice object
68
+ * @todo add concatenate array = slice | array
69
+ * @tparam T real_t/cmplx_t
70
+ */
80
71
template <typename T>
81
- class const_slice_t : public base_slice_t
72
+ class slice_t : public base_slice_t
82
73
{
83
74
public:
84
- friend class slice_t <T>;
75
+ friend class mut_slice_t <T>;
85
76
using const_iterator = SliceIterator<const T>;
86
77
87
- const_slice_t () = default ;
78
+ slice_t () = default ;
88
79
89
- const_slice_t (const T* data, int size, int i1, int i2, int m)
80
+ slice_t (const T* data, int size, int i1, int i2, int m)
90
81
: base_slice_t (size, i1, i2, m)
91
82
, _data{data} {
92
83
}
93
84
94
- const_slice_t (const const_slice_t & rhs)
85
+ slice_t (const slice_t & rhs)
95
86
: base_slice_t (rhs.size(), rhs._i1, rhs._i2, rhs._m)
96
87
, _data{rhs._data } {
97
88
}
98
89
99
- const_slice_t (const slice_t <T>& rhs)
90
+ slice_t (const mut_slice_t <T>& rhs)
100
91
: base_slice_t (rhs._n, rhs._i1, rhs._i2, rhs._m)
101
92
, _data{rhs._data } {
102
93
}
@@ -127,23 +118,26 @@ class const_slice_t : public base_slice_t
127
118
const T* _data{nullptr };
128
119
};
129
120
130
- // ----------------------------------------------------------------------------------------
121
+ /* *
122
+ * @brief Mutable slice object
123
+ * @tparam T real_t/cmplx_t
124
+ */
131
125
template <typename T>
132
- class slice_t : public base_slice_t
126
+ class mut_slice_t : public base_slice_t
133
127
{
134
128
public:
135
- friend class const_slice_t <T>;
129
+ friend class slice_t <T>;
136
130
using iterator = SliceIterator<T>;
137
131
using const_iterator = SliceIterator<const T>;
138
132
139
- slice_t () = default ;
133
+ mut_slice_t () = default ;
140
134
141
- slice_t (T* data, int size, int i1, int i2, int m)
135
+ mut_slice_t (T* data, int size, int i1, int i2, int m)
142
136
: base_slice_t (size, i1, i2, m)
143
137
, _data{data} {
144
138
}
145
139
146
- slice_t (const slice_t & rhs)
140
+ mut_slice_t (const mut_slice_t & rhs)
147
141
: base_slice_t (rhs._n, rhs._i1, rhs._i2, rhs._m)
148
142
, _data{rhs._data } {
149
143
}
@@ -156,7 +150,7 @@ class slice_t : public base_slice_t
156
150
return _m;
157
151
}
158
152
159
- slice_t & operator =(const const_slice_t <T>& rhs) {
153
+ mut_slice_t & operator =(const slice_t <T>& rhs) {
160
154
DSPLIB_ASSERT (this ->size () == rhs.size (), " Slices size must be equal" );
161
155
const int count = this ->size ();
162
156
@@ -189,22 +183,23 @@ class slice_t : public base_slice_t
189
183
return *this ;
190
184
}
191
185
192
- slice_t & operator =(const slice_t <T>& rhs) {
193
- *this = const_slice_t <T>(rhs);
186
+ mut_slice_t & operator =(const mut_slice_t <T>& rhs) {
187
+ *this = slice_t <T>(rhs);
194
188
return *this ;
195
189
}
196
190
197
- slice_t & operator =(const base_array<T>& rhs) {
191
+ mut_slice_t & operator =(const base_array<T>& rhs) {
198
192
DSPLIB_ASSERT (!_is_same_array (rhs.slice (0 , rhs.size ())), " Assigned array to same slice" );
199
- return (*this = rhs.slice (0 , rhs.size ()));
193
+ *this = rhs.slice (0 , rhs.size ());
194
+ return *this ;
200
195
}
201
196
202
- slice_t & operator =(const T& value) {
197
+ mut_slice_t & operator =(const T& value) {
203
198
std::fill (begin (), end (), value);
204
199
return *this ;
205
200
}
206
201
207
- slice_t & operator =(const std::initializer_list<T>& rhs) {
202
+ mut_slice_t & operator =(const std::initializer_list<T>& rhs) {
208
203
std::copy (rhs.begin (), rhs.end (), this ->begin ());
209
204
return *this ;
210
205
}
@@ -234,7 +229,7 @@ class slice_t : public base_slice_t
234
229
}
235
230
236
231
protected:
237
- bool _is_same_array (const const_slice_t <T>& rhs) const noexcept {
232
+ bool _is_same_array (const slice_t <T>& rhs) const noexcept {
238
233
auto l1 = _data;
239
234
auto r1 = _data + _nc;
240
235
auto l2 = rhs._data ;
0 commit comments