Skip to content

Commit 83a0119

Browse files
committed
Code refactoring
1 parent fb49d3f commit 83a0119

File tree

2 files changed

+34
-34
lines changed

2 files changed

+34
-34
lines changed

include/dsplib/slice.h

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -108,35 +108,7 @@ class mut_slice_t
108108
}
109109

110110
mut_slice_t& operator=(const slice_t<T>& rhs) {
111-
DSPLIB_ASSERT(this->size() == rhs.size(), "Slices size must be equal");
112-
const int count = this->size();
113-
114-
//empty slice assign
115-
if (count == 0) {
116-
return *this;
117-
}
118-
119-
//simple block copy/move (optimization)
120-
const bool is_same = is_same_memory(rhs, slice_t(*this));
121-
if ((this->stride() == 1) && (rhs.stride() == 1)) {
122-
const auto* src = &(*rhs.begin());
123-
auto* dst = &(*begin());
124-
if (!is_same) {
125-
std::memcpy(dst, src, count * sizeof(*src));
126-
} else {
127-
//overlapped
128-
std::memmove(dst, src, count * sizeof(*src));
129-
}
130-
return *this;
131-
}
132-
133-
//same array, specific indexing
134-
if (is_same) {
135-
*this = base_array<T>(rhs);
136-
return *this;
137-
}
138-
139-
std::copy(rhs.begin(), rhs.end(), this->begin());
111+
this->copy(*this, rhs);
140112
return *this;
141113
}
142114

@@ -218,6 +190,38 @@ class mut_slice_t
218190
return mut_slice_t(data + i1, step, count);
219191
}
220192

193+
static void copy(mut_slice_t<T> lhs, slice_t<T> rhs) {
194+
DSPLIB_ASSERT(lhs.size() == rhs.size(), "Slices size must be equal");
195+
const int count = lhs.size();
196+
197+
//empty slice assign
198+
if (count == 0) {
199+
return;
200+
}
201+
202+
//simple block copy/move (optimization)
203+
const bool is_same = is_same_memory(rhs, slice_t(lhs));
204+
if ((lhs.stride() == 1) && (rhs.stride() == 1)) {
205+
const auto* src = &(*rhs.begin());
206+
auto* dst = &(*lhs.begin());
207+
if (!is_same) {
208+
std::memcpy(dst, src, count * sizeof(*src));
209+
} else {
210+
//overlapped
211+
std::memmove(dst, src, count * sizeof(*src));
212+
}
213+
return;
214+
}
215+
216+
//same array, specific indexing
217+
if (is_same) {
218+
lhs = base_array<T>(rhs);
219+
return;
220+
}
221+
222+
std::copy(rhs.begin(), rhs.end(), lhs.begin());
223+
}
224+
221225
protected:
222226
explicit mut_slice_t(T* data, int stride, int count)
223227
: data_{data}

include/dsplib/span.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ class mut_span_t : public mut_slice_t<T>
6464
return this->data_[i];
6565
}
6666

67-
//TODO: override fast implementation for span
68-
6967
mut_span_t& operator=(const mut_span_t& rhs) {
7068
if (this == &rhs) {
7169
return *this;
@@ -74,14 +72,13 @@ class mut_span_t : public mut_slice_t<T>
7472
return *this;
7573
}
7674

75+
//TODO: override fast implementation for span
7776
mut_span_t& operator=(const slice_t<T>& rhs) {
78-
//TODO: check if slice ~ span (but check overlap memory)
7977
mut_slice_t<T>::operator=(rhs);
8078
return *this;
8179
}
8280

8381
mut_span_t& operator=(const mut_slice_t<T>& rhs) {
84-
//TODO: check if slice ~ span
8582
mut_slice_t<T>::operator=(rhs);
8683
return *this;
8784
}
@@ -216,7 +213,6 @@ mut_span_t<T> make_span(base_array<T>& x) noexcept {
216213
return mut_span_t<T>(x.data(), x.size());
217214
}
218215

219-
//TODO: short naming, span_r/span_c?
220216
using span_real = span_t<real_t>;
221217
using span_cmplx = span_t<cmplx_t>;
222218

0 commit comments

Comments
 (0)