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
/*
 * ******************************************************************************************
 * Copyright (c) 2019 Pascal Kuthe. This file is part of the OpenVAF project.
 * It is subject to the license terms in the LICENSE file found in the top-level directory
 *  of this distribution and at  https://gitlab.com/DSPOM/OpenVAF/blob/master/LICENSE.
 *  No part of OpenVAF, including this file, may be copied, modified, propagated, or
 *  distributed except according to the terms contained in the LICENSE file.
 * *****************************************************************************************
 */
use core::fmt::Display;
use core::num::NonZeroU16;
use std::fmt::{Debug, Formatter};
use std::sync::Mutex;

pub type Index = u32;
pub type IndexOffset = i64;
pub type Range = std::ops::Range<Index>;
pub type LineNumber = u16;

#[derive(Copy, Clone, Debug)]
enum SpanData {
    ///This Span is too large to fit its length or zero
    LargeSpan,
    Length(NonZeroU16),
}
lazy_static! {
    static ref INTERNER: Mutex<SpanInterner> = Mutex::new(SpanInterner::new());
}
struct SpanInterner {
    data: Vec<(u32, u32)>,
}
impl SpanInterner {
    fn new() -> Self {
        Self {
            data: Vec::with_capacity(64),
        }
    }
    fn add(&mut self, start: u32, end: u32) -> usize {
        let idx = self.data.len();
        self.data.push((start, end));
        idx
    }
    fn get(&self, idx: usize) -> (u32, u32) {
        unsafe { *self.data.get_unchecked(idx) }
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Span {
    start_or_idx: Index,
    data: SpanData,
}
impl Span {
    #[inline]
    pub fn new(start: Index, end: Index) -> Self {
        Self::new_with_length(start, end - start)
    }

    const U16_MAX_U32: u32 = std::u16::MAX as u32;
    #[inline]
    pub fn new_with_length(start: Index, len: Index) -> Self {
        match len {
            0 if start < (1 << 31) => Self {
                start_or_idx: (start << 1) | 1,
                data: SpanData::LargeSpan,
            },
            len @ 1..=Self::U16_MAX_U32 => Self {
                start_or_idx: start,
                data: SpanData::Length(unsafe { NonZeroU16::new_unchecked(len as u16) }),
            },
            len => {
                debug_assert!(len == 0 || len > std::u16::MAX as u32);
                Self {
                    start_or_idx: (INTERNER.lock().unwrap().add(start, start + len) as u32) << 1,
                    data: SpanData::LargeSpan,
                }
            }
        }
    }

    #[inline]
    pub const fn new_short_span(start: Index, len: NonZeroU16) -> Self {
        Self {
            start_or_idx: start,
            data: SpanData::Length(len),
        }
    }
    #[inline]
    pub fn new_empty_span(start: Index) -> Self {
        if start < 1 << 31 {
            Self {
                start_or_idx: (start << 1) | 1,
                data: SpanData::LargeSpan,
            }
        } else {
            Self {
                start_or_idx: (INTERNER.lock().unwrap().add(start, start) as u32) << 1,
                data: SpanData::LargeSpan,
            }
        }
    }
    #[inline]
    pub const fn new_short_empty_span(start: u16) -> Self {
        Self {
            start_or_idx: ((start as u32) << 1) | 1,
            data: SpanData::LargeSpan,
        }
    }

    #[inline]
    pub fn get_start(self) -> Index {
        match self.data {
            SpanData::Length(_) => self.start_or_idx,
            SpanData::LargeSpan if self.start_or_idx & 1 == 1 => self.start_or_idx >> 1,
            SpanData::LargeSpan => {
                INTERNER
                    .lock()
                    .unwrap()
                    .get((self.start_or_idx >> 1) as usize)
                    .0
            }
        }
    }

    #[inline]
    pub fn get_end(self) -> Index {
        match self.data {
            SpanData::Length(length) => self.start_or_idx + (length.get() as u32),
            SpanData::LargeSpan if self.start_or_idx & 1 == 1 => self.start_or_idx >> 1,
            SpanData::LargeSpan => {
                INTERNER
                    .lock()
                    .unwrap()
                    .get((self.start_or_idx >> 1) as usize)
                    .1
            }
        }
    }

    pub fn get_len(self) -> Index {
        match self.data {
            SpanData::Length(length) => length.get() as Index,
            SpanData::LargeSpan if self.start_or_idx & 1 == 1 => 0,
            SpanData::LargeSpan => {
                let (start, end) = INTERNER
                    .lock()
                    .unwrap()
                    .get((self.start_or_idx >> 1) as usize);
                end - start
            }
        }
    }
    pub fn offset(mut self, offset: Index) -> Self {
        match self.data {
            SpanData::Length(_) => self.start_or_idx += offset,
            SpanData::LargeSpan if (self.start_or_idx & 1) == 1 => {
                let new_start = (self.start_or_idx >> 1) + offset;
                if new_start + offset <= (1 << 31) - 1 {
                    self.start_or_idx = new_start
                } else {
                    self.start_or_idx =
                        (INTERNER.lock().unwrap().add(new_start, new_start) as u32) << 1
                }
            }
            SpanData::LargeSpan => {
                let mut inter = INTERNER.lock().unwrap();
                let (start, end) = inter.get((self.start_or_idx >> 1) as usize);
                self.start_or_idx = inter.add(start + offset, end + offset) as u32;
            }
        }
        self
    }
    pub fn signed_offset(self, offset: IndexOffset) -> Self {
        if offset < 0 {
            self.negative_offset(-offset as Index)
        } else {
            self.offset(offset as Index)
        }
    }
    pub fn negative_offset(mut self, offset: Index) -> Self {
        match self.data {
            SpanData::LargeSpan if self.start_or_idx & 1 == 1 => {
                let new_start = (self.start_or_idx >> 1) - offset;
                if new_start + offset <= (1 << 31) - 1 {
                    self.start_or_idx = new_start
                } else {
                    self.start_or_idx =
                        (INTERNER.lock().unwrap().add(new_start, new_start) as u32) << 1
                }
            }
            SpanData::LargeSpan => {
                let mut inter = INTERNER.lock().unwrap();
                let (start, end) = inter.get((self.start_or_idx >> 1) as usize);
                self.start_or_idx = inter.add(start - offset, end - offset) as u32;
            }
            SpanData::Length(_) => self.start_or_idx -= offset,
        }
        self
    }
    pub fn extend(self, to: Self) -> Self {
        if to.get_end() > self.get_start() {
            Self::new(self.get_start(), to.get_end())
        } else {
            self
        }
    }
}
impl From<Range> for Span {
    fn from(range: Range) -> Self {
        Self::new(range.start, range.end)
    }
}
impl Into<Range> for Span {
    fn into(self) -> Range {
        Range {
            start: self.get_start(),
            end: self.get_end(),
        }
    }
}
impl Into<std::ops::Range<usize>> for Span {
    fn into(self) -> std::ops::Range<usize> {
        std::ops::Range {
            start: self.get_start() as usize,
            end: self.get_end() as usize,
        }
    }
}
impl Display for Span {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!(" [{},{}]", self.get_start(), self.get_end()))
    }
}
impl Eq for Span {}
impl PartialEq for Span {
    fn eq(&self, other: &Self) -> bool {
        self.get_start() == other.get_start() && self.get_end() == other.get_end()
    }
}