Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant sort #42

Merged
merged 2 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions automata/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use crate::{
try_merge, Check, Ctrl, CurryInput, CurryStack, IllFormed, Input, InputError, ParseError,
RangeMap, Stack, State, Transition,
RangeMap, Stack, State, ToSrc, Transition,
};
use core::{iter, num::NonZeroUsize};
use std::{
Expand Down Expand Up @@ -324,6 +324,7 @@ impl<I: Input, S: Stack, C: Ctrl<I, S>> Graph<I, S, C> {

/// Change nothing about the semantics but sort the internal vector of states.
#[inline]
#[allow(clippy::panic)] // <-- TODO
#[allow(clippy::missing_panics_doc)]
pub fn sort(&mut self) {
// Associate each original index with a concrete state instead of just an index,
Expand All @@ -337,7 +338,12 @@ impl<I: Input, S: Stack, C: Ctrl<I, S>> Graph<I, S, C> {
.clone()
.map_indices(|i| unwrap!(self.states.binary_search(unwrap!(index_map.get(&i)))));
for i in self.tags.values_mut() {
*i = unwrap!(self.states.binary_search(unwrap!(index_map.get(i))));
// *i = unwrap!(self.states.binary_search(unwrap!(index_map.get(i)))); // <-- TODO: reinstate
*i = unwrap!(self.states.binary_search(
index_map
.get(i)
.unwrap_or_else(|| panic!("Couldn't find {i:?} in {:?}", index_map.to_src()))
));
}
// Can't do this in-place since the entire state array is required as an argument.
self.states = self
Expand Down
2 changes: 1 addition & 1 deletion automata/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<I: Input, S: Stack, C: Ctrl<I, S>> Eq for State<I, S, C> {}
impl<I: Input, S: Stack, C: Ctrl<I, S>> PartialEq for State<I, S, C> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.non_accepting == other.non_accepting && self.transitions == other.transitions
self.transitions == other.transitions && self.non_accepting == other.non_accepting
}
}

Expand Down
18 changes: 4 additions & 14 deletions src/fixpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,12 @@ impl<I: Input, S: Stack> ops::Shr<Deterministic<I, S>> for Fixpoint {
type Output = Deterministic<I, S>;
#[inline]
#[allow(clippy::manual_assert, clippy::panic)]
fn shr(self, rhs: Deterministic<I, S>) -> Self::Output {
let Graph {
states,
initial,
mut tags,
} = rhs;
if tags.insert(self.0, initial).is_some() {
fn shr(self, mut rhs: Deterministic<I, S>) -> Self::Output {
if rhs.tags.insert(self.0, rhs.initial).is_some() {
panic!("Fixpoint name already in use");
}
let mut out = Graph {
states,
initial,
tags,
};
out.sort();
out
// rhs.sort();
rhs
}
}

Expand Down
176 changes: 176 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ mod reduced {
use super::*;
use std::panic;

fn fixpoint_unused(parser: Deterministic<u8, u8>, input: Vec<u8>) {
let pre = parser.accept(input.iter().copied());
let post = (fixpoint("unused") >> parser).accept(input);
assert_eq!(pre, post);
}

fn fixpoint_repeat(parser: Deterministic<u8, u8>, both: Vec<u8>) {
parser.check().unwrap();
if parser.accept(iter::empty()).is_err() {
Expand Down Expand Up @@ -159,6 +165,176 @@ mod reduced {
}
*/

#[test]
#[allow(clippy::too_many_lines)]
fn fixpoint_unused_1() {
fixpoint_unused(
Graph {
states: vec![
State {
transitions: CurryStack {
wildcard: None,
map_none: None,
map_some: BTreeMap::new(),
},
non_accepting: BTreeSet::new(),
},
State {
transitions: CurryStack {
wildcard: None,
map_none: None,
map_some: [
(
65,
CurryInput::Scrutinize(RangeMap {
entries: iter::once((
Range {
first: 61,
last: 227,
},
Transition {
dst: 2,
act: Action::Local,
update: update!(|(), _| {}),
},
))
.collect(),
}),
),
(
140,
CurryInput::Wildcard(Transition {
dst: 0,
act: Action::Local,
update: update!(|(), _| {}),
}),
),
]
.into_iter()
.collect(),
},
non_accepting: [
"\u{19}",
"%\u{9d}\u{f}#\u{fff8}",
"1\u{8771}%\u{17}",
"\u{a4}+\u{b372}",
]
.into_iter()
.map(str::to_owned)
.collect(),
},
State {
transitions: CurryStack {
wildcard: Some(CurryInput::Wildcard(Transition {
dst: 0,
act: Action::Pop,
update: update!(|(), _| {}),
})),
map_none: Some(CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
})),
map_some: [
(
18,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
71,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
76,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
116,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
164,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
]
.into_iter()
.collect(),
},
non_accepting: ["", "#", "-\u{80}0", "["]
.into_iter()
.map(str::to_owned)
.collect(),
},
State {
transitions: CurryStack {
wildcard: Some(CurryInput::Wildcard(Transition {
dst: 1,
act: Action::Pop,
update: update!(|(), _| {}),
})),
map_none: None,
map_some: [
(
71,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
164,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
205,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
213,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
(
220,
CurryInput::Scrutinize(RangeMap {
entries: BTreeMap::new(),
}),
),
]
.into_iter()
.collect(),
},
non_accepting: BTreeSet::new(),
},
],
initial: 0,
tags: [
("!/", 0),
("`\u{602}\u{206d}", 0),
("m\u{5920}\u{99}\u{61c}", 0),
("u\u{8b}", 1),
("\u{c4c8}\u{8a}\u{98}\u{e}", 4),
]
.into_iter()
.map(|(k, v)| (k.to_owned(), v))
.collect(),
},
vec![],
);
}

#[test]
fn fixpoint_repeat_1() {
fixpoint_repeat(
Expand Down