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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! \file index.h
//! \author Benjamin Navarro
//! \brief Define the Index class and all its related operators
//! \date 12-2022

#pragma once

#include <pid/unreachable.h>

#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <stdexcept>

#ifndef PID_UTILS_INDEX_FORCE_CHECKS
#define PID_UTILS_INDEX_FORCE_CHECKS 0
#endif

#ifndef PID_UTILS_INDEX_CHECKS_THROW
#define PID_UTILS_INDEX_CHECKS_THROW 0
#endif

#if defined(NDEBUG) and PID_UTILS_INDEX_FORCE_CHECKS == 0
#define PID_UTILS_INDEX_NOEXCEPT noexcept
#define PID_UTILS_INDEX_ASSERT(expr, msg)
#else
#if PID_UTILS_INDEX_CHECKS_THROW == 1
#define PID_UTILS_INDEX_NOEXCEPT
#define PID_UTILS_INDEX_ASSERT(expr, msg)                                      \
    if (not(expr)) {                                                           \
        throw std::domain_error(msg);                                          \
    }
#else
#define PID_UTILS_INDEX_NOEXCEPT noexcept
#define PID_UTILS_INDEX_ASSERT(expr, msg) assert(expr&& msg)
#endif
#endif

namespace pid {

//! \brief Represent an index for a container that can be safely constructed
//! from/casted to any standard integer type. Relational and basic arithmetic
//! operators are provided to cover all the possible signedness/width
//! configurations.
//!
//! If an operation would result in a value not representable by an Index, an
//! error will be triggered. The error will take the form of an assert (default)
//! or of an exception (std::domain_error) depending on the value of
//! PID_UTILS_INDEX_CHECKS_THROW (default is 0, define to 1 to switch behavior).
//! Checks are performed only when NDEBUG is defined or if
//! PID_UTILS_INDEX_FORCE_CHECKS is defined to 1 (default is 0).
//!
//! The index value stored internally is an std::int64_t so that it can store
//! all the possible integer values, except the ones >= 2^63 which can be stored
//! in a std::uint64_t. These values should rarely, if ever, be used as indexes
//! and so shouldn't be missed by anyone. Using an std::uint64_t would prohibit
//! negative indexes, which are more commonly useful.
//!
struct Index {
    using type = std::int64_t;
    using utype = std::make_unsigned_t<type>;

    //! \brief Tell if the given value can be represented by an Index and so
    //! stored without changing its value
    template <typename T>
    [[nodiscard]] static constexpr bool
    is_representable([[maybe_unused]] T idx) PID_UTILS_INDEX_NOEXCEPT {
        static_assert(std::is_integral_v<T>);

        static_assert(sizeof(T) <= sizeof(type),
                      "Only standard integer types are supported");

        if constexpr (std::is_same_v<utype, T>) {
            return idx <= umax();
        }

        return true;
    }

    //! \brief Construct an index with a value of 0
    constexpr Index() noexcept = default;

    //! \brief Construct an Index from any integral value
    template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
    /* implicit */ constexpr Index(T idx) PID_UTILS_INDEX_NOEXCEPT
        : index{static_cast<type>(idx)} {
        PID_UTILS_INDEX_ASSERT(
            is_representable(idx),
            "The given value is too large to be held as an index");
    }

    //! \brief Convert the current index value to the given type
    template <typename T>
    [[nodiscard]] constexpr T as() const PID_UTILS_INDEX_NOEXCEPT {
        static_assert(std::is_integral_v<T>);

        static_assert(sizeof(T) <= sizeof(type),
                      "Only standard integer types are supported");

        if constexpr (std::is_unsigned_v<T>) {
            PID_UTILS_INDEX_ASSERT(
                index >= 0,
                "Cannot convert a negative index to an unsigned value");
        }

        if constexpr (std::is_same_v<T, utype>) {
            PID_UTILS_INDEX_ASSERT(static_cast<T>(index) <=
                                       std::numeric_limits<T>::max(),
                                   "Index is out of bound for the target type");
        } else {
            PID_UTILS_INDEX_ASSERT(
                index <= static_cast<type>(std::numeric_limits<T>::max()),
                "Index is out of bound for the target type");
            PID_UTILS_INDEX_ASSERT(
                index >= static_cast<type>(std::numeric_limits<T>::min()),
                "Index is out of bound for the target type");
        }

        return static_cast<T>(index);
    }

    //! \brief Conversion operator for integral types. Forward the call to
    //! cast()
    template <typename T, std::enable_if_t<std::is_integral_v<T> and
                                               not std::is_same_v<T, type>,
                                           int> = 0>
    [[nodiscard]] /* implicit */ constexpr
    operator T() const PID_UTILS_INDEX_NOEXCEPT {
        return as<T>();
    }

