Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
Kayak UI
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
Bevy Forks
Kayak UI
Commits
a90226ba
Commit
a90226ba
authored
3 years ago
by
Gino Valente
Browse files
Options
Downloads
Patches
Plain Diff
Added UpwardIterator
parent
6307346a
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
kayak_core/src/tree.rs
+92
-15
92 additions, 15 deletions
kayak_core/src/tree.rs
with
92 additions
and
15 deletions
kayak_core/src/tree.rs
+
92
−
15
View file @
a90226ba
use
std
::{
use
std
::{
collections
::
HashMap
,
collections
::
HashMap
,
iter
::
Rev
,
sync
::{
Arc
,
RwLock
},
sync
::{
Arc
,
RwLock
},
};
};
use
std
::
iter
::
Rev
;
use
morphorm
::
Hierarchy
;
use
morphorm
::
Hierarchy
;
...
@@ -664,19 +664,31 @@ impl<'a> Iterator for DownwardIterator<'a> {
...
@@ -664,19 +664,31 @@ impl<'a> Iterator for DownwardIterator<'a> {
}
}
}
}
// pub struct UpwardIterator<'a> {
pub
struct
UpwardIterator
<
'a
>
{
// tree: &'a Tree,
tree
:
&
'a
Tree
,
// current_node: Option<NodeIndex>,
current_node
:
Option
<
Index
>
,
// }
include_self
:
bool
,
}
impl
<
'a
>
UpwardIterator
<
'a
>
{
pub
fn
new
(
tree
:
&
'a
Tree
,
starting_node
:
Option
<
Index
>
,
include_self
:
bool
)
->
Self
{
Self
{
tree
,
current_node
:
starting_node
,
include_self
}
}
}
impl
<
'a
>
Iterator
for
UpwardIterator
<
'a
>
{
type
Item
=
Index
;
// impl<'a> Iterator for UpwardIterator<'a> {
fn
next
(
&
mut
self
)
->
Option
<
Self
::
Item
>
{
// type Item = NodeIndex;
if
self
.include_self
{
self
.include_self
=
false
;
return
self
.current_node
;
}
// // TODO - Needs Testing
self
.current_node
=
self
.tree
.get_parent
(
self
.current_node
?
);
// fn next(&mut self) -> Option<NodeIndex> {
return
self
.current_node
;
// None
}
// }
}
// }
pub
struct
ChildIterator
<
'a
>
{
pub
struct
ChildIterator
<
'a
>
{
pub
tree
:
&
'a
Tree
,
pub
tree
:
&
'a
Tree
,
...
@@ -702,8 +714,9 @@ impl<'a> Hierarchy<'a> for Tree {
...
@@ -702,8 +714,9 @@ impl<'a> Hierarchy<'a> for Tree {
type
ChildIter
=
ChildIterator
<
'a
>
;
type
ChildIter
=
ChildIterator
<
'a
>
;
fn
up_iter
(
&
'a
self
)
->
Self
::
UpIter
{
fn
up_iter
(
&
'a
self
)
->
Self
::
UpIter
{
let
up_iter
=
self
.flatten
()
.into_iter
()
.rev
();
// We need to convert the downwards iterator into a Vec so that we can reverse it.
up_iter
// Morphorm expects the iteration to be the same as Self::DownIter but "in reverse".
self
.flatten
()
.into_iter
()
.rev
()
}
}
fn
down_iter
(
&
'a
self
)
->
Self
::
DownIter
{
fn
down_iter
(
&
'a
self
)
->
Self
::
DownIter
{
...
@@ -780,7 +793,7 @@ impl WidgetTree {
...
@@ -780,7 +793,7 @@ impl WidgetTree {
mod
tests
{
mod
tests
{
use
crate
::
node
::
NodeBuilder
;
use
crate
::
node
::
NodeBuilder
;
use
crate
::{
Arena
,
Index
,
Tree
};
use
crate
::{
Arena
,
Index
,
Tree
};
use
crate
::
tree
::
DownwardIterator
;
use
crate
::
tree
::
{
DownwardIterator
,
UpwardIterator
}
;
#[test]
#[test]
fn
test_tree
()
{
fn
test_tree
()
{
...
@@ -883,6 +896,70 @@ mod tests {
...
@@ -883,6 +896,70 @@ mod tests {
assert_descent!
(
"G"
:
g
->
[]);
assert_descent!
(
"G"
:
g
->
[]);
}
}
#[test]
fn
should_ascend_tree
()
{
let
mut
tree
=
Tree
::
default
();
// Tree Structure:
// A
// B C
// D E F
// G
let
a
=
Index
::
from_raw_parts
(
0
,
0
);
let
b
=
Index
::
from_raw_parts
(
1
,
0
);
let
c
=
Index
::
from_raw_parts
(
2
,
0
);
let
d
=
Index
::
from_raw_parts
(
3
,
0
);
let
e
=
Index
::
from_raw_parts
(
4
,
0
);
let
f
=
Index
::
from_raw_parts
(
5
,
0
);
let
g
=
Index
::
from_raw_parts
(
6
,
0
);
tree
.add
(
a
,
None
);
tree
.add
(
b
,
Some
(
a
));
tree
.add
(
c
,
Some
(
a
));
tree
.add
(
d
,
Some
(
b
));
tree
.add
(
e
,
Some
(
b
));
tree
.add
(
g
,
Some
(
d
));
tree
.add
(
f
,
Some
(
c
));
macro_rules!
assert_ascent
{
(
$title
:
literal
:
$start
:
ident
->
[
$
(
$node
:
ident
),
*
$
(,)
?
]
)
=>
{
let
iter
=
UpwardIterator
::
new
(
&
tree
,
Some
(
$start
),
true
);
let
expected_nodes
=
vec!
[
$start
,
$
(
$node
),
*
];
let
mut
total
=
0
;
for
(
index
,
node
)
in
iter
.enumerate
()
{
let
expected
=
expected_nodes
.get
(
index
);
assert_eq!
(
expected
,
Some
(
&
node
),
"{} (including self) - expected {:?}, but got {:?}"
,
$title
,
expected
,
node
);
total
+=
1
;
}
assert_eq!
(
expected_nodes
.len
(),
total
,
"{} (including self) - expected {} nodes, but got {}"
,
$title
,
expected_nodes
.len
(),
total
);
let
iter
=
UpwardIterator
::
new
(
&
tree
,
Some
(
$start
),
false
);
let
expected_nodes
=
vec!
[
$
(
$node
),
*
];
let
mut
total
=
0
;
for
(
index
,
node
)
in
iter
.enumerate
()
{
let
expected
=
expected_nodes
.get
(
index
);
assert_eq!
(
expected
,
Some
(
&
node
),
"{} (excluding self) - expected {:?}, but got {:?}"
,
$title
,
expected
,
node
);
total
+=
1
;
}
assert_eq!
(
expected_nodes
.len
(),
total
,
"{} (excluding self) - expected {} nodes, but got {}"
,
$title
,
expected_nodes
.len
(),
total
);
};
}
assert_ascent!
(
"A"
:
a
->
[]);
assert_ascent!
(
"B"
:
b
->
[
a
]);
assert_ascent!
(
"C"
:
c
->
[
a
]);
assert_ascent!
(
"D"
:
d
->
[
b
,
a
]);
assert_ascent!
(
"E"
:
e
->
[
b
,
a
]);
assert_ascent!
(
"F"
:
f
->
[
c
,
a
]);
assert_ascent!
(
"G"
:
g
->
[
d
,
b
,
a
]);
}
#[test]
#[test]
fn
should_replace
()
{
fn
should_replace
()
{
let
mut
tree
=
Tree
::
default
();
let
mut
tree
=
Tree
::
default
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment