forked from bruderstein/PythonScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicIDManager.cpp
More file actions
147 lines (122 loc) · 2.8 KB
/
DynamicIDManager.cpp
File metadata and controls
147 lines (122 loc) · 2.8 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "stdafx.h"
#include "DynamicIDManager.h"
using namespace std;
void DynamicIDManager::reserve(int quantity)
{
if (quantity > m_capacity)
{
reserveAdditional(quantity - m_capacity);
}
}
void DynamicIDManager::addBlock(int start, int quantity)
{
m_idList.push_back(pair<int, int>(start, quantity));
// Assume no overlaps (should really fix this, but we just need to use this carefully)
m_capacity += quantity;
}
void DynamicIDManager::reserveAdditional(int quantity)
{
int start;
if (allocateIDs(quantity, &start))
{
t_idList::reverse_iterator iter = m_idList.rbegin();
// If this is a continuation of the last block,
// just increase the quantity of the last block
if (iter != m_idList.rend()
&& start == (iter->first + iter->second))
{
iter->second += quantity;
}
else // Otherwise just add a new block
{
m_idList.push_back(pair<int, int>(start, quantity));
}
m_capacity += quantity;
}
}
int DynamicIDManager::begin()
{
m_current = m_idList.begin();
if (m_current == m_idList.end())
{
reserveAdditional(10);
m_current = m_idList.begin();
m_nextID = m_current->first;
}
else
{
m_nextID = m_current->first;
}
return m_nextID;
}
int DynamicIDManager::currentID()
{
return m_nextID;
}
DynamicIDManager& DynamicIDManager::operator++(int)
{
// If nothing has ever been allocated
if (m_current == m_idList.end())
{
reserveAdditional(10);
m_current = m_idList.begin();
m_nextID = m_current->first;
}
else
{
++m_nextID;
if (m_nextID >= (m_current->first + m_current->second))
{
++m_current;
if (m_current == m_idList.end())
{
reserveAdditional(10);
m_current = m_idList.end();
--m_current;
if (m_nextID >= (m_current->first + m_current->second))
{
throw exception("Out of IDs");
}
else
{
// If the current ID is now within the limits of the last block
// then we can just increment m_nextID
if (m_nextID > m_current->first && m_nextID < m_current->first + m_current->second)
{
++m_nextID;
}
else
{
// Otherwise, we can just set the next ID to the first in the last block
m_nextID = m_current->first;
}
}
}
else
{
m_nextID = m_current->first;
}
}
}
return *this;
}
bool DynamicIDManager::allocateIDs(int quantity, int *start)
{
return m_allocator->allocate(quantity, start);
}
bool DynamicIDManager::inRange(int id)
{
bool retVal = false;
list< pair<int, int> >::iterator it = m_idList.begin();
for(;it != m_idList.end(); ++it)
{
if (it->first > id)
break;
if (id >= it->first && (id < (it->first + it->second)))
{
retVal = true;
break;
}
}
return retVal;
}