oxidd-core 0.11.0

Core traits and types of the OxiDD decision diagram framework
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
//! Efficient bi-directional mapping between variables and names

use std::collections::{hash_map::Entry, HashMap};
use std::fmt;
use std::iter::FusedIterator;
use std::ops::Range;

use crate::error::DuplicateVarName;
use crate::VarNo;

mod unowned {
    use std::{fmt, ptr::NonNull};

    /// A reference like `&'static T`, but (unsafely) convertible to a [`Box`].
    /// One may also think of it as an unowned [`Box`], or an [`Rc`] without a
    /// counter, i.e., external reasoning about the number of references and
    /// manual dropping.
    ///
    /// In context of the `VarNameMap` implementation, we would like to store
    /// the `str` content only once. We know that there are exactly two
    /// references for each stored name, so there is no need for a reference
    /// counter. However, we cannot use [`Box::leak`], and turn the resulting
    /// `&mut T` into a `&T` without loosing the ability to drop the allocation
    /// later on. This is because Rust's proposed aliasing model forbids turning
    /// a `&T` into a `&mut T` again (which would happen in the [`Drop`]
    /// implementation).
    // Type invariant: An `Unowned<T>` is generally
    // [convertible to a reference](std::ptr#pointer-to-reference-conversion).
    #[derive(Eq)]
    pub struct Unowned<T: ?Sized>(NonNull<T>);

    // SAFETY: `Unowned<T>` behaves like `&T`, and `&T` is `Send` iff `T` is
    // `Sync`
    unsafe impl<T: ?Sized + Sync> Send for Unowned<T> {}
    // SAFETY: `Unowned<T>` behaves like `&T`, and `&T` is `Sync` iff `T` is
    // `Sync`
    unsafe impl<T: ?Sized + Sync> Sync for Unowned<T> {}

    impl<T: ?Sized> Clone for Unowned<T> {
        #[inline(always)]
        fn clone(&self) -> Self {
            *self
        }
    }
    impl<T: ?Sized> Copy for Unowned<T> {}

    impl<T: ?Sized> std::ops::Deref for Unowned<T> {
        type Target = T;

        #[inline(always)]
        fn deref(&self) -> &T {
            // SAFETY: follows from the type invariant
            unsafe { self.0.as_ref() }
        }
    }
    impl<T: ?Sized> std::borrow::Borrow<T> for Unowned<T> {
        #[inline(always)]
        fn borrow(&self) -> &T {
            self
        }
    }

    impl<T: ?Sized + PartialEq> PartialEq for Unowned<T> {
        #[inline(always)]
        fn eq(&self, other: &Self) -> bool {
            **self == **other
        }
    }
    impl<T: ?Sized + std::hash::Hash> std::hash::Hash for Unowned<T> {
        #[inline(always)]
        fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
            (**self).hash(state);
        }
    }

    impl<T: ?Sized> From<Box<T>> for Unowned<T> {
        #[inline(always)]
        fn from(value: Box<T>) -> Self {
            Self(NonNull::from(Box::leak(value)))
        }
    }
    impl<T: ?Sized> From<&'static T> for Unowned<T> {
        #[inline(always)]
        fn from(value: &'static T) -> Self {
            Self(NonNull::from(value))
        }
    }
    impl<T: ?Sized> Unowned<T> {
        /// Convert an `Unowned<T>` back to a `Box<T>`
        ///
        /// # Safety
        ///
        /// 1. `this` must have been created from a [`Box`]
        /// 2. `this` must be the only existing reference
        /// 3. If `T` is not `Send`, then `into_box` must be called from the
        ///    thread that created the value of type `T`.
        #[inline(always)]
        pub unsafe fn into_box(this: Self) -> Box<T> {
            // SAFETY: ensured by caller, see above
            unsafe { Box::from_raw(this.0.as_ptr()) }
        }
    }

    impl<T: ?Sized + fmt::Debug> fmt::Debug for Unowned<T> {
        #[inline(always)]
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            (**self).fmt(f)
        }
    }
    impl<T: ?Sized + fmt::Display> fmt::Display for Unowned<T> {
        #[inline(always)]
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            (**self).fmt(f)
        }
    }
}
use unowned::Unowned;

/// Bi-directional mapping between variables and names, intended for the
/// implementation of a [`Manager`][crate::Manager].
///
/// The map requires variable names to be unique, but allows unnamed variables
/// (represented by empty strings).
// Type invariant: All `Unowned<str>` values in `index` are created from
// `Box<str>`. Unless the map is borrowed, there each `Unowned<str>` reference
// has an aliasing copy in `names`, and no other copies elsewhere. All
// `Unowned<str>` values in `names` are either a copy of a value in `index` or
// are the result of `Unowned::from("")`.
#[derive(Clone)]
pub struct VarNameMap {
    names: Vec<Unowned<str>>,
    index: HashMap<Unowned<str>, VarNo>,
}

