Skip to content

Commit 2ad3e73

Browse files
author
kevyuu
committed
Use visitDependents for discardDependantContents and anyDependantDiscardedContents
1 parent d0a0245 commit 2ad3e73

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

include/nbl/asset/IPreHashed.h

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,66 +43,56 @@ class IPreHashed : public IAsset
4343
{
4444
core::stack<IAsset*> stack;
4545
core::unordered_set<IAsset*> alreadyVisited; // whether we have push the node to the stack
46-
core::unordered_set<IAsset*> alreadyDescended; // whether we have push the children to the stack
47-
auto push = [&stack,&alreadyVisited](IAsset* node) -> void
46+
auto push = [&stack,&alreadyVisited](IAsset* node) -> bool
4847
{
4948
const auto [dummy,inserted] = alreadyVisited.insert(node);
5049
if (inserted)
5150
stack.push(node);
51+
return true;
5252
};
5353
for (const auto& root : roots)
5454
push(root);
5555
while (!stack.empty())
5656
{
5757
auto* entry = stack.top();
58-
const auto [dummy, inserted] = alreadyDescended.insert(entry);
59-
if (inserted)
60-
{
61-
core::unordered_set<IAsset*> dependants = entry->computeDependants();
62-
for (auto* dependant : dependants) push(dependant);
63-
} else
64-
{
65-
// post order traversal does discard
66-
auto* isPrehashed = dynamic_cast<IPreHashed*>(entry);
67-
if (isPrehashed)
68-
isPrehashed->discardContent();
69-
stack.pop();
70-
}
58+
stack.pop();
59+
entry->visitDependents(push);
60+
// post order traversal does discard
61+
auto* isPrehashed = dynamic_cast<IPreHashed*>(entry);
62+
if (isPrehashed)
63+
isPrehashed->discardContent();
7164
}
7265
}
7366
static inline bool anyDependantDiscardedContents(const IAsset* root)
7467
{
7568
core::stack<const IAsset*> stack;
7669
core::unordered_set<const IAsset*> alreadyVisited; // whether we have push the node to the stack
77-
core::unordered_set<const IAsset*> alreadyDescended; // whether we have push the children to the stack
78-
auto push = [&stack,&alreadyVisited](const IAsset* node) -> bool
70+
bool result = false;
71+
auto push = [&stack,&alreadyVisited,&result](const IAsset* node) -> bool
7972
{
80-
if (!node)
81-
return false;
8273
const auto [dummy,inserted] = alreadyVisited.insert(node);
8374
if (inserted)
8475
{
8576
auto* isPrehashed = dynamic_cast<const IPreHashed*>(node);
8677
if (isPrehashed && isPrehashed->missingContent())
87-
return true;
78+
{
79+
stack = {};
80+
result = true;
81+
return false;
82+
}
8883
stack.push(node);
8984
}
90-
return false;
85+
return true;
9186
};
92-
if (push(root))
87+
if (!push(root))
9388
return true;
9489
while (!stack.empty())
9590
{
9691
auto* entry = stack.top();
97-
const auto [dummy, inserted] = alreadyDescended.insert(entry);
98-
if (inserted)
99-
{
100-
core::unordered_set<const IAsset*> dependants = entry->computeDependants();
101-
for (auto* dependant : dependants) push(dependant);
102-
} else
103-
stack.pop();
92+
stack.pop();
93+
entry->visitDependents(push);
10494
}
105-
return false;
95+
return result;
10696
}
10797

10898
protected:

0 commit comments

Comments
 (0)