Skip to content

Commit

Permalink
chore(into_children): avoid having children as root nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed Jun 9, 2024
1 parent 598b3f9 commit 004b567
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 130 deletions.
43 changes: 32 additions & 11 deletions src/core/ir/jit/model.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};

use crate::core::ir::IR;
Expand Down Expand Up @@ -53,6 +54,7 @@ pub struct Field<A: Clone> {
}

const EMPTY_VEC: &Vec<Field<Children>> = &Vec::new();

impl Field<Children> {
#[allow(unused)]
pub fn children(&self) -> &Vec<Field<Children>> {
Expand All @@ -68,12 +70,23 @@ impl Field<Parent> {
self.refs.as_ref().map(|Parent(id)| id)
}

fn into_children(self, fields: &[Field<Parent>]) -> Field<Children> {
fn into_children(
self,
fields: &[Field<Parent>],
set: &mut HashSet<FieldId>,
) -> Option<Field<Children>> {
if set.contains(&self.id) {
return None;
}

let mut children = Vec::new();
for field in fields.iter() {
if let Some(id) = field.parent() {
if *id == self.id {
children.push(field.to_owned().into_children(fields));
if let Some(child) = field.to_owned().into_children(fields, set) {
children.push(child);
set.insert(field.id.to_owned());
}
}
}
}
Expand All @@ -83,15 +96,17 @@ impl Field<Parent> {
} else {
Some(Children(children))
};

Field {
if set.contains(&self.id) {
return None;
}
Some(Field {
id: self.id,
name: self.name,
ir: self.ir,
type_of: self.type_of,
args: self.args,
refs,
}
})
}
}

Expand All @@ -116,12 +131,14 @@ impl<A: Debug + Clone> Debug for Field<A> {

#[derive(Clone)]
pub struct Parent(FieldId);

#[allow(unused)]
impl Parent {
pub fn new(id: FieldId) -> Self {
Parent(id)
}
}

impl Debug for Parent {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Parent({:?})", self.0)
Expand All @@ -139,13 +156,17 @@ pub struct ExecutionPlan {

impl ExecutionPlan {
pub fn new(fields: Vec<Field<Parent>>) -> Self {
let field_children = fields
.clone()
.into_iter()
.map(|f| f.into_children(&fields))
.collect::<Vec<_>>();
let children = Self::make_children(&fields);
Self { parent: fields, children }
}

Self { parent: fields, children: field_children }
fn make_children(fields: &[Field<Parent>]) -> Vec<Field<Children>> {
let mut set = HashSet::new();
fields
.iter()
.cloned()
.filter_map(|f| f.into_children(fields, &mut set))
.collect::<Vec<_>>()
}

#[allow(unused)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,5 @@ ExecutionPlan {
),
),
},
Field {
id: 1,
name: "id",
type_of: Int!,
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,37 +71,5 @@ ExecutionPlan {
),
),
},
Field {
id: 1,
name: "user",
ir: "Some(..)",
type_of: User,
refs: Some(
Children(
[
Field {
id: 2,
name: "id",
type_of: Int!,
},
Field {
id: 3,
name: "name",
type_of: String!,
},
],
),
),
},
Field {
id: 2,
name: "id",
type_of: Int!,
},
Field {
id: 3,
name: "name",
type_of: String!,
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ ExecutionPlan {
),
),
},
Field {
id: 1,
name: "id",
type_of: Int!,
},
Field {
id: 2,
name: "username",
type_of: String!,
},
Field {
id: 3,
name: "posts",
Expand All @@ -130,15 +120,5 @@ ExecutionPlan {
),
),
},
Field {
id: 4,
name: "id",
type_of: Int!,
},
Field {
id: 5,
name: "title",
type_of: String!,
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,35 +190,5 @@ ExecutionPlan {
),
),
},
Field {
id: 1,
name: "id",
type_of: Int!,
},
Field {
id: 2,
name: "name",
type_of: String!,
},
Field {
id: 3,
name: "email",
type_of: String!,
},
Field {
id: 4,
name: "phone",
type_of: String,
},
Field {
id: 5,
name: "website",
type_of: String,
},
Field {
id: 6,
name: "username",
type_of: String!,
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,5 @@ ExecutionPlan {
),
),
},
Field {
id: 1,
name: "user",
ir: "Some(..)",
type_of: User,
refs: Some(
Children(
[
Field {
id: 2,
name: "id",
type_of: Int!,
},
],
),
),
},
Field {
id: 2,
name: "id",
type_of: Int!,
},
],
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,5 @@ ExecutionPlan {
),
),
},
Field {
id: 1,
name: "id",
type_of: Int!,
},
Field {
id: 2,
name: "name",
type_of: String!,
},
],
}

0 comments on commit 004b567

Please sign in to comment.