impl Drop for VarNameMap {
    fn drop(&mut self) {
        self.names.clear();
        for (s, _) in self.index.drain() {
            // SAFETY:
            // 1. By the type invariant, `s` has been created from a `Box<str>`
            // 2. Since `self.names` is cleared now, `s` is the only reference
            // 3. `str` is `Send`
            drop(unsafe { Unowned::into_box(s) });
        }
    }
}

impl fmt::Debug for VarNameMap {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map()
            .entries(self.names.iter().enumerate().filter(|(_, s)| !s.is_empty()))
            .finish()?;
        write!(f, " (length: {})", self.len())
    }
}

impl Default for VarNameMap {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl VarNameMap {
    /// Create an empty map
    #[inline]
    pub fn new() -> Self {
        Self {
            names: Vec::new(),
            index: HashMap::new(),
        }
    }

    /// Get the number of variables (including unnamed ones)
    #[inline(always)]
    pub fn len(&self) -> VarNo {
        self.names.len() as VarNo
    }

    /// `true` if and only if [`self.len()`][Self::len()] is 0
    #[inline(always)]
    pub fn is_empty(&self) -> bool {
        self.names.is_empty()
    }

    /// Reserve space for `additional` entries
    ///
    /// Adding the next `additional` entries will not cause an allocation in the
    /// underlying vector and map.
    #[inline]
    pub fn reserve(&mut self, additional: VarNo) {
        let additional = additional.try_into().unwrap_or(usize::MAX);
        self.names.reserve(additional);
        self.index.reserve(additional);
    }

    /// Get the number of named variables
    #[inline(always)]
    pub fn named_count(&self) -> VarNo {
        self.index.len() as VarNo
    }

    /// Add `additional` unnamed variables
    ///
    /// Panics if [`self.len()`][Self::len()] plus `additional` is greater than
    /// to [`VarNo::MAX`].
    #[inline]
    pub fn add_unnamed(&mut self, additional: VarNo) {
        let msg = "too many variables";
        let new_len = self.len().checked_add(additional).expect(msg);
        self.names.resize(new_len.try_into().expect(msg), "".into());
    }

    /// Add fresh variables with names from `names`
    ///
    /// Returns `Ok(range)` on success, where `range` contains the new variable
    /// numbers. If `names` is too long (there would be more than `VarNo::MAX`
    /// variables), the remaining elements are not consumed.
    ///
    /// If a variable name is not unique, this method returns a
    /// [`DuplicateVarName`] error.
    #[track_caller]
    pub fn add_named<T: IntoIterator<Item = S>, S: Into<String>>(
        &mut self,
        names: T,
    ) -> Result<Range<VarNo>, DuplicateVarName> {
        let len_pre = self.names.len() as VarNo;
        let it = names.into_iter();
        let size_hint = it.size_hint().0;
        self.index.reserve(size_hint);
        self.names.reserve(size_hint);

        for (name, v) in it.zip(len_pre..VarNo::MAX) {
            let name: String = name.into();
            if name.is_empty() {
                self.names.push("".into());
                continue;
            }
            let name = name.into_boxed_str().into();
            match self.index.entry(name) {
                Entry::Occupied(entry) => {
                    let present_var = *entry.get();
                    // SAFETY:
                    // 1. `name` has been created from a `Box<str>` above
                    // 2. `name` was not added to the map, so it is the only reference
                    // 3. `str` is `Send`
                    let name: Box<str> = unsafe { Unowned::into_box(name) };
                    return Err(DuplicateVarName {
                        name: name.into_string(),
                        present_var,
                        added_vars: len_pre..self.names.len() as VarNo,
                    });
                }
                Entry::Vacant(entry) => entry.insert(v),
            };
            self.names.push(name);
        }

        Ok(len_pre..self.names.len() as VarNo)
    }

    /// Get the variable number for the given name if present, or add a new
    /// variable
    ///
    /// Returns a pair `(var_no, found)`. If the provided variable name is
    /// empty, this method will always create a fresh variable.
    ///
    /// If a variable with the given name is not present yet, and there is no
    /// variable free in range `0..VarNo::MAX`, then the variable is not added.
    /// Still, the return value is `VarNo::MAX`.
    pub fn get_or_add(&mut self, name: impl Into<String>) -> (VarNo, bool) {
        let name: String = name.into();
        if name.is_empty() {
            let n = self.names.len() as VarNo;
            self.names.push("".into());
            return (n, false);
        }

        let name = name.into_boxed_str().into();
        match self.index.entry(name) {
            Entry::Occupied(entry) => {
                // SAFETY:
                // 1. `name` has been created from a `Box<str>` above
                // 2. `name` was not added to the map, so it is the only reference
                // 3. `str` is `Send`
                drop(unsafe { Unowned::into_box(name) });
                (*entry.get(), true)
            }
            Entry::Vacant(entry) => {
                let n = self.names.len() as VarNo;
                if n == VarNo::MAX {
                    // SAFETY: as above
                    drop(unsafe { Unowned::into_box(name) });
                    return (n, false);
                }
                entry.insert(n);
                self.names.push(name);
                (n, false)
            }
        }
    }

    /// Get the variable number for the given name, if present
    ///
    /// `self.name_to_var("")` always returns `None`.
    #[inline]
    pub fn name_to_var(&self, name: impl AsRef<str>) -> Option<VarNo> {
        self.index.get(name.as_ref()).copied()
    }

    /// Get `var`'s name
    ///
    /// For unnamed vars, this will return the empty string.
    ///
    /// Panics if `var` is greater or equal to [`self.len()`][Self::len()].
    #[inline(always)]
    #[track_caller]
    pub fn var_name(&self, var: VarNo) -> &str {
        &self.names[var as usize]
    }

    /// Label `var` as `name`
    ///
    /// An empty name means that the variable will become unnamed, and cannot be
    /// retrieved via [`Self::name_to_var()`] anymore.
    ///
    /// Returns `Err((name, other_var))` if `name` is not unique (and not `""`).
    ///
    /// Panics if `var` is greater or equal to the number of variables in this
    /// map.
    #[track_caller]
    pub fn set_var_name(
        &mut self,
        var: VarNo,
        name: impl Into<String>,
    ) -> Result<(), DuplicateVarName> {
        let name: String = name.into();
        if name.is_empty() {
            self.index
                .remove(&std::mem::replace(&mut self.names[var as usize], "".into()));
            return Ok(());
        }
        let name = name.into_boxed_str().into();
        match self.index.entry(name) {
            Entry::Occupied(entry) => {
                // SAFETY:
                // 1. `name` has been created from a `Box<str>` above
                // 2. `name` was not added to the map, so it is the only reference
                // 3. `str` is `Send`
                let name = unsafe { Unowned::into_box(name) };
                let present_var = *entry.get();
                if present_var != var {
                    let len = self.len() as VarNo;
                    return Err(DuplicateVarName {
                        name: name.into_string(),
                        present_var,
                        added_vars: len..len,
                    });
                }
            }
            Entry::Vacant(entry) => {
                let prev = std::mem::replace(&mut self.names[var as usize], name);
                entry.insert(var);
                if !prev.is_empty() {
                    // SAFETY:
                    // 1. By the type invariant and since `prev` is not empty, it `prev` has been
                    //    created from a `Box<str>`
                    // 2. `prev` was removed from `self.names`, and its copy in `index` has been
                    //    dropped since `entry.insert(var)`. By the type invariant, it follows that
                    //    `prev` is the only reference.
                    // 3. `str` is `Send`
                    drop(unsafe { Unowned::into_box(prev) });
                }
            }
        };
        Ok(())
    }

    /// Iterate over the variable names (including all unnamed variables)
    pub fn into_names_iter(mut self) -> IntoNamesIter {
        self.index.clear();
        IntoNamesIter(std::mem::take(&mut self.names).into_iter())
    }
}

/// Owning iterator over variable names
// Type invariant: All non-empty `Unowned<str>` values are created from
// `Box<str>`. There are no aliasing copies of these values.
#[derive(Debug)]
pub struct IntoNamesIter(std::vec::IntoIter<Unowned<str>>);

/// Convert an [`Unowned<str>`] back to a [`String`]
///
/// # Safety
///
/// If `s` is non-empty, then the SAFETY conditions for [`Unowned::into_box()`]
/// must be fulfilled.
#[inline(always)]
unsafe fn unowned_to_string(s: Unowned<str>) -> String {
    if s.is_empty() {
        String::new()
    } else {
        // SAFETY: upheld by caller
        unsafe { Unowned::into_box(s) }.into_string()
    }
}

impl Drop for IntoNamesIter {
    fn drop(&mut self) {
        for &name in self.0.as_slice() {
            // SAFETY follows from type invariant
            drop(unsafe { unowned_to_string(name) });
        }
    }
}

impl Iterator for IntoNamesIter {
    type Item = String;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let name = self.0.next()?;
        // SAFETY follows from type invariant
        Some(unsafe { unowned_to_string(name) })
    }

    #[inline]
    fn nth(&mut self, n: usize) -> Option<Self::Item> {
        let name = self.0.nth(n)?;
        // SAFETY follows from type invariant
        Some(unsafe { unowned_to_string(name) })
    }
}

impl ExactSizeIterator for IntoNamesIter {
    #[inline]
    fn len(&self) -> usize {
        self.0.len()
    }
}
impl FusedIterator for IntoNamesIter where std::vec::IntoIter<Unowned<str>>: FusedIterator {}

impl DoubleEndedIterator for IntoNamesIter {
    #[inline]
    fn next_back(&mut self) -> Option<Self::Item> {
        let name = self.0.next_back()?;
        // SAFETY follows from type invariant
        Some(unsafe { unowned_to_string(name) })
    }

    #[inline]
    fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
        let name = self.0.nth_back(n)?;
        // SAFETY follows from type invariant
        Some(unsafe { unowned_to_string(name) })
    }
}

// TODO: replace String by Box<str>?