-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDownElement.h
68 lines (57 loc) · 1.72 KB
/
DownElement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2017 Technical University of Munich
//
// SPDX-License-Identifier: BSD-3-Clause
/**
* @file
* This file is part of PUML
*
* For conditions of distribution and use, please see the copyright
* notice in the file 'COPYING' at the root directory of this package
* and the copyright notice at https://github.com/TUM-I5/PUMGen
*
* @author Sebastian Rettenberger <[email protected]>
*/
#ifndef PUML_DOWNELEMENT_H
#define PUML_DOWNELEMENT_H
#include <algorithm>
#include <cstring>
#include <functional>
namespace PUML::internal {
/**
* A hashable element defined by the global ids of
* the down elements
*
* @tparam N The number of down elements
*/
template <unsigned int N>
struct DownElement {
/** The global ids of the downward elements */
unsigned long down[N]{};
DownElement(const unsigned long down[N]) {
memcpy(this->down, down, N * sizeof(unsigned long));
std::sort(this->down, this->down + N);
}
auto operator==(const DownElement& other) const -> bool {
return memcmp(down, other.down, N * sizeof(unsigned long)) == 0;
}
};
template <unsigned int N>
struct DownElementHash {
auto operator()(const DownElement<N>& element) const -> std::size_t {
std::size_t h = std::hash<unsigned long>{}(element.down[0]);
for (unsigned int i = 1; i < N; i++) {
hashCombine(h, element.down[i]);
}
return h;
}
/**
* Taken from: https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x
*/
template <typename T>
static void hashCombine(std::size_t& seed, const T& v) {
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
};
} // namespace PUML::internal
#endif // PUML_DOWNELEMENT_H