oxidd_core/
lib.rs

1//! Collection of fundamental traits and types to represent decision diagrams
2//!
3//! # Overview
4//!
5//! One of the most central traits is [`Manager`]. The manager is responsible
6//! for storing the nodes of a decision diagram ([`InnerNode`]s and terminal
7//! nodes) and provides [`Edge`]s to identify them.
8//!
9//! From the user's perspective, [`Function`][function::Function] is very
10//! important. A function is some kind of external reference to a node and is
11//! the basis for assigning semantics to nodes and providing operations such as
12//! applying connectives of boolean logic.
13
14#![warn(missing_docs)]
15#![deny(unsafe_op_in_unsafe_fn)]
16#![allow(clippy::double_must_use)]
17// Explicitly writing out `'id` lifetimes possibly makes some code easier to
18// read.
19#![allow(clippy::needless_lifetimes)]
20// `match` syntax may be easier to read
21#![allow(clippy::manual_map)]
22
23use std::borrow::Borrow;
24use std::hash::Hash;
25use std::ops::Range;
26
27pub mod error;
28pub mod function;
29pub mod util;
30
31use error::DuplicateVarName;
32use util::{AllocResult, Borrowed, DropWith, NodeSet};
33
34/// Manager reference
35///
36/// The methods of this trait synchronize accesses to the manager: In a
37/// concurrent setting, a manager has some kind of read/write lock, and
38/// [`Self::with_manager_shared()`] / [`Self::with_manager_exclusive()`] acquire
39/// this lock accordingly. In a sequential implementation, a
40/// [`RefCell`][std::cell::RefCell] or the like may be used instead of lock.
41pub trait ManagerRef: Clone + Eq + Hash + for<'a, 'id> From<&'a Self::Manager<'id>> {
42    /// Type of the associated manager
43    ///
44    /// For more details on why this type is generic over `'id`, see the
45    /// documentation of [`Function::Manager`][function::Function::Manager].
46    type Manager<'id>: Manager;
47
48    /// Obtain a shared manager reference
49    ///
50    /// Locking behavior: acquires the manager's lock for shared access.
51    fn with_manager_shared<F, T>(&self, f: F) -> T
52    where
53        F: for<'id> FnOnce(&Self::Manager<'id>) -> T;
54
55    /// Obtain an exclusive manager reference
56    ///
57    /// Locking behavior: acquires the manager's lock for exclusive access.
58    fn with_manager_exclusive<F, T>(&self, f: F) -> T
59    where
60        F: for<'id> FnOnce(&mut Self::Manager<'id>) -> T;
61}
62
63/// Reduction rules for decision diagrams
64///
65/// This trait is intended to be implemented on a zero-sized type. Refer to
66/// [`DiagramRules::reduce()`] for an example implementation.
67pub trait DiagramRules<E: Edge, N: InnerNode<E>, T> {
68    /// Iterator created by [`DiagramRules::cofactors()`]
69    type Cofactors<'a>: Iterator<Item = Borrowed<'a, E>>
70    where
71        E: 'a,
72        N: 'a;
73
74    /// Apply the reduction rule(s)
75    ///
76    /// Besides uniqueness of nodes (there are no two nodes with the same
77    /// children at the same level), decision diagrams typically impose some
78    /// other reduction rules. The former is provided by the [`Manager`] /
79    /// [`LevelView`]s, the latter is implemented here.
80    ///
81    /// The implementation is responsible for consuming the entire `children`
82    /// iterator and dropping unused edges.
83    ///
84    /// # Example implementation
85    ///
86    /// In binary decision diagrams (BDDs), there are no nodes with equal
87    /// children. An implementation might look like this:
88    ///
89    /// ```
90    /// # use oxidd_core::{*, util::BorrowedEdgeIter};
91    /// struct BDDRules;
92    /// impl<E: Edge, N: InnerNode<E>, T> DiagramRules<E, N, T> for BDDRules {
93    ///     type Cofactors<'a> = N::ChildrenIter<'a> where N: 'a, E: 'a;
94    ///
95    ///     fn reduce<M: Manager<Edge = E, InnerNode = N, Terminal = T>>(
96    ///         manager: &M,
97    ///         level: LevelNo,
98    ///         children: impl IntoIterator<Item = E>,
99    ///     ) -> ReducedOrNew<E, N> {
100    ///         let mut it = children.into_iter();
101    ///         let f0 = it.next().unwrap();
102    ///         let f1 = it.next().unwrap();
103    ///         debug_assert!(it.next().is_none());
104    ///
105    ///         if f0 == f1 {
106    ///             manager.drop_edge(f1);
107    ///             ReducedOrNew::Reduced(f0)
108    ///         } else {
109    ///             ReducedOrNew::New(InnerNode::new(level, [f0, f1]), Default::default())
110    ///         }
111    ///     }
112    ///
113    ///     fn cofactors(_tag: E::Tag, node: &N) -> Self::Cofactors<'_> {
114    ///         node.children()
115    ///     }
116    /// }
117    /// ```
118    ///
119    /// Note that we assume no complemented edges, hence the cofactor iterator
120    /// is just `node.children()`.
121    ///
122    /// The implementation assumes that there are no panics, otherwise it would
123    /// leak some edges. It might be a bit better to use
124    /// [`EdgeDropGuard`][util::EdgeDropGuard]s, but this would make the code
125    /// more clumsy, and our assumption is usually fair enough.
126    fn reduce<M: Manager<Edge = E, InnerNode = N, Terminal = T>>(
127        manager: &M,
128        level: LevelNo,
129        children: impl IntoIterator<Item = E>,
130    ) -> ReducedOrNew<E, N>;
131
132    /// Get the cofactors of `node` assuming an incoming edge with `tag`
133    ///
134    /// In some diagram types, this is the same as [`InnerNode::children()`].
135    /// However, in a binary decision diagram with complement edges, we need to
136    /// respect the tag of the incoming edge: If the incoming edge is
137    /// complemented, then we need to complement the outgoing edges as well.
138    fn cofactors(tag: E::Tag, node: &N) -> Self::Cofactors<'_>;
139
140    /// Get the `n`-th cofactor of `node` assuming an incoming edge with `tag`
141    ///
142    /// This is equivalent to `Self::cofactors(tag, node).nth(n).unwrap()`.
143    #[inline]
144    fn cofactor(tag: E::Tag, node: &N, n: usize) -> Borrowed<'_, E> {
145        Self::cofactors(tag, node).nth(n).expect("out of range")
146    }
147}
148
149/// Result of the attempt to create a new node
150///
151/// Before actually creating a new node, reduction rules should be applied
152/// (see [`DiagramRules::reduce()`]). If a reduction was applied, then
153/// [`DiagramRules::reduce()`] returns the `Reduced` variant, otherwise the
154/// `New` variant.
155pub enum ReducedOrNew<E: Edge, N: InnerNode<E>> {
156    /// A reduction rule was applied
157    Reduced(E),
158    /// The node is new. After inserting it into the manager, the edge should be
159    /// tagged with the given tag.
160    New(N, E::Tag),
161}
162
163impl<E: Edge, N: InnerNode<E>> ReducedOrNew<E, N> {
164    /// Insert `self` into `manager` and `unique_table` at the given `level` if
165    /// it is `New`, otherwise return the `Reduced` edge.
166    ///
167    /// `level` must agree with the level used for creating the node, and must
168    /// be strictly above (i.e. less than) the children's levels.
169    #[must_use]
170    #[inline(always)]
171    pub fn then_insert<M>(self, manager: &M, level: LevelNo) -> AllocResult<E>
172    where
173        M: Manager<InnerNode = N, Edge = E>,
174    {
175        match self {
176            ReducedOrNew::Reduced(e) => Ok(e),
177            ReducedOrNew::New(node, tag) => {
178                debug_assert_ne!(level, LevelNo::MAX);
179                debug_assert!(node.check_level(|l| l == level));
180                debug_assert!(node.children().all(|c| {
181                    if let Node::Inner(node) = manager.get_node(&*c) {
182                        node.check_level(|l| level < l)
183                    } else {
184                        true
185                    }
186                }));
187
188                let edge = manager.level(level).get_or_insert(node)?;
189                Ok(edge.with_tag_owned(tag))
190            }
191        }
192    }
193}
194
195/// Node in a decision diagram
196///
197/// [`Eq`] and [`Hash`] should consider the children only, in particular no
198/// level information. This means that if `Self` implements [`HasLevel`],
199/// [`HasLevel::set_level()`] may be called while the node is present in a
200/// [`LevelView`]. This is not the case for [`InnerNode::set_child()`]: The user
201/// must remove the node from the [`LevelView`] before setting the children (and
202/// re-insert it afterwards).
203#[must_use]
204pub trait InnerNode<E: Edge>: Sized + Eq + Hash + DropWith<E> {
205    /// The node's arity (upper bound)
206    const ARITY: usize;
207
208    /// Iterator over children of an inner node
209    type ChildrenIter<'a>: ExactSizeIterator<Item = Borrowed<'a, E>>
210    where
211        Self: 'a,
212        E: 'a;
213
214    /// Create a new node
215    ///
216    /// Note that this does not apply any reduction rules. A node type that does
217    /// not store levels internally (does not implement [`HasLevel`]) may simply
218    /// ignore the `level` parameter.
219    ///
220    /// Panics if `children`'s length does not match the node's requirements
221    /// (typically, the length should be [`Self::ARITY`], but some node types
222    /// may deviate from that).
223    #[must_use]
224    fn new(level: LevelNo, children: impl IntoIterator<Item = E>) -> Self;
225
226    /// Returns the result of `check` applied to the node's level in case this
227    /// node type stores levels, otherwise returns `true`.
228    ///
229    /// Use [`HasLevel::level()`] if you require your nodes to store the level
230    /// number and want to get the level number.
231    fn check_level(&self, check: impl FnOnce(LevelNo) -> bool) -> bool;
232
233    /// Panics if the node types stores a level and the node's level is not
234    /// `level`
235    fn assert_level_matches(&self, level: LevelNo);
236
237    /// Get the children of this node as an iterator
238    #[must_use]
239    fn children(&self) -> Self::ChildrenIter<'_>;
240
241    /// Get the `n`-th child of this node
242    fn child(&self, n: usize) -> Borrowed<'_, E>;
243
244    /// Set the `n`-th child of this node
245    ///
246    /// Returns the previous `n`-th child.
247    ///
248    /// Note that this function may also change the node's hash value etc., so
249    /// in case the node is stored in a hash table ([`LevelView`]), it needs to
250    /// be removed before calling this method.
251    ///
252    /// Panics if the node does not have an `n`-th child.
253    ///
254    /// # Safety
255    ///
256    /// The caller must have exclusive access to the node. In the first place,
257    /// this is granted by acquiring an exclusive manager lock
258    /// ([`Function::with_manager_exclusive()`][function::Function::with_manager_exclusive] or
259    /// [`ManagerRef::with_manager_exclusive()`]). However, exclusive access to
260    /// some nodes may be delegated to other threads (which is the reason why we
261    /// only require a shared and not a mutable manager reference). Furthermore,
262    /// there must not be a borrowed child (obtained via
263    /// [`InnerNode::children()`]).
264    #[must_use = "call `Manager::drop_edge()` if you don't need the previous edge"]
265    unsafe fn set_child(&self, n: usize, child: E) -> E;
266
267    /// Get the node's reference count
268    ///
269    /// This ignores all internal references used by the manager
270    /// implementation.
271    #[must_use]
272    fn ref_count(&self) -> usize;
273}
274
275/// Level number type
276///
277/// Levels with lower numbers are located at the top of the diagram, higher
278/// numbers at the bottom. [`LevelNo::MAX`] is reserved for terminal nodes.
279/// Adjacent levels have adjacent level numbers.
280pub type LevelNo = u32;
281
282/// Atomic version of [`LevelNo`]
283pub type AtomicLevelNo = std::sync::atomic::AtomicU32;
284
285/// Variable number type
286pub type VarNo = LevelNo;
287
288/// Atomic version of [`VarNo`]
289pub type AtomicVarNo = AtomicLevelNo;
290
291/// Trait for nodes that have a level
292///
293/// Quasi-reduced BDDs, for instance, do not need the level information stored
294/// in their nodes, so there is no need to implement this trait.
295///
296/// Implementors should also implement [`InnerNode`]. If `Self` is [`Sync`],
297/// then the level number should be implemented using [`AtomicLevelNo`]. In
298/// particular, concurrent calls to [`Self::level()`] and [`Self::set_level()`]
299/// must not lead to data races.
300///
301/// # Safety
302///
303/// 1. A node in a [`LevelView`] with level number L has level number L (i.e.
304///    `self.level()` returns L).
305/// 2. [`InnerNode::check_level()`] with a check `c` must return
306///    `c(self.level())`. Similarly, [`InnerNode::assert_level_matches()`] must
307///    panic if the level does not match.
308///
309/// These conditions are crucial to enable concurrent level swaps as part of
310/// reordering (see the `oxidd-reorder` crate): The algorithm iterates over the
311/// nodes at the upper level and needs to know whether a node is part of the
312/// level directly below it. The procedure only has access to nodes at these two
313/// levels, hence it must rely on the level information for SAFETY.
314///
315/// Note that invariant 1 may be broken by [`HasLevel::set_level()`] and
316/// [`LevelView::swap()`]; the caller of these functions is responsible to
317/// re-establish the invariant.
318pub unsafe trait HasLevel {
319    /// Get the node's level
320    #[must_use]
321    fn level(&self) -> LevelNo;
322
323    /// Set the node's level
324    ///
325    /// # Safety
326    ///
327    /// This method may break SAFETY invariant 1 of the [`HasLevel`] trait: A
328    /// node in a [`LevelView`] with level number L has level number L (i.e.
329    /// `self.level()` returns L). The caller is responsible to re-establish the
330    /// invariant. (Make sure that the calling code is exception-safe!)
331    unsafe fn set_level(&self, level: LevelNo);
332}
333
334/// Node identifier returned by [`Edge::node_id()`]
335///
336/// The most significant bit is reserved, i.e. `NodeID`s must (normally) be less
337/// than `1 << (NodeID::BITS - 1)`
338pub type NodeID = usize;
339
340/// Edge in a decision diagram
341///
342/// Generally speaking, an edge is the directed connection between two nodes,
343/// with some kind of annotation. In a binary decision diagram with complement
344/// edges, there are the "true" edges as well as the normal and the complemented
345/// "false" edges. When considering a single edge, it usually is not so
346/// important whether this edge is a "true" or "false" edge; we can simply have
347/// distinguishable "slots" in the source node. In contrast, whether an edge is
348/// complemented or not has a much greater influence on the meaning of an edge.
349///
350/// In a decision diagram, obtaining the source of an edge is usually not so
351/// important, hence this trait does not provide such functionality. This means
352/// that an edge can (more or less) be implemented as a (tagged) pointer to the
353/// target node.
354///
355/// This trait requires implementors to also implement [`Ord`]. Edges should be
356/// considered equal if and only if they point to the same node with the same
357/// tag. Besides that, there are no further restrictions. The order implemented
358/// for [`Ord`] can be an arbitrary, fixed order (e.g. using addresses of the
359/// nodes). The main idea of this is to give the set `{f, g}` of two edges `f`
360/// and `g` a unique tuple/array representation.
361#[must_use]
362pub trait Edge: Sized + Ord + Hash {
363    /// Edge tag
364    ///
365    /// For instance, an edge tag can be used to mark an edge as complemented.
366    ///
367    /// If the decision diagram does not need any special edge tags, this can
368    /// simply be `()`.
369    type Tag: Tag;
370
371    /// Turn a reference into a borrowed handle
372    fn borrowed(&self) -> Borrowed<'_, Self>;
373    /// Get a version of this [`Edge`] with the given tag
374    ///
375    /// Refer to [`Borrowed::edge_with_tag()`] for cases in which this method
376    /// cannot be used due to lifetime restrictions.
377    fn with_tag(&self, tag: Self::Tag) -> Borrowed<'_, Self>;
378    /// Get a version of this [`Edge`] with the given tag
379    fn with_tag_owned(self, tag: Self::Tag) -> Self;
380
381    /// Get the [`Tag`] of this [`Edge`]
382    fn tag(&self) -> Self::Tag;
383
384    /// Returns some unique identifier for the node, e.g. for I/O purposes
385    fn node_id(&self) -> NodeID;
386}
387
388/// Trait for tags that can be attached to pointers (e.g. edges, see
389/// [`Edge::Tag`])
390///
391/// This trait is automatically implemented for types that implement [`Eq`],
392/// [`Default`], and [`Countable`].
393pub trait Tag: Sized + Copy + Eq + Default + Countable {}
394impl<T: Eq + Default + Countable> Tag for T {}
395
396/// Types whose values can be counted, i.e. there is a bijection between the
397/// values of the type and the range `0..=MAX_VALUE`.
398///
399/// This is mainly intended to be implemented on `enum`s. In most cases, you can
400/// simply derive it.
401///
402/// # Safety
403///
404/// [`Countable::as_usize()`] and [`Countable::from_usize()`] must form a
405/// bijection between the values of type `Self` and `0..=MAX_VALUE`, more
406/// formally: For all `t: Self` it must hold that
407/// `Self::from_usize(t.as_usize()) == t`.
408/// For all `u: usize` such that `t.as_usize() == u` for some `t: Self`,
409/// `Self::from_usize(u).as_usize() == u` must be true. Furthermore,
410/// `t.as_usize() <= Self::MAX_VALUE` must hold.
411///
412/// This trait is marked unsafe because violating any invariant of the above may
413/// e.g. result in out-of-bounds accesses.
414pub unsafe trait Countable: Sized + Copy {
415    /// Maximum value returned by `self.as_usize()`.
416    const MAX_VALUE: usize;
417
418    /// Convert `self` into a `usize`.
419    #[must_use]
420    fn as_usize(self) -> usize;
421    /// Convert the given `value` into an instance of `Self`.
422    ///
423    /// May panic if an invalid value is passed, or return some default value.
424    #[must_use]
425    fn from_usize(value: usize) -> Self;
426}
427
428// SAFETY: There is a bijection for all values of type `()` and `0..=MAX_VALUE`.
429unsafe impl Countable for () {
430    const MAX_VALUE: usize = 0;
431
432    #[inline]
433    fn as_usize(self) -> usize {
434        0
435    }
436
437    #[inline]
438    fn from_usize(_value: usize) -> Self {}
439}
440
441// SAFETY: There is a bijection for all values of type `bool` and `0..=1`.
442unsafe impl Countable for bool {
443    const MAX_VALUE: usize = 1;
444
445    #[inline]
446    fn as_usize(self) -> usize {
447        self as usize
448    }
449
450    #[inline]
451    fn from_usize(value: usize) -> Self {
452        value != 0
453    }
454}
455
456/// Either an inner or a terminal node
457pub enum Node<'a, M: Manager + 'a> {
458    #[allow(missing_docs)]
459    Inner(&'a M::InnerNode),
460    #[allow(missing_docs)]
461    Terminal(M::TerminalRef<'a>),
462}
463
464impl<'a, M: Manager + 'a> Clone for Node<'a, M> {
465    fn clone(&self) -> Self {
466        *self
467    }
468}
469impl<'a, M: Manager + 'a> Copy for Node<'a, M> {}
470
471impl<'a, M: Manager> Node<'a, M> {
472    /// Get the reference count of the underlying node
473    #[must_use]
474    #[inline]
475    pub fn ref_count(self) -> usize {
476        match self {
477            Node::Inner(node) => node.ref_count(),
478            Node::Terminal(_) => usize::MAX, /* TODO: should we support concrete reference counts
479                                              * for terminals? */
480        }
481    }
482}
483
484impl<'a, M: Manager> Node<'a, M>
485where
486    M::InnerNode: HasLevel,
487{
488    /// Get the level of the underlying node (`LevelNo::MAX` for terminals)
489    #[must_use]
490    #[inline]
491    pub fn level(self) -> LevelNo {
492        match self {
493            Node::Inner(node) => node.level(),
494            Node::Terminal(_) => LevelNo::MAX,
495        }
496    }
497}
498
499impl<'a, M: Manager> Node<'a, M> {
500    /// Unwrap the inner node
501    ///
502    /// Panics if `self` is a terminal.
503    #[must_use]
504    #[track_caller]
505    #[inline]
506    pub fn unwrap_inner(self) -> &'a M::InnerNode {
507        match self {
508            Node::Inner(node) => node,
509            Node::Terminal(_) => panic!("expected an inner node, but this is a terminal"),
510        }
511    }
512
513    /// Unwrap the inner node
514    ///
515    /// Panics with `msg` if `self` is a terminal.
516    #[must_use]
517    #[track_caller]
518    #[inline]
519    pub fn expect_inner(self, msg: &str) -> &'a M::InnerNode {
520        match self {
521            Node::Inner(node) => node,
522            Node::Terminal(_) => panic!("{}", msg),
523        }
524    }
525
526    /// Returns `true` if this is an inner node
527    #[inline]
528    pub fn is_inner(self) -> bool {
529        match self {
530            Node::Inner(_) => true,
531            Node::Terminal(_) => false,
532        }
533    }
534
535    /// Unwrap the terminal
536    ///
537    /// Panics if `self` is an inner node
538    #[must_use]
539    #[track_caller]
540    #[inline]
541    pub fn unwrap_terminal(&self) -> &M::Terminal {
542        match self {
543            Node::Inner(_) => panic!("expected a terminal node, but this is an inner node"),
544            Node::Terminal(ref t) => t.borrow(),
545        }
546    }
547
548    /// Unwrap the terminal
549    ///
550    /// Panics with `msg` if `self` is an inner node
551    #[must_use]
552    #[track_caller]
553    #[inline]
554    pub fn expect_terminal(&self, msg: &str) -> &M::Terminal {
555        match self {
556            Node::Inner(_) => panic!("{}", msg),
557            Node::Terminal(ref t) => t.borrow(),
558        }
559    }
560
561    /// Returns `true` if this is any terminal node
562    #[inline]
563    pub fn is_any_terminal(self) -> bool {
564        match self {
565            Node::Inner(_) => false,
566            Node::Terminal(_) => true,
567        }
568    }
569
570    /// Returns `true` if this is the given `terminal`
571    #[inline]
572    pub fn is_terminal(self, terminal: &M::Terminal) -> bool {
573        match self {
574            Node::Inner(_) => false,
575            Node::Terminal(t) => t.borrow() == terminal,
576        }
577    }
578}
579
580/// Manager for nodes in a decision diagram
581///
582/// In the basic formulation, a decision diagram is a directed acyclic graph
583/// where all inner nodes are associated with a level, and each level in turn is
584/// associated with a variable. A decision diagram can represent functions
585/// Dⁿ → T, where n is the number of variables. Every inner node has |D|
586/// outgoing edges, pointing to child nodes. The semantics of an inner node
587/// depends on the value of its associated variable. If the variable has
588/// value d ∈ D, then the node's semantics is the semantics of the child
589/// referenced by the edge corresponding to d. Every terminal node is associated
590/// with a value t ∈ T, and the node's semantics is just that value t. Some
591/// kinds of decision diagrams deviate from this formulation in one way or the
592/// other, but still fit into the framework.
593///
594/// The manager's responsibility is to store nodes and provide edges to identify
595/// them. Internally, it has a unique table to ensure uniqueness of nodes
596/// (typically, there should be no two nodes with the same children at the same
597/// level). The manager supports some kind of garbage collection: If there are
598/// no more edges pointing to a node, the node does not necessarily have to be
599/// deleted immediately. It may well be that the node revives. But from time to
600/// time it may make sense to remove all currently dead nodes. The manager also
601/// supports a levelized view on the diagram (via [`LevelView`]s).
602///
603/// Note that we are way more concerned about levels than about variables here.
604/// This is because variables can easily be handled externally. For many kinds
605/// of diagrams, we can just use the function representation of a variable and
606/// get the mapping between variables and levels "for free". This is especially
607/// nice as there are reordering operations which change the mapping between
608/// levels and variables but implicitly update the function representations
609/// accordingly.
610///
611/// # Safety
612///
613/// The implementation must ensure that every inner node only refers to nodes
614/// stored in this manager.
615///
616/// Every level view is associated with a manager and a level number.
617/// [`Manager::level()`] must always return the level view associated to this
618/// manager with the given level number.
619pub unsafe trait Manager: Sized {
620    /// Type of edge
621    type Edge: Edge<Tag = Self::EdgeTag>;
622    /// Type of edge tags
623    type EdgeTag: Tag;
624    /// Type of inner nodes
625    type InnerNode: InnerNode<Self::Edge>;
626    /// Type of terminals
627    type Terminal: Eq + Hash;
628    /// References to [`Self::Terminal`]s
629    ///
630    /// Should either be a `&'a Self::Terminal` or just `Self::Terminal`. The
631    /// latter is useful for "static terminal managers" which don't actually
632    /// store terminal nodes but can map between edges and terminal nodes on the
633    /// fly. In this case, it would be hard to hand out node references.
634    type TerminalRef<'a>: Borrow<Self::Terminal> + Copy
635    where
636        Self: 'a;
637    /// Diagram rules, see [`DiagramRules`] for more details
638    type Rules: DiagramRules<Self::Edge, Self::InnerNode, Self::Terminal>;
639
640    /// Iterator over all terminals
641    ///
642    /// The actual items are edges pointing to terminals since this allows us to
643    /// get a [`NodeID`].
644    type TerminalIterator<'a>: Iterator<Item = Self::Edge>
645    where
646        Self: 'a;
647
648    /// Node set type, possibly more efficient than a `HashSet<NodeID>`
649    type NodeSet: NodeSet<Self::Edge>;
650
651    /// A view on a single level of the unique table.
652    type LevelView<'a>: LevelView<Self::Edge, Self::InnerNode>
653    where
654        Self: 'a;
655
656    /// Iterator over levels
657    type LevelIterator<'a>: DoubleEndedIterator<Item = Self::LevelView<'a>> + ExactSizeIterator
658    where
659        Self: 'a;
660
661    /// Get a reference to the node to which `edge` points
662    #[must_use]
663    fn get_node(&self, edge: &Self::Edge) -> Node<'_, Self>;
664
665    /// Clone `edge`
666    #[must_use]
667    fn clone_edge(&self, edge: &Self::Edge) -> Self::Edge;
668
669    /// Drop `edge`
670    fn drop_edge(&self, edge: Self::Edge);
671    /// Drop `edge` and try to remove the node it points to
672    ///
673    /// `level` is the node's level. This is required because nodes do not
674    /// necessarily store their level, but the lookup in a unique table split by
675    /// levels needs the level.
676    ///
677    /// Returns whether the node has been removed. There are multiple reasons
678    /// why removing the node can fail. Obviously, it could still be referenced
679    /// by other edges. It might also be that removing nodes is currently not
680    /// possible, e.g., because the manager is not prepared for it. Also, if
681    /// `level` does not match the node's actual level, the node referenced by
682    /// `edge` may not be removed. In this case, the method might even remove
683    /// another node with the same children which is only referenced from the
684    /// unique table.
685    ///
686    /// Passing the wrong `level` is considered to be a programming mistake. To
687    /// aid debugging, the implementation is allowed (but not required) to panic
688    /// if it can diagnose such a mistake.
689    fn try_remove_node(&self, edge: Self::Edge, level: LevelNo) -> bool;
690
691    /// Get the count of inner nodes
692    #[must_use]
693    fn num_inner_nodes(&self) -> usize;
694
695    /// Get an approximate count of inner nodes
696    ///
697    /// For concurrent implementations, it may be much less costly to determine
698    /// an approximation of the inner node count than an accurate count
699    /// ([`Self::num_inner_nodes()`]).
700    #[must_use]
701    fn approx_num_inner_nodes(&self) -> usize {
702        self.num_inner_nodes()
703    }
704
705    /// Get the number of variables
706    ///
707    /// Same as [`Self::num_levels()`]
708    #[must_use]
709    #[inline(always)]
710    fn num_vars(&self) -> VarNo {
711        self.num_levels()
712    }
713
714    /// Get the number of levels
715    ///
716    /// Same as [`Self::num_vars()`]
717    #[must_use]
718    fn num_levels(&self) -> LevelNo;
719
720    /// Get the number of named variables
721    #[must_use]
722    fn num_named_vars(&self) -> VarNo;
723
724    /// Add `additional` unnamed variables to the decision diagram
725    ///
726    /// The new variables are added at the bottom of the variable order. More
727    /// precisely, the level number equals the variable number for each new
728    /// variable.
729    ///
730    /// Note that some algorithms may assume that the domain of a function
731    /// represented by a decision diagram is just the set of all variables. In
732    /// this regard, adding variables can change the semantics of decision
733    /// diagram nodes.
734    ///
735    /// Returns the range of new variable numbers.
736    ///
737    /// Panics if [`self.num_vars()`][Self::num_vars()] plus `additional` is
738    /// greater than to [`VarNo::MAX`].
739    fn add_vars(&mut self, additional: VarNo) -> Range<VarNo>;
740
741    /// Add named variables to the decision diagram
742    ///
743    /// This is a shorthand for [`Self::add_vars()`] and respective
744    /// [`Self::set_var_name()`] calls. More details can be found there.
745    ///
746    /// Returns the range of new variable numbers on success. In case a name
747    /// is not unique (and not `""`), only the first variables with unique names
748    /// are added, and a [`DuplicateVarName`] error provides more details.
749    ///
750    /// Panics if there would be more than [`VarNo::MAX`] variables after adding
751    /// the ones from `names`.
752    fn add_named_vars<S: Into<String>>(
753        &mut self,
754        names: impl IntoIterator<Item = S>,
755    ) -> Result<Range<VarNo>, DuplicateVarName>;
756
757    /// Add named variables to the decision diagram
758    ///
759    /// This is a possibly specialized version of [`Self::add_named_vars()`].
760    ///
761    /// Panics if there would be more than [`VarNo::MAX`] variables after adding
762    /// the ones from `names`.
763    fn add_named_vars_from_map(
764        &mut self,
765        map: crate::util::VarNameMap,
766    ) -> Result<Range<VarNo>, DuplicateVarName> {
767        self.add_named_vars(map.into_names_iter())
768    }
769
770    /// Get `var`'s name
771    ///
772    /// For unnamed variables, this will return the empty string.
773    ///
774    /// Panics if `var` is greater or equal to the number of variables in this
775    /// manager.
776    fn var_name(&self, var: VarNo) -> &str;
777
778    /// Label `var` as `name`
779    ///
780    /// An empty name means that the variable will become unnamed, and cannot be
781    /// retrieved via [`Self::name_to_var()`] anymore.
782    ///
783    /// Returns a [`DuplicateVarName`] error if `name` is not unique (and not
784    /// `""`).
785    ///
786    /// Panics if `var` is greater or equal to the number of variables in this
787    /// manager.
788    fn set_var_name(&mut self, var: VarNo, name: impl Into<String>)
789        -> Result<(), DuplicateVarName>;
790
791    /// Get the variable number for the given variable name, if present
792    ///
793    /// Note that you cannot retrieve unnamed variables.
794    /// `manager.name_to_var("")` always returns `None`.
795    fn name_to_var(&self, name: impl AsRef<str>) -> Option<VarNo>;
796
797    /// Get the level for the given variable
798    ///
799    /// Panics if `level >= self.num_vars()`.
800    fn var_to_level(&self, var: VarNo) -> LevelNo;
801
802    /// Get the variable for the given level
803    ///
804    /// Panics if `level >= self.num_levels()`.
805    fn level_to_var(&self, level: LevelNo) -> VarNo;
806
807    /// Get the level given by `no`
808    ///
809    /// Implementations may or may not acquire a lock here.
810    ///
811    /// Panics if `no >= self.num_levels()`.
812    #[must_use]
813    fn level(&self, no: LevelNo) -> Self::LevelView<'_>;
814
815    /// Iterate over the levels from top to bottom
816    #[must_use]
817    fn levels(&self) -> Self::LevelIterator<'_>;
818
819    /// Get an edge for the given terminal
820    ///
821    /// Locking behavior: May acquire a lock for the internal terminal unique
822    /// table. In particular, this means that calling this function while
823    /// holding a terminal iterator ([`Manager::terminals()`]) may cause a
824    /// deadlock.
825    #[must_use]
826    fn get_terminal(&self, terminal: Self::Terminal) -> AllocResult<Self::Edge>;
827
828    /// Get the number of terminals
829    ///
830    /// Should agree with the length of the iterator returned by
831    /// [`Manager::terminals()`].
832    ///
833    /// Locking behavior: May acquire a lock for the internal terminal unique
834    /// table. In particular, this means that calling this function while
835    /// holding a terminal iterator ([`Manager::terminals()`]) may cause a
836    /// deadlock.
837    #[must_use]
838    fn num_terminals(&self) -> usize;
839
840    /// Iterator over all terminals
841    ///
842    /// Locking behavior: May acquire a lock for the internal terminal unique
843    /// table.
844    #[must_use]
845    fn terminals(&self) -> Self::TerminalIterator<'_>;
846
847    /// Perform garbage collection
848    ///
849    /// This method looks for nodes that are neither referenced by a
850    /// [`Function`][function::Function] nor another node and removes them. The
851    /// method works from top to bottom, so if a node is only referenced by
852    /// nodes that can be removed, this node will be removed as well.
853    ///
854    /// Returns the number of nodes removed.
855    ///
856    /// The implementation should emit the [`ManagerEventSubscriber::pre_gc()`]
857    /// and [`ManagerEventSubscriber::post_gc()`] events unless called from the
858    /// closure passed to [`Self::reorder()`].
859    fn gc(&self) -> usize;
860
861    /// Prepare and postprocess a reordering operation. The reordering itself is
862    /// performed in `f`.
863    ///
864    /// Returns the value returned by `f`.
865    ///
866    /// In case of a recursive call (i.e., the closure passed to this method
867    /// calls this method again), the implementation should just call `f` and
868    /// return.
869    ///
870    /// Otherwise, the implementation should emit
871    /// [events][ManagerEventSubscriber] in the following order:
872    ///
873    /// 1. [`pre_gc`][ManagerEventSubscriber::pre_gc()]. This enables node
874    ///    removal during the `pre_reorder` events.
875    /// 2. [`pre_reorder`][ManagerEventSubscriber::pre_reorder()] and
876    ///    [`pre_reorder_mut`][ManagerEventSubscriber::pre_reorder_mut()]
877    /// 3. Call `f`
878    /// 4. [`post_reorder`][ManagerEventSubscriber::post_reorder()] and
879    ///    [`post_reorder_mut`][ManagerEventSubscriber::post_reorder_mut()]
880    /// 5. [`post_gc`][ManagerEventSubscriber::post_gc()]
881    fn reorder<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T;
882
883    /// Get the count of garbage collections
884    ///
885    /// This counter should monotonically increase to ensure that caches are
886    /// invalidated accordingly.
887    fn gc_count(&self) -> u64;
888
889    /// Get the count of reordering operations
890    ///
891    /// This counter should monotonically increase to ensure that caches are
892    /// invalidated accordingly.
893    fn reorder_count(&self) -> u64;
894}
895
896/// Event subscriber for [`Manager`]-related events
897///
898/// This is intended to be implemented by data structures that can be plugged
899/// into the [`Manager`], e.g., an [`ApplyCache`] implementation.
900///
901/// # Use Cases
902///
903/// When using reference counting to implement garbage collection of dead nodes,
904/// cloning and dropping edges when inserting entries into the apply cache may
905/// cause many CPU cache misses. To circumvent this performance issue, the apply
906/// cache may store [`Borrowed<M::Edge>`]s (e.g., using the unsafe
907/// [`Borrowed::into_inner()`]). Now, the apply cache implementation has to
908/// guarantee that every edge returned by the [`get()`][ApplyCache::get]
909/// method still points to a valid node. To that end, the cache may, e.g., clear
910/// itself when [`Self::pre_gc()`] is called and reject any insertion of new
911/// entries until [`Self::post_gc()`].
912///
913/// # Safety
914///
915/// A use-case such as the one described above requires the `pre_*` methods to
916/// actually be called before any garbage collection / reordering for SAFETY.
917/// This means the struct implementing `ManagerEventSubscriber` must only be
918/// plugged into [`Manager`] implementations or other wrapper structures that
919/// uphold this contract, i.e., creation of the `ManagerEventSubscriber`
920/// implementation must be `unsafe`.
921///
922/// Additionally, we require each [`post_gc`][Self::post_gc] call to be paired
923/// with a distinct preceding [`pre_gc`][Self::pre_gc] call, see below.
924pub trait ManagerEventSubscriber<M: Manager> {
925    /// Initialization
926    ///
927    /// This method is called once at the very end of initializing a new
928    /// manager.
929    #[inline(always)]
930    #[allow(unused_variables)]
931    fn init(&self, manager: &M) {}
932
933    /// Initialization
934    ///
935    /// This is an alternative to [`Self::post_reorder()`] with a mutable
936    /// reference to the manager as an argument. Since `self` may be a
937    /// substructure of `manager`, the method cannot provide mutable references
938    /// to both `self` and `manager`.
939    #[inline(always)]
940    #[allow(unused_variables)]
941    fn init_mut(manager: &mut M) {}
942
943    /// Prepare a garbage collection
944    ///
945    /// The [`Manager`] implementation should only remove nodes (as part of a
946    /// garbage collection or reordering) after calling this method. Between a
947    /// `pre_gc` and the subsequent [`post_gc`][Self::post_gc] call, there
948    /// should not be another `pre_gc` call.
949    ///
950    /// This method may lock (parts of) `self`. Unlocking is then done in
951    /// [`Self::post_gc()`].
952    ///
953    /// Note that this method and [`Self::pre_reorder`] may both be called in
954    /// case of reordering, but can also be
955    #[inline(always)]
956    #[allow(unused_variables)]
957    fn pre_gc(&self, manager: &M) {}
958
959    /// Post-process a garbage collection
960    ///
961    /// # Safety
962    ///
963    /// Each call to this method must be paired with a distinct preceding
964    /// [`Self::pre_gc()`] call. All operations potentially removing nodes must
965    /// happen between such a pair of method calls.
966    #[inline(always)]
967    #[allow(unused_variables)]
968    unsafe fn post_gc(&self, manager: &M) {}
969
970    /// Prepare a reordering operation (including any addition of levels)
971    ///
972    /// The [`Manager`] implementation should only add or reorder levels after
973    /// calling this method and [`pre_reorder_mut`][Self::pre_reorder_mut].
974    /// Between a `pre_reorder` and the subsequent
975    /// [`post_reorder`][Self::post_reorder] call, there should not be
976    /// another `pre_reorder` call.
977    #[inline(always)]
978    #[allow(unused_variables)]
979    fn pre_reorder(&self, manager: &M) {}
980
981    /// Prepare a reordering operation (including any addition of levels)
982    ///
983    /// This is an alternative to [`Self::pre_reorder()`] with a mutable
984    /// reference to the manager as an argument. Since `self` may be a
985    /// substructure of `manager`, the method cannot provide mutable references
986    /// to both `self` and `manager`.
987    #[inline(always)]
988    #[allow(unused_variables)]
989    fn pre_reorder_mut(manager: &mut M) {}
990
991    /// Post-process a reordering operation
992    ///
993    /// Each call to this method should be paired with a distinct preceding
994    /// [`Self::pre_reorder()`] call. All operations reordering (or adding)
995    /// levels of the decision diagram should happen between such a pair of
996    /// method calls.
997    #[inline(always)]
998    #[allow(unused_variables)]
999    fn post_reorder(&self, manager: &M) {}
1000
1001    /// Post-process a reordering operation
1002    ///
1003    /// This is an alternative to [`Self::post_reorder()`] with a mutable
1004    /// reference to the manager as an argument. Since `self` may be a
1005    /// substructure of `manager`, the method cannot provide mutable references
1006    /// to both `self` and `manager`.
1007    #[inline(always)]
1008    #[allow(unused_variables)]
1009    fn post_reorder_mut(manager: &mut M) {}
1010}
1011
1012/// View of a single level in the manager
1013///
1014/// # Safety
1015///
1016/// Each level view is associated with a [`Manager`] M and a [`LevelNo`] L. The
1017/// level view must ensure that all contained nodes and their descendants are
1018/// stored in M. The edges returned by [`LevelView::get()`] and
1019/// [`LevelView::get_or_insert()`] must reference nodes at this level.
1020/// [`LevelView::iter()`] must yield all edges to nodes at this level (it must
1021/// not hide some away).
1022///
1023/// [`LevelView::swap()`] conceptually removes all nodes from this level and
1024/// inserts them at the other level and vice versa.
1025pub unsafe trait LevelView<E: Edge, N: InnerNode<E>> {
1026    /// Iterator over [`Edge`]s pointing to nodes at this level
1027    type Iterator<'a>: Iterator<Item = &'a E>
1028    where
1029        Self: 'a,
1030        E: 'a;
1031
1032    /// Taken level view, see [`LevelView::take()`]
1033    type Taken: LevelView<E, N>;
1034
1035    /// Get the number of nodes on this level
1036    #[must_use]
1037    fn len(&self) -> usize;
1038
1039    /// Returns `true` iff this level contains nodes
1040    #[must_use]
1041    fn is_empty(&self) -> bool {
1042        self.len() == 0
1043    }
1044
1045    /// Get the level number of this level
1046    #[must_use]
1047    fn level_no(&self) -> LevelNo;
1048
1049    /// Reserve space for `additional` nodes on this level
1050    fn reserve(&mut self, additional: usize);
1051
1052    /// Get the edge corresponding to the given node (if present)
1053    #[must_use]
1054    fn get(&self, node: &N) -> Option<&E>;
1055
1056    /// Insert the given edge into the unique table at this level, assuming that
1057    /// the referenced node is already stored in the associated manager.
1058    ///
1059    /// Returns `true` if the edge was inserted, `false` if it was already
1060    /// present.
1061    ///
1062    /// Panics if `edge`
1063    /// - points to a terminal node,
1064    /// - references a node from a different manager, or
1065    /// - `N` implements [`HasLevel`] and `edge` references a node for which
1066    ///   [`HasLevel::level()`] returns a different level.
1067    ///
1068    /// Furthermore, this function should panic if `edge` is tagged, but the
1069    /// caller must not rely on that. An implementation may simply remove the
1070    /// tag for optimization purposes.
1071    fn insert(&mut self, edge: E) -> bool;
1072
1073    /// Get the edge corresponding to `level` and `node` if present, or insert
1074    /// it.
1075    ///
1076    /// Panics if
1077    /// - the children of `node` are stored in a different manager, or
1078    /// - `N` implements [`HasLevel`] and
1079    ///   [`HasLevel::level(node)`][HasLevel::level()] returns a different
1080    ///   level.
1081    #[must_use]
1082    fn get_or_insert(&mut self, node: N) -> AllocResult<E>;
1083
1084    /// Perform garbage collection on this level
1085    ///
1086    /// This method may be a no-op unless a garbage collection has been
1087    /// prepared. For instance, this is what [`Manager::reorder()`] does.
1088    fn gc(&mut self);
1089
1090    /// Remove `node` from (this level of) the manager
1091    ///
1092    /// Returns whether the node was present at this level and has been removed.
1093    ///
1094    /// This method may be a no-op unless a garbage collection has been
1095    /// prepared. For instance, this is what [`Manager::reorder()`] does. In the
1096    /// no-op case, the return value is always false.
1097    fn remove(&mut self, node: &N) -> bool;
1098
1099    /// Move all nodes from this level to the other level and vice versa.
1100    ///
1101    /// # Safety
1102    ///
1103    /// This method does not necessarily change the level returned by
1104    /// [`HasLevel::level()`] for the nodes at this or the `other` level. The
1105    /// caller must ensure a consistent state using calls to
1106    /// [`HasLevel::set_level()`]. (Be aware of exception safety!)
1107    unsafe fn swap(&mut self, other: &mut Self);
1108
1109    /// Iterate over all the edges at this level
1110    #[must_use]
1111    fn iter(&self) -> Self::Iterator<'_>;
1112
1113    /// Clear this level, returning a level view containing all the previous
1114    /// edges.
1115    #[must_use]
1116    fn take(&mut self) -> Self::Taken;
1117}
1118
1119/// Cache for the result of apply operations
1120///
1121/// This trait provides methods to add computation results to the apply cache
1122/// and to query the cache for these results. Just as every cache, the
1123/// implementation may decide to evict results from the cache.
1124pub trait ApplyCache<M: Manager, O: Copy>: DropWith<M::Edge> {
1125    /// Get the result of `operation`, if cached
1126    #[must_use]
1127    fn get_with_numeric(
1128        &self,
1129        manager: &M,
1130        operator: O,
1131        operands: &[Borrowed<M::Edge>],
1132        numeric_operands: &[u32],
1133    ) -> Option<M::Edge>;
1134
1135    /// Add the result of `operation` to this cache
1136    ///
1137    /// An implementation is free to not cache any result. (This is why we use
1138    /// `Borrowed<M::Edge>`, which in this case elides a few clone and drop
1139    /// operations.) If the cache already contains a key equal to `operation`,
1140    /// there is no need to update its value. (Again, we can elide clone and
1141    /// drop operations.)
1142    fn add_with_numeric(
1143        &self,
1144        manager: &M,
1145        operator: O,
1146        operands: &[Borrowed<M::Edge>],
1147        numeric_operands: &[u32],
1148        value: Borrowed<M::Edge>,
1149    );
1150
1151    /// Shorthand for [`Self::get_with_numeric()`] without numeric operands
1152    #[inline(always)]
1153    #[must_use]
1154    fn get(&self, manager: &M, operator: O, operands: &[Borrowed<M::Edge>]) -> Option<M::Edge> {
1155        self.get_with_numeric(manager, operator, operands, &[])
1156    }
1157
1158    /// Shorthand for [`Self::add_with_numeric()`] without numeric operands
1159    #[inline(always)]
1160    fn add(
1161        &self,
1162        manager: &M,
1163        operator: O,
1164        operands: &[Borrowed<M::Edge>],
1165        value: Borrowed<M::Edge>,
1166    ) {
1167        self.add_with_numeric(manager, operator, operands, &[], value)
1168    }
1169
1170    /// Remove all entries from the cache
1171    fn clear(&self, manager: &M);
1172}
1173
1174/// Apply cache container
1175///
1176/// Intended to be implemented by [`Manager`]s such that generic implementations
1177/// of the recursive apply algorithm can simply require
1178/// `M: Manager + HasApplyCache<M, O>`, where `O` is the operator type.
1179pub trait HasApplyCache<M: Manager, O: Copy> {
1180    /// The concrete apply cache type
1181    type ApplyCache: ApplyCache<M, O>;
1182
1183    /// Get a shared reference to the contained apply cache
1184    #[must_use]
1185    fn apply_cache(&self) -> &Self::ApplyCache;
1186
1187    /// Get a mutable reference to the contained apply cache
1188    #[must_use]
1189    fn apply_cache_mut(&mut self) -> &mut Self::ApplyCache;
1190}
1191
1192/// Worker thread pool associated with a [`Manager`]
1193///
1194/// A manager having its own thread pool has the advantage that it may use
1195/// thread-local storage for its workers to pre-allocate some resources (e.g.,
1196/// slots for nodes) and thereby reduce lock contention.
1197pub trait WorkerPool: Sync {
1198    /// Get the current number of threads
1199    fn current_num_threads(&self) -> usize;
1200
1201    /// Get the recursion depth up to which operations are split
1202    fn split_depth(&self) -> u32;
1203
1204    /// Set the recursion depth up to which operations are split
1205    ///
1206    /// `None` means that the implementation should automatically choose the
1207    /// depth. `Some(0)` means that no operations are split.
1208    fn set_split_depth(&self, depth: Option<u32>);
1209
1210    /// Execute `op` within the thread pool
1211    ///
1212    /// If this method is called from another thread pool, it may cooperatively
1213    /// yield execution to that pool until `op` has finished.
1214    fn install<R: Send>(&self, op: impl FnOnce() -> R + Send) -> R;
1215
1216    /// Execute `op_a` and `op_b` in parallel within the thread pool
1217    ///
1218    /// Note that the split depth has no influence on this method. Checking
1219    /// whether to split an operation must be done externally.
1220    fn join<RA: Send, RB: Send>(
1221        &self,
1222        op_a: impl FnOnce() -> RA + Send,
1223        op_b: impl FnOnce() -> RB + Send,
1224    ) -> (RA, RB);
1225
1226    /// Execute `op` on every worker in the thread pool
1227    fn broadcast<R: Send>(&self, op: impl Fn(BroadcastContext) -> R + Sync) -> Vec<R>;
1228}
1229
1230/// Context provided to workers by [`WorkerPool::broadcast()`]
1231#[derive(Clone, Copy, Debug)]
1232pub struct BroadcastContext {
1233    /// Index of this worker (in range `0..num_threads`)
1234    pub index: u32,
1235
1236    /// Number of threads receiving the broadcast
1237    pub num_threads: u32,
1238}
1239
1240/// Helper trait to be implemented by [`Manager`] and [`ManagerRef`] if they
1241/// feature a [`WorkerPool`].
1242pub trait HasWorkers: Sync {
1243    /// Type of the worker pool
1244    type WorkerPool: WorkerPool;
1245
1246    /// Get the worker pool
1247    fn workers(&self) -> &Self::WorkerPool;
1248}