MCPcopy Create free account
hub / github.com/GothenburgBitFactory/taskwarrior / dependencyIsCircular

Function dependencyIsCircular

src/dependency.cpp:42–82  ·  view source on GitHub ↗

/////////////////////////////////////////////////////////////////////////// Returns true if the supplied task adds a cycle to the dependency chain.

Source from the content-addressed store, hash-verified

40////////////////////////////////////////////////////////////////////////////////
41// Returns true if the supplied task adds a cycle to the dependency chain.
42bool dependencyIsCircular(const Task& task) {
43 // A new task has no UUID assigned yet, and therefore cannot be part of any
44 // dependency chain.
45 if (task.has("uuid")) {
46 auto task_uuid = task.get("uuid");
47
48 std::stack<Task> s;
49 s.push(task);
50
51 std::unordered_set<std::string> visited;
52 visited.insert(task_uuid);
53
54 while (!s.empty()) {
55 Task& current = s.top();
56 auto deps_current = current.getDependencyUUIDs();
57
58 // This is a basic depth first search that always terminates given the
59 // fact that we do not visit any task twice
60 for (const auto& dep : deps_current) {
61 if (Context::getContext().tdb2.get(dep, current)) {
62 auto current_uuid = current.get("uuid");
63
64 if (task_uuid == current_uuid) {
65 // Cycle found, initial task reached for the second time!
66 return true;
67 }
68
69 if (visited.find(current_uuid) == visited.end()) {
70 // Push the task to the stack, if it has not been processed yet
71 s.push(current);
72 visited.insert(current_uuid);
73 }
74 }
75 }
76
77 s.pop();
78 }
79 }
80
81 return false;
82}
83
84////////////////////////////////////////////////////////////////////////////////
85// Determine whether a dependency chain is being broken, assuming that 'task' is

Callers 1

addDependencyMethod · 0.85

Calls 4

getDependencyUUIDsMethod · 0.80
findMethod · 0.80
hasMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected