Small improvements

release/4.3a0
Frank Dellaert 2013-10-26 20:12:49 +00:00
parent 48dd3cb769
commit 4e68feff37
2 changed files with 183 additions and 137 deletions

View File

@ -128,6 +128,12 @@ namespace gtsam {
root_(new Node(l, keyValue, r)) { root_(new Node(l, keyValue, r)) {
} }
/** assignment operator */
BTree & operator= (const BTree & other) {
root_ = other.root_;
return *this;
}
/** Check whether tree is empty */ /** Check whether tree is empty */
bool empty() const { bool empty() const {
return !root_; return !root_;

View File

@ -46,33 +46,65 @@ namespace gtsam {
typedef std::pair<KEY, KEY> KeyLabel; typedef std::pair<KEY, KEY> KeyLabel;
// constructor // constructor
DSF() : Tree() { } DSF() :
Tree() {
}
// constructor // constructor
DSF(const Tree& tree) : Tree(tree) {} DSF(const Tree& tree) :
Tree(tree) {
}
// constructor with a list of unconnected keys // constructor with a list of unconnected keys
DSF(const std::list<KEY>& keys) : Tree() { BOOST_FOREACH(const KEY& key, keys) *this = this->add(key, key); } DSF(const std::list<KEY>& keys) :
Tree() {
BOOST_FOREACH(const KEY& key, keys)
*this = this->add(key, key);
}
// constructor with a set of unconnected keys // constructor with a set of unconnected keys
DSF(const std::set<KEY>& keys) : Tree() { BOOST_FOREACH(const KEY& key, keys) *this = this->add(key, key); } DSF(const std::set<KEY>& keys) :
Tree() {
BOOST_FOREACH(const KEY& key, keys)
*this = this->add(key, key);
}
// create a new singleton, does nothing if already exists // create a new singleton, does nothing if already exists
Self makeSet(const KEY& key) const { if (this->mem(key)) return *this; else return this->add(key, key); } Self makeSet(const KEY& key) const {
if (this->mem(key))
return *this;
else
return this->add(key, key);
}
// create a new singleton, does nothing if already exists
void makeSetInPlace(const KEY& key) {
if (!this->mem(key))
*this = this->add(key, key);
}
// find the label of the set in which {key} lives // find the label of the set in which {key} lives
KEY findSet(const KEY& key) const { KEY findSet(const KEY& key) const {
KEY parent = this->find(key); KEY parent = this->find(key);
return parent == key ? key : findSet(parent); } return parent == key ? key : findSet(parent);
}
// return a ***new*** DSF where x and y are in the same set. No path compression // return a new DSF where x and y are in the same set. No path compression
Self makeUnion(const KEY& key1, const KEY& key2) const { return this->add(findSet(key2), findSet(key1)); } Self makeUnion(const KEY& key1, const KEY& key2) const {
DSF<KEY> copy = *this;
copy.makeUnionInPlace(key1,key2);
return copy;
}
// the in-place version of makeUnion // the in-place version of makeUnion
void makeUnionInPlace(const KEY& key1, const KEY& key2) { *this = this->add(findSet_(key2), findSet_(key1)); } void makeUnionInPlace(const KEY& key1, const KEY& key2) {
*this = this->add(findSet_(key2), findSet_(key1));
}
// create a new singleton with two connected keys // create a new singleton with two connected keys
Self makePair(const KEY& key1, const KEY& key2) const { return makeSet(key1).makeSet(key2).makeUnion(key1, key2); } Self makePair(const KEY& key1, const KEY& key2) const {
return makeSet(key1).makeSet(key2).makeUnion(key1, key2);
}
// create a new singleton with a list of fully connected keys // create a new singleton with a list of fully connected keys
Self makeList(const std::list<KEY>& keys) const { Self makeList(const std::list<KEY>& keys) const {
@ -102,12 +134,15 @@ namespace gtsam {
size_t numSets() const { size_t numSets() const {
size_t num = 0; size_t num = 0;
BOOST_FOREACH(const KeyLabel& pair, (Tree)*this) BOOST_FOREACH(const KeyLabel& pair, (Tree)*this)
if (pair.first == pair.second) num++; if (pair.first == pair.second)
num++;
return num; return num;
} }
// return the numer of keys // return the numer of keys
size_t size() const { return Tree::size(); } size_t size() const {
return Tree::size();
}
// return all sets, i.e. a partition of all elements // return all sets, i.e. a partition of all elements
std::map<KEY, Set> sets() const { std::map<KEY, Set> sets() const {
@ -136,16 +171,21 @@ namespace gtsam {
} }
/** equality */ /** equality */
bool operator==(const Self& t) const { return (Tree)*this == (Tree)t; } bool operator==(const Self& t) const {
return (Tree) *this == (Tree) t;
}
/** inequality */ /** inequality */
bool operator!=(const Self& t) const { return (Tree)*this != (Tree)t; } bool operator!=(const Self& t) const {
return (Tree) *this != (Tree) t;
}
// print the object // print the object
void print(const std::string& name = "DSF") const { void print(const std::string& name = "DSF") const {
std::cout << name << std::endl; std::cout << name << std::endl;
BOOST_FOREACH(const KeyLabel& pair, (Tree)*this) BOOST_FOREACH(const KeyLabel& pair, (Tree)*this)
std::cout << (std::string)pair.first << " " << (std::string)pair.second << std::endl; std::cout << (std::string) pair.first << " " << (std::string) pair.second
<< std::endl;
} }
protected: protected: