1 |
#include "IDGenerator.h" |
2 |
|
3 |
namespace LinuxSampler { |
4 |
|
5 |
IDGenerator::IDGenerator(bool simpleAlgorithm) { |
6 |
previousId = -1; |
7 |
simple = simpleAlgorithm; |
8 |
} |
9 |
|
10 |
int IDGenerator::create() { |
11 |
int newId = previousId + 1; |
12 |
if (newId < 0 /*reached upper value range limit*/ || |
13 |
ids.find(newId) != ids.end() /*already in use*/) |
14 |
{ |
15 |
if (simple) return -1; // whole value range was used, and we dont know what we could pick next |
16 |
|
17 |
newId = -1; |
18 |
for (int i = 0; i >= 0; i++) { |
19 |
if (ids.find(i) == ids.end()) { |
20 |
// we found an unused id ... |
21 |
newId = i; |
22 |
break; |
23 |
} |
24 |
} |
25 |
if (newId < 0) { |
26 |
// we didn't find a free ID, the whole value range is full! |
27 |
return -1; |
28 |
} |
29 |
} |
30 |
|
31 |
if (!simple) ids.insert(newId); |
32 |
previousId = newId; |
33 |
return newId; |
34 |
} |
35 |
|
36 |
void IDGenerator::destroy(int id) { |
37 |
if (simple) return; // nothing to do |
38 |
ids.erase(id); |
39 |
} |
40 |
|
41 |
} // namespace LinuxSampler |