Loading [MathJax]/extensions/tex2jax.js

dune-mmesh (unstable)

multiid.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3#ifndef DUNE_MMESH_GRID_MULTIID_HH
4#define DUNE_MMESH_GRID_MULTIID_HH
5
11
12namespace Dune {
13namespace MMeshImpl {
14
15class MultiId {
16 public:
17 using ThisType = MultiId;
18 using T = std::size_t;
19 using VT = std::vector<T>;
20
21 // we use an array as internal storage to make MultiId trivially copyable
22 using Storage = std::array<T, 4>;
23
24 MultiId() : size_(0), hash_(-1) {}
25
26 MultiId(const MultiId& other)
27 : vt_(other.vt_), size_(other.size_), hash_(other.hash_) {}
28
29 MultiId(const std::vector<T>& vt) : size_(vt.size()), hash_(-1) {
30 int i = 0;
31 for (const auto& v : vt) vt_[i++] = v;
32 }
33
34 MultiId(std::initializer_list<T> l) : MultiId(std::vector<T>(l)) {}
35
36 MultiId(T t) : MultiId({t}) {}
37
38 ThisType& operator=(const ThisType& b) {
39 if (this != &b) {
40 vt_ = b.vt_;
41 size_ = b.size_;
42 hash_ = b.hash_;
43 }
44 return *this;
45 }
46
47 bool operator<(const ThisType& b) const {
48 if (size() != b.size()) return size() < b.size();
49
50 for (int i = 0; i < size_; ++i)
51 if (vt_[i] != b.vt_[i]) return vt_[i] < b.vt_[i];
52
53 return false;
54 }
55
56 bool operator==(const ThisType& b) const {
57 if (size() != b.size()) return false;
58
59 for (int i = 0; i < size_; ++i)
60 if (vt_[i] != b.vt_[i]) return false;
61
62 return true;
63 }
64
65 bool operator<=(const ThisType& b) const { return !b.operator<(*this); }
66
67 bool operator!=(const ThisType& b) const { return !operator==(b); }
68
69 std::size_t size() const { return size_; }
70
71 VT vt() const {
72 std::vector<T> vec;
73 vec.reserve(size_);
74 for (int i = 0; i < size_; ++i) vec.push_back(vt_[i]);
75 return vec;
76 }
77
79 std::size_t hash() const {
80 if (hash_ == std::size_t(-1)) {
81 if (size_ == 0) {
82 hash_ = 0;
83 return hash_;
84 }
85
86 static constexpr std::hash<std::size_t> hasher;
87 hash_ = hasher(vt_[0]);
88 for (std::size_t i = 1; i < size_; ++i)
89 hash_ = hash_ ^ (hasher(vt_[i]) << i);
90 }
91 return hash_;
92 }
93
94 private:
95 Storage vt_;
96 std::size_t size_;
97 mutable std::size_t hash_;
98};
99
100} // end namespace MMeshImpl
101} // end namespace Dune
102
103namespace std {
105template <>
106struct hash<Dune::MMeshImpl::MultiId> {
107 size_t operator()(const Dune::MMeshImpl::MultiId& id) const {
108 return id.hash();
109 }
110};
111
113inline ostream& operator<<(ostream& os,
114 const Dune::MMeshImpl::MultiId& multiId) {
115 for (const auto& v : multiId.vt()) os << v << " ";
116 return os;
117}
118} // namespace std
119
120#endif
Some common helper methods.
STL namespace.
ostream & operator<<(ostream &os, const Dune::MMeshImpl::MultiId &multiId)
overload operator<<
Definition: multiid.hh:113
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 12, 23:28, 2025)