tipc
A TIP to LLVM compiler
UnionFind.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <TipType.h>
4 #include <iostream>
5 #include <map>
6 #include <vector>
7 
14 class UnionFind {
15 public:
16  UnionFind() = default;
17  explicit UnionFind(std::vector<std::shared_ptr<TipType>> seed);
18  ~UnionFind() = default;
19 
22  void add(std::vector<std::shared_ptr<TipType>> seed);
23 
24  std::shared_ptr<TipType> find(std::shared_ptr<TipType> t1);
25  void quick_union(std::shared_ptr<TipType> t1, std::shared_ptr<TipType> t2);
26  bool connected(std::shared_ptr<TipType> t1, std::shared_ptr<TipType> t2);
27 
28  friend std::ostream &operator<<(std::ostream &os, const UnionFind &obj);
29 
30 private:
31  // A mapping from terms to parents.
32  std::map<std::shared_ptr<TipType>, std::shared_ptr<TipType>> edges;
33 
34  std::shared_ptr<TipType> lookup(std::shared_ptr<TipType> t);
35 
36  std::shared_ptr<TipType> get_parent(std::shared_ptr<TipType> t);
37 
38  // Returns interred equivalent value or creates new interred value
39  std::shared_ptr<TipType> smart_insert(std::shared_ptr<TipType> t);
40 
41  // Assert datastructure invariants
42  void invariant();
43 
44  std::ostream &print(std::ostream &out) const;
45 };
Specialized implementation of a union-find data structure tailored to work with TipTypes wrapped in s...
Definition: UnionFind.h:14
std::shared_ptr< TipType > find(std::shared_ptr< TipType > t1)
Definition: UnionFind.cpp:81
UnionFind()=default
void quick_union(std::shared_ptr< TipType > t1, std::shared_ptr< TipType > t2)
Definition: UnionFind.cpp:98
friend std::ostream & operator<<(std::ostream &os, const UnionFind &obj)
Definition: UnionFind.cpp:61
bool connected(std::shared_ptr< TipType > t1, std::shared_ptr< TipType > t2)
Definition: UnionFind.cpp:123
~UnionFind()=default
void add(std::vector< std::shared_ptr< TipType >> seed)
add additional types to the union find structure
Definition: UnionFind.cpp:54