1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2016 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Distorted. //
|
5
|
// //
|
6
|
// Distorted is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Distorted is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.library.main;
|
21
|
|
22
|
import java.util.ArrayList;
|
23
|
|
24
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25
|
/**
|
26
|
* Return unique IDs of objects in a lazy way. When we create a new Object, return the lowest unused
|
27
|
* integer. When an objects gets deleted, its ID then gets marked unused. We do not try to 'collapse'
|
28
|
* unused holes.
|
29
|
*/
|
30
|
class DistortedObjectCounter
|
31
|
{
|
32
|
private ArrayList<Integer> mUsed;
|
33
|
private int mFirstUnused;
|
34
|
|
35
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
36
|
|
37
|
DistortedObjectCounter()
|
38
|
{
|
39
|
mUsed = new ArrayList<>();
|
40
|
mFirstUnused = 0;
|
41
|
}
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
int returnNext()
|
46
|
{
|
47
|
int size = mUsed.size();
|
48
|
|
49
|
if( mFirstUnused<size )
|
50
|
{
|
51
|
int ret = mFirstUnused;
|
52
|
|
53
|
mUsed.set(mFirstUnused,1);
|
54
|
|
55
|
int found=-1;
|
56
|
|
57
|
for(int i=mFirstUnused+1; i<size; i++ )
|
58
|
{
|
59
|
if( mUsed.get(i)==0 )
|
60
|
{
|
61
|
found = i;
|
62
|
break;
|
63
|
}
|
64
|
}
|
65
|
|
66
|
mFirstUnused = found<0 ? size : found;
|
67
|
|
68
|
return ret;
|
69
|
}
|
70
|
|
71
|
mUsed.add(1);
|
72
|
mFirstUnused++;
|
73
|
|
74
|
return size;
|
75
|
}
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
void release(int objID)
|
80
|
{
|
81
|
int size = mUsed.size();
|
82
|
|
83
|
if( objID<size && objID>=0 && mUsed.get(objID)==1 )
|
84
|
{
|
85
|
mUsed.set(objID,0);
|
86
|
if( objID<mFirstUnused ) mFirstUnused = objID;
|
87
|
}
|
88
|
}
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
void releaseAll()
|
93
|
{
|
94
|
mUsed.clear();
|
95
|
mFirstUnused = 0;
|
96
|
}
|
97
|
}
|