/* * Adjacency.hpp * * Created on: Jun 15, 2015 * Author: heber */ #ifndef ADJACENCY_HPP_ #define ADJACENCY_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include namespace detail { struct Adjacency { //!> typedef for the adjacency index type typedef size_t index_t; //!> typedef for the structure to store adjacency indices typedef std::vector indices_t; //!> constant for invalid index enum { INVALIDINDEX=-1 }; /** Constructor for struct adjacency. * */ Adjacency() {} /** Constructor for struct adjacency. * * \param _indices initial indices to set */ Adjacency(const indices_t &_indices) : indices(_indices) { if (!isSorted()) sortIndices(); } /** Setter for the indices of the adjacency. * * \param _elements indices to be set (need not be sorted) */ void set(const indices_t &_elements) { indices = _elements; if (!isSorted()) sortIndices(); } /** Getter for the indices. * * \return set of sorted indices */ const indices_t &get() const { return indices; } /** Less comparator for adjacency, comparing indices. * * \return true - at least one index in this adjacency is smaller */ bool operator<(const Adjacency &_other) const { return indices < _other.indices; } /** Comparator for equivalence of adjacency. * * \return true - both index triples are the same */ bool operator==(const Adjacency &_other) const { return indices == _other.indices; } /** Comparator for non-equivalence of adjacency. * * \return true - both index triples are not the same */ bool operator!=(const Adjacency &_other) const { return !(*this == _other); } private: /** Sort the indices. * */ void sortIndices() { std::sort(indices.begin(), indices.end()); // if (indices[0] > indices[1]) { // if (indices[0] > indices[2]) // std::swap(indices[0], indices[2]); // if (indices[0] > indices[1]) // std::swap(indices[0], indices[1]); // } else { // // last element in between or up front? // if (indices[0] > indices[2]) { // std::swap(indices[1], indices[2]); // std::swap(indices[0], indices[1]); // } else // std::swap(indices[1], indices[2]); // } assert( isSorted() ); } private: /** Check whether indices are sorted. * * \return true - indices are sorted ascendingly */ bool isSorted() const { bool status = true; indices_t::const_iterator advancer = indices.begin(); indices_t::const_iterator iter = advancer++; for (; advancer != indices.end(); iter = advancer++) status &= *iter <= *advancer; return status; } private: //!> fixed size array for containing indices indices_t indices; }; }; /* namespace detail */ std::ostream& operator<<(std::ostream &_ost, const detail::Adjacency &_t) { const detail::Adjacency::indices_t &indices = _t.get(); _ost << "[" << indices[0] << "," << indices[1] << "," << indices[2] << "]"; return _ost; } #endif /* ADJACENCY_HPP_ */