| Taskflow: A General-purpose Task-parallel Programming System

Loading...
Searching...
No Matches
hash Struct Reference

hash specialization for std::hash<tf::Task> More...

`#include <taskflow/core/task.hpp>`

## Detailed Description

hash specialization for std::hash<tf::Task>

hash specialization for std::hash<tf::TaskView>

The hash value of a tf::Task is computed based on the underlying task node pointer. Two tf::Task objects have the same hash value if and only if they reference the same underlying node in the taskflow graph.

Equality and Inequality

Two tf::Task objects are:

- Equal in hash: when they reference the same task node (same pointer)
- Unequal in hash: when they reference different task nodes

tf::Task objects created from different taskflow calls or different task insertions will have different hash values, even if they represent logically similar work. The hash function is based on the associated object identity (i.e., underlying node pointer), not semantic equivalence.

tf::Taskflow taskflow;
// Create two tasks
tf::Task task1 = taskflow.emplace([]() { std::cout << "Task 1\n"; });
tf::Task task2 = taskflow.emplace([]() { std::cout << "Task 2\n"; });
// Create a copy reference to task1
tf::Task task1_copy = task1;
// Hash values
assert(std::hash<tf::Task>{}(task1) == std::hash<tf::Task>{}(task1_copy));
// task1 and task1_copy reference the same node → same hash
assert(std::hash<tf::Task>{}(task1) != std::hash<tf::Task>{}(task2));
// task1 and task2 are different nodes → different hashes
// Use in hash-based containers
std::unordered_set<tf::Task> task_set;
task_set.insert(task1);
task_set.insert(task2);
assert(task_set.size() == 2); // Both tasks stored
task_set.insert(task1_copy);
assert(task_set.size() == 2); // task1_copy is same node as task1, no new insertion
tf::FlowBuilder::emplace
Task emplace(C &&callable)
creates a static task
Definition flow_builder.hpp:1738
tf::Task
class to create a task handle over a taskflow node
Definition task.hpp:569
tf::Taskflow
class to create a taskflow object
Definition taskflow.hpp:64

See also
  tf::Task::hash_value() for obtaining the hash value directly.

The hash value of a tf::TaskView is computed based on the underlying task node address. Two tf::TaskView objects have the same hash value if and only if they reference the same underlying node in the taskflow graph. The hash function is based on the associated object identity (i.e., underlying node pointer), not semantic equivalence.

Equality and Inequality

Two tf::TaskView objects are:

- Equal in hash: when they reference the same task node
- Unequal in hash: when they reference different task nodes

Since tf::TaskView is a lightweight read-only view (typically passed by reference or returned by graph traversal functions), hash equality follows the same node identity principle as tf::Task: it depends on which task node is being viewed, not on any semantic property of the task.

tf::Taskflow taskflow;
tf::Task task1 = taskflow.emplace([]() {});
tf::Task task2 = taskflow.emplace([]() {});
// TaskView can be created from Task (implicit conversion)
tf::TaskView view1 = task1;
tf::TaskView view2 = task2;
tf::TaskView view1_copy = task1; // Another view of the same node
// Hash values
assert(std::hash<tf::TaskView>{}(view1) == std::hash<tf::TaskView>{}(view1_copy));
// Both views reference the same node → same hash
assert(std::hash<tf::TaskView>{}(view1) != std::hash<tf::TaskView>{}(view2));
// Different nodes → different hashes
// TaskView and Task hash to the same value for the same node
assert(std::hash<tf::TaskView>{}(view1) == std::hash<tf::Task>{}(task1));
// view1 and task1 reference the same node → same hash
// Use in hash-based containers
std::unordered_map<tf::TaskView, int> task_map;
task_map[view1] = 10;
task_map[view2] = 20;
assert(task_map[view1_copy] == 10); // view1_copy views same node as view1

See also
  tf::TaskView::hash_value() for obtaining the hash value directly.

The documentation for this struct was generated from the following file:

- taskflow/core/task.hpp