Skip to content

Commit

Permalink
Require Label to be Copy
Browse files Browse the repository at this point in the history
  • Loading branch information
AZWN committed Dec 1, 2023
1 parent d2e169b commit a5479b3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
28 changes: 14 additions & 14 deletions scopegraphs-lib/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ pub mod generic_resolution;
pub mod topdown;

#[derive(Hash, PartialEq, Eq, Debug)]
enum InnerPath<'sg, LABEL: 'sg> {
enum InnerPath<LABEL> {
Start {
source: Scope,
},
Step {
prefix: Path<'sg, LABEL>,
label: &'sg LABEL,
prefix: Path<LABEL>,
label: LABEL,
target: Scope,
},
}

#[derive(Clone)]
pub struct Path<'sg, LABEL> {
inner_path: Arc<InnerPath<'sg, LABEL>>,
pub struct Path<LABEL> {
inner_path: Arc<InnerPath<LABEL>>,
/// Set of all scopes in this path.
///
/// Paths are alternating sequences of scopes and labels.
Expand All @@ -36,7 +36,7 @@ pub struct Path<'sg, LABEL> {
scopes: TrieSet<Scope>,
}

impl<LABEL> PartialEq for Path<'_, LABEL>
impl<LABEL> PartialEq for Path<LABEL>
where
Scope: PartialEq,
LABEL: PartialEq,
Expand All @@ -47,14 +47,14 @@ where
}
}

impl<LABEL> Eq for Path<'_, LABEL>
impl<LABEL> Eq for Path<LABEL>
where
Scope: Eq,
LABEL: Eq,
{
}

impl<LABEL> Hash for Path<'_, LABEL>
impl<LABEL> Hash for Path<LABEL>
where
Scope: Hash,
LABEL: Hash,
Expand All @@ -65,7 +65,7 @@ where
}
}

impl<LABEL> Debug for Path<'_, LABEL>
impl<LABEL> Debug for Path<LABEL>
where
LABEL: Debug,
{
Expand All @@ -78,12 +78,12 @@ where
}

#[derive(Hash, PartialEq, Eq, Debug, Clone)]
pub struct ResolvedPath<'sg, LABEL: 'sg, DATA> {
path: Path<'sg, LABEL>,
pub struct ResolvedPath<'sg, LABEL, DATA> {
path: Path<LABEL>,
data: &'sg DATA,
}

