-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from Schaeff/string-free-flat-absy
Remove Strings from the flat absy
- Loading branch information
Showing
18 changed files
with
967 additions
and
589 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::fmt; | ||
use substitution::Substitution; | ||
|
||
|
||
// A variable in a constraint system | ||
// id > 0 for intermediate variables | ||
// id == 0 for ~one | ||
// id < 0 for public outputs | ||
#[derive(Serialize, Deserialize, Clone, PartialEq, Hash, Eq, Ord, PartialOrd, Copy)] | ||
pub struct FlatVariable { | ||
id: isize, | ||
} | ||
|
||
impl FlatVariable { | ||
pub fn new(id: usize) -> Self { | ||
FlatVariable { | ||
id: 1 + id as isize, | ||
} | ||
} | ||
|
||
pub fn one() -> Self { | ||
FlatVariable { | ||
id: 0, | ||
} | ||
} | ||
|
||
pub fn public(id: usize) -> Self { | ||
FlatVariable { | ||
id: - (id as isize) - 1, | ||
} | ||
} | ||
|
||
pub fn id(&self) -> usize { | ||
assert!(self.id > 0); | ||
(self.id as usize) - 1 | ||
} | ||
} | ||
|
||
impl fmt::Display for FlatVariable { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self.id { | ||
0 => write!(f, "~one"), | ||
i if i > 0 => write!(f, "_{}", i + 1), | ||
i => write!(f, "~out_{}", - (i + 1)) | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Debug for FlatVariable { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "FlatVariable(id: {})", self.id) | ||
} | ||
} | ||
|
||
impl FlatVariable { | ||
pub fn apply_substitution(self, substitution: &Substitution, should_fallback: bool) -> Self { | ||
match should_fallback { | ||
true => substitution.get(&self).unwrap_or(self), | ||
false => substitution.get(&self).unwrap() | ||
} | ||
} | ||
|
||
pub fn is_output(&self) -> bool { | ||
self.id < 0 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn one() { | ||
assert_eq!(FlatVariable::one().id, 0); | ||
} | ||
|
||
#[test] | ||
fn public() { | ||
assert_eq!(FlatVariable::public(0).id, - 1); | ||
assert_eq!(FlatVariable::public(42).id, - 43); | ||
} | ||
|
||
#[test] | ||
fn private() { | ||
assert_eq!(FlatVariable::new(0).id, 1); | ||
assert_eq!(FlatVariable::new(42).id, 43); | ||
} | ||
} |
Oops, something went wrong.