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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Specifies the conversions between data types.
use super::{
    ComplexData, ComplexFreqVec, ComplexTimeVec, DataDomain, Domain, DspVec, FrequencyData,
    GenDspVec, MetaData, NumberSpace, RealData, RealFreqVec, RealOrComplexData, RealTimeVec,
    ResizeOps, TimeData, TimeOrFrequencyData, ToSlice, Vector,
};
use numbers::*;

/// This trait allows to change a data type. The operations will
/// convert a type to a different one and set `self.len()` to zero.
/// However `self.allocated_len()` will remain unchanged. The use case for this
/// is to allow to reuse the memory of a vector for different operations.
///
/// If a type should always be converted without any checks then the `RededicateForceOps`
/// trait provides option for that.
pub trait RededicateOps<Other>: RededicateForceOps<Other>
where
    Other: MetaData,
{
    /// Make `Other` a `Self`.
    /// # Example
    ///
    /// ```
    /// use basic_dsp_vector::*;
    /// let complex = vec!(1.0, 2.0, 3.0, 4.0).to_complex_freq_vec();
    /// let real = complex.phase();
    /// let complex = ComplexTimeVec32::rededicate_from(real);
    /// assert_eq!(true, complex.is_complex());
    /// assert_eq!(DataDomain::Time, complex.domain());
    /// assert_eq!(0, complex.len());
    /// assert_eq!(4, complex.alloc_len());
    /// ```
    fn rededicate_from(origin: Other) -> Self;
}

/// This trait allows to change a data type and performs the Conversion
/// without any checks. `RededicateOps` provides the same functionality
/// but performs runtime checks to avoid that data is interpreted the wrong
/// way.
///
/// In almost all cases this trait shouldn't be used directly.
pub trait RededicateForceOps<Other> {
    /// Make `Other` a `Self` without performing any checks.
    fn rededicate_from_force(origin: Other) -> Self;

    /// Make `Other` a `Self` without performing any checks.
    ///
    /// Try to set the domain and number space. There is no guarantee
    /// that this will succeed, since some rededication targets only
    /// support one domain and number space value. Failures will
    /// be silenty ignored (which is by design).
    fn rededicate_with_runtime_data(origin: Other, is_complex: bool, domain: DataDomain) -> Self;
}

/// This trait allows to change a data type. The operations will
/// convert a type to a different one and set `self.len()` to zero.
/// However `self.allocated_len()` will remain unchanged. The use case for this
/// is to allow to reuse the memory of a vector for different operations.
pub trait RededicateToOps<Other>
where
    Other: MetaData,
{
    /// Converts `Self` inot `Other`.
    fn rededicate(self) -> Other;
}

/// Specifies what the the result is if a type is transformed to real numbers.
pub trait ToRealResult {
    type RealResult;
}

/// Specifies what the the result is if a type is transformed to complex numbers.
pub trait ToComplexResult {
    type ComplexResult;
}

/// Specifies what the the result is if a type is transformed to time domain.
pub trait ToTimeResult {
    /// Specifies what the the result is if a type is transformed to time domain.
    type TimeResult;
}

/// Specifies what the the result is if a type is transformed to frequency domain.
pub trait ToFreqResult {
    type FreqResult;
}

/// Specifies what the the result is if a type is transformed to real numbers in time domain.
pub trait ToRealTimeResult {
    type RealTimeResult;
}

impl<S, T> ToRealResult for ComplexTimeVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type RealResult = RealTimeVec<S, T>;
}

impl<S, T> ToRealResult for ComplexFreqVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type RealResult = RealFreqVec<S, T>;
}

impl<S, T> ToRealResult for GenDspVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type RealResult = GenDspVec<S, T>;
}

impl<S, T> ToComplexResult for RealTimeVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type ComplexResult = ComplexTimeVec<S, T>;
}

impl<S, T> ToComplexResult for RealFreqVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type ComplexResult = ComplexFreqVec<S, T>;
}

impl<S, T> ToComplexResult for GenDspVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type ComplexResult = GenDspVec<S, T>;
}

impl<S, T> ToTimeResult for ComplexFreqVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type TimeResult = ComplexTimeVec<S, T>;
}

impl<S, T> ToTimeResult for GenDspVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type TimeResult = GenDspVec<S, T>;
}

impl<S, T> ToFreqResult for RealTimeVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type FreqResult = ComplexFreqVec<S, T>;
}

impl<S, T> ToFreqResult for ComplexTimeVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type FreqResult = ComplexFreqVec<S, T>;
}

impl<S, T> ToFreqResult for GenDspVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type FreqResult = GenDspVec<S, T>;
}

impl<S, T> ToRealTimeResult for ComplexFreqVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type RealTimeResult = RealTimeVec<S, T>;
}

impl<S, T> ToRealTimeResult for GenDspVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
{
    type RealTimeResult = GenDspVec<S, T>;
}

impl<S, T, N, D> RededicateForceOps<DspVec<S, T, N, D>> for RealTimeVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
    N: NumberSpace,
    D: Domain,
{
    fn rededicate_from_force(origin: DspVec<S, T, N, D>) -> Self {
        RealTimeVec {
            data: origin.data,
            delta: origin.delta,
            domain: TimeData,
            number_space: RealData,
            valid_len: origin.valid_len,
            multicore_settings: origin.multicore_settings,
        }
    }

    fn rededicate_with_runtime_data(origin: DspVec<S, T, N, D>, _: bool, _: DataDomain) -> Self {
        Self::rededicate_from_force(origin)
    }
}

impl<S, T, N, D> RededicateForceOps<DspVec<S, T, N, D>> for RealFreqVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
    N: NumberSpace,
    D: Domain,
{
    fn rededicate_from_force(origin: DspVec<S, T, N, D>) -> Self {
        RealFreqVec {
            data: origin.data,
            delta: origin.delta,
            domain: FrequencyData,
            number_space: RealData,
            valid_len: origin.valid_len,
            multicore_settings: origin.multicore_settings,
        }
    }

    fn rededicate_with_runtime_data(origin: DspVec<S, T, N, D>, _: bool, _: DataDomain) -> Self {
        Self::rededicate_from_force(origin)
    }
}

impl<S, T, N, D> RededicateForceOps<DspVec<S, T, N, D>> for ComplexTimeVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
    N: NumberSpace,
    D: Domain,
{
    fn rededicate_from_force(origin: DspVec<S, T, N, D>) -> Self {
        ComplexTimeVec {
            data: origin.data,
            delta: origin.delta,
            domain: TimeData,
            number_space: ComplexData,
            valid_len: origin.valid_len,
            multicore_settings: origin.multicore_settings,
        }
    }

    fn rededicate_with_runtime_data(origin: DspVec<S, T, N, D>, _: bool, _: DataDomain) -> Self {
        Self::rededicate_from_force(origin)
    }
}

impl<S, T, N, D> RededicateForceOps<DspVec<S, T, N, D>> for ComplexFreqVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
    N: NumberSpace,
    D: Domain,
{
    fn rededicate_from_force(origin: DspVec<S, T, N, D>) -> Self {
        ComplexFreqVec {
            data: origin.data,
            delta: origin.delta,
            domain: FrequencyData,
            number_space: ComplexData,
            valid_len: origin.valid_len,
            multicore_settings: origin.multicore_settings,
        }
    }

    fn rededicate_with_runtime_data(origin: DspVec<S, T, N, D>, _: bool, _: DataDomain) -> Self {
        Self::rededicate_from_force(origin)
    }
}

impl<S, T, N, D> RededicateForceOps<DspVec<S, T, N, D>> for GenDspVec<S, T>
where
    S: ToSlice<T>,
    T: RealNumber,
    N: NumberSpace,
    D: Domain,
{
    fn rededicate_from_force(origin: DspVec<S, T, N, D>) -> Self {
        let domain = origin.domain();
        let is_complex = origin.is_complex();
        GenDspVec {
            data: origin.data,
            delta: origin.delta,
            domain: TimeOrFrequencyData {
                domain_current: domain,
            },
            number_space: RealOrComplexData {
                is_complex_current: is_complex,
            },
            valid_len: origin.valid_len,
            multicore_settings: origin.multicore_settings,
        }
    }

    fn rededicate_with_runtime_data(
        origin: DspVec<S, T, N, D>,
        is_complex: bool,
        domain: DataDomain,
    ) -> Self {
        let mut result = Self::rededicate_from_force(origin);
        result.number_space.is_complex_current = is_complex;
        result.domain.domain_current = domain;
        result
    }
}

impl<S, T, N, D, O> RededicateOps<O> for DspVec<S, T, N, D>
where
    S: ToSlice<T>,
    T: RealNumber,
    DspVec<S, T, N, D>: RededicateForceOps<O>,
    N: NumberSpace,
    D: Domain,
    O: Vector<T>,
{
    fn rededicate_from(origin: O) -> Self {
        let is_complex = origin.is_complex();
        let domain = origin.domain();
        let mut result = Self::rededicate_from_force(origin);
        if result.is_complex() != is_complex && result.domain() != domain {
            result
                .resize(0)
                .expect("Setting size to 0 should always succeed");
        }
        result
    }
}

impl<S, T, N, D, O> RededicateToOps<O> for DspVec<S, T, N, D>
where
    S: ToSlice<T>,
    T: RealNumber,
    N: NumberSpace,
    D: Domain,
    O: Vector<T> + RededicateOps<Self>,
{
    fn rededicate(self) -> O {
        O::rededicate_from(self)
    }
}