impl<'sg, LABEL> Path<'sg, LABEL> {
impl<LABEL> Path<LABEL> {
pub fn new(source: Scope) -> Self {
Self {
inner_path: Arc::new(InnerPath::Start { source }),
Expand All @@ -98,7 +98,7 @@ impl<'sg, LABEL> Path<'sg, LABEL> {
}
}

pub fn step(&self, label: &'sg LABEL, target: Scope) -> Option<Self> {
pub fn step(&self, label: LABEL, target: Scope) -> Option<Self> {
if self.scopes.search(&target) {
None
} else {
Expand All @@ -116,7 +116,7 @@ impl<'sg, LABEL> Path<'sg, LABEL> {
}
}

pub fn resolve<DATA>(self, data: &'sg DATA) -> ResolvedPath<'sg, LABEL, DATA> {
pub fn resolve<DATA>(self, data: &DATA) -> ResolvedPath<'_, LABEL, DATA> {
ResolvedPath { path: self, data }
}
}
Expand Down
43 changes: 22 additions & 21 deletions scopegraphs-lib/src/resolve/topdown.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::iter;
use std::hash::Hash;
use std::iter;

use crate::{
label::Label,
Expand Down Expand Up @@ -33,20 +33,20 @@ impl<LABEL> Clone for EdgeOrData<'_, LABEL> {
}
impl<LABEL> Copy for EdgeOrData<'_, LABEL> {}

pub fn resolve<'sg: 'query, 'query, LABEL: 'sg, DATA, CMPL>(
pub fn resolve<'sg: 'query, 'query, LABEL, DATA, CMPL>(
sg: &'sg mut ScopeGraph<LABEL, DATA, CMPL>,
path_wellformedness: &'query mut impl RegexMatcher<&'query LABEL>,
path_wellformedness: &'query mut impl for<'a> RegexMatcher<&'a LABEL>,
data_wellformedness: &'query impl DataWellformedness<DATA>,
label_order: &'query impl LabelOrder<LABEL>,
data_order: &'query impl DataOrder<DATA>,
source: Scope,
) -> Env<'sg, LABEL, DATA>
where
LABEL: Label<'sg>,
LABEL: Label<'sg> + Copy + 'sg,
CMPL: Completeness<LABEL, DATA>,
for<'a> CMPL::GetEdgesResult<'a>: Iterator<Item = Scope>,
ResolvedPath<'sg, LABEL, DATA>: Hash + Eq,
Path<'sg, LABEL>: Clone,
Path<LABEL>: Clone,
{
let all_edges: Vec<EdgeOrData<LABEL>> = LABEL::iter_ref()
.map(EdgeOrData::Edge)
Expand Down Expand Up @@ -75,18 +75,19 @@ struct ResolutionContext<'sg: 'query, 'query, LABEL, DATA, CMPL, DWF, LO, DO> {
impl<'sg, 'query, LABEL, DATA, CMPL, DWF, LO, DO>
ResolutionContext<'sg, 'query, LABEL, DATA, CMPL, DWF, LO, DO>
where
LABEL: Copy,
ResolvedPath<'sg, LABEL, DATA>: Hash + Eq,
CMPL: Completeness<LABEL, DATA>,
for<'a> CMPL::GetEdgesResult<'a>: Iterator<Item = Scope>,
DO: DataOrder<DATA>,
DWF: DataWellformedness<DATA>,
LO: LabelOrder<LABEL>,
Path<'sg, LABEL>: Clone,
Path<LABEL>: Clone,
{
fn resolve_all(
&self,
path_wellformedness: &mut impl RegexMatcher<&'query LABEL>,
path: &Path<'sg, LABEL>,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
path: &Path<LABEL>,
) -> Env<'sg, LABEL, DATA> {
let edges: Vec<_> = self
.all_edges
Expand All @@ -103,9 +104,9 @@ where

fn resolve_edges(
&self,
path_wellformedness: &mut impl RegexMatcher<&'query LABEL>,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
edges: &[EdgeOrData<'query, LABEL>],
path: &Path<'sg, LABEL>,
path: &Path<LABEL>,
) -> Env<'sg, LABEL, DATA> {
let max = self.max(edges);
let mut env: Env<LABEL, DATA> = Env::new();
Expand All @@ -120,10 +121,10 @@ where

fn resolve_shadow(
&self,
path_wellformedness: &mut impl RegexMatcher<&'query LABEL>,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
edge: EdgeOrData<'query, LABEL>,
edges: &[EdgeOrData<'query, LABEL>],
path: &Path<'sg, LABEL>,
path: &Path<LABEL>,
) -> Env<'sg, LABEL, DATA> {
let mut env = Env::new();
env.merge(self.resolve_edges(path_wellformedness, edges, path));
Expand All @@ -141,25 +142,25 @@ where

fn resolve_edge(
&self,
path_wellformedness: &mut impl RegexMatcher<&'query LABEL>,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
edge: EdgeOrData<'query, LABEL>,
path: &Path<'sg, LABEL>,
path: &Path<LABEL>,
) -> Env<'sg, LABEL, DATA> {
match edge {
EdgeOrData::Edge(label) => self.resolve_label(path_wellformedness, label, path),
EdgeOrData::Edge(label) => self.resolve_label(path_wellformedness, *label, path),
EdgeOrData::Data => self.resolve_data(path),
}
}

fn resolve_label(
&self,
path_wellformedness: &mut impl RegexMatcher<&'query LABEL>,
label: &'query LABEL,
path: &Path<'sg, LABEL>,
path_wellformedness: &mut impl for<'a> RegexMatcher<&'a LABEL>,
label: LABEL,
path: &Path<LABEL>,
) -> Env<'sg, LABEL, DATA> {
path_wellformedness.step(label);
path_wellformedness.step(&label);
let mut env = Env::new();
let targets =self.sg.get_edges(path.target(), label);
let targets = self.sg.get_edges(path.target(), &label);
for tgt in targets {
if let Some(p) = path.step(label, tgt) {
let sub_env = self.resolve_all(path_wellformedness, &p);
Expand All @@ -170,7 +171,7 @@ where
env
}

fn resolve_data(&self, path: &Path<'sg, LABEL>) -> Env<'sg, LABEL, DATA> {
fn resolve_data(&self, path: &Path<LABEL>) -> Env<'sg, LABEL, DATA> {
let data = self.sg.get_data(path.target());
if (self.data_wellformedness)(data) {
Env::single(path.clone().resolve(data))
Expand Down
10 changes: 7 additions & 3 deletions scopegraphs-lib/src/scopegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,15 @@ where
{
pub fn new_scope(&mut self, data: DATA) -> Scope {
let scope = self.inner_scope_graph.add_scope(data);
self.completeness.borrow_mut().new_scope(&self.inner_scope_graph, scope);
self.completeness
.borrow_mut()
.new_scope(&self.inner_scope_graph, scope);
scope
}

pub fn new_edge(&mut self, src: Scope, lbl: LABEL, dst: Scope) -> CMPL::NewEdgeResult {
self.completeness.borrow_mut()
self.completeness
.borrow_mut()
.new_edge(&mut self.inner_scope_graph, src, lbl, dst)
}

Expand All @@ -143,7 +146,8 @@ where
}

pub fn get_edges<'b: 'a>(&'a self, src: Scope, lbl: &'b LABEL) -> CMPL::GetEdgesResult<'a> {
self.completeness.borrow_mut()
self.completeness
.borrow_mut()
.get_edges(&self.inner_scope_graph, src, lbl)
}
}

0 comments on commit a5479b3

Please sign in to comment.