forked from joyieldInc/predixy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathID.h
94 lines (86 loc) · 1.42 KB
/
ID.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* predixy - A high performance and full features proxy for redis.
* Copyright (C) 2017 Joyield, Inc. <[email protected]>
* All rights reserved.
*/
#ifndef _PREDIXY_ID_H_
#define _PREDIXY_ID_H_
#include "Sync.h"
#include <vector>
template<class T>
class TID
{
public:
TID():
mId(++Id)
{
}
long id() const
{
return mId;
}
private:
long mId;
thread_local static long Id;
};
template<class T>
thread_local long TID<T>::Id(0);
template<class T>
class ID
{
public:
ID():
mId(++Id - 1)
{
}
int id() const
{
return mId;
}
static int maxId()
{
return Id;
}
protected:
~ID()
{
}
private:
int mId;
static AtomicInt Id;
};
template<class T>
AtomicInt ID<T>::Id(0);
class IDUnique
{
public:
IDUnique(int sz = 0):
mMarker(sz, false)
{
}
void resize(int sz)
{
if (sz > (int)mMarker.size()) {
mMarker.resize(sz, false);
}
}
template<class C>
int unique(C d, int num)
{
int n = 0;
for (int i = 0; i < num; ++i) {
auto p = d[i];
if (!mMarker[p->id()]) {
mMarker[p->id()] = true;
d[n++] = p;
}
}
for (int i = 0; i < n; ++i) {
mMarker[d[i]->id()] = false;
}
return n;
}
private:
std::vector<bool> mMarker;
};
#endif