use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt;
use std::hash::BuildHasher;
use std::iter::FusedIterator;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use crate::{Edge, LevelNo, Manager, NodeID};
mod on_drop;
mod substitution;
pub mod edge_hash_map;
pub use edge_hash_map::EdgeHashMap;
pub mod num;
pub use on_drop::*;
pub use substitution::*;
pub mod var_name_map;
pub use var_name_map::VarNameMap;
pub use nanorand::WyRand as Rng;
#[repr(transparent)]
#[must_use = "Own<H> cannot be dropped on its own"]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Own<H: Copy>(H);
impl<H: Copy> Own<H> {
#[inline(always)]
pub unsafe fn from_raw(raw: H) -> Self {
Self(raw)
}
#[inline(always)]
pub fn raw(&self) -> H {
self.0
}
#[inline]
pub fn into_raw(self) -> H {
let this = ManuallyDrop::new(self);
this.0
}
#[inline(always)]
pub fn borrowed(&self) -> Ref<'_, H> {
Ref(self.0, PhantomData)
}
}
impl<H: Copy> Drop for Own<H> {
#[cold]
#[inline(never)]
fn drop(&mut self) {
eprintln!(
"{} must not be dropped. Backtrace:\n{}",
std::any::type_name::<Self>(),
std::backtrace::Backtrace::capture()
);
#[cfg(feature = "static_leak_check")]
{
extern "C" {
#[link_name = "\n\n`Own<H>`s must not be dropped.`.\n"]
fn trigger() -> !;
}
unsafe { trigger() }
}
}
}
impl<H: Copy + fmt::Debug> fmt::Debug for Own<H> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Ref<'a, H: Copy>(H, PhantomData<&'a H>);
impl<'a, H: Copy> Ref<'a, H> {
#[must_use]
#[inline]
pub unsafe fn from_raw(raw: H) -> Self {
Self(raw, PhantomData)
}
#[inline]
pub fn raw(self) -> H {
self.0
}
}
impl<'a, H: Copy> std::borrow::Borrow<Own<H>> for Ref<'a, H> {
fn borrow(&self) -> &Own<H> {
let ptr = self as *const Self as *const Own<H>;
unsafe { &*ptr }
}
}
impl<'a, H: Copy + fmt::Debug> fmt::Debug for Ref<'a, H> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl<E: Edge> Own<E> {
#[inline]
pub fn with_tag(self, tag: E::Tag) -> Self {
let this = ManuallyDrop::new(self);
Self(this.0.with_tag(tag))
}
#[inline]
pub fn tag(&self) -> E::Tag {
self.0.tag()
}
#[inline]
pub fn node_id(&self) -> usize {
self.0.node_id()
}
}
impl<'a, E: Edge> Ref<'a, E> {
#[inline]
pub fn with_tag(self, tag: E::Tag) -> Self {
Self(self.0.with_tag(tag), PhantomData)
}
#[inline]
pub fn tag(self) -> E::Tag {
self.0.tag()
}
#[inline]
pub fn node_id(self) -> usize {
self.0.node_id()
}
}
pub trait DropWith<E: Edge>: Sized {
fn drop_with(self, drop_edge: impl Fn(Own<E>));
#[inline]
fn drop_with_manager<M: Manager<Edge = E>>(self, manager: &M) {
self.drop_with(|e| manager.drop_edge(e));
}
}
pub struct EdgeRefIter<'a, E: Edge + 'a, I>(I, PhantomData<Ref<'a, E>>);
impl<'a, E: Edge, I: Iterator<Item = &'a Own<E>>> From<I> for EdgeRefIter<'a, E, I> {
fn from(it: I) -> Self {
Self(it, PhantomData)
}
}
impl<'a, E: Edge, I: Iterator<Item = &'a Own<E>>> Iterator for EdgeRefIter<'a, E, I> {
type Item = Ref<'a, E>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
Some(self.0.next()?.borrowed())
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl<'a, E: Edge, I: FusedIterator<Item = &'a Own<E>>> FusedIterator for EdgeRefIter<'a, E, I> {}
impl<'a, E: Edge, I: ExactSizeIterator<Item = &'a Own<E>>> ExactSizeIterator
for EdgeRefIter<'a, E, I>
{
#[inline]
fn len(&self) -> usize {
self.0.len()
}
}
pub trait NodeSet<E: Copy>: Clone + Default + Eq {
#[must_use]
fn len(&self) -> usize;
#[must_use]
fn is_empty(&self) -> bool {
self.len() == 0
}
fn insert(&mut self, edge: Ref<'_, E>) -> bool;
#[must_use]
fn contains(&self, edge: Ref<'_, E>) -> bool;
fn remove(&mut self, edge: Ref<'_, E>) -> bool;
}
impl<E: Edge, S: Clone + Default + BuildHasher> NodeSet<E> for HashSet<NodeID, S> {
#[inline]
fn len(&self) -> usize {
HashSet::len(self)
}
#[inline]
fn insert(&mut self, edge: Ref<'_, E>) -> bool {
self.insert(edge.node_id())
}
#[inline]
fn contains(&self, edge: Ref<'_, E>) -> bool {
self.contains(&edge.node_id())
}
#[inline]
fn remove(&mut self, edge: Ref<'_, E>) -> bool {
self.remove(&edge.node_id())
}
}
impl<E: Edge> NodeSet<E> for BTreeSet<NodeID> {
#[inline]
fn len(&self) -> usize {
BTreeSet::len(self)
}
#[inline]
fn insert(&mut self, edge: Ref<'_, E>) -> bool {
self.insert(edge.node_id())
}
#[inline]
fn contains(&self, edge: Ref<'_, E>) -> bool {
self.contains(&edge.node_id())
}
#[inline]
fn remove(&mut self, edge: Ref<'_, E>) -> bool {
self.remove(&edge.node_id())
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[repr(i8)]
pub enum OptBool {
None = -1,
#[allow(missing_docs)]
False = 0,
#[allow(missing_docs)]
True = 1,
}
impl From<bool> for OptBool {
fn from(value: bool) -> Self {
if value { Self::True } else { Self::False }
}
}
pub type AllocResult<T> = Result<T, crate::error::OutOfMemory>;
pub trait IsFloatingPoint {
const FLOATING_POINT: bool;
const MIN_EXP: i32;
}
impl<T: std::ops::ShlAssign<i32>> IsFloatingPoint for T {
const FLOATING_POINT: bool = false;
const MIN_EXP: i32 = 0;
}
pub trait SatCountNumber:
Clone
+ From<u32>
+ std::ops::Add<Self, Output = Self>
+ std::ops::Shl<u32, Output = Self>
+ std::ops::Shr<u32, Output = Self>
+ IsFloatingPoint
{
}
impl<T> SatCountNumber for T where
T: Clone
+ From<u32>
+ std::ops::Add<Self, Output = Self>
+ std::ops::Shl<u32, Output = Self>
+ std::ops::Shr<u32, Output = Self>
+ IsFloatingPoint
{
}
pub struct SatCountCache<N: SatCountNumber, S: BuildHasher> {
pub map: HashMap<NodeID, N, S>,
vars: LevelNo,
epoch: u64,
pub cache_all: bool,
}
impl<N: SatCountNumber, S: BuildHasher + Default> Default for SatCountCache<N, S> {
fn default() -> Self {
Self {
map: HashMap::default(),
vars: 0,
epoch: 0,
cache_all: false,
}
}
}
impl<N: SatCountNumber, S: BuildHasher> SatCountCache<N, S> {
pub fn with_hasher(hash_builder: S) -> Self {
Self {
map: HashMap::with_hasher(hash_builder),
vars: 0,
epoch: 0,
cache_all: false,
}
}
pub fn clear_if_invalid<M: Manager>(&mut self, manager: &M, vars: LevelNo) {
let epoch = manager.gc_count();
if epoch != self.epoch || vars != self.vars {
self.epoch = epoch;
self.vars = vars;
self.map.clear();
}
}
}