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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/*
* Copyright 2001-2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*
*/
package javax.media.j3d;
class NnuIdManager {
static int nnuId = 0;
final static int getId() {
if(nnuId == Integer.MAX_VALUE) {
nnuId = 0;
}
return nnuId++;
}
final static int equals(NnuId nnuIdArr[], NnuId key, int start, int end) {
int mid;
mid = start +((end - start)/ 2);
if(nnuIdArr[mid] != null) {
int test = key.equal(nnuIdArr[mid]);
if((test < 0) && (start != mid))
return equals(nnuIdArr, key, start, mid);
else if((test > 0) && (start != mid))
return equals(nnuIdArr, key, mid, end);
else if(test == 0) {
// Since id is not necessary unique, we've to do
// some extra check.
// check for equal reference.
if(key == nnuIdArr[mid]) {
return mid;
}
int temp = mid - 1;
// Look to the left.
while((temp >= start) && (key.equal(nnuIdArr[temp]) == 0)) {
if(key == nnuIdArr[temp]) {
return temp;
}
temp--;
}
// Look to the right.
temp = mid + 1;
while((temp < end) && (key.equal(nnuIdArr[temp]) == 0)) {
if(key == nnuIdArr[temp]) {
return temp;
}
temp++;
}
// Fail equal reference check.
return -1;
}
else
return -1;
}
// A null NnuId object encountered.
return -2;
}
final static boolean equals(NnuId nnuIdArr[], NnuId key, int[] index,
int start, int end) {
int mid;
mid = start +((end - start)/ 2);
if(nnuIdArr[mid] != null) {
int test = key.equal(nnuIdArr[mid]);
if(start != mid) {
if(test < 0) {
return equals(nnuIdArr, key, index, start, mid);
}
else if(test > 0) {
return equals(nnuIdArr, key, index, mid, end);
}
}
else { // (start == mid)
if(test < 0) {
index[0] = mid;
return false;
}
else if(test > 0) {
index[0] = mid+1;
return false;
}
}
// (test == 0)
// Since id is not necessary unique, we've to do
// some extra check.
// check for equal reference.
if(key == nnuIdArr[mid]) {
index[0] = mid;
return true;
}
int temp = mid - 1;
// Look to the left.
while((temp >= start) && (key.equal(nnuIdArr[temp]) == 0)) {
if(key == nnuIdArr[temp]) {
index[0] = temp;
return true;
}
temp--;
}
// Look to the right.
temp = mid + 1;
while((temp < end) && (key.equal(nnuIdArr[temp]) == 0)) {
if(key == nnuIdArr[temp]) {
index[0] = temp;
return true;
}
temp++;
}
// Fail equal reference check.
index[0] = temp;
return false;
}
// A null entry encountered.
// But we still want to return the index where we encounter it.
index[0] = mid;
return false;
}
final static void sort(NnuId nnuIdArr[]) {
if (nnuIdArr.length < 20) {
insertSort(nnuIdArr);
} else {
quicksort(nnuIdArr, 0, nnuIdArr.length-1);
}
}
// Insertion sort on smaller array
final static void insertSort(NnuId nnuIdArr[]) {
for (int i=0; i<nnuIdArr.length; i++) {
for (int j=i; j>0 &&
(nnuIdArr[j-1].getId() > nnuIdArr[j].getId()); j--) {
NnuId temp = nnuIdArr[j];
nnuIdArr[j] = nnuIdArr[j-1];
nnuIdArr[j-1] = temp;
}
}
}
final static void quicksort( NnuId nnuIdArr[], int l, int r ) {
int i = l;
int j = r;
int k = nnuIdArr[(l+r) / 2].getId();
do {
while (nnuIdArr[i].getId() < k) i++;
while (k < nnuIdArr[j].getId()) j--;
if (i<=j) {
NnuId tmp = nnuIdArr[i];
nnuIdArr[i] = nnuIdArr[j];
nnuIdArr[j] = tmp;
i++;
j--;
}
} while (i<=j);
if (l<j) quicksort(nnuIdArr, l,j);
if (l<r) quicksort(nnuIdArr, i,r);
}
// This method assumes that nnuIdArr0 and nnuIdArr1 are sorted.
final static NnuId[] delete( NnuId nnuIdArr0[], NnuId nnuIdArr1[] ) {
int i, index, len;
int curStart =0, newStart =0;
boolean found = false;
int size = nnuIdArr0.length - nnuIdArr1.length;
if(size > 0) {
NnuId newNnuIdArr[] = new NnuId[size];
for (i = 0; i < nnuIdArr1.length; i++) {
index = equals(nnuIdArr0, nnuIdArr1[i], 0, nnuIdArr0.length);
if (index >= 0) {
found = true;
if ((i < (nnuIdArr1.length - 1)) && nnuIdArr1[i].getId() == nnuIdArr1[i + 1].getId()) {
// Remove element from original array
NnuId[] tmpNnuIdArr0 = new NnuId[nnuIdArr0.length - 1];
System.arraycopy(nnuIdArr0, 0, tmpNnuIdArr0, 0, index);
System.arraycopy(nnuIdArr0, index + 1,
tmpNnuIdArr0, index, nnuIdArr0.length - index - 1);
nnuIdArr0 = tmpNnuIdArr0;
} else {
// Copy elements from original array to new array up to
// but not including the element we are removing
if (index == curStart) {
curStart++;
} else {
len = index - curStart;
System.arraycopy(nnuIdArr0, curStart,
newNnuIdArr, newStart, len);
curStart = index + 1;
newStart = newStart + len;
}
}
} else {
found = false;
MasterControl.getCoreLogger().severe("Can't Find matching nnuId.");
}
}
if((found == true) && (curStart < nnuIdArr0.length)) {
len = nnuIdArr0.length - curStart;
System.arraycopy(nnuIdArr0, curStart, newNnuIdArr, newStart, len);
}
return newNnuIdArr;
}
else if( size == 0) {
// Remove all.
}
else {
// We are in trouble !!!
MasterControl.getCoreLogger().severe("Attempt to remove more elements than are present");
}
return null;
}
// This method assumes that nnuIdArr0 and nnuIdArr1 are sorted.
final static NnuId[] merge( NnuId nnuIdArr0[], NnuId nnuIdArr1[] ) {
int index[] = new int[1];
int indexPlus1, blkSize, i, j;
int size = nnuIdArr0.length + nnuIdArr1.length;
NnuId newNnuIdArr[] = new NnuId[size];
// Copy the nnuIdArr0 data into the newly created newNnuIdArr.
System.arraycopy(nnuIdArr0, 0, newNnuIdArr, 0, nnuIdArr0.length);
for(i=nnuIdArr0.length, j=0; i<size; i++, j++) {
// True or false, it doesn't matter.
equals(newNnuIdArr, nnuIdArr1[j], index, 0, i);
if(index[0] == i) { // Append to last.
newNnuIdArr[i] = nnuIdArr1[j];
}
else { // Insert in between array elements.
indexPlus1 = index[0] + 1;
blkSize = i - index[0];
// Shift the later portion of array elements by one position.
// This is the make room for the new data entry.
System.arraycopy(newNnuIdArr, index[0], newNnuIdArr,
indexPlus1, blkSize);
newNnuIdArr[index[0]] = nnuIdArr1[j];
}
}
return newNnuIdArr;
}
final static void replace(NnuId oldObj, NnuId newObj, NnuId nnuIdArr[]) {
int[] index = new int[1];
int lenLess1 = nnuIdArr.length - 1;
int blkSize;
// delete old from nnuIdArr.
index[0] = equals(nnuIdArr, oldObj, 0, nnuIdArr.length);
if(index[0] == lenLess1) {
nnuIdArr[index[0]] = null;
}
else if(index[0] >= 0) {
blkSize = lenLess1 - index[0];
System.arraycopy(nnuIdArr, index[0]+1,
nnuIdArr, index[0], blkSize);
nnuIdArr[lenLess1] = null;
}
else {
MasterControl.getCoreLogger().severe("Can't Find matching nnuId.");
}
// insert new to nnuIdArr.
equals(nnuIdArr, newObj, index, 0, lenLess1);
if(index[0] == lenLess1) { // Append to last.
nnuIdArr[index[0]] = newObj;
}
else { // Insert in between array elements.
blkSize = lenLess1 - index[0];
// Shift the later portion of array elements by one position.
// This is the make room for the new data entry.
System.arraycopy(nnuIdArr, index[0], nnuIdArr,
index[0]+1, blkSize);
nnuIdArr[index[0]] = newObj;
}
}
final static void printIds(NnuId nnuIdArr[]) {
for(int i=0; i<nnuIdArr.length; i++) {
System.err.println("[" + i +"] is " + nnuIdArr[i].getId());
}
}
}
|