| 240 | |
| 241 | template <typename Handle> |
| 242 | typename DisjointSet<Handle>::Rep* DisjointSet<Handle>::Find(Handle value) { |
| 243 | auto it = nodes_.find(value); |
| 244 | if (it == nodes_.end()) { |
| 245 | // This is the first time we process this handle, create an entry for it. |
| 246 | Rep* node = new Rep; |
| 247 | node->parent = node; |
| 248 | node->rank = 0; |
| 249 | processor_.ExtractValue(value, &node->value); |
| 250 | nodes_[value] = node; |
| 251 | return node; |
| 252 | } |
| 253 | // Return the representative for the set, which is the root of the tree. Apply |
| 254 | // path compression to speedup future queries. |
| 255 | Rep* node = it->second; |
| 256 | Rep* root = node->parent; |
| 257 | while (root != root->parent) { |
| 258 | root = root->parent; |
| 259 | } |
| 260 | while (node->parent != root) { |
| 261 | Rep* next = node->parent; |
| 262 | node->parent = root; |
| 263 | node = next; |
| 264 | } |
| 265 | return root; |
| 266 | } |
| 267 | |
| 268 | // TODO(dyoon): Move many helper functions in this file (including those within |
| 269 | // SymbolicShapeRefiner class) to shared utils. |