dyng
DynamicGraphLayout
identifiers.h
1 /*
2  Copyright 2020 František Bráblík
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 #pragma once
17 
18 #include <type_traits> // std::enable_if, std::is_same, std::integral_constant
19 #include <functional> // std::hash
20 #include <ostream>
21 
22 namespace dyng {
23 
24 // distinctive types for identifying nodes and edges.
25 struct node_id;
26 struct edge_id;
27 
28 template<typename T>
29 using is_id_type = std::integral_constant<bool,
30  std::is_same<T, edge_id>::value || std::is_same<T, node_id>::value>;
31 
32 // enables if T is either node_id or edge_id
33 template<typename T, typename Result>
34 using enable_if_id = typename std::enable_if<dyng::is_id_type<T>::value, Result>::type;
35 
36 // operators for comparision and ostream function overload for edge_id and node_id
37 
38 template<typename T>
39 enable_if_id<T, bool> operator==(const T& a, const T& b) {
40  return a.value == b.value;
41 }
42 
43 template<typename T>
44 enable_if_id<T, bool> operator!=(const T& a, const T& b) {
45  return !(a == b);
46 }
47 
48 template<typename T>
49 enable_if_id<T, bool> operator<(const T& a, const T& b) {
50  return a.value < b.value;
51 }
52 
53 template<typename T>
54 enable_if_id<T, bool> operator>(const T& a, const T& b) {
55  return a.value > b.value;
56 }
57 
58 template<typename T>
59 enable_if_id<T, bool> operator<=(const T& a, const T& b) {
60  return !(a > b);
61 }
62 
63 template<typename T>
64 enable_if_id<T, bool> operator>=(const T& a, const T& b) {
65  return !(a < b);
66 }
67 
68 template <typename T>
69 enable_if_id<T, std::ostream&> operator<<(std::ostream& out, const T& id) {
70  return out << id.value;
71 }
72 
74 
77 struct node_id {
78  unsigned value;
79  node_id(unsigned value)
80  : value(value) {}
81 };
82 
84 
87 struct edge_id {
88  unsigned value;
89  edge_id(unsigned value)
90  : value(value) {}
91 };
92 
93 } // namespace dyng
94 
95 namespace std {
96 
97 template <>
98 struct hash<dyng::node_id>{
99  size_t operator()(const dyng::node_id& id) const {
100  return std::hash<unsigned>()(id.value);
101  }
102 };
103 
104 template <>
105 struct hash<dyng::edge_id>{
106  size_t operator()(const dyng::edge_id& id) const {
107  return std::hash<unsigned>()(id.value);
108  }
109 };
110 
111 } // namespace std
dyng::edge_id
Type used as an identifier for edges.
Definition: identifiers.h:87
dyng::node_id
Type used as an identifier for nodes.
Definition: identifiers.h:77