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
#[macro_export]
macro_rules! uid_basic_operations_impl {
    ($name:ident) => {
        impl std::ops::Add<usize> for $name {
            type Output = $name;

            fn add(self, rhs: usize) -> Self::Output {
                Self(self.0 + rhs)
            }
        }

        impl std::ops::Sub<usize> for $name {
            type Output = $name;

            fn sub(self, rhs: usize) -> Self::Output {
                Self(self.0 - rhs)
            }
        }

        impl std::ops::Add<u32> for $name {
            type Output = $name;

            fn add(self, rhs: u32) -> Self::Output {
                self.add(rhs as usize)
            }
        }

        impl std::ops::Sub<u32> for $name {
            type Output = $name;

            fn sub(self, rhs: u32) -> Self::Output {
                self.sub(rhs as usize)
            }
        }

        impl std::ops::Add<i32> for $name {
            type Output = $name;

            fn add(self, rhs: i32) -> Self::Output {
                Self(((self.0 as i32) + rhs) as usize)
            }
        }

        impl std::ops::Sub<i32> for $name {
            type Output = $name;

            fn sub(self, rhs: i32) -> Self::Output {
                Self(((self.0 as i32) - rhs) as usize)
            }
        }

        impl std::ops::Add for $name {
            type Output = $name;

            fn add(self, rhs: $name) -> Self::Output {
                Self(self.0 + rhs.0)
            }
        }

        impl std::ops::AddAssign<usize> for $name {
            fn add_assign(&mut self, rhs: usize) {
                self.0 += rhs
            }
        }

        impl std::ops::Sub for $name {
            type Output = $name;

            fn sub(self, rhs: $name) -> Self::Output {
                Self(self.0 - rhs.0)
            }
        }

        impl std::ops::SubAssign<usize> for $name {
            fn sub_assign(&mut self, rhs: usize) {
                self.0 -= rhs
            }
        }

        impl std::ops::AddAssign for $name {
            fn add_assign(&mut self, rhs: $name) {
                self.0 += rhs.0
            }
        }

        impl std::ops::SubAssign for $name {
            fn sub_assign(&mut self, rhs: $name) {
                self.0 -= rhs.0
            }
        }

        impl $name {
            /// Successor.
            pub fn succ(mut self) -> Self {
                self.0 += 1;
                self
            }

            /// Predecessor.
            pub fn pred(self) -> Self {
                self.nat().unwrap()
            }

            /// Pattern matcher.
            pub fn nat(self) -> Option<Self> {
                if self.0 == 0 {
                    None
                } else {
                    Some(Self(self.0 - 1))
                }
            }
        }

        impl std::fmt::Display for $name {
            fn fmt(&self, f: &mut std::fmt::Formatter) -> std::result::Result<(), std::fmt::Error> {
                self.0.fmt(f)
            }
        }
    };
}

/// De Bruijn Indices. Checkout [Wikipedia](https://en.wikipedia.org/wiki/De_Bruijn_index) if you
/// are curious but have no idea about it.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Default)]
pub struct DBI(pub usize);
uid_basic_operations_impl!(DBI);

#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Default)]
pub struct UID(pub usize);
uid_basic_operations_impl!(UID);

/// Global reference indices.
#[derive(Debug, Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Default)]
pub struct GI(pub usize);
uid_basic_operations_impl!(GI);

/// Unique-ID generator internal counter.
static mut UID_COUNT: usize = 0;

/// Reset the unique-ID generator.
pub unsafe fn reset_uid_counter() {
    UID_COUNT = 0;
}

/// Unique-ID generation function.
pub unsafe fn next_uid() -> UID {
    let val = UID_COUNT;
    UID_COUNT += 1;
    UID(val)
}