10 #include "core/adt/id.h"
25 mutable unsigned Rank;
27 std::unique_ptr<T> Element;
29 Entry(
ID<T> n, std::unique_ptr<T> &&element)
32 , Element(std::move(element))
41 class iterator :
public std::iterator<std::forward_iterator_tag, T *> {
43 bool operator==(
const iterator &that)
const {
return it_ == that.it_; }
44 bool operator!=(
const iterator &that)
const {
return !operator==(that); }
58 T *operator*()
const {
return it_->Element.get(); }
65 typename std::vector<Entry>::iterator it)
74 while (it_ != that_->entries_.end() && !it_->Element.get()) {
83 typename std::vector<Entry>::iterator it_;
89 template <
typename... Args>
90 ID<T> Emplace(Args&&... args)
93 unsigned n = entries_.size();
95 entries_.emplace_back(
97 std::make_unique<T>(
id, std::forward<Args>(args)...)
104 unsigned idxA = Find(idA);
105 unsigned idxB = Find(idB);
112 Entry &entryA = entries_[idxA];
113 Entry &entryB = entries_[idxB];
114 T *a = entryA.Element.get();
115 T *b = entryB.Element.get();
117 if (entryA.Rank < entryB.Rank) {
118 entryA.Parent = idxB;
120 entries_[idxA].Element =
nullptr;
123 entryB.Parent = idxA;
125 entries_[idxB].Element =
nullptr;
126 if (entryA.Rank == entryB.Rank) {
133 T *Map(
ID<T> id)
const
135 return entries_[Find(
id)].Element.get();
138 T *Get(
ID<T> id)
const
140 return entries_[id].Element.get();
146 while (entries_[root].Parent != root) {
147 root = entries_[root].Parent;
149 while (entries_[
id].Parent !=
id) {
150 unsigned parent = entries_[id].Parent;
151 entries_[id].Parent = root;
158 iterator
begin() {
return iterator(
this, entries_.begin()); }
160 iterator
end() {
return iterator(
this, entries_.end()); }
162 unsigned Size()
const {
return size_; }
166 std::vector<Entry> entries_;