Skip to content

Commit

Permalink
Remove label borrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
AZWN committed Nov 24, 2023
1 parent 2b8d6e7 commit 76d9c5e
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions scopegraphs-lib/src/resolve/topdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn resolve<'sg, SCOPE, LABEL, DATA>(
sg: &'sg ScopeGraph<SCOPE, LABEL, DATA>,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
data_wellformedness: &impl for<'a> Fn(&'a DATA) -> bool,
label_order: &impl Fn(&EdgeOrData<&LABEL>, &EdgeOrData<&LABEL>) -> bool, // FIXME: LabelOrder trait
label_order: &impl Fn(&EdgeOrData<LABEL>, &EdgeOrData<LABEL>) -> bool, // FIXME: LabelOrder trait
data_order: &impl for<'a> Fn(&'a DATA, &'a DATA) -> bool,
source: &'sg SCOPE,
) -> Env<'sg, SCOPE, LABEL, DATA>
Expand All @@ -25,16 +25,13 @@ where
DATA: Hash + Eq + Clone,
{
// Ugh: how can I make thos borrowed properly?
let labels: Vec<LABEL> = LABEL::iter().collect();
let edges: Vec<EdgeOrData<&LABEL>> = labels
.iter()
let all_edges: Vec<EdgeOrData<LABEL>> = LABEL::iter()
.map(EdgeOrData::Edge)
.chain(once(EdgeOrData::Data))
.collect();
let all_edges: Vec<&EdgeOrData<&LABEL>> = edges.iter().collect();

let context = ResolutionContext {
all_edges: &all_edges,
all_edges,
sg,
data_wellformedness,
label_order,
Expand All @@ -44,28 +41,28 @@ where
context.resolve_all(path_wellformedness, &Path::new(source))
}

struct ResolutionContext<'sg, 'query, 'local, SCOPE, LABEL, DATA, DWF, LO, DO>
struct ResolutionContext<'sg, 'query, SCOPE, LABEL, DATA, DWF, LO, DO>
where
DATA: 'query,
DWF: for<'a> Fn(&'a DATA) -> bool,
LO: Fn(&EdgeOrData<&LABEL>, &EdgeOrData<&LABEL>) -> bool,
LO: Fn(&EdgeOrData<LABEL>, &EdgeOrData<LABEL>) -> bool,
DO: for<'a> Fn(&'a DATA, &'a DATA) -> bool,
{
all_edges: &'local [&'local EdgeOrData<&'local LABEL>],
all_edges: Vec<EdgeOrData<LABEL>>,
sg: &'sg ScopeGraph<SCOPE, LABEL, DATA>,
data_wellformedness: &'query DWF,
label_order: &'query LO, // FIXME: LabelOrder trait
data_order: &'query DO,
}

impl<'sg, 'query, 'local, SCOPE, LABEL, DATA, DWF, LO, DO>
ResolutionContext<'sg, 'query, 'local, SCOPE, LABEL, DATA, DWF, LO, DO>
impl<'sg, 'query, SCOPE, LABEL, DATA, DWF, LO, DO>
ResolutionContext<'sg, 'query, SCOPE, LABEL, DATA, DWF, LO, DO>
where
SCOPE: Hash + Eq + Clone,
LABEL: Label + Hash + Copy,
DATA: Hash + Eq + Clone + 'query,
DWF: for<'a> Fn(&'a DATA) -> bool,
LO: Fn(&EdgeOrData<&LABEL>, &EdgeOrData<&LABEL>) -> bool,
LO: Fn(&EdgeOrData<LABEL>, &EdgeOrData<LABEL>) -> bool,
DO: for<'a> Fn(&'a DATA, &'a DATA) -> bool,
{
fn resolve_all(
Expand All @@ -78,7 +75,7 @@ where
.iter()
.filter(|e| match e {
EdgeOrData::Data => path_wellformedness.is_accepting(),
EdgeOrData::Edge(label) => path_wellformedness.accepts([*label]),
EdgeOrData::Edge(label) => path_wellformedness.accepts([label]),
})
.copied()
.collect();
Expand All @@ -89,7 +86,7 @@ where
fn resolve_edges(
&self,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
edges: &[&EdgeOrData<&LABEL>],
edges: &[EdgeOrData<LABEL>],
path: &Path<'sg, SCOPE, LABEL>,
) -> Env<'sg, SCOPE, LABEL, DATA> {
let max = self.max(edges);
Expand All @@ -106,8 +103,8 @@ where
fn resolve_shadow(
&self,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
edge: &EdgeOrData<&LABEL>,
edges: &[&EdgeOrData<&LABEL>],
edge: EdgeOrData<LABEL>,
edges: &[EdgeOrData<LABEL>],
path: &Path<'sg, SCOPE, LABEL>,
) -> Env<'sg, SCOPE, LABEL, DATA> {
let mut env = Env::new();
Expand All @@ -127,7 +124,7 @@ where
fn resolve_edge(
&self,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
edge: &EdgeOrData<&LABEL>,
edge: EdgeOrData<LABEL>,
path: &Path<'sg, SCOPE, LABEL>,
) -> Env<'sg, SCOPE, LABEL, DATA> {
match edge {
Expand All @@ -139,13 +136,13 @@ where
fn resolve_label(
&self,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
label: &LABEL,
label: LABEL,
path: &Path<'sg, SCOPE, LABEL>,
) -> Env<'sg, SCOPE, LABEL, DATA> {
path_wellformedness.step(label);
path_wellformedness.step(&label);
let mut env = Env::new();
for tgt in self.sg.get_edges(path.target(), label) {
path.step(*label, tgt)
for tgt in self.sg.get_edges(path.target(), &label) {
path.step(label, tgt)
.inspect(|p| env.merge(self.resolve_all(path_wellformedness, p)));
}

Expand All @@ -161,22 +158,22 @@ where
}
}

fn max<'a>(&self, edges: &'a [&'a EdgeOrData<&'a LABEL>]) -> Vec<&'a EdgeOrData<&'a LABEL>> {
fn max(&self, edges: &[EdgeOrData<LABEL>]) -> Vec<EdgeOrData<LABEL>> {
edges
.iter()
.filter(|l| !edges.iter().any(|ll| (self.label_order)(l, ll)))
.copied()
.collect()
}

fn smaller<'a>(
fn smaller(
&self,
edge: &EdgeOrData<&'a LABEL>,
edges: &'a [&'a EdgeOrData<&'a LABEL>],
) -> Vec<&'a EdgeOrData<&'a LABEL>> {
edge: EdgeOrData<LABEL>,
edges: &[EdgeOrData<LABEL>],
) -> Vec<EdgeOrData<LABEL>> {
edges
.iter()
.filter(|l| (self.label_order)(l, edge))
.filter(|l| (self.label_order)(l, &edge))
.copied()
.collect()
}
Expand Down

0 comments on commit 76d9c5e

Please sign in to comment.