Choreonoid  1.8
IdPair.h
Go to the documentation of this file.
1 
6 #ifndef CNOID_UTIL_ID_PAIR_H
7 #define CNOID_UTIL_ID_PAIR_H
8 
9 #include <functional>
10 #include <cstdint>
11 
12 namespace cnoid {
13 
14 template<class T = int> class IdPair
15 {
16 protected:
17  T id[2];
18 
19 public:
20  IdPair(){ }
21 
22  IdPair(T id0, T id1){
23  set(id0, id1);
24  }
25 
26  IdPair(const T* src){
27  if(src[0] <= src[1]){
28  id[0] = src[0];
29  id[1] = src[1];
30  } else {
31  id[0] = src[1];
32  id[1] = src[0];
33  }
34  }
35 
36  void set(T id0, T id1){
37  if(id0 <= id1){
38  id[0] = id0;
39  id[1] = id1;
40  } else {
41  id[0] = id1;
42  id[1] = id0;
43  }
44  }
45 
46  T operator()(int which) const { return id[which]; }
47 
48  T operator[](int which) const { return id[which]; }
49  T& operator[](int which) { return id[which]; }
50 
51  bool operator==(const IdPair& pair2) const {
52  return (id[0] == pair2.id[0] && id[1] == pair2.id[1]);
53  }
54 
55  bool operator<(const IdPair& pair2) const {
56  if(id[0] < pair2.id[0]){
57  return true;
58  } else if(id[0] == pair2.id[0]){
59  return (id[1] < pair2.id[1]);
60  } else {
61  return false;
62  }
63  }
64 };
65 
66 }
67 
68 namespace std {
69 
70 template<class T> struct hash<cnoid::IdPair<T>>
71 {
72  void hash_combine(std::size_t& seed, const T& v) const {
73  std::hash<T> hasher;
74  seed ^= hasher(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
75  }
76  std::size_t operator()(const cnoid::IdPair<T>& idPair) const {
77 
78  std::size_t seed = 0;
79  hash_combine(seed, idPair(0));
80  hash_combine(seed, idPair(1));
81  return seed;
82  }
83 };
84 
85 template<> struct hash<cnoid::IdPair<int32_t>>
86 {
87  std::size_t operator()(const cnoid::IdPair<int32_t>& idPair) const{
88  return hash<int64_t>()(static_cast<int64_t>(idPair(0)) | static_cast<int64_t>(idPair(1)) << 32);
89  }
90 };
91 
92 }
93 
94 #endif
cnoid::IdPair::operator==
bool operator==(const IdPair &pair2) const
Definition: IdPair.h:51
cnoid::IdPair::operator[]
T & operator[](int which)
Definition: IdPair.h:49
cnoid::IdPair::IdPair
IdPair()
Definition: IdPair.h:20
cnoid::IdPair::set
void set(T id0, T id1)
Definition: IdPair.h:36
cnoid::IdPair::operator<
bool operator<(const IdPair &pair2) const
Definition: IdPair.h:55
cnoid::IdPair::IdPair
IdPair(T id0, T id1)
Definition: IdPair.h:22
cnoid::IdPair::operator()
T operator()(int which) const
Definition: IdPair.h:46
cnoid
Definition: AbstractSceneLoader.h:11
cnoid::IdPair::operator[]
T operator[](int which) const
Definition: IdPair.h:48
cnoid::IdPair::id
T id[2]
Definition: IdPair.h:17
cnoid::IdPair
Definition: IdPair.h:14
cnoid::IdPair::IdPair
IdPair(const T *src)
Definition: IdPair.h:26