merge TinySet and SortedVector classes
This commit is contained in:
parent
f7ba86d3ed
commit
23b180b6f0
@ -12,7 +12,7 @@ CTNode::mergeSubtree (CTNode* n, bool updateLevels)
|
|||||||
if (updateLevels) {
|
if (updateLevels) {
|
||||||
updateChildLevels (n, level_ + 1);
|
updateChildLevels (n, level_ + 1);
|
||||||
}
|
}
|
||||||
CTChilds::iterator chIt = findChild (n);
|
CTChilds::iterator chIt = childs_.find (n);
|
||||||
if (chIt != childs_.end()) {
|
if (chIt != childs_.end()) {
|
||||||
assert ((*chIt)->symbol() == n->symbol());
|
assert ((*chIt)->symbol() == n->symbol());
|
||||||
const CTChilds& childsToAdd = n->childs();
|
const CTChilds& childsToAdd = n->childs();
|
||||||
@ -31,10 +31,8 @@ CTNode::mergeSubtree (CTNode* n, bool updateLevels)
|
|||||||
void
|
void
|
||||||
CTNode::removeChild (CTNode* child)
|
CTNode::removeChild (CTNode* child)
|
||||||
{
|
{
|
||||||
CTChilds::iterator it;
|
assert (childs_.contains (child));
|
||||||
it = findChild (child);
|
childs_.remove (child);
|
||||||
assert (it != childs_.end());
|
|
||||||
childs_.erase (it);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -119,7 +117,7 @@ CTNode::copySubtree (const CTNode* root1)
|
|||||||
for (CTChilds::const_iterator chIt = n1->childs().begin();
|
for (CTChilds::const_iterator chIt = n1->childs().begin();
|
||||||
chIt != n1->childs().end(); ++ chIt) {
|
chIt != n1->childs().end(); ++ chIt) {
|
||||||
CTNode* chCopy = new CTNode (**chIt);
|
CTNode* chCopy = new CTNode (**chIt);
|
||||||
n2->childs().push_back (chCopy);
|
n2->childs().insert_sorted (chCopy);
|
||||||
if ((*chIt)->nrChilds() != 0) {
|
if ((*chIt)->nrChilds() != 0) {
|
||||||
stack.push_back (StackPair (*chIt, chCopy));
|
stack.push_back (StackPair (*chIt, chCopy));
|
||||||
}
|
}
|
||||||
@ -860,7 +858,7 @@ ConstraintTree::copyLogVar (LogVar X_1, LogVar X_2)
|
|||||||
moveToBottom ({X_1});
|
moveToBottom ({X_1});
|
||||||
CTNodes leafs = getNodesAtLevel (logVars_.size());
|
CTNodes leafs = getNodesAtLevel (logVars_.size());
|
||||||
for (unsigned i = 0; i < leafs.size(); i++) {
|
for (unsigned i = 0; i < leafs.size(); i++) {
|
||||||
leafs[i]->childs().push_back (
|
leafs[i]->childs().insert_sorted (
|
||||||
new CTNode (leafs[i]->symbol(), leafs[i]->level() + 1));
|
new CTNode (leafs[i]->symbol(), leafs[i]->level() + 1));
|
||||||
}
|
}
|
||||||
logVars_.push_back (X_2);
|
logVars_.push_back (X_2);
|
||||||
@ -989,7 +987,7 @@ ConstraintTree::swapLogVar (LogVar X)
|
|||||||
CTNode* childCopy = new CTNode (
|
CTNode* childCopy = new CTNode (
|
||||||
(*ccIt)->symbol(), (*ccIt)->level() + 1, (*gsIt)->childs());
|
(*ccIt)->symbol(), (*ccIt)->level() + 1, (*gsIt)->childs());
|
||||||
(*gsIt)->removeChilds();
|
(*gsIt)->removeChilds();
|
||||||
(*gsIt)->childs().push_back (childCopy);
|
(*gsIt)->childs().insert_sorted (childCopy);
|
||||||
(*gsIt)->setLevel ((*gsIt)->level() - 1);
|
(*gsIt)->setLevel ((*gsIt)->level() - 1);
|
||||||
(*nodeIt)->mergeSubtree ((*gsIt), false);
|
(*nodeIt)->mergeSubtree ((*gsIt), false);
|
||||||
}
|
}
|
||||||
@ -1114,18 +1112,18 @@ ConstraintTree::split (
|
|||||||
chIt1 != childs1.end(); ++ chIt1) {
|
chIt1 != childs1.end(); ++ chIt1) {
|
||||||
CTChilds::iterator chIt2 = n2->findSymbol ((*chIt1)->symbol());
|
CTChilds::iterator chIt2 = n2->findSymbol ((*chIt1)->symbol());
|
||||||
if (chIt2 == n2->childs().end()) {
|
if (chIt2 == n2->childs().end()) {
|
||||||
exclChilds.push_back (CTNode::copySubtree (*chIt1));
|
exclChilds.insert_sorted (CTNode::copySubtree (*chIt1));
|
||||||
} else {
|
} else {
|
||||||
if ((*chIt1)->level() == stopLevel) {
|
if ((*chIt1)->level() == stopLevel) {
|
||||||
commChilds.push_back (CTNode::copySubtree (*chIt1));
|
commChilds.insert_sorted (CTNode::copySubtree (*chIt1));
|
||||||
} else {
|
} else {
|
||||||
CTChilds lowerCommChilds, lowerExclChilds;
|
CTChilds lowerCommChilds, lowerExclChilds;
|
||||||
split (*chIt1, *chIt2, lowerCommChilds, lowerExclChilds, stopLevel);
|
split (*chIt1, *chIt2, lowerCommChilds, lowerExclChilds, stopLevel);
|
||||||
if (lowerCommChilds.empty() == false) {
|
if (lowerCommChilds.empty() == false) {
|
||||||
commChilds.push_back (new CTNode (**chIt1, lowerCommChilds));
|
commChilds.insert_sorted (new CTNode (**chIt1, lowerCommChilds));
|
||||||
}
|
}
|
||||||
if (lowerExclChilds.empty() == false) {
|
if (lowerExclChilds.empty() == false) {
|
||||||
exclChilds.push_back (new CTNode (**chIt1, lowerExclChilds));
|
exclChilds.insert_sorted (new CTNode (**chIt1, lowerExclChilds));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ class CTNode
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
typedef SortedVector<CTNode*, CompareSymbol> CTChilds_;
|
typedef TinySet<CTNode*, CompareSymbol> CTChilds_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -64,11 +64,6 @@ class CTNode
|
|||||||
|
|
||||||
bool isLeaf (void) const { return childs_.empty(); }
|
bool isLeaf (void) const { return childs_.empty(); }
|
||||||
|
|
||||||
CTChilds_::iterator findChild (CTNode* n)
|
|
||||||
{
|
|
||||||
return childs_.find (n);
|
|
||||||
}
|
|
||||||
|
|
||||||
CTChilds_::iterator findSymbol (Symbol symb)
|
CTChilds_::iterator findSymbol (Symbol symb)
|
||||||
{
|
{
|
||||||
CTNode tmp (symb, 0);
|
CTNode tmp (symb, 0);
|
||||||
@ -102,7 +97,7 @@ class CTNode
|
|||||||
ostream& operator<< (ostream &out, const CTNode&);
|
ostream& operator<< (ostream &out, const CTNode&);
|
||||||
|
|
||||||
|
|
||||||
typedef SortedVector<CTNode*, CTNode::CompareSymbol> CTChilds;
|
typedef TinySet<CTNode*, CTNode::CompareSymbol> CTChilds;
|
||||||
|
|
||||||
|
|
||||||
class ConstraintTree
|
class ConstraintTree
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
- Refactor sum out in factor
|
- Refactor sum out in factor
|
||||||
- Add a way to sum out several vars at the same time
|
- Add a way to sum out several vars at the same time
|
||||||
- Receive ranges as a constant reference in Indexer
|
- Receive ranges as a constant reference in Indexer
|
||||||
- Merge TinySet and SortedVector classes
|
|
||||||
- Check if evidence remains in the compressed factor graph
|
- Check if evidence remains in the compressed factor graph
|
||||||
- Consider using hashs instead of vectors of colors to calculate the groups in
|
- Consider using hashs instead of vectors of colors to calculate the groups in
|
||||||
counting bp
|
counting bp
|
||||||
- use more psize_t instead of unsigned for looping through params
|
- use more psize_t instead of unsigned for looping through params
|
||||||
- use more Util::abort and Util::vectorIndex
|
- use more Util::abort and Util::vectorIndex
|
||||||
- LogVar should not cast to int
|
|
||||||
|
|
||||||
|
@ -7,53 +7,73 @@
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
template <typename T, typename Compare = std::less<T>>
|
||||||
class TinySet
|
class TinySet
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TinySet (void) { }
|
|
||||||
|
|
||||||
TinySet (const T& t)
|
TinySet (const TinySet& s)
|
||||||
|
: vec_(s.vec_), cmp_(s.cmp_) { }
|
||||||
|
|
||||||
|
TinySet (const Compare& cmp = Compare())
|
||||||
|
: vec_(), cmp_(cmp) { }
|
||||||
|
|
||||||
|
TinySet (const T& t, const Compare& cmp = Compare())
|
||||||
|
: vec_(1, t), cmp_(cmp) { }
|
||||||
|
|
||||||
|
TinySet (const vector<T>& elements, const Compare& cmp = Compare())
|
||||||
|
: vec_(elements), cmp_(cmp)
|
||||||
{
|
{
|
||||||
elements_.push_back (t);
|
std::sort (begin(), end(), cmp_);
|
||||||
}
|
}
|
||||||
|
|
||||||
TinySet (const vector<T>& elements)
|
typedef typename vector<T>::iterator iterator;
|
||||||
|
typedef typename vector<T>::const_iterator const_iterator;
|
||||||
|
|
||||||
|
iterator insert (const T& t)
|
||||||
{
|
{
|
||||||
elements_.reserve (elements.size());
|
iterator it = std::lower_bound (begin(), end(), t, cmp_);
|
||||||
for (unsigned i = 0; i < elements.size(); i++) {
|
if (it == end() || cmp_(t, *it)) {
|
||||||
insert (elements[i]);
|
vec_.insert (it, t);
|
||||||
}
|
}
|
||||||
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
TinySet (const TinySet<T>& s) : elements_(s.elements_) { }
|
void insert_sorted (const T& t)
|
||||||
|
|
||||||
void insert (const T& t)
|
|
||||||
{
|
{
|
||||||
typename vector<T>::iterator it;
|
vec_.push_back (t);
|
||||||
it = std::lower_bound (elements_.begin(), elements_.end(), t);
|
assert (consistent());
|
||||||
if (it == elements_.end() || *it != t) {
|
|
||||||
elements_.insert (it, t);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove (const T& t)
|
void remove (const T& t)
|
||||||
{
|
{
|
||||||
typename vector<T>::iterator it;
|
iterator it = std::lower_bound (begin(), end(), t, cmp_);
|
||||||
it = std::lower_bound (elements_.begin(), elements_.end(), t);
|
if (it != end()) {
|
||||||
if (it != elements_.end()) {
|
vec_.erase (it);
|
||||||
elements_.erase (it);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const_iterator find (const T& t) const
|
||||||
|
{
|
||||||
|
const_iterator it = std::lower_bound (begin(), end(), t, cmp_);
|
||||||
|
return it == end() || cmp_(t, *it) ? end() : it;
|
||||||
|
}
|
||||||
|
|
||||||
|
iterator find (const T& t)
|
||||||
|
{
|
||||||
|
iterator it = std::lower_bound (begin(), end(), t, cmp_);
|
||||||
|
return it == end() || cmp_(t, *it) ? end() : it;
|
||||||
|
}
|
||||||
|
|
||||||
/* set union */
|
/* set union */
|
||||||
TinySet operator| (const TinySet& s) const
|
TinySet operator| (const TinySet& s) const
|
||||||
{
|
{
|
||||||
TinySet res;
|
TinySet res;
|
||||||
std::set_union (
|
std::set_union (
|
||||||
elements_.begin(), elements_.end(),
|
vec_.begin(), vec_.end(),
|
||||||
s.elements_.begin(), s.elements_.end(),
|
s.vec_.begin(), s.vec_.end(),
|
||||||
std::back_inserter (res.elements_));
|
std::back_inserter (res.vec_),
|
||||||
|
cmp_);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,9 +82,10 @@ class TinySet
|
|||||||
{
|
{
|
||||||
TinySet res;
|
TinySet res;
|
||||||
std::set_intersection (
|
std::set_intersection (
|
||||||
elements_.begin(), elements_.end(),
|
vec_.begin(), vec_.end(),
|
||||||
s.elements_.begin(), s.elements_.end(),
|
s.vec_.begin(), s.vec_.end(),
|
||||||
std::back_inserter (res.elements_));
|
std::back_inserter (res.vec_),
|
||||||
|
cmp_);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,9 +94,10 @@ class TinySet
|
|||||||
{
|
{
|
||||||
TinySet res;
|
TinySet res;
|
||||||
std::set_difference (
|
std::set_difference (
|
||||||
elements_.begin(), elements_.end(),
|
vec_.begin(), vec_.end(),
|
||||||
s.elements_.begin(), s.elements_.end(),
|
s.vec_.begin(), s.vec_.end(),
|
||||||
std::back_inserter (res.elements_));
|
std::back_inserter (res.vec_),
|
||||||
|
cmp_);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,25 +119,27 @@ class TinySet
|
|||||||
bool contains (const T& t) const
|
bool contains (const T& t) const
|
||||||
{
|
{
|
||||||
return std::binary_search (
|
return std::binary_search (
|
||||||
elements_.begin(), elements_.end(), t);
|
vec_.begin(), vec_.end(), t, cmp_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool contains (const TinySet& s) const
|
bool contains (const TinySet& s) const
|
||||||
{
|
{
|
||||||
return std::includes (
|
return std::includes (
|
||||||
elements_.begin(),
|
vec_.begin(),
|
||||||
elements_.end(),
|
vec_.end(),
|
||||||
s.elements_.begin(),
|
s.vec_.begin(),
|
||||||
s.elements_.end());
|
s.vec_.end(),
|
||||||
|
cmp_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool in (const TinySet& s) const
|
bool in (const TinySet& s) const
|
||||||
{
|
{
|
||||||
return std::includes (
|
return std::includes (
|
||||||
s.elements_.begin(),
|
s.vec_.begin(),
|
||||||
s.elements_.end(),
|
s.vec_.end(),
|
||||||
elements_.begin(),
|
vec_.begin(),
|
||||||
elements_.end());
|
vec_.end(),
|
||||||
|
cmp_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool intersects (const TinySet& s) const
|
bool intersects (const TinySet& s) const
|
||||||
@ -123,148 +147,97 @@ class TinySet
|
|||||||
return (*this & s).size() > 0;
|
return (*this & s).size() > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
T operator[] (unsigned i) const
|
const T& operator[] (typename vector<T>::size_type i) const
|
||||||
{
|
{
|
||||||
return elements_[i];
|
return vec_[i];
|
||||||
}
|
|
||||||
|
|
||||||
const vector<T>& elements (void) const
|
|
||||||
{
|
|
||||||
return elements_;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
T front (void) const
|
T front (void) const
|
||||||
{
|
{
|
||||||
return elements_.front();
|
return vec_.front();
|
||||||
|
}
|
||||||
|
|
||||||
|
T& front (void)
|
||||||
|
{
|
||||||
|
return vec_.front();
|
||||||
}
|
}
|
||||||
|
|
||||||
T back (void) const
|
T back (void) const
|
||||||
{
|
{
|
||||||
return elements_.back();
|
return vec_.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned size (void) const
|
T& back (void)
|
||||||
{
|
{
|
||||||
return elements_.size();
|
return vec_.back();
|
||||||
|
}
|
||||||
|
|
||||||
|
const vector<T>& elements (void) const
|
||||||
|
{
|
||||||
|
return vec_;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool empty (void) const
|
bool empty (void) const
|
||||||
{
|
{
|
||||||
return elements_.size() == 0;
|
return size() == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef typename std::vector<T>::const_iterator const_iterator;
|
typename vector<T>::size_type size (void) const
|
||||||
|
|
||||||
const_iterator begin (void) const
|
|
||||||
{
|
{
|
||||||
return elements_.begin();
|
return vec_.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator end (void) const
|
void clear (void)
|
||||||
{
|
{
|
||||||
return elements_.end();
|
vec_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool operator== (const TinySet& s1, const TinySet& s2)
|
void reserve (typename vector<T>::size_type size)
|
||||||
{
|
{
|
||||||
return s1.elements_ == s2.elements_;
|
vec_.reserve (size);
|
||||||
}
|
}
|
||||||
|
|
||||||
friend bool operator!= (const TinySet& s1, const TinySet& s2)
|
|
||||||
{
|
|
||||||
return s1.elements_ != s2.elements_;
|
|
||||||
}
|
|
||||||
|
|
||||||
friend std::ostream& operator << (std::ostream& out, const TinySet<T>& s)
|
|
||||||
{
|
|
||||||
out << "{" ;
|
|
||||||
for (unsigned i = 0; i < s.size(); i++) {
|
|
||||||
out << ((i != 0) ? "," : "") << s.elements()[i];
|
|
||||||
}
|
|
||||||
out << "}" ;
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
vector<T> elements_;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename Compare = std::less<T>>
|
|
||||||
class SortedVector
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
SortedVector (const Compare& c = Compare()) : vec_(), cmp_(c) { }
|
|
||||||
/*
|
|
||||||
template <class InputIterator>
|
|
||||||
SortedVector (InputIterator first, InputIterator last,
|
|
||||||
const Compare& c = Compare()) : vec_(first, last), cmp_(c)
|
|
||||||
{
|
|
||||||
std::sort (begin(), end(), cmp_);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
typedef typename vector<T>::iterator iterator;
|
|
||||||
typedef typename vector<T>::const_iterator const_iterator;
|
|
||||||
iterator begin (void) { return vec_.begin(); }
|
iterator begin (void) { return vec_.begin(); }
|
||||||
iterator end (void) { return vec_.end(); }
|
iterator end (void) { return vec_.end(); }
|
||||||
const_iterator begin (void) const { return vec_.begin(); }
|
const_iterator begin (void) const { return vec_.begin(); }
|
||||||
const_iterator end (void) const { return vec_.end(); }
|
const_iterator end (void) const { return vec_.end(); }
|
||||||
|
|
||||||
iterator insert (const T& t)
|
friend bool operator== (const TinySet& s1, const TinySet& s2)
|
||||||
{
|
{
|
||||||
iterator i = std::lower_bound (begin(), end(), t, cmp_);
|
return s1.vec_ == s2.vec_;
|
||||||
if (i == end() || cmp_(t, *i))
|
|
||||||
vec_.insert(i, t);
|
|
||||||
return i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void push_back (const T& t)
|
friend bool operator!= (const TinySet& s1, const TinySet& s2)
|
||||||
{
|
{
|
||||||
vec_.push_back (t);
|
return ! (s1.vec_ == s2.vec_);
|
||||||
assert (consistent());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const_iterator find (const T& t) const
|
friend std::ostream& operator << (std::ostream& out, const TinySet& s)
|
||||||
{
|
{
|
||||||
const_iterator i = std::lower_bound (begin(), end(), t, cmp_);
|
out << "{" ;
|
||||||
return i == end() || cmp_(t, *i) ? end() : i;
|
typename vector<T>::size_type i;
|
||||||
|
for (i = 0; i < s.size(); i++) {
|
||||||
|
out << ((i != 0) ? "," : "") << s.vec_[i];
|
||||||
}
|
}
|
||||||
|
out << "}" ;
|
||||||
iterator find (const T& t)
|
return out;
|
||||||
{
|
|
||||||
iterator i = std::lower_bound (begin(), end(), t, cmp_);
|
|
||||||
return i == end() || cmp_(t, *i) ? end() : i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<T>& elements (void) { return vec_; }
|
|
||||||
|
|
||||||
void reserve (unsigned space) { vec_.reserve (space); }
|
|
||||||
|
|
||||||
unsigned size (void) const { return vec_.size(); }
|
|
||||||
|
|
||||||
bool empty (void) const { return vec_.empty(); }
|
|
||||||
|
|
||||||
void clear (void) { vec_.clear(); }
|
|
||||||
|
|
||||||
iterator erase (iterator it) { return vec_.erase (it); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool consistent (void) const
|
bool consistent (void) const
|
||||||
{
|
{
|
||||||
for (unsigned i = 0; i < vec_.size() - 1; i++) {
|
typename vector<T>::size_type i;
|
||||||
if (cmp_(vec_[i], vec_[i+1]) == false) {
|
for (i = 0; i < vec_.size() - 1; i++) {
|
||||||
|
if (cmp_(vec_[i], vec_[i + 1]) == false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<T> vec_;
|
vector<T> vec_;
|
||||||
Compare cmp_;
|
Compare cmp_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
#endif // HORUS_TINYSET_H
|
#endif // HORUS_TINYSET_H
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user