-
Notifications
You must be signed in to change notification settings - Fork 0
/
getid.h
59 lines (47 loc) · 1.07 KB
/
getid.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
#ifndef HASHWRAPPER_H
#define HASHWRAPPER_H
#include<set>
#include<unordered_map>
template<typename T>
class getId
{
public:
getId(){}
~getId(){}
size_t operator()(T* object)
{
auto savedObjectId = objectToId.find(object);
if(savedObjectId!=objectToId.end())
{
return (*savedObjectId).second;
}
else
{
if(!freeId.empty())
{
auto it_b = freeId.begin();
id = *it_b;
freeId.erase(it_b);
return id;
}
else
{
id = ++maxId;
objectToId.insert({{object,id}});
return id;
}
}
}
private:
size_t id;
static size_t maxId;
static std::set<size_t> freeId;
static std::unordered_map<T*,size_t> objectToId;
};
template<typename T>
size_t getId<T>::maxId = 0;
template<typename T>
std::set<size_t> getId<T>::freeId;
template<typename T>
std::unordered_map<T*,size_t> getId<T>::objectToId;
#endif // HASHWRAPPER_H