Loading...
Searching...
No Matches
index_set_body.icc
Go to the documentation of this file.
#ifndef _RHEOLEF_INDEX_SET_BODY_ICC
#define _RHEOLEF_INDEX_SET_BODY_ICC
//
// this code is shared by:
// - index_set.h (with extension to serialization & mpi)
// - bamg2geo.cc
// - msh2geo.cc
// => avoid code redundancy
//
namespace rheolef {
index_set::index_set (const index_set& x)
: base()
{
for (std::set<size_type>::const_iterator iter = x.base::begin(), last = x.base::end(); iter != last; iter++) {
base::insert (*iter);
}
}
index_set&
index_set::operator= (const index_set& x)
{
base::clear();
for (std::set<size_type>::const_iterator iter = x.base::begin(), last = x.base::end(); iter != last; iter++) {
base::insert (*iter);
}
return *this;
}
index_set&
index_set::operator+= (const index_set& x)
{
for (std::set<size_type>::const_iterator iter = x.base::begin(), last = x.base::end(); iter != last; iter++) {
base::insert (*iter);
}
return *this;
}
std::istream&
operator>> (std::istream& is, index_set& x)
{
typedef index_set::size_type size_type;
size_type n;
is >> n;
x.clear();
for (size_type i = 0; i < n; i++) {
size_type xi;
is >> xi;
x.insert (xi);
}
return is;
}
std::ostream&
operator<< (std::ostream& os, const index_set& x)
{
typedef index_set::size_type size_type;
os << x.base::size() << "\t";
for (std::set<size_type>::const_iterator iter = x.base::begin(), last = x.base::end(); iter != last; iter++) {
os << " " << *iter;
}
return os;
}
// c := a union b
void
set_union (const index_set& a, const index_set& b, index_set& c)
{
c.erase(c.begin(), c.end());
std::set_union (a.begin(), a.end(), b.begin(), b.end(), std::inserter((index_set::base&)c, c.end()));
}
// c := a inter b
void
set_intersection (const index_set& a, const index_set& b, index_set& c)
{
c.erase(c.begin(), c.end());
std::set_intersection (a.begin(), a.end(), b.begin(), b.end(), std::inserter((index_set::base&)c, c.end()));
}
// a := a union b
void
index_set::inplace_union (const index_set& b)
{
const size_type infty = std::numeric_limits<size_type>::max();
iterator iter_a = begin();
iterator last_a = end();
const_iterator iter_b = b.begin();
const_iterator last_b = b.end();
while (iter_a != last_a || iter_b != last_b) {
if (iter_b == last_b) return;
if (iter_a == last_a) {
base::insert(iter_b,last_b);
return;
}
size_type ia = (*iter_a);
size_type ib = (*iter_b);
if (ia == ib) {
iter_a++;
iter_b++;
} else if (ia < ib) {
iter_a++;
} else { // ib < ia
// Set has the important property that inserting a new element
// into a set does not invalidate iterators that point to existing elements.
insert(ib);
iter_b++;
}
}
}
// a := a union b
void
index_set::inplace_intersection (const index_set& b)
{
const size_type infty = std::numeric_limits<size_type>::max();
iterator iter_a = begin();
iterator last_a = end();
const_iterator iter_b = b.begin();
const_iterator last_b = b.end();
while (iter_a != last_a || iter_b != last_b) {
if (iter_a == last_a) return;
if (iter_b == last_b) { // last_b is reached
base::erase(iter_a, last_a);
return;
}
size_type ia = (*iter_a);
size_type ib = (*iter_b);
if (ia == ib) {
iter_a++;
iter_b++;
} else if (ia < ib) {
// Erasing an element from a set also does not invalidate any iterators, except,
// of course, for iterators that actually point to the element that is being erased.
iterator curr_a = iter_a++;
base::erase(curr_a);
} else { // ib < ia
iter_b++;
}
}
}
} // namepace rheolef
#endif // _RHEOLEF_INDEX_SET_BODY_ICC
size_type
field::size_type size_type
Definition branch.cc:430
rheolef::index_set
Definition index_set_header.icc:58
rheolef::index_set::base
std::set< std::size_t > base
Definition index_set_header.icc:63
rheolef::index_set::inplace_intersection
void inplace_intersection(const index_set &b)
Definition index_set_body.icc:126
rheolef::index_set::inplace_union
void inplace_union(const index_set &b)
Definition index_set_body.icc:96
rheolef::index_set::insert
void insert(size_type dis_i)
Definition index_set_header.icc:159
rheolef::index_set::operator=
index_set & operator=(const index_set &x)
Definition index_set_body.icc:40
rheolef::index_set::size_type
std::size_t size_type
Definition index_set_header.icc:65
rheolef::index_set::clear
void clear()
Definition index_set_header.icc:153
rheolef::index_set::operator+=
index_set & operator+=(size_type dis_i)
Definition index_set_header.icc:165
rheolef::index_set::index_set
index_set()
Definition index_set_header.icc:133
rheolef
This file is part of Rheolef.
Definition compiler_eigen.h:39
rheolef::operator<<
std::ostream & operator<<(std::ostream &os, const catchmark &m)
Definition catchmark.h:99
rheolef::set_intersection
void set_intersection(const index_set &a, const index_set &b, index_set &c)
Definition index_set_body.icc:89
rheolef::operator>>
std::istream & operator>>(std::istream &is, const catchmark &m)
Definition catchmark.h:88
rheolef::set_union
void set_union(const index_set &a, const index_set &b, index_set &c)
Definition index_set_body.icc:82