| [c66ae6] | 1 | /* | 
|---|
|  | 2 | * Adjacency.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Jun 15, 2015 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef ADJACENCY_HPP_ | 
|---|
|  | 9 | #define ADJACENCY_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 | // include config.h | 
|---|
|  | 12 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 13 | #include <config.h> | 
|---|
|  | 14 | #endif | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include <cassert> | 
|---|
|  | 17 | #include <iosfwd> | 
|---|
|  | 18 | #include <utility> | 
|---|
|  | 19 | #include <vector> | 
|---|
|  | 20 |  | 
|---|
|  | 21 | namespace detail { | 
|---|
|  | 22 |  | 
|---|
|  | 23 | struct Adjacency | 
|---|
|  | 24 | { | 
|---|
|  | 25 | //!> typedef for the adjacency index type | 
|---|
|  | 26 | typedef size_t index_t; | 
|---|
|  | 27 |  | 
|---|
|  | 28 | //!> typedef for the structure to store adjacency indices | 
|---|
|  | 29 | typedef std::vector<index_t> indices_t; | 
|---|
|  | 30 |  | 
|---|
|  | 31 | //!> constant for invalid index | 
|---|
|  | 32 | enum { INVALIDINDEX=-1 }; | 
|---|
|  | 33 |  | 
|---|
|  | 34 | /** Constructor for struct adjacency. | 
|---|
|  | 35 | * | 
|---|
|  | 36 | */ | 
|---|
|  | 37 | Adjacency() | 
|---|
|  | 38 | {} | 
|---|
|  | 39 |  | 
|---|
|  | 40 | /** Constructor for struct adjacency. | 
|---|
|  | 41 | * | 
|---|
|  | 42 | * \param _indices initial indices to set | 
|---|
|  | 43 | */ | 
|---|
|  | 44 | Adjacency(const indices_t &_indices) : | 
|---|
|  | 45 | indices(_indices) | 
|---|
|  | 46 | { | 
|---|
|  | 47 | if (!isSorted()) | 
|---|
|  | 48 | sortIndices(); | 
|---|
|  | 49 | } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | /** Setter for the indices of the adjacency. | 
|---|
|  | 52 | * | 
|---|
|  | 53 | * \param _elements indices to be set (need not be sorted) | 
|---|
|  | 54 | */ | 
|---|
|  | 55 | void set(const indices_t &_elements) { | 
|---|
|  | 56 | indices = _elements; | 
|---|
|  | 57 | if (!isSorted()) | 
|---|
|  | 58 | sortIndices(); | 
|---|
|  | 59 | } | 
|---|
|  | 60 |  | 
|---|
|  | 61 | /** Getter for the indices. | 
|---|
|  | 62 | * | 
|---|
|  | 63 | * \return set of sorted indices | 
|---|
|  | 64 | */ | 
|---|
|  | 65 | const indices_t &get() const { | 
|---|
|  | 66 | return indices; | 
|---|
|  | 67 | } | 
|---|
|  | 68 |  | 
|---|
|  | 69 | /** Less comparator for adjacency, comparing indices. | 
|---|
|  | 70 | * | 
|---|
|  | 71 | * \return true - at least one index in this adjacency is smaller | 
|---|
|  | 72 | */ | 
|---|
|  | 73 | bool operator<(const Adjacency &_other) const { | 
|---|
|  | 74 | return indices < _other.indices; | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
|  | 77 | /** Comparator for equivalence of adjacency. | 
|---|
|  | 78 | * | 
|---|
|  | 79 | * \return true - both index triples are the same | 
|---|
|  | 80 | */ | 
|---|
|  | 81 | bool operator==(const Adjacency &_other) const { | 
|---|
|  | 82 | return indices == _other.indices; | 
|---|
|  | 83 | } | 
|---|
|  | 84 |  | 
|---|
|  | 85 | /** Comparator for non-equivalence of adjacency. | 
|---|
|  | 86 | * | 
|---|
|  | 87 | * \return true - both index triples are not the same | 
|---|
|  | 88 | */ | 
|---|
|  | 89 | bool operator!=(const Adjacency &_other) const { | 
|---|
|  | 90 | return !(*this == _other); | 
|---|
|  | 91 | } | 
|---|
|  | 92 |  | 
|---|
|  | 93 | private: | 
|---|
|  | 94 | /** Sort the indices. | 
|---|
|  | 95 | * | 
|---|
|  | 96 | */ | 
|---|
|  | 97 | void sortIndices() { | 
|---|
|  | 98 | std::sort(indices.begin(), indices.end()); | 
|---|
|  | 99 | //              if (indices[0] > indices[1]) { | 
|---|
|  | 100 | //                      if (indices[0] > indices[2]) | 
|---|
|  | 101 | //                              std::swap(indices[0], indices[2]); | 
|---|
|  | 102 | //                      if (indices[0] > indices[1]) | 
|---|
|  | 103 | //                              std::swap(indices[0], indices[1]); | 
|---|
|  | 104 | //              } else { | 
|---|
|  | 105 | //                      // last element in between or up front? | 
|---|
|  | 106 | //                      if (indices[0] > indices[2]) { | 
|---|
|  | 107 | //                              std::swap(indices[1], indices[2]); | 
|---|
|  | 108 | //                              std::swap(indices[0], indices[1]); | 
|---|
|  | 109 | //                      } else | 
|---|
|  | 110 | //                              std::swap(indices[1], indices[2]); | 
|---|
|  | 111 | //              } | 
|---|
|  | 112 | assert( isSorted() ); | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | private: | 
|---|
|  | 116 | /** Check whether indices are sorted. | 
|---|
|  | 117 | * | 
|---|
|  | 118 | * \return true - indices are sorted ascendingly | 
|---|
|  | 119 | */ | 
|---|
|  | 120 | bool isSorted() const | 
|---|
|  | 121 | { | 
|---|
|  | 122 | bool status = true; | 
|---|
|  | 123 | indices_t::const_iterator advancer = indices.begin(); | 
|---|
|  | 124 | indices_t::const_iterator iter = advancer++; | 
|---|
|  | 125 | for (; advancer != indices.end(); iter = advancer++) | 
|---|
|  | 126 | status &= *iter <= *advancer; | 
|---|
|  | 127 | return status; | 
|---|
|  | 128 | } | 
|---|
|  | 129 |  | 
|---|
|  | 130 | private: | 
|---|
|  | 131 | //!> fixed size array for containing indices | 
|---|
|  | 132 | indices_t indices; | 
|---|
|  | 133 | }; | 
|---|
|  | 134 |  | 
|---|
|  | 135 | }; /* namespace detail */ | 
|---|
|  | 136 |  | 
|---|
|  | 137 | std::ostream& operator<<(std::ostream &_ost, const detail::Adjacency &_t) { | 
|---|
|  | 138 | const detail::Adjacency::indices_t &indices = _t.get(); | 
|---|
| [725a1d] | 139 | _ost << "["; | 
|---|
|  | 140 | for (detail::Adjacency::indices_t::const_iterator iter = _t.get().begin(); | 
|---|
|  | 141 | iter != _t.get().end(); ++iter) { | 
|---|
|  | 142 | if (iter != _t.get().begin()) | 
|---|
|  | 143 | _ost << ","; | 
|---|
|  | 144 | _ost << *iter; | 
|---|
|  | 145 | } | 
|---|
| [c66ae6] | 146 | return _ost; | 
|---|
|  | 147 | } | 
|---|
|  | 148 |  | 
|---|
|  | 149 | #endif /* ADJACENCY_HPP_ */ | 
|---|