    //! \brief Conversion operator for integral types. Forward the call to
    //! cast()
    //!
    //! Keep this not non-templated so that compilers resolve it for C-style
    //! array indexing
    [[nodiscard]] /* implicit */ constexpr
    operator type() const PID_UTILS_INDEX_NOEXCEPT {
        return index;
    }

    [[nodiscard]] constexpr bool
    operator==(Index other) const PID_UTILS_INDEX_NOEXCEPT {
        return index == other.index;
    }

    [[nodiscard]] constexpr bool
    operator!=(Index other) const PID_UTILS_INDEX_NOEXCEPT {
        return index != other.index;
    }

    [[nodiscard]] constexpr bool
    operator<(Index other) const PID_UTILS_INDEX_NOEXCEPT {
        return index < other.index;
    }

    [[nodiscard]] constexpr bool
    operator<=(Index other) const PID_UTILS_INDEX_NOEXCEPT {
        return index <= other.index;
    }

    [[nodiscard]] constexpr bool
    operator>(Index other) const PID_UTILS_INDEX_NOEXCEPT {
        return index > other.index;
    }

    [[nodiscard]] constexpr bool
    operator>=(Index other) const PID_UTILS_INDEX_NOEXCEPT {
        return index >= other.index;
    }

    constexpr Index& operator++() PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(index != max(),
                               "Incrementing the index would overflow it (UB)");
        ++index;
        return *this;
    }

