sat-solver 0.2.1

A SAT solver implemented in Rust, focusing on performance, efficiency and experimentation.
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
//! Defines how literals are stored within a clause.
//!
//! This module introduces the `LiteralStorage` trait, which abstracts the underlying
//! data structure used to store a collection of literals (e.g. `Vec<L>` or
//! `SmallVec<[L; N]>`). This allows clauses to be generic over their literal
//! storage mechanism, enabling optimisations like `SmallVec` for small clauses
//! while still supporting heap-allocated `Vec` for larger ones.
//!
//! The trait provides a common interface for operations like adding, removing,
//! accessing, and iterating over literals.

use crate::sat::literal; // For the `convert` function in the module's `convert` function
use crate::sat::literal::Literal;
use clap::ValueEnum;
use smallvec::SmallVec;
use std::fmt::{Debug, Display};
use std::ops::{Index, IndexMut};
use std::slice::{Iter, IterMut};

/// A trait abstracting the storage of literals within a clause.
///
/// This trait defines a common interface for collections that can store literals,
/// such as `Vec<L>` or `SmallVec<[L; N]>`. It ensures that clauses can interact
/// with their literal storage in a generic way.
///
/// # Type Parameters
///
/// * `L`: The type of `Literal` being stored.
///
/// # Required Super traits
///
/// Implementors must also implement several standard traits to ensure broad compatibility:
/// - `Index<usize, Output = L>`: For direct read access by index.
/// - `IndexMut<usize, Output = L>`: For direct mutable access by index.
/// - `FromIterator<L>`: To construct the storage from an iterator of literals.
/// - `Clone`: To clone the storage.
/// - `From<Vec<L>>`: To construct the storage from a `Vec<L>`.
/// - `Default`: To create a default (usually empty) instance of the storage.
/// - `Extend<L>`: To add multiple literals from an iterator.
/// - `Debug`: For debugging purposes.
/// - `AsRef<[L]>`: To get a slice view of the stored literals.
pub trait LiteralStorage<L: Literal>:
    Index<usize, Output = L>
    + FromIterator<L>
    + Clone
    + From<Vec<L>>
    + Default
    + IndexMut<usize, Output = L>
    + Extend<L>
    + Debug
    + AsRef<[L]>
{
    /// Appends a literal to the end of the storage.
    ///
    /// # Arguments
    ///
    /// * `literal`: The literal to add.
    fn push(&mut self, literal: L);

    /// Returns the number of literals in the storage.
    fn len(&self) -> usize;

    /// Returns `true` if the storage contains no literals.
    fn is_empty(&self) -> bool;

    /// Returns an iterator over the literals in the storage.
    fn iter(&self) -> Iter<L>;

    /// Returns a mutable iterator over the literals in the storage.
    fn iter_mut(&mut self) -> IterMut<L>;

    /// Removes and returns the literal at `index`, shifting all elements after it to the left.
    ///
    /// # Arguments
    ///
    /// * `index`: The index of the literal to remove.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    fn remove(&mut self, index: usize) -> L;

    /// Removes all literals from the storage.
    fn clear(&mut self);

    /// Swaps the literals at indices `a` and `b`.
    ///
    /// # Arguments
    ///
    /// * `a`: The index of the first literal.
    /// * `b`: The index of the second literal.
    ///
    /// # Panics
    ///
    /// Panics if `a` or `b` are out of bounds.
    fn swap(&mut self, a: usize, b: usize);

    /// Returns a reference to the literal at `index` without bounds checking.
    ///
    /// # Arguments
    ///
    /// * `index`: The index of the literal to access.
    ///
    /// # Safety
    ///
    /// Calling this method with an out-of-bounds `index` is undefined behavior.
    unsafe fn get_unchecked(&self, index: usize) -> &L;

    /// Returns a mutable reference to the literal at `index` without bounds checking.
    ///
    /// # Arguments
    ///
    /// * `index`: The index of the literal to access.
    ///
    /// # Safety
    ///
    /// Calling this method with an out-of-bounds `index` is undefined behavior.
    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut L;

    /// Removes the literal at `index` by swapping it with the last literal and then popping.
    ///
    /// This is generally faster than `remove` because it does not require shifting elements,
    /// but it changes the order of literals in the storage.
    ///
    /// # Arguments
    ///
    /// * `index`: The index of the literal to remove.
    ///
    /// # Panics
    ///
    /// Panics if `index` is out of bounds.
    fn swap_remove(&mut self, index: usize) -> L {
        let last_idx = self.len().saturating_sub(1);
        self.swap(index, last_idx);
        self.remove(last_idx)
    }
}

/// Implementation of `LiteralStorage` for `Vec<L>`.
///
/// This allows `Vec<L>` to be used as a concrete storage type for literals in clauses.
/// Most methods delegate directly to the corresponding `Vec` methods.
impl<L: Literal> LiteralStorage<L> for Vec<L> {
    fn push(&mut self, literal: L) {
        Self::push(self, literal);
    }

    fn len(&self) -> usize {
        Self::len(self)
    }

    fn is_empty(&self) -> bool {
        Self::is_empty(self)
    }

    fn iter(&self) -> Iter<L> {
        self.as_slice().iter()
    }

    fn iter_mut(&mut self) -> IterMut<L> {
        self.as_mut_slice().iter_mut()
    }

    fn remove(&mut self, index: usize) -> L {
        Self::remove(self, index)
    }

    fn clear(&mut self) {
        Self::clear(self);
    }

    fn swap(&mut self, a: usize, b: usize) {
        self.as_mut_slice().swap(a, b);
    }

    unsafe fn get_unchecked(&self, index: usize) -> &L {
        unsafe { self.as_slice().get_unchecked(index) }
    }

    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut L {
        unsafe { self.as_mut_slice().get_unchecked_mut(index) }
    }

    fn swap_remove(&mut self, index: usize) -> L {
        Self::swap_remove(self, index)
    }
}

/// Implementation of `LiteralStorage` for `SmallVec<[L; N]>`.
///
/// `SmallVec` is a vector-like collection that stores a small number (`N`) of elements
/// on the stack and spills to the heap if more elements are added. This can be an
/// optimisation for clauses, as many clauses are small.
///
/// # Type Parameters
///
/// * `L`: The type of `Literal` being stored.
/// * `N`: A compile-time constant specifying the stack capacity of the `SmallVec`.
impl<L: Literal, const N: usize> LiteralStorage<L> for SmallVec<[L; N]> {
    fn push(&mut self, literal: L) {
        Self::push(self, literal);
    }

    fn len(&self) -> usize {
        Self::len(self)
    }

    fn is_empty(&self) -> bool {
        Self::is_empty(self)
    }

    fn iter(&self) -> Iter<L> {
        self.as_slice().iter()
    }

    fn iter_mut(&mut self) -> IterMut<L> {
        self.as_mut_slice().iter_mut()
    }

    fn remove(&mut self, index: usize) -> L {
        Self::remove(self, index)
    }

    fn clear(&mut self) {
        Self::clear(self);
    }

    fn swap(&mut self, a: usize, b: usize) {
        self.as_mut_slice().swap(a, b);
    }

    unsafe fn get_unchecked(&self, index: usize) -> &L {
        unsafe { self.as_slice().get_unchecked(index) }
    }

    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut L {
        unsafe { self.as_mut_slice().get_unchecked_mut(index) }
    }

    fn swap_remove(&mut self, index: usize) -> L {
        Self::swap_remove(self, index)
    }
}

/// Converts a collection of literals of one type (`L`) to a `Vec` of literals of another type (`U`).
///
/// This function iterates over the input `literals` (which must implement `LiteralStorage<L>`),
/// converts each literal from type `L` to type `U` using `crate::sat::literal::convert`,
/// and collects the results into a new `Vec<U>`.
///
/// # Type Parameters
///
/// * `L`: The source `Literal` type.
/// * `U`: The target `Literal` type.
/// * `S`: The `LiteralStorage` type for the target literals `U` (unused in this specific signature, but implies context).
/// * `T`: The `LiteralStorage` type for the source literals `L`.
///
/// # Arguments
///
/// * `literals`: A reference to a `LiteralStorage<L>` containing the literals to convert.
///
/// # Returns
///
/// A `Vec<U>` containing the converted literals.
#[allow(dead_code)]
pub fn convert<L: Literal, U: Literal, S: LiteralStorage<U>, T: LiteralStorage<L>>(
    literals: &T,
) -> Vec<U> {
    literals.iter().map(literal::convert::<L, U>).collect()
}

/// Enum representing the storage implementation for literals in a clause.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LiteralStorageImpls<L: Literal, const N: usize> {
    /// Uses a standard `Vec` to store literals.
    VecStorage(Vec<L>),
    /// Uses a `SmallVec` to store literals, optimised for small collections.
    SmallVecStorage(SmallVec<[L; N]>),
}

impl<L: Literal, const N: usize> Index<usize> for LiteralStorageImpls<L, N> {
    type Output = L;

    fn index(&self, index: usize) -> &Self::Output {
        match self {
            Self::VecStorage(vec) => &vec[index],
            Self::SmallVecStorage(small_vec) => &small_vec[index],
        }
    }
}

impl<L: Literal, const N: usize> IndexMut<usize> for LiteralStorageImpls<L, N> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        match self {
            Self::VecStorage(vec) => &mut vec[index],
            Self::SmallVecStorage(small_vec) => &mut small_vec[index],
        }
    }
}

impl<L: Literal, const N: usize> FromIterator<L> for LiteralStorageImpls<L, N> {
    fn from_iter<T: IntoIterator<Item = L>>(iter: T) -> Self {
        let mut vec = Vec::new();
        for item in iter {
            vec.push(item);
        }
        if vec.len() <= N {
            Self::SmallVecStorage(SmallVec::from(vec))
        } else {
            Self::VecStorage(vec)
        }
    }
}

impl<L: Literal, const N: usize> From<Vec<L>> for LiteralStorageImpls<L, N> {
    fn from(value: Vec<L>) -> Self {
        if value.len() <= N {
            Self::SmallVecStorage(SmallVec::from(value))
        } else {
            Self::VecStorage(value)
        }
    }
}

impl<L: Literal, const N: usize> Extend<L> for LiteralStorageImpls<L, N> {
    fn extend<T: IntoIterator<Item = L>>(&mut self, iter: T) {
        match self {
            Self::VecStorage(vec) => vec.extend(iter),
            Self::SmallVecStorage(small_vec) => small_vec.extend(iter),
        }
    }
}

impl<L: Literal, const N: usize> AsRef<[L]> for LiteralStorageImpls<L, N> {
    fn as_ref(&self) -> &[L] {
        match self {
            Self::VecStorage(vec) => vec.as_ref(),
            Self::SmallVecStorage(small_vec) => small_vec.as_ref(),
        }
    }
}

impl<L: Literal, const N: usize> Default for LiteralStorageImpls<L, N> {
    fn default() -> Self {
        Self::VecStorage(Vec::default())
    }
}

impl<L: Literal, const N: usize> LiteralStorage<L> for LiteralStorageImpls<L, N> {
    fn push(&mut self, literal: L) {
        match self {
            Self::VecStorage(vec) => vec.push(literal),
            Self::SmallVecStorage(small_vec) => small_vec.push(literal),
        }
    }

    fn len(&self) -> usize {
        match self {
            Self::VecStorage(vec) => vec.len(),
            Self::SmallVecStorage(small_vec) => small_vec.len(),
        }
    }

    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn iter(&self) -> Iter<L> {
        match self {
            Self::VecStorage(vec) => vec.iter(),
            Self::SmallVecStorage(small_vec) => small_vec.iter(),
        }
    }

    fn iter_mut(&mut self) -> IterMut<L> {
        match self {
            Self::VecStorage(vec) => vec.iter_mut(),
            Self::SmallVecStorage(small_vec) => small_vec.iter_mut(),
        }
    }

    fn remove(&mut self, index: usize) -> L {
        match self {
            Self::VecStorage(vec) => vec.remove(index),
            Self::SmallVecStorage(small_vec) => small_vec.remove(index),
        }
    }

    fn clear(&mut self) {
        match self {
            Self::VecStorage(vec) => vec.clear(),
            Self::SmallVecStorage(small_vec) => small_vec.clear(),
        }
    }

    fn swap(&mut self, a: usize, b: usize) {
        match self {
            Self::VecStorage(vec) => vec.swap(a, b),
            Self::SmallVecStorage(small_vec) => small_vec.swap(a, b),
        }
    }

    unsafe fn get_unchecked(&self, index: usize) -> &L {
        match self {
            Self::VecStorage(vec) => unsafe { vec.get_unchecked(index) },
            Self::SmallVecStorage(small_vec) => unsafe { small_vec.get_unchecked(index) },
        }
    }

    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut L {
        match self {
            Self::VecStorage(vec) => unsafe { vec.get_unchecked_mut(index) },
            Self::SmallVecStorage(small_vec) => unsafe { small_vec.get_unchecked_mut(index) },
        }
    }

    fn swap_remove(&mut self, index: usize) -> L {
        match self {
            Self::VecStorage(vec) => vec.swap_remove(index),
            Self::SmallVecStorage(small_vec) => small_vec.swap_remove(index),
        }
    }
}

/// Enum representing the storage type for literals in a clause.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy, Default, ValueEnum)]
pub enum LiteralStorageType {
    /// Uses a standard `Vec` to store literals.
    Vec,
    /// Uses a `SmallVec` to store literals, optimised for small collections.
    #[default]
    SmallVec,
}

impl Display for LiteralStorageType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Vec => write!(f, "vec"),
            Self::SmallVec => write!(f, "small-vec"),
        }
    }
}

impl LiteralStorageType {
    /// Returns the name of the storage type as a string.
    #[allow(dead_code)]
    #[must_use]
    pub fn to_impl<L: Literal, const N: usize>(self) -> LiteralStorageImpls<L, N> {
        match self {
            Self::Vec => LiteralStorageImpls::VecStorage(Vec::new()),
            Self::SmallVec => LiteralStorageImpls::SmallVecStorage(SmallVec::new()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sat::literal::{PackedLiteral, StructLiteral};

    #[allow(clippy::cognitive_complexity)]
    fn test_literal_storage_behavior<S: LiteralStorage<PackedLiteral>>(mut storage: S) {
        assert!(storage.is_empty());
        assert_eq!(storage.len(), 0);

        let lit1 = PackedLiteral::new(1u32, true);
        let lit2 = PackedLiteral::new(2u32, false);
        let lit3 = PackedLiteral::new(3u32, true);

        storage.push(lit1);
        assert!(!storage.is_empty());
        assert_eq!(storage.len(), 1);
        assert_eq!(storage[0], lit1);

        storage.push(lit2);
        assert_eq!(storage.len(), 2);
        assert_eq!(storage[1], lit2);

        let mut iter = storage.iter();
        assert_eq!(iter.next(), Some(&lit1));
        assert_eq!(iter.next(), Some(&lit2));
        assert_eq!(iter.next(), None);

        let removed_lit = storage.remove(0);
        assert_eq!(removed_lit, lit1);
        assert_eq!(storage.len(), 1);
        assert_eq!(storage[0], lit2);

        let new_literals = vec![lit1, lit3];
        storage.extend(new_literals);
        assert_eq!(storage.len(), 3);
        assert_eq!(storage[0], lit2);
        assert_eq!(storage[1], lit1);
        assert_eq!(storage[2], lit3);

        storage.swap(0, 2);
        assert_eq!(storage[0], lit3);
        assert_eq!(storage[2], lit2);

        let removed_by_swap = storage.swap_remove(0);
        assert_eq!(removed_by_swap, lit3);
        assert_eq!(storage.len(), 2);
        assert!(storage.iter().any(|&l| l == lit1));
        assert!(storage.iter().any(|&l| l == lit2));

        if !storage.is_empty() {
            unsafe {
                assert_eq!(*storage.get_unchecked(0), storage[0]);
            }
        }

        storage.clear();
        assert!(storage.is_empty());
        assert_eq!(storage.len(), 0);
    }

    #[test]
    fn test_vec_literal_storage() {
        let vec_storage: Vec<PackedLiteral> = Vec::new();
        test_literal_storage_behavior(vec_storage);
    }

    #[test]
    fn test_smallvec_literal_storage_stack() {
        let small_vec_storage: SmallVec<[PackedLiteral; 3]> = SmallVec::new();
        test_literal_storage_behavior(small_vec_storage);
    }

    #[test]
    fn test_smallvec_literal_storage_heap() {
        let small_vec_storage: SmallVec<[PackedLiteral; 1]> = SmallVec::new();
        test_literal_storage_behavior(small_vec_storage);
    }

    #[test]
    fn test_from_vec_and_from_iterator() {
        let literals = vec![
            PackedLiteral::new(1u32, true),
            PackedLiteral::new(2u32, false),
        ];

        let storage_vec_from_vec = literals.clone();
        assert_eq!(storage_vec_from_vec.len(), 2);

        let storage_vec_from_iter = literals.clone().into_iter();
        assert_eq!(storage_vec_from_iter.len(), 2);

        let storage_smallvec_from_vec: SmallVec<[PackedLiteral; 3]> =
            SmallVec::from(literals.clone());
        assert_eq!(storage_smallvec_from_vec.len(), 2);

        let storage_smallvec_from_iter: SmallVec<[PackedLiteral; 3]> =
            literals.into_iter().collect();
        assert_eq!(storage_smallvec_from_iter.len(), 2);
    }

    #[test]
    fn test_module_convert_function() {
        type TargetStorage = Vec<StructLiteral>;

        let source_literals_vec: Vec<PackedLiteral> = vec![
            PackedLiteral::new(1u32, true),
            PackedLiteral::new(2u32, false),
        ];

        let converted_literals: Vec<StructLiteral> =
            convert::<PackedLiteral, StructLiteral, TargetStorage, _>(&source_literals_vec);

        assert_eq!(converted_literals.len(), 2);
        assert_eq!(converted_literals[0].variable(), 1u32);
        assert!(converted_literals[0].polarity());
        assert_eq!(converted_literals[1].variable(), 2u32);
        assert!(!converted_literals[1].polarity());
    }
}