open-vaf 0.4.2

A compiler frontend for VerilogA aimed predominently at compact modelling
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
/*

 * ******************************************************************************************
 *  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.
 *  OpenVAF, including this file, may be copied, modified, propagated, or
 *  distributed except according to the terms contained in the LICENSE file.
 * *****************************************************************************************
*/

// Provides a wrapper around fixed bit_set similar to index vec

use bitflags::_core::fmt::Binary;
use bitflags::_core::iter::FromIterator;
use bitflags::_core::marker::PhantomData;
use bitflags::_core::ops::{BitAndAssign, BitOr, BitXor, BitXorAssign, Index};
use fixedbitset::{FixedBitSet, IndexRange};
use index_vec::Idx;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::ops::{BitAnd, BitOrAssign};

//TODO switch to usize/u64
//BLOCKS upstream
type Block = u32;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct BitSet<I: Idx + From<usize>> {
    internal: FixedBitSet,
    tag: PhantomData<fn(&I)>,
}

impl<I: Idx + From<usize>> BitSet<I> {
    /// Create a new empty `BitSet` which can hold elements x < end_index
    pub fn new_empty(end_index: I) -> Self {
        Self {
            internal: FixedBitSet::with_capacity(end_index.index()),
            tag: PhantomData,
        }
    }

    /// Create a new empty `BitSet` which can hold elements x < end_index
    pub fn new_filled(end_index: I) -> Self {
        let mut res = Self {
            internal: FixedBitSet::with_capacity(end_index.index()),
            tag: PhantomData,
        };
        res.enable_all();
        res
    }

    /// Create a new `BitSet` with a specific number of bits,
    /// initialized from provided blocks.
    ///
    /// If the blocks are not the exact size needed for the capacity
    /// they will be padded with zeros (if shorter) or truncated to
    /// the capacity (if longer).
    pub fn with_capacity_and_blocks<Iter: IntoIterator<Item = Block>>(
        bits: I,
        blocks: Iter,
    ) -> Self {
        Self {
            internal: FixedBitSet::with_capacity_and_blocks(bits.index(), blocks),
            tag: PhantomData,
        }
    }

    /// Grow capacity to **bits**, all new bits initialized to zero
    pub fn grow(&mut self, len_idx: I) {
        self.internal.grow(len_idx.index())
    }

    /// Return the length of the `FixedBitSet` in bits.
    #[inline]
    pub fn len(&self) -> usize {
        self.internal.len()
    }

    /// Return the length of the `FixedBitSet` in bits.
    #[inline]
    pub fn len_idx(&self) -> I {
        self.internal.len().into()
    }
    /// Return the length of the `FixedBitSet` in bits.
    #[inline]
    pub fn max(&self) -> I {
        I::from_usize(self.internal.len())
    }

    /// Return **true** if the bit is enabled in the **FixedBitSet**,
    /// **false** otherwise.
    ///
    /// Note: bits outside the capacity are always disabled.
    ///
    /// Note: Also available with index syntax: `bitset[bit]`.
    #[inline]
    pub fn contains(&self, bit: I) -> bool {
        self.internal.contains(bit.index())
    }

    /// Clear all bits.
    #[inline]
    pub fn clear(&mut self) {
        self.internal.clear()
    }

    /// Enable `bit`.
    ///
    /// **Panics** if **bit** is out of bounds.
    #[inline]
    pub fn insert(&mut self, bit: I) {
        self.internal.insert(bit.index())
    }

    /// Disable `bit`.
    ///
    /// **Panics** if **bit** is out of bounds.
    #[inline]
    pub fn remove(&mut self, bit: I) -> bool {
        let prev = self.internal[bit.index()];
        self.internal.set(bit.index(), false);
        prev
    }

    /// Enable `bit`, and return its previous value.
    ///
    /// **Panics** if `bit` is out of bounds.
    ///
    #[inline]
    pub fn put(&mut self, bit: I) -> bool {
        self.internal.put(bit.index())
    }