    constexpr Index operator++(int) PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(index != max(),
                               "Incrementing the index would overflow it (UB)");
        return Index{index++};
    }

    constexpr Index& operator--() PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(
            index != min(), "Decrementing the index would underflow it (UB)");
        --index;
        return *this;
    }

    constexpr Index operator--(int) PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(
            index != min(), "Decrementing the index would underflow it (UB)");
        return Index{index--};
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    [[nodiscard]] constexpr Index
    operator+(T other) const PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(
            (other.index >= 0 and (index <= max() - other.index)) or
                (other.index < 0 and (min() - other.index <= index)),
            "Performing the addition would overflow/underflow the index \
                   (UB)");
        return Index{index + other.index};
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    constexpr Index& operator+=(T other) PID_UTILS_INDEX_NOEXCEPT {
        *this = *this + other;
        return *this;
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    [[nodiscard]] constexpr Index
    operator-(T other) const PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(
            (other.index >= 0 and (min() + other.index) <= index) or
                (other.index < 0 and (index <= max() + other.index)),
            "Performing the subtraction would overflow/underflow the \
                   index (UB)");
        return Index{index - other.index};
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    constexpr Index& operator-=(T other) PID_UTILS_INDEX_NOEXCEPT {
        *this = *this - other;
        return *this;
    }

    [[nodiscard]] constexpr Index operator-() const PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(
            index != min(),
            "Negating the index minimum value would overflow it (UB)");
        return Index{-index};
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    [[nodiscard]] constexpr Index
    operator*(T other) const PID_UTILS_INDEX_NOEXCEPT {
        [[maybe_unused]] auto check = [](type a, type b) {
            if (b == 0) {
                return true;
            } else if (b == -1) {
                return a != min();
            } else {
                return (min() / b <= a) and (a <= max() / b);
            }
        };

        PID_UTILS_INDEX_ASSERT(
            check(index, other.index),
            "Performing the multiplication would overflow the index (UB)");

        return Index{index * other.index};
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    [[nodiscard]] constexpr Index&
    operator*=(T other) PID_UTILS_INDEX_NOEXCEPT {
        *this = *this * other;
        return *this;
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    [[nodiscard]] constexpr Index
    operator/(T other) const PID_UTILS_INDEX_NOEXCEPT {
        PID_UTILS_INDEX_ASSERT(other.index != 0,
                               "Trying to perform a division by zero (UB)");
        PID_UTILS_INDEX_ASSERT(
            not(index == min() and other.index == -1),
            "Performing the division would overflow the index (UB)");
        return Index{index / other.index};
    }

    template <typename T, std::enable_if_t<std::is_same_v<T, Index>, int> = 0>
    [[nodiscard]] constexpr Index&
    operator/=(T other) PID_UTILS_INDEX_NOEXCEPT {
        *this = *this / other;
        return *this;
    }

    [[nodiscard]] static constexpr type min() {
        return std::numeric_limits<type>::min();
    }

    [[nodiscard]] static constexpr type max() {
        return std::numeric_limits<type>::max();
    }

    [[nodiscard]] static constexpr utype umax() {
        return static_cast<utype>(std::numeric_limits<type>::max());
    }

    type index{};
};

//! \brief type alias pid::Index as pid::index for naming consistency with
//! integer types
using index = pid::Index;

namespace literals {
//! \brief User-defined literal to create a pid::Index
//!
//! ### Example
//! ```cpp
//! using namespace pid::literals;
//! auto idx = 42_index;
//! ```
constexpr pid::Index operator""_index(unsigned long long index) {
    return static_cast<pid::Index::type>(index);
}
} // namespace literals

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool operator==(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs == Index{rhs};
    } else {
        return false;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool
operator==(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    return rhs == lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool operator!=(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    return not(lhs == rhs);
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool
operator!=(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    return rhs != lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool operator<(const Index& lhs,
                                       const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs < Index{rhs};
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return true;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool
operator<(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(lhs)) {
        return Index{lhs} < rhs;
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return false;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool operator<=(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs <= Index{rhs};
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return true;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool
operator<=(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(lhs)) {
        return Index{lhs} <= rhs;
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return false;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool operator>(const Index& lhs,
                                       const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs > Index{rhs};
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return false;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool
operator>(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(lhs)) {
        return Index{lhs} > rhs;
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return false;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool operator>=(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs >= Index{rhs};
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return false;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr bool
operator>=(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(lhs)) {
        return Index{lhs} >= rhs;
    } else {
        // The only non-representable values are from an uint64_t and greater
        // than any possible index (msb set)
        return false;
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index operator+(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if constexpr (std::is_same_v<T, Index::type>) {
        return lhs + Index{rhs};
    } else {
        if (Index::is_representable(rhs)) {
            return lhs + Index{rhs};
        } else {
            // There is a chance that the result is itself representable by an
            // index if the lhs is negative (2^63 is not representable but -2^63
            // is)
            PID_UTILS_INDEX_ASSERT(
                lhs <= Index{0},
                "Performing the addition would overflow the index (UB)");
            const auto urhs = static_cast<Index::utype>(rhs);
            if (lhs.index == Index::min()) {
                // -min is not representable so cannot use std::abs but in this
                // case all possible rhs are valid
                const auto abs_lhs = Index::umax() + 1;
                return Index{urhs - abs_lhs};
            } else {
                const auto abs_lhs =
                    static_cast<Index::utype>(std::abs(lhs.index));
                const auto target_value = urhs - abs_lhs;
                PID_UTILS_INDEX_ASSERT(
                    target_value <= Index::umax(),
                    "Performing the addition would overflow the index (UB)");
                return Index{target_value};
            }
        }
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index
operator+(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    return rhs + lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr Index& operator+=(Index& lhs, const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs + rhs;
    return lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr T& operator+=(T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs + rhs;
    return lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index operator-(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs - Index{rhs};
    } else {
        // There is a chance that the result is itself representable by an index
        // if the lhs is positive
        PID_UTILS_INDEX_ASSERT(
            lhs >= Index{0},
            "Performing the subtraction would underflow the index (UB)");
        const auto target_value = static_cast<Index::utype>(rhs) -
                                  static_cast<Index::utype>(lhs.index);
        PID_UTILS_INDEX_ASSERT(
            target_value <= Index::umax(),
            "Performing the subtraction would underflow the index (UB)");
        return Index{-static_cast<Index::type>(target_value)};
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index
operator-(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    return -rhs + lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr Index& operator-=(Index& lhs, const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs - rhs;
    return lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr T& operator-=(T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs - rhs;
    return lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index operator*(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs * Index{rhs};
    } else {
        PID_UTILS_INDEX_ASSERT(
            lhs == 0 or (lhs == Index{0} and rhs == Index::umax()),
            "Performing the multiplication would underflow the index (UB)");
        if (lhs == 0) {
            return Index{0};
        } else {
            // lhs == Index{0} and rhs == Index::umax()
            return Index::min();
        }
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index
operator*(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    return rhs * lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr Index& operator*=(Index& lhs, const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs * rhs;
    return lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr T& operator*=(T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs * rhs;
    return lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index operator/(const Index& lhs,
                                        const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(rhs)) {
        return lhs / Index{rhs};
    } else {
        if (lhs == Index::min() and rhs == Index::umax()) {
            return Index{-1};
        } else {
            return Index{0};
        }
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
[[nodiscard]] constexpr Index
operator/(const T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    if (Index::is_representable(lhs)) {
        return Index{lhs} / rhs;
    } else {
        PID_UTILS_INDEX_ASSERT(
            false, "Division of a non-representable value is not implemented");
        pid::unreachable();
    }
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr Index& operator/=(Index& lhs, const T& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs / rhs;
    return lhs;
}

template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
constexpr T& operator/=(T& lhs, const Index& rhs) PID_UTILS_INDEX_NOEXCEPT {
    lhs = lhs / rhs;
    return lhs;
}

} // namespace pid