dune-mmesh 1.4.1-git
Loading...
Searching...
No Matches
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:
18 using T = std::size_t;
20
21 // we use an array as internal storage to make MultiId trivially copyable
23
24 MultiId() : size_(0), hash_(-1) {}
25
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
35
36 MultiId(T t) : MultiId({t}) {}
37
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 {
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
STL namespace.
std::ostream & operator<<(std::ostream &s, const bigunsignedint< k > &x)
virtual void operator()()=0
StackAllocator< U, S > other
Definition multiid.hh:15
bool operator!=(const ThisType &b) const
Definition multiid.hh:67
std::size_t size() const
Definition multiid.hh:69
bool operator<(const ThisType &b) const
Definition multiid.hh:47
ThisType & operator=(const ThisType &b)
Definition multiid.hh:38
bool operator==(const ThisType &b) const
Definition multiid.hh:56
MultiId()
Definition multiid.hh:24
std::size_t hash() const
Hash function with caching.
Definition multiid.hh:79
MultiId(T t)
Definition multiid.hh:36
VT vt() const
Definition multiid.hh:71
std::array< T, 4 > Storage
Definition multiid.hh:22
MultiId(const std::vector< T > &vt)
Definition multiid.hh:29
MultiId(std::initializer_list< T > l)
Definition multiid.hh:34
bool operator<=(const ThisType &b) const
Definition multiid.hh:65
MultiId(const MultiId &other)
Definition multiid.hh:26
Some common helper methods.
T push_back(T... args)
T reserve(T... args)