Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-model
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
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
Renaud Boyer
swh-model
Commits
9cf7a04a
Commit
9cf7a04a
authored
5 years ago
by
vlorentz
Browse files
Options
Downloads
Patches
Plain Diff
Add method MerkleNode.iter_tree, to visit all nodes in the subtree of a node.
parent
c0ce38ed
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/model/merkle.py
+15
-1
15 additions, 1 deletion
swh/model/merkle.py
swh/model/tests/test_merkle.py
+4
-0
4 additions, 0 deletions
swh/model/tests/test_merkle.py
with
19 additions
and
1 deletion
swh/model/merkle.py
+
15
−
1
View file @
9cf7a04a
...
...
@@ -8,7 +8,7 @@
import
abc
import
collections
from
typing
import
List
,
Optional
from
typing
import
Iterator
,
List
,
Optional
,
Set
def
deep_update
(
left
,
right
):
...
...
@@ -273,6 +273,20 @@ class MerkleNode(dict, metaclass=abc.ABCMeta):
for
child
in
self
.
values
():
child
.
reset_collect
()
def
iter_tree
(
self
)
->
Iterator
[
'
MerkleNode
'
]:
"""
Yields all children nodes, recursively. Common nodes are
deduplicated.
"""
yield
from
self
.
_iter_tree
(
set
())
def
_iter_tree
(
self
,
seen
:
Set
[
bytes
])
->
Iterator
[
'
MerkleNode
'
]:
if
self
.
hash
not
in
seen
:
seen
.
add
(
self
.
hash
)
yield
self
for
child
in
self
.
values
():
yield
from
child
.
_iter_tree
(
seen
=
seen
)
class
MerkleLeaf
(
MerkleNode
):
"""
A leaf to a Merkle tree.
...
...
This diff is collapsed.
Click to expand it.
swh/model/tests/test_merkle.py
+
4
−
0
View file @
9cf7a04a
...
...
@@ -184,6 +184,10 @@ class TestMerkleNode(unittest.TestCase):
collected2
=
self
.
root
.
collect
()
self
.
assertEqual
(
collected2
,
{})
def
test_iter_tree
(
self
):
nodes
=
list
(
self
.
root
.
iter_tree
())
self
.
assertCountEqual
(
nodes
,
self
.
nodes
.
values
())
def
test_get
(
self
):
for
key
in
(
b
'
a
'
,
b
'
b
'
,
b
'
c
'
):
self
.
assertEqual
(
self
.
root
[
key
],
self
.
nodes
[
b
'
root/
'
+
key
])
...
...
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