    /// Toggle `bit` (inverting its state).
    ///
    /// `Panics` if `bit` is out of bounds
    #[inline]
    pub fn toggle(&mut self, bit: I) {
        self.internal.toggle(bit.index())
    }

    /// **Panics** if **bit** is out of bounds.
    #[inline]
    pub fn set(&mut self, bit: I, enabled: bool) {
        self.internal.set(bit.index(), enabled)
    }

    /// Copies boolean value from specified bit to the specified bit.
    ///
    /// **Panics** if **to** is out of bounds.
    #[inline]
    pub fn copy_bit(&mut self, from: I, to: I) {
        self.internal.copy_bit(from.index(), to.index())
    }

    /// Count the number of set bits in the given bit range.
    ///
    /// Use `..` to count the whole content of the bitset.
    ///
    /// **Panics** if the range extends past the end of the bitset.
    #[inline]
    pub fn count_ones<T: IndexRange<I>>(&self, range: T) -> usize {
        let start = range.start().map_or(0, |idx| idx.index());
        let end = range.end().map_or(self.internal.len(), |idx| idx.index());
        self.internal.count_ones(start..end)
    }

    /// Sets every bit in the given range to the given state (`enabled`)
    ///
    /// Use `..` to set the whole bitset.
    ///
    /// **Panics** if the range extends past the end of the bitset.
    #[inline]
    pub fn set_range<T: IndexRange<I>>(&mut self, range: T, enabled: bool) {
        let start = range.start().map_or(0, |idx| idx.index());
        let end = range.end().map_or(self.internal.len(), |idx| idx.index());
        self.internal.set_range(start..end, enabled)
    }

    /// Enables every bit in the given range.
    ///
    /// Use `..` to make the whole bitset ones.
    ///
    /// **Panics** if the range extends past the end of the bitset.
    #[inline]
    pub fn insert_range<T: IndexRange<I>>(&mut self, range: T) {
        let start = range.start().map_or(0, |idx| idx.index());
        let end = range.end().map_or(self.internal.len(), |idx| idx.index());
        self.internal.insert_range(start..end)
    }

    /// Equivalent to [`insert_range(..)`](insert_range) but significantly faster
    #[inline]
    pub fn enable_all(&mut self) {
        for block in self.as_mut_slice().iter_mut() {
            *block = Block::MAX
        }
    }

    /// Toggles (inverts) every bit in the given range.
    ///
    /// Use `..` to toggle the whole bitset.
    ///
    /// **Panics** if the range extends past the end of the bitset.
    #[inline]
    pub fn toggle_range<T: IndexRange<I>>(&mut self, range: T) {
        let start = range.start().map_or(0, |idx| idx.index());
        let end = range.end().map_or(self.internal.len(), |idx| idx.index());
        self.internal.toggle_range(start..end)
    }

    /// View the bitset as a slice of `u32` blocks
    #[inline]
    pub fn as_slice(&self) -> &[u32] {
        self.internal.as_slice()
    }

    /// View the bitset as a mutable slice of `u32` blocks. Writing past the bitlength in the last
    /// will cause `contains` to return potentially incorrect results for bits past the bitlength.
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [u32] {
        self.internal.as_mut_slice()
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.as_slice().iter().all(|block| *block == 0)
    }

    /// Iterates over all enabled bits.
    ///
    /// Iterator element is the index of the `1` bit, type `usize`.
    #[inline]
    pub fn ones(&self) -> Ones<'_, I> {
        Ones {
            iter: self.internal.ones(),
            tag: PhantomData,
        }
    }

    /// Returns a lazy iterator over the intersection of two `FixedBitSet`s
    pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, I> {
        Intersection {
            iter: self.internal.intersection(&other.internal),
            tag: PhantomData,
        }
    }

    /// Returns a lazy iterator over the union of two `FixedBitSet`s.
    pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, I> {
        Union {
            iter: self.internal.union(&other.internal),
            tag: PhantomData,
        }
    }

    /// Returns a lazy iterator over the difference of two `FixedBitSet`s. The difference of `a`
    /// and `b` is the elements of `a` which are not in `b`.
    pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, I> {
        Difference {
            iter: self.internal.difference(&other.internal),
            tag: PhantomData,
        }
    }

    /// Returns a lazy iterator over the symmetric difference of two `FixedBitSet`s.
    /// The symmetric difference of `a` and `b` is the elements of one, but not both, sets.
    pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, I> {
        SymmetricDifference {
            iter: self.internal.symmetric_difference(&other.internal),
            tag: PhantomData,
        }
    }

    /// In-place union of two `FixedBitSet`s.
    ///
    /// On calling this method, `self`'s capacity may be increased to match `other`'s.
    pub fn union_with(&mut self, other: &Self) {
        self.internal.union_with(&other.internal)
    }

    /// In-place intersection of two `FixedBitSet`s.
    ///
    /// On calling this method, `self`'s capacity will remain the same as before.
    pub fn intersect_with(&mut self, other: &Self) {
        self.internal.intersect_with(&other.internal)
    }

    /// In-place difference of two `FixedBitSet`s.
    ///
    /// On calling this method, `self`'s capacity will remain the same as before.
    pub fn difference_with(&mut self, other: &Self) {
        self.internal.difference_with(&other.internal)
    }

    /// In-place symmetric difference of two `FixedBitSet`s.
    ///
    /// On calling this method, `self`'s capacity may be increased to match `other`'s.
    pub fn symmetric_difference_with(&mut self, other: &Self) {
        self.internal.symmetric_difference_with(&other.internal)
    }

    /// Returns `true` if `self` has no elements in common with `other`. This
    /// is equivalent to checking for an empty intersection.
    pub fn is_disjoint(&self, other: &Self) -> bool {
        self.internal.is_disjoint(&other.internal)
    }

    /// Returns `true` if the set is a subset of another, i.e. `other` contains
    /// at least all the values in `self`.
    pub fn is_subset(&self, other: &Self) -> bool {
        self.internal.is_subset(&other.internal)
    }

    /// Returns `true` if the set is a superset of another, i.e. `self` contains
    /// at least all the values in `other`.
    pub fn is_superset(&self, other: &Self) -> bool {
        self.internal.is_superset(&other.internal)
    }
}

impl<I: Idx + From<usize>> Binary for BitSet<I> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        Binary::fmt(&self.internal, f)
    }
}

impl<I: Idx + From<usize> + Display> Display for BitSet<I> {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> {
        f.write_str("{")?;
        for idx in self.ones() {
            Display::fmt(&idx, f)?;
            f.write_str(",")?;
        }
        f.write_str("}")
    }
}

/// An iterator producing elements in the difference of two sets.
///
/// This struct is created by the [`FixedBitSet::difference`] method.
pub struct Difference<'a, I: Idx + From<usize>> {
    iter: fixedbitset::Difference<'a>,
    tag: PhantomData<fn(&I)>,
}

impl<'a, I: Idx + From<usize>> Iterator for Difference<'a, I> {
    type Item = I;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|idx| idx.into())
    }
}

/// An iterator producing elements in the symmetric difference of two sets.
///
/// This struct is created by the [`FixedBitSet::symmetric_difference`] method.
pub struct SymmetricDifference<'a, I: Idx + From<usize>> {
    iter: fixedbitset::SymmetricDifference<'a>,
    tag: PhantomData<fn(&I)>,
}

impl<'a, I: Idx + From<usize>> Iterator for SymmetricDifference<'a, I> {
    type Item = I;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|idx| idx.into())
    }
}

/// An iterator producing elements in the intersection of two sets.
///
/// This struct is created by the [`FixedBitSet::intersection`] method.
pub struct Intersection<'a, I: Idx + From<usize>> {
    iter: fixedbitset::Intersection<'a>,
    tag: PhantomData<fn(&I)>,
}

impl<'a, I: Idx + From<usize>> Iterator for Intersection<'a, I> {
    type Item = I; // the bit position of the '1'

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|idx| idx.into())
    }
}

/// An iterator producing elements in the union of two sets.
///
/// This struct is created by the [`FixedBitSet::union`] method.
pub struct Union<'a, I: Idx + From<usize>> {
    iter: fixedbitset::Union<'a>,
    tag: PhantomData<fn(&I)>,
}

impl<'a, I: Idx + From<usize>> Iterator for Union<'a, I> {
    type Item = I;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|idx| idx.into())
    }
}

/// An  iterator producing the indices of the set bit in a set.
///
/// This struct is created by the [`FixedBitSet::ones`] method.
pub struct Ones<'a, I: Idx + From<usize>> {
    iter: fixedbitset::Ones<'a>,
    tag: PhantomData<fn(&I)>,
}

impl<'a, I: Idx + From<usize>> Iterator for Ones<'a, I> {
    type Item = I; // the bit position of the '1'

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|idx| idx.into())
    }
}

impl<I: Idx + From<usize>> Clone for BitSet<I> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            internal: self.internal.clone(),
            tag: PhantomData,
        }
    }
}

/// Return **true** if the bit is enabled in the bitset,
/// or **false** otherwise.
///
/// Note: bits outside the capacity are always disabled, and thus
/// indexing a FixedBitSet will not panic.
impl<I: Idx + From<usize>> Index<I> for BitSet<I> {
    type Output = bool;

    #[inline]
    fn index(&self, bit: I) -> &bool {
        &self.internal[bit.index()]
    }
}

/// Sets the bit at index **i** to **true** for each item **i** in the input **src**.
impl<I: Idx + From<usize>> Extend<I> for BitSet<I> {
    fn extend<Iter: IntoIterator<Item = I>>(&mut self, src: Iter) {
        let src = src.into_iter().map(|id| id.index());
        self.internal.extend(src)
    }
}

/// Return a FixedBitSet containing bits set to **true** for every bit index in
/// the iterator, other bits are set to **false**.
impl<I: Idx + From<usize>> FromIterator<I> for BitSet<I> {
    fn from_iter<Iter: IntoIterator<Item = I>>(src: Iter) -> Self {
        let iter = src.into_iter();
        let mut res = BitSet::new_empty(iter.size_hint().0.into());
        res.extend(iter);
        res
    }
}

impl<'a, I: Idx + From<usize>> BitAnd for &'a BitSet<I> {
    type Output = BitSet<I>;
    fn bitand(self, other: &BitSet<I>) -> BitSet<I> {
        BitSet {
            internal: (&self.internal) & (&other.internal),
            tag: PhantomData,
        }
    }
}

impl<'a, I: Idx + From<usize>> BitAndAssign for BitSet<I> {
    fn bitand_assign(&mut self, other: Self) {
        self.internal &= other.internal
    }
}

impl<'a, I: Idx + From<usize>> BitOr for &'a BitSet<I> {
    type Output = BitSet<I>;
    fn bitor(self, other: &BitSet<I>) -> BitSet<I> {
        BitSet {
            internal: (&self.internal) | (&other.internal),
            tag: PhantomData,
        }
    }
}

impl<'a, I: Idx + From<usize>> BitOrAssign for BitSet<I> {
    fn bitor_assign(&mut self, other: Self) {
        self.internal |= other.internal
    }
}

impl<'a, I: Idx + From<usize>> BitXor for &'a BitSet<I> {
    type Output = BitSet<I>;
    fn bitxor(self, other: &BitSet<I>) -> BitSet<I> {
        BitSet {
            internal: (&self.internal) ^ (&other.internal),
            tag: PhantomData,
        }
    }
}

impl<'a, I: Idx + From<usize>> BitXorAssign for BitSet<I> {
    fn bitxor_assign(&mut self, other: Self) {
        self.internal ^= other.internal
    }
}