hexga_math 0.0.11-beta.53

Math related crate that define number and casting, and support array programming...
Documentation
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use super::*;

macro_rules! impl_new_unit_or_number {
    ($name:ident) => {
        impl<T> std::iter::Sum for $name<T>
        where
            T: std::ops::Add<T, Output = T> + Zero,
        {
            fn sum<I: Iterator<Item = Self>>(iter: I) -> Self { iter.fold(Self::ZERO, Self::add) }
        }

        impl<T, T2> CastFrom<$name<T2>> for $name<T>
        where
            T: CastFrom<T2>,
        {
            fn cast_from(value: $name<T2>) -> Self { $name(T::cast_from(value.0)) }
        }

        impl<T> $crate::unit::WrappedType<T> for $name<T>
        {
            unsafe fn inner_value(self) -> T { self.0 }

            unsafe fn from_inner_value(inner_value: T) -> Self { Self(inner_value) }
        }

        impl<T, I> RangeSampleExtension<I> for Range<$name<T>>
        where
            Range<T>: RangeSampleExtension<I, Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <Range<T> as RangeSampleExtension<I>>::Output,
            >;
            type Item = $name<T>;

            fn sample(self, nb_sample: I) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(<Range<T> as RangeSampleExtension<I>>::sample(
                    unsafe { self.start.inner_value() }..unsafe { self.end.inner_value() },
                    nb_sample,
                ))
            }
        }
        impl<T, I> RangeSampleExtension<I> for RangeInclusive<$name<T>>
        where
            RangeInclusive<T>: RangeSampleExtension<I, Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeInclusive<T> as RangeSampleExtension<I>>::Output,
            >;
            type Item = $name<T>;

            fn sample(self, nb_sample: I) -> Self::Output
            {
                let (start, end) = self.into_inner();
                $crate::unit::WrappedIterator::new(
                    <RangeInclusive<T> as RangeSampleExtension<I>>::sample(
                        unsafe { start.inner_value() }..=unsafe { end.inner_value() },
                        nb_sample,
                    ),
                )
            }
        }
        impl<T, I> RangeSampleExtension<I> for RangeTo<$name<T>>
        where
            RangeTo<T>: RangeSampleExtension<I, Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeTo<T> as RangeSampleExtension<I>>::Output,
            >;
            type Item = $name<T>;

            fn sample(self, nb_sample: I) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(<RangeTo<T> as RangeSampleExtension<I>>::sample(
                    ..unsafe { self.end.inner_value() },
                    nb_sample,
                ))
            }
        }
        impl<T, I> RangeSampleExtension<I> for RangeToInclusive<$name<T>>
        where
            RangeToInclusive<T>: RangeSampleExtension<I, Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeToInclusive<T> as RangeSampleExtension<I>>::Output,
            >;
            type Item = $name<T>;

            fn sample(self, nb_sample: I) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(<RangeToInclusive<T> as RangeSampleExtension<
                    I,
                >>::sample(
                    ..=unsafe { self.end.inner_value() },
                    nb_sample,
                ))
            }
        }
        impl<T, I> RangeSampleExtension<I> for RangeFrom<$name<T>>
        where
            RangeFrom<T>: RangeSampleExtension<I, Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeFrom<T> as RangeSampleExtension<I>>::Output,
            >;
            type Item = $name<T>;

            fn sample(self, nb_sample: I) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(
                    <RangeFrom<T> as RangeSampleExtension<I>>::sample(
                        unsafe { self.start.inner_value() }..,
                        nb_sample,
                    ),
                )
            }
        }

        impl<T> RangeStepExtension for Range<$name<T>>
        where
            Range<T>: RangeStepExtension<Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <Range<T> as RangeStepExtension>::Output,
            >;
            type Item = $name<T>;

            fn step(self, step: Self::Item) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(<Range<T> as RangeStepExtension>::step(
                    unsafe { self.start.inner_value() }..unsafe { self.end.inner_value() },
                    unsafe { step.inner_value() },
                ))
            }
        }
        impl<T> RangeStepExtension for RangeInclusive<$name<T>>
        where
            RangeInclusive<T>: RangeStepExtension<Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeInclusive<T> as RangeStepExtension>::Output,
            >;
            type Item = $name<T>;

            fn step(self, step: Self::Item) -> Self::Output
            {
                let (start, end) = self.into_inner();
                $crate::unit::WrappedIterator::new(<RangeInclusive<T> as RangeStepExtension>::step(
                    unsafe { start.inner_value() }..=unsafe { end.inner_value() },
                    unsafe { step.inner_value() },
                ))
            }
        }
        impl<T> RangeStepExtension for RangeTo<$name<T>>
        where
            RangeTo<T>: RangeStepExtension<Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeTo<T> as RangeStepExtension>::Output,
            >;
            type Item = $name<T>;

            fn step(self, step: Self::Item) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(<RangeTo<T> as RangeStepExtension>::step(
                    ..unsafe { self.end.inner_value() },
                    unsafe { step.inner_value() },
                ))
            }
        }
        impl<T> RangeStepExtension for RangeToInclusive<$name<T>>
        where
            RangeToInclusive<T>: RangeStepExtension<Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeToInclusive<T> as RangeStepExtension>::Output,
            >;
            type Item = $name<T>;

            fn step(self, step: Self::Item) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(
                    <RangeToInclusive<T> as RangeStepExtension>::step(
                        ..=unsafe { self.end.inner_value() },
                        unsafe { step.inner_value() },
                    ),
                )
            }
        }
        impl<T> RangeStepExtension for RangeFrom<$name<T>>
        where
            RangeFrom<T>: RangeStepExtension<Item = T>,
        {
            type Output = $crate::unit::WrappedIterator<
                $name<T>,
                T,
                <RangeFrom<T> as RangeStepExtension>::Output,
            >;
            type Item = $name<T>;

            fn step(self, step: Self::Item) -> Self::Output
            {
                $crate::unit::WrappedIterator::new(<RangeFrom<T> as RangeStepExtension>::step(
                    unsafe { self.start.inner_value() }..,
                    unsafe { step.inner_value() },
                ))
            }
        }
    };
}
pub(crate) use impl_new_unit_or_number;

