25 char id()
const {
return fID; }
31 SkDebugf(
"%d: id %c", fIndexInSort, fID);
32 if (fNodesIDependOn.
size()) {
34 for (
Node* tmp : fNodesIDependOn) {
38 if (fNodesThatDependOnMe.
size()) {
39 SkDebugf(
" (%d) depend on me: ", fNodesThatDependOnMe.
size());
40 for (
Node* tmp : fNodesThatDependOnMe) {
49 for (
Node* dependedOn : fNodesIDependOn) {
59 return a->indexInSort() >
b->indexInSort();
64 SkASSERT(0 <= index && index < fNodesThatDependOnMe.
size());
65 return fNodesThatDependOnMe[index];
71 explicit Node(
char id) : fID(
id), fIndexInSort(-1), fVisited(
false) {}
75 void addDependency(
Node* dependedOn) {
78 dependedOn->addDependent(
this);
96 : fNodes(numNodesToReserve)
105 tmp->setIndexInSort(fNodes.
size()-1);
137 for (
int i = 0;
i < dependedOn->
size(); ++
i) {
138 if ((*dependedOn)[
i]->indexInSort() < dependent->
indexInSort()) {
139 dependent->addDependency((*dependedOn)[
i]);
143 dependent->addDependency((*dependedOn)[
i]);
147 if (dependedOn->
empty()) {
160 int lowerBound = fNodes.
size();
161 for (
int i = 0;
i < dependedOn->
size(); ++
i) {
162 if ((*dependedOn)[
i]->indexInSort() < lowerBound) {
163 this->shift(lowerBound);
167 this->dfs(dependent, (*dependedOn)[
i]->indexInSort());
173 this->shift(lowerBound);
182 for (
int i = 0;
i < fNodes.
size(); ++
i) {
183 (*actual) += fNodes[
i]->id();
184 if (
i < fNodes.
size()-1) {
193 for (
int i = 0;
i < fNodes.
size(); ++
i) {
202 for (
int i = 0;
i < fNodes.
size(); ++
i) {
209 for (
int i = 0;
i < fStack.
size(); ++
i) {
210 SkDebugf(
"%c/%c ", fStack[
i].fNode->id(), fStack[
i].fDest->id());
217 void validate()
const {
220 for (
int i = 0;
i < fNodes.
size(); ++
i) {
223 fNodes[
i]->validate(fReporter);
227 for (
int i = 0;
i < fStack.
size(); ++
i) {
234 void dfs(
Node* node,
int upperBound) {
235 node->setVisited(
true);
243 this->dfs(dependent, upperBound);
252 void moveNodeInSort(
const sk_sp<Node>& node,
int index) {
254 fNodes[index] = node;
255 node->setIndexInSort(index);
261 bool stackContains(
Node* node) {
262 for (
int i = 0;
i < fStack.
size(); ++
i) {
263 if (node == fStack[
i].fNode.get()) {
275 void shift(
int index) {
277 while (!fStack.
empty()) {
285 node->setVisited(
false);
286 fNodes[index] =
nullptr;
294 this->moveNodeInSort(node, index - numRemoved);
295 fNodes[index] =
nullptr;
298 while (!fStack.
empty() && node.
get() == fStack.
back().fDest) {
305 this->moveNodeInSort(fStack.
back().fNode, index - numRemoved);
349 const SkString kExpectedInitialState(
"q,y,a,z,b,c,w,d,x,r");
359 const SkString kExpectedFinalState(
"q,a,b,c,d,x,y,z,w,r");
388 const SkString kExpectedInitialState(
"y,a,w,b,z,u,c,x,d,v");
398 const SkString kExpectedFirstState(
"a,w,b,u,c,x,y,z,d,v");
408 const SkString kExpectedSecondState(
"a,b,c,d,v,w,u,x,y,z");
445 const SkString kExpected0(
"a,c,b,d");
446 const SkString kExpected1(
"a,b,c,d");
486 const SkString kExpected(
"a,b,c,d,e,f,g");
static void test_2(skiatest::Reporter *reporter)
DEF_TEST(IncrTopoSort, reporter)
static void test_lopsided_binary_tree(skiatest::Reporter *reporter)
static void test_1(skiatest::Reporter *reporter)
static void test_diamond(skiatest::Reporter *reporter)
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
sk_sp< T > sk_ref_sp(T *obj)
#define REPORTER_ASSERT(r, cond,...)
Node * addNode(uint32_t id)
void getActual(SkString *actual) const
void addEdges(SkTDArray< Node * > *dependedOn, Node *dependent)
Graph(int numNodesToReserve, skiatest::Reporter *reporter)
void addEdge(Node *dependedOn, Node *dependent)
void push_back(const T &v)
void removeShuffle(int index)
static float min(float r, float g, float b)
Node * dependent(int index) const
void validate(skiatest::Reporter *reporter) const
int numDependents() const
static bool CompareIndicesGT(Node *const &a, Node *const &b)
def print(*args, **kwargs)