/** * @file key.hpp * @author Julian Iseringhausen * @date Mon Nov 21 13:27:22 2011 * * @brief Class to distinguish the different entities in maps. * */ #ifndef KEY_HPP_ #define KEY_HPP_ #ifndef HAVE_MPI #error MPI is needed to compile this class #endif /* HAVE_MPI */ #include #include #include "grid/grid.hpp" namespace VMG { class Grid; class Index; namespace MPI { class KeyStorage { public: KeyStorage(const Grid& grid) : begin(grid.Global().BeginLocal()), end(grid.Global().EndLocal()), size_local(grid.Local().SizeTotal()), size_global(grid.Global().SizeGlobal()) {} KeyStorage(const Index& begin, const Index& end, const Index& size_local, const Index& size_global) : begin(begin), end(end), size_local(size_local), size_global(size_global) {} KeyStorage(const KeyStorage& other) : begin(other.begin), end(other.end), size_local(other.size_local), size_global(other.size_global) {} ~KeyStorage() {} bool operator==(const KeyStorage& other) const { return this->begin == other.begin && this->end == other.end && this->size_local == other.size_local && this->size_global == other.size_global; } bool operator!=(const KeyStorage& other) const { return !(*this==other); } bool operator<(const KeyStorage& other) const { if (this->begin < other.begin) return true; else if (this->begin != other.begin) return false; else if (this->end < other.end) return true; else if (this->end != other.end) return false; else if (this->size_local < other.size_local) return true; else if (this->size_local != other.size_local) return false; else if (this->size_global < other.size_global) return true; else return false; } private: const Index begin, end, size_local, size_global; const void* ptr; }; class KeySorted { public: KeySorted(const KeySorted& other) { std::list::const_iterator i; this->keys.clear(); for (i=other.keys.begin(); i!=other.keys.end(); ++i) this->keys.push_back(*i); } KeySorted(const Grid& grid) { keys.push_back(KeyStorage(grid)); } KeySorted(const Grid& grid_1, const Grid& grid_2) { keys.push_back(KeyStorage(grid_1)); keys.push_back(KeyStorage(grid_2)); keys.sort(); } KeySorted(const Index& begin, const Index& end, const Index& size_local, const Index& size_global) { keys.push_back(KeyStorage(begin, end, size_local, size_global)); } ~KeySorted() {} bool operator<(const KeySorted& other) const { if (this->keys.size() < other.keys.size()) return true; if (this->keys.size() > other.keys.size()) return false; std::list::const_iterator i1, i2; for (i1=this->keys.begin(), i2=other.keys.begin(); i1!=this->keys.end(), i2!=other.keys.end(); ++i1, ++i2) { if (*i1 < *i2) return true; if (*i1 != *i2) return false; } return false; } private: std::list keys; }; class KeyUnsorted { public: KeyUnsorted(const KeyUnsorted& other) { std::list::const_iterator i; this->keys.clear(); for (i=other.keys.begin(); i!=other.keys.end(); ++i) this->keys.push_back(*i); } KeyUnsorted(const Grid& grid) { keys.push_back(KeyStorage(grid)); } KeyUnsorted(const Grid& grid_1, const Grid& grid_2) { keys.push_back(KeyStorage(grid_1)); keys.push_back(KeyStorage(grid_2)); } KeyUnsorted(const Index& begin, const Index& end, const Index& size_local, const Index& size_global) { keys.push_back(KeyStorage(begin, end, size_local, size_global)); } ~KeyUnsorted() {} bool operator<(const KeyUnsorted& other) const { if (this->keys.size() < other.keys.size()) return true; if (this->keys.size() > other.keys.size()) return false; std::list::const_iterator i1, i2; for (i1=this->keys.begin(), i2=other.keys.begin(); i1!=this->keys.end(), i2!=other.keys.end(); ++i1, ++i2) { if (*i1 < *i2) return true; if (*i1 != *i2) return false; } return false; } private: std::list keys; }; } } #endif /* KEY_HPP_ */