macro_rules! new_unit
{
    ($(#[$attr:meta])* $name:ident) => {
        $(#[$attr])*
        #[derive(Clone, Copy, Default, PartialEq, Eq, Ord, PartialOrd, Hash)]
        pub struct $name<T>(pub(crate) T);

        impl<T> $name<T>
        {
            /// Return the inner value.
            /// Unsafe because it expose the inner value, but the unit is not specified and may change
            pub unsafe fn inner_value(self) -> T { self.0 }

            /// Create from the inner value.
            /// Unsafe because it expose the inner value, but the unit is not specified and may change
            pub const unsafe fn from_inner_value(inner_value : T) -> Self { Self(inner_value) }
        }

        map_on_operator_binary_arithmetic_unit!(
            (($trait_name: tt, $fn_name: tt)) =>
            {
                impl<T> std::ops::$trait_name<Self> for $name<T> where T: std::ops::$trait_name<T,Output=T>
                {
                    type Output = Self;
                    fn $fn_name(self, rhs : Self) -> Self::Output { Self(self.0.$fn_name(rhs.0)) }
                }
            }
        );

        map_on_operator_assign_arithmetic_unit!(
            (($trait_name: tt, $fn_name: tt)) =>
            {
                impl<T> std::ops::$trait_name<Self> for $name<T> where T: std::ops::$trait_name<T>
                {
                    fn $fn_name(&mut self, rhs : Self) { self.0.$fn_name(rhs.0); }
                }
            }
        );

        impl<T> std::ops::Mul<T> for $name<T> where T: std::ops::Mul<T,Output=T>
        {
            type Output = Self;
            fn mul(self, rhs : T) -> Self::Output { Self(self.0.mul(rhs)) }
        }
        impl<T> std::ops::MulAssign<T> for $name<T> where T: std::ops::MulAssign<T>
        {
            fn mul_assign(&mut self, rhs : T) { self.0.mul_assign(rhs); }
        }

        impl<T> std::ops::Div<T> for $name<T> where T: std::ops::Div<T,Output=T>
        {
            type Output = Self;
            fn div(self, rhs : T) -> Self::Output { Self(self.0.div(rhs)) }
        }
        impl<T> std::ops::DivAssign<T> for $name<T> where T: std::ops::DivAssign<T>
        {
            fn div_assign(&mut self, rhs : T) { self.0.div_assign(rhs); }
        }

        impl<T> std::ops::Rem<Self> for $name<T> where T: std::ops::Rem<T,Output=T>
        {
            type Output = Self;
            fn rem(self, rhs : Self) -> Self::Output { Self(self.0.rem(rhs.0)) }
        }
        impl<T> std::ops::RemAssign<Self> for $name<T> where T: std::ops::RemAssign<T>
        {
            fn rem_assign(&mut self, rhs : Self) { self.0.rem_assign(rhs.0); }
        }

        map_on_constant_unit!
        (
            (($trait_name: tt, $constant_name: tt)) =>
            {
                impl<T> $trait_name for $name<T> where T: $trait_name { const $constant_name : Self = Self(T::$constant_name); }
            }
        );

        map_on_operator_unary_arithmetic_unit!
        (
            (($trait_name: tt, $fn_name: tt)) =>
            {
                impl<T> $trait_name for $name<T> where T: $trait_name<Output = T>
                {
                    type Output = $name<T>;
                    fn $fn_name(self) -> Self { Self(self.0.$fn_name()) }
                }
            }
        );

        impl_new_unit_or_number!($name);
    };
}
pub(crate) use new_unit;

#[allow(unused_macros)]
macro_rules! new_number
{
    ($(#[$attr:meta])* $name:ident) => {
        $(#[$attr])*
        #[derive(PartialEq, Eq, Ord, PartialOrd, Hash)]
        pub struct $name<T>(pub T);

        map_on_operator_binary!(
            (($trait_name: tt, $fn_name: tt)) =>
            {
                impl<T> std::ops::$trait_name<Self> for $name<T> where T: std::ops::$trait_name<T,Output=T>
                {
                    type Output = Self;
                    fn $fn_name(self, rhs : Self) -> Self::Output { Self(self.0.$fn_name(rhs.0)) }
                }
            }
        );

        map_on_operator_assign!(
            (($trait_name: tt, $fn_name: tt)) =>
            {
                impl<T> std::ops::$trait_name<Self> for $name<T> where T: std::ops::$trait_name<T>
                {
                    fn $fn_name(&mut self, rhs : Self) { self.0.$fn_name(rhs.0); }
                }
            }
        );

        map_on_constant!
        (
            (($trait_name: tt, $constant_name: tt)) =>
            {
                impl<T> $trait_name for $name<T> where T: $trait_name { const $constant_name : Self = Self(T::$constant_name); }
            }
        );

        impl<T> Product for $name<T> where T: std::ops::Mul<T,Output=T> + One
        {
            fn product<I: Iterator<Item = Self>>(iter: I) -> Self {
                iter.fold(Self::ONE, Self::mul)
            }
        }

        map_on_operator_unary!
        (
            (($trait_name: tt, $fn_name: tt)) =>
            {
                impl<T> $trait_name for $name<T> where T: $trait_name<Output = T>
                {
                    type Output = $name<T>;
                    fn $fn_name(self) -> Self { Self(self.0.$fn_name()) }
                }
            }
        );

        impl_new_unit_or_number!($name);
    };
}
pub(crate) use new_number;

// To construct basic wrapped type from their inner type
pub trait WrappedType<T>: Sized
{
    /// Return the inner value.
    /// Unsafe because it expose the inner value, but the unit is not specified and may change
    unsafe fn inner_value(self) -> T;

    /// Create from the inner value.
    /// Unsafe because it expose the inner value, but the unit is not specified and may change
    unsafe fn from_inner_value(inner_value: T) -> Self;
}

pub struct WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
{
    pub it: It,
    phantom: PhantomData<(Wrapped, Precision)>,
}
impl<Wrapped, Precision, It> WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
{
    pub const fn new(it: It) -> Self
    {
        Self {
            it,
            phantom: PhantomData,
        }
    }
}

impl<Wrapped, Precision, It> Debug for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: Debug,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{:?}", &self.it) }
}

impl<Wrapped, Precision, It> Copy for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: Copy,
{
}
impl<Wrapped, Precision, It> Clone for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: Clone,
{
    fn clone(&self) -> Self
    {
        Self {
            it: self.it.clone(),
            phantom: PhantomData,
        }
    }
}

impl<Wrapped, Precision, It> PartialEq for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: PartialEq,
{
    fn eq(&self, other: &Self) -> bool { PartialEq::eq(&self, &other) }
}
impl<Wrapped, Precision, It> Eq for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: Eq,
{
}

impl<Wrapped, Precision, It> PartialOrd for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: PartialOrd,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering>
    {
        PartialOrd::partial_cmp(&self.it, &other.it)
    }
}
impl<Wrapped, Precision, It> Ord for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: Ord,
{
    fn cmp(&self, other: &Self) -> Ordering { Ord::cmp(&self.it, &other.it) }
}

impl<Wrapped, Precision, It> Hash for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: Hash,
{
    fn hash<H: Hasher>(&self, state: &mut H) { self.it.hash(state); }
}

impl<Wrapped, Precision, It> Iterator for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: Iterator,
{
    type Item = Wrapped;

    fn next(&mut self) -> Option<Self::Item>
    {
        self.it
            .next()
            .map(|v| unsafe { Wrapped::from_inner_value(v) })
    }
}

impl<Wrapped, Precision, It> DoubleEndedIterator for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: DoubleEndedIterator,
{
    fn next_back(&mut self) -> Option<Self::Item>
    {
        self.it
            .next_back()
            .map(|v| unsafe { Wrapped::from_inner_value(v) })
    }
}

impl<Wrapped, Precision, It> FusedIterator for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: FusedIterator,
{
}
impl<Wrapped, Precision, It> ExactSizeIterator for WrappedIterator<Wrapped, Precision, It>
where
    It: Iterator<Item = Precision>,
    Wrapped: WrappedType<Precision>,
    It: ExactSizeIterator,
{
}