1use std::collections::{BTreeSet, HashMap, HashSet};
4use std::fmt;
5use std::hash::BuildHasher;
6use std::iter::FusedIterator;
7use std::marker::PhantomData;
8use std::mem::ManuallyDrop;
9
10use crate::{Edge, LevelNo, Manager, NodeID};
11
12mod on_drop;
13mod substitution;
14
15pub mod edge_hash_map;
16pub use edge_hash_map::EdgeHashMap;
17pub mod num;
18pub use on_drop::*;
19pub use substitution::*;
20pub mod var_name_map;
21pub use var_name_map::VarNameMap;
22
23pub use nanorand::WyRand as Rng;
24
25#[repr(transparent)]
35#[must_use = "Own<H> cannot be dropped on its own"]
36#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
37pub struct Own<H: Copy>(H);
38
39impl<H: Copy> Own<H> {
40 #[inline(always)]
47 pub unsafe fn from_raw(raw: H) -> Self {
48 Self(raw)
49 }
50
51 #[inline(always)]
53 pub fn raw(&self) -> H {
54 self.0
55 }
56 #[inline]
58 pub fn into_raw(self) -> H {
59 let this = ManuallyDrop::new(self);
60 this.0
61 }
62
63 #[inline(always)]
65 pub fn borrowed(&self) -> Ref<'_, H> {
66 Ref(self.0, PhantomData)
67 }
68}
69
70impl<H: Copy> Drop for Own<H> {
71 #[cold]
72 #[inline(never)]
73 fn drop(&mut self) {
74 eprintln!(
75 "{} must not be dropped. Backtrace:\n{}",
76 std::any::type_name::<Self>(),
77 std::backtrace::Backtrace::capture()
78 );
79
80 #[cfg(feature = "static_leak_check")]
81 {
82 extern "C" {
83 #[link_name = "\n\n`Own<H>`s must not be dropped.`.\n"]
84 fn trigger() -> !;
85 }
86 unsafe { trigger() }
87 }
88 }
89}
90
91impl<H: Copy + fmt::Debug> fmt::Debug for Own<H> {
92 #[inline]
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94 self.0.fmt(f)
95 }
96}
97
98#[repr(transparent)]
110#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
111pub struct Ref<'a, H: Copy>(H, PhantomData<&'a H>);
112
113impl<'a, H: Copy> Ref<'a, H> {
114 #[must_use]
121 #[inline]
122 pub unsafe fn from_raw(raw: H) -> Self {
123 Self(raw, PhantomData)
124 }
125
126 #[inline]
128 pub fn raw(self) -> H {
129 self.0
130 }
131}
132
133impl<'a, H: Copy> std::borrow::Borrow<Own<H>> for Ref<'a, H> {
134 fn borrow(&self) -> &Own<H> {
135 let ptr = self as *const Self as *const Own<H>;
136 unsafe { &*ptr }
140 }
141}
142
143impl<'a, H: Copy + fmt::Debug> fmt::Debug for Ref<'a, H> {
144 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 self.0.fmt(f)
146 }
147}
148
149impl<E: Edge> Own<E> {
150 #[inline]
152 pub fn with_tag(self, tag: E::Tag) -> Self {
153 let this = ManuallyDrop::new(self);
154 Self(this.0.with_tag(tag))
155 }
156
157 #[inline]
159 pub fn tag(&self) -> E::Tag {
160 self.0.tag()
161 }
162
163 #[inline]
166 pub fn node_id(&self) -> usize {
167 self.0.node_id()
168 }
169}
170
171impl<'a, E: Edge> Ref<'a, E> {
172 #[inline]
174 pub fn with_tag(self, tag: E::Tag) -> Self {
175 Self(self.0.with_tag(tag), PhantomData)
176 }
177
178 #[inline]
180 pub fn tag(self) -> E::Tag {
181 self.0.tag()
182 }
183
184 #[inline]
187 pub fn node_id(self) -> usize {
188 self.0.node_id()
189 }
190}
191
192pub trait DropWith<E: Edge>: Sized {
201 fn drop_with(self, drop_edge: impl Fn(Own<E>));
217
218 #[inline]
225 fn drop_with_manager<M: Manager<Edge = E>>(self, manager: &M) {
226 self.drop_with(|e| manager.drop_edge(e));
227 }
228}
229
230pub struct EdgeRefIter<'a, E: Edge + 'a, I>(I, PhantomData<Ref<'a, E>>);
233
234impl<'a, E: Edge, I: Iterator<Item = &'a Own<E>>> From<I> for EdgeRefIter<'a, E, I> {
235 fn from(it: I) -> Self {
236 Self(it, PhantomData)
237 }
238}
239
240impl<'a, E: Edge, I: Iterator<Item = &'a Own<E>>> Iterator for EdgeRefIter<'a, E, I> {
241 type Item = Ref<'a, E>;
242
243 #[inline]
244 fn next(&mut self) -> Option<Self::Item> {
245 Some(self.0.next()?.borrowed())
246 }
247
248 #[inline]
249 fn size_hint(&self) -> (usize, Option<usize>) {
250 self.0.size_hint()
251 }
252}
253
254impl<'a, E: Edge, I: FusedIterator<Item = &'a Own<E>>> FusedIterator for EdgeRefIter<'a, E, I> {}
255
256impl<'a, E: Edge, I: ExactSizeIterator<Item = &'a Own<E>>> ExactSizeIterator
257 for EdgeRefIter<'a, E, I>
258{
259 #[inline]
260 fn len(&self) -> usize {
261 self.0.len()
262 }
263}
264
265pub trait NodeSet<E: Copy>: Clone + Default + Eq {
267 #[must_use]
269 fn len(&self) -> usize;
270
271 #[must_use]
273 fn is_empty(&self) -> bool {
274 self.len() == 0
275 }
276
277 fn insert(&mut self, edge: Ref<'_, E>) -> bool;
281
282 #[must_use]
284 fn contains(&self, edge: Ref<'_, E>) -> bool;
285
286 fn remove(&mut self, edge: Ref<'_, E>) -> bool;
290}
291impl<E: Edge, S: Clone + Default + BuildHasher> NodeSet<E> for HashSet<NodeID, S> {
292 #[inline]
293 fn len(&self) -> usize {
294 HashSet::len(self)
295 }
296 #[inline]
297 fn insert(&mut self, edge: Ref<'_, E>) -> bool {
298 self.insert(edge.node_id())
299 }
300 #[inline]
301 fn contains(&self, edge: Ref<'_, E>) -> bool {
302 self.contains(&edge.node_id())
303 }
304 #[inline]
305 fn remove(&mut self, edge: Ref<'_, E>) -> bool {
306 self.remove(&edge.node_id())
307 }
308}
309impl<E: Edge> NodeSet<E> for BTreeSet<NodeID> {
310 #[inline]
311 fn len(&self) -> usize {
312 BTreeSet::len(self)
313 }
314 #[inline]
315 fn insert(&mut self, edge: Ref<'_, E>) -> bool {
316 self.insert(edge.node_id())
317 }
318 #[inline]
319 fn contains(&self, edge: Ref<'_, E>) -> bool {
320 self.contains(&edge.node_id())
321 }
322 #[inline]
323 fn remove(&mut self, edge: Ref<'_, E>) -> bool {
324 self.remove(&edge.node_id())
325 }
326}
327
328#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
330#[repr(i8)]
331pub enum OptBool {
332 None = -1,
334 #[allow(missing_docs)]
335 False = 0,
336 #[allow(missing_docs)]
337 True = 1,
338}
339
340impl From<bool> for OptBool {
341 fn from(value: bool) -> Self {
342 if value { Self::True } else { Self::False }
343 }
344}
345
346pub type AllocResult<T> = Result<T, crate::error::OutOfMemory>;
348
349pub trait IsFloatingPoint {
351 const FLOATING_POINT: bool;
353
354 const MIN_EXP: i32;
357}
358
359impl<T: std::ops::ShlAssign<i32>> IsFloatingPoint for T {
362 const FLOATING_POINT: bool = false;
363 const MIN_EXP: i32 = 0;
364}
365
366pub trait SatCountNumber:
368 Clone
369 + From<u32>
370 + std::ops::Add<Self, Output = Self>
371 + std::ops::Shl<u32, Output = Self>
372 + std::ops::Shr<u32, Output = Self>
373 + IsFloatingPoint
374{
375}
376
377impl<T> SatCountNumber for T where
378 T: Clone
379 + From<u32>
380 + std::ops::Add<Self, Output = Self>
381 + std::ops::Shl<u32, Output = Self>
382 + std::ops::Shr<u32, Output = Self>
383 + IsFloatingPoint
384{
385}
386
387pub struct SatCountCache<N: SatCountNumber, S: BuildHasher> {
389 pub map: HashMap<NodeID, N, S>,
391
392 vars: LevelNo,
394
395 epoch: u64,
402
403 pub cache_all: bool,
406}
407
408impl<N: SatCountNumber, S: BuildHasher + Default> Default for SatCountCache<N, S> {
409 fn default() -> Self {
410 Self {
411 map: HashMap::default(),
412 vars: 0,
413 epoch: 0,
414 cache_all: false,
415 }
416 }
417}
418
419impl<N: SatCountNumber, S: BuildHasher> SatCountCache<N, S> {
420 pub fn with_hasher(hash_builder: S) -> Self {
422 Self {
423 map: HashMap::with_hasher(hash_builder),
424 vars: 0,
425 epoch: 0,
426 cache_all: false,
427 }
428 }
429
430 pub fn clear_if_invalid<M: Manager>(&mut self, manager: &M, vars: LevelNo) {
433 let epoch = manager.gc_count();
434 if epoch != self.epoch || vars != self.vars {
435 self.epoch = epoch;
436 self.vars = vars;
437 self.map.clear();
438 }
439 }
440}