@@ -11,92 +11,83 @@ namespace dsplib {
1111template <typename T>
1212class base_array ;
1313
14- // TODO: rename `slice_t` to `mut_slice_t`
1514template <typename T>
16- class slice_t ;
15+ class mut_slice_t ;
1716
18- // TODO: rename `const_slice_t` to `slice_t`
1917template <typename T>
20- class const_slice_t ;
18+ class slice_t ;
2119
22- // ----------------------------------------------------------------------------------------
23- // TODO: support empty slices
2420class base_slice_t
2521{
2622public:
2723 base_slice_t () = default ;
2824
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 ) {
3232 return ;
3333 }
3434
3535 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" );
6441 }
6542
6643 bool empty () const noexcept {
6744 return (_nc == 0 );
6845 }
6946
7047protected:
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+ }
7664};
7765
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+ */
8071template <typename T>
81- class const_slice_t : public base_slice_t
72+ class slice_t : public base_slice_t
8273{
8374public:
84- friend class slice_t <T>;
75+ friend class mut_slice_t <T>;
8576 using const_iterator = SliceIterator<const T>;
8677
87- const_slice_t () = default ;
78+ slice_t () = default ;
8879
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)
9081 : base_slice_t (size, i1, i2, m)
9182 , _data{data} {
9283 }
9384
94- const_slice_t (const const_slice_t & rhs)
85+ slice_t (const slice_t & rhs)
9586 : base_slice_t (rhs.size(), rhs._i1, rhs._i2, rhs._m)
9687 , _data{rhs._data } {
9788 }
9889
99- const_slice_t (const slice_t <T>& rhs)
90+ slice_t (const mut_slice_t <T>& rhs)
10091 : base_slice_t (rhs._n, rhs._i1, rhs._i2, rhs._m)
10192 , _data{rhs._data } {
10293 }
@@ -127,23 +118,26 @@ class const_slice_t : public base_slice_t
127118 const T* _data{nullptr };
128119};
129120
130- // ----------------------------------------------------------------------------------------
121+ /* *
122+ * @brief Mutable slice object
123+ * @tparam T real_t/cmplx_t
124+ */
131125template <typename T>
132- class slice_t : public base_slice_t
126+ class mut_slice_t : public base_slice_t
133127{
134128public:
135- friend class const_slice_t <T>;
129+ friend class slice_t <T>;
136130 using iterator = SliceIterator<T>;
137131 using const_iterator = SliceIterator<const T>;
138132
139- slice_t () = default ;
133+ mut_slice_t () = default ;
140134
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)
142136 : base_slice_t (size, i1, i2, m)
143137 , _data{data} {
144138 }
145139
146- slice_t (const slice_t & rhs)
140+ mut_slice_t (const mut_slice_t & rhs)
147141 : base_slice_t (rhs._n, rhs._i1, rhs._i2, rhs._m)
148142 , _data{rhs._data } {
149143 }
@@ -156,7 +150,7 @@ class slice_t : public base_slice_t
156150 return _m;
157151 }
158152
159- slice_t & operator =(const const_slice_t <T>& rhs) {
153+ mut_slice_t & operator =(const slice_t <T>& rhs) {
160154 DSPLIB_ASSERT (this ->size () == rhs.size (), " Slices size must be equal" );
161155 const int count = this ->size ();
162156
@@ -189,22 +183,23 @@ class slice_t : public base_slice_t
189183 return *this ;
190184 }
191185
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);
194188 return *this ;
195189 }
196190
197- slice_t & operator =(const base_array<T>& rhs) {
191+ mut_slice_t & operator =(const base_array<T>& rhs) {
198192 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 ;
200195 }
201196
202- slice_t & operator =(const T& value) {
197+ mut_slice_t & operator =(const T& value) {
203198 std::fill (begin (), end (), value);
204199 return *this ;
205200 }
206201
207- slice_t & operator =(const std::initializer_list<T>& rhs) {
202+ mut_slice_t & operator =(const std::initializer_list<T>& rhs) {
208203 std::copy (rhs.begin (), rhs.end (), this ->begin ());
209204 return *this ;
210205 }
@@ -234,7 +229,7 @@ class slice_t : public base_slice_t
234229 }
235230
236231protected:
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 {
238233 auto l1 = _data;
239234 auto r1 = _data + _nc;
240235 auto l2 = rhs._data ;
0 commit comments