37 typename std::map<KEY, Entry>::iterator parent_;
42 typedef typename std::map<KEY, Entry> Map;
43 typedef typename Map::iterator iterator;
47 iterator
find__(
const KEY& key)
const {
48 static const Entry empty;
49 iterator it = entries_.find(key);
51 if (it == entries_.end()) {
52 it = entries_.insert(std::make_pair(key, empty)).first;
53 it->second.parent_ = it;
60 iterator
find_(
const iterator& it)
const {
62 iterator& parent = it->second.parent_;
63 if (parent != it) parent =
find_(parent);
68 inline iterator
find_(
const KEY& key)
const {
69 iterator initial =
find__(key);
70 return find_(initial);
74 typedef std::set<KEY> Set;
80 inline KEY
find(
const KEY& key)
const {
81 iterator root =
find_(key);
86 void merge(
const KEY& x,
const KEY& y) {
88 iterator xRoot =
find_(x);
89 iterator yRoot =
find_(y);
90 if (xRoot == yRoot)
return;
93 if (xRoot->second.rank_ < yRoot->second.rank_)
94 xRoot->second.parent_ = yRoot;
95 else if (xRoot->second.rank_ > yRoot->second.rank_)
96 yRoot->second.parent_ = xRoot;
98 yRoot->second.parent_ = xRoot;
99 xRoot->second.rank_ = xRoot->second.rank_ + 1;
104 std::map<KEY, Set>
sets()
const {
105 std::map<KEY, Set>
sets;
106 iterator it = entries_.begin();
107 for (; it != entries_.end(); it++) {
108 iterator root =
find_(it);
109 sets[root->first].insert(it->first);
118 IndexPair(): std::pair<size_t,size_t>(0,0) {}
119 IndexPair(
size_t i,
size_t j) : std::pair<size_t,size_t>(i,j) {}
120 inline size_t i()
const {
return first; };
121 inline size_t j()
const {
return second; };
std::map< KEY, Set > sets() const
return all sets, i.e. a partition of all elements
Definition: DSFMap.h:104
KEY find(const KEY &key) const
Given key, find the representative key for the set in which it lives.
Definition: DSFMap.h:80
iterator find_(const iterator &it) const
Given iterator to initial entry, find the root Entry.
Definition: DSFMap.h:60
Small utility class for representing a wrappable pairs of ints.
Definition: DSFMap.h:116
DSFMap()
constructor
Definition: DSFMap.h:77
We store the forest in an STL map, but parents are done with pointers.
Definition: DSFMap.h:36
iterator find_(const KEY &key) const
Given key, find the root Entry.
Definition: DSFMap.h:68
iterator find__(const KEY &key) const
Given key, find iterator to initial entry.
Definition: DSFMap.h:47
void merge(const KEY &x, const KEY &y)
Merge two sets.
Definition: DSFMap.h:86
Global functions in a separate testing namespace.
Definition: chartTesting.h:28