Browse Source

Started work on underlying types

clause_generator
Thomas Johnson 2 years ago
parent
commit
081d73e81d
  1. 2
      .gitignore
  2. 9
      Cargo.toml
  3. 26
      recommended-pre-commit
  4. 66
      src/clause.rs
  5. 47
      src/hash_wrapper.rs
  6. 3
      src/main.rs

2
.gitignore

@ -0,0 +1,2 @@
/target
Cargo.lock

9
Cargo.toml

@ -0,0 +1,9 @@
[package]
name = "cs451-proj"
version = "0.1.0"
authors = ["thajohns <thajohns@clarkson.edu>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

26
recommended-pre-commit

@ -0,0 +1,26 @@
#!/bin/bash
# Modification of https://gist.github.com/eugene-babichenko/ca9645fa8b579b9c56668f7b0eb74095
HAS_ISSUES=0
FIRST_FILE=1
for file in $(git diff --name-only --staged); do
FMT_RESULT="$(rustfmt --check $file 2>/dev/null || true)"
MODE_LINE="$(head -n1 $file || true)"
if [ "$FMT_RESULT" != "" -o "$MODE_LINE" != "// vim: ts=4 sw=4 et" ]; then
if [ $FIRST_FILE -eq 0 ]; then
echo -n ", "
fi
echo -n "$file"
HAS_ISSUES=1
FIRST_FILE=0
fi
done
if [ $HAS_ISSUES -eq 0 ]; then
exit 0
fi
echo ". Your code has formatting issues or missing modelines in files listed above. Format your code with rustfmt. (Use rustfmt --check to see what mistakes there are.)"
exit 1

66
src/clause.rs

@ -0,0 +1,66 @@
// vim: ts=4 sw=4 et
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug)]
struct VariableInner {
name: String,
assignment: RefCell<Option<bool>>,
}
#[derive(Clone, Debug)]
pub struct Variable(Rc<VariableInner>);
impl Variable {
pub fn get_name(&self) -> &String {
&self.0.name
}
pub fn assign(&self, v: bool) {
*self.0.assignment.borrow_mut() = Some(v);
}
pub fn unassign(&self) {
*self.0.assignment.borrow_mut() = None;
}
pub fn get_assignment(&self) -> Option<bool> {
self.0.assignment.borrow().clone()
}
pub fn get_literal(&self, neg: bool) -> Literal {
Literal {
neg,
var: self.clone(),
}
}
pub fn get_pos_literal(&self) -> Literal { self.get_literal(false) }
pub fn get_neg_literal(&self) -> Literal { self.get_literal(true) }
}
#[derive(Clone, Debug)]
pub struct Literal {
neg: bool,
var: Variable,
}
impl Literal {
pub fn assign(&self, v: bool) {
self.var.assign(v ^ self.neg);
}
pub fn unassign(&self) {
self.var.unassign();
}
pub fn get_assignment(&self) -> Option<bool> {
self.var.get_assignment().map(|x| x ^ self.neg)
}
}
pub struct Clause {
pub literals: Vec<Literal>,
}

47
src/hash_wrapper.rs

@ -0,0 +1,47 @@
// vim: ts=4 sw=4 et
use core::hash::{Hash, Hasher};
use core::ops::{Deref, DerefMut};
// A type for manipulating how something is hashed.
pub struct HashWrapper<T, U, F>(T, F) where
F: Fn(&T) -> U,
U: Hash;
impl<T, U, F> HashWrapper<T, U, F> where
F: Fn(&T) -> U,
U: Hash {
pub fn into_innner(self) -> T {
self.0
}
}
impl<T, U, F> Hash for HashWrapper<T, U, F> where
F: Fn(&T) -> U,
U: Hash {
fn hash<H: Hasher>(&self, state: &mut H) {
self.1(&self.0).hash(state);
}
}
impl<T, U, F> Deref for HashWrapper<T, U, F> where
F: Fn(&T) -> U,
U: Hash {
type Target = T;
fn deref(&self) -> &T
{
&self.0
}
}
impl<T, U, F> DerefMut for HashWrapper<T, U, F> where
F: Fn(&T) -> U,
U: Hash {
fn deref_mut(&mut self) -> &mut T
{
&mut self.0
}
}

3
src/main.rs

@ -1,4 +1,7 @@
// vim: ts=4 sw=4 et
pub mod clause;
pub mod hash_wrapper;
fn main() {
}
Loading…
Cancel
Save