01: // Copyright (c) 2003, 2004 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.lists;
05:
06: public class PositionManager {
07: static final PositionManager manager = new PositionManager();
08:
09: static public SeqPosition getPositionObject(int ipos) {
10: PositionManager m = manager;
11: synchronized (m) {
12: return m.positions[ipos];
13: }
14: }
15:
16: SeqPosition[] positions = new SeqPosition[50];
17: int[] ivals = new int[50];
18: int freeListHead = -1;
19:
20: private void addToFreeList(int[] ivals, int first, int end) {
21: int head = freeListHead;
22: for (int i = first; i < end; i++) {
23: ivals[i] = head;
24: head = i;
25: }
26: freeListHead = head;
27: }
28:
29: private int getFreeSlot() {
30: int head = freeListHead;
31: if (head < 0) {
32: int old_size = positions.length;
33: SeqPosition[] npositions = new SeqPosition[2 * old_size];
34: int[] nvals = new int[2 * old_size];
35: System.arraycopy(positions, 0, npositions, 0, old_size);
36: System.arraycopy(ivals, 0, nvals, 0, old_size);
37: positions = npositions;
38: ivals = nvals;
39: addToFreeList(nvals, old_size, 2 * old_size);
40: head = freeListHead;
41: }
42: freeListHead = ivals[head];
43: return head;
44: }
45:
46: public PositionManager() {
47: // We don't use positions[0], because ipos==0 is reserved for
48: // createPosition(0, false).
49: addToFreeList(ivals, 1, ivals.length);
50: }
51:
52: public synchronized int register(SeqPosition pos) {
53: int i = getFreeSlot();
54: positions[i] = pos;
55: ivals[i] = -1;
56: return i;
57: }
58:
59: public synchronized void release(int ipos) {
60: SeqPosition pos = positions[ipos];
61: if (pos instanceof ExtPosition)
62: ((ExtPosition) pos).position = -1;
63: positions[ipos] = null;
64: ivals[ipos] = freeListHead;
65: freeListHead = ipos;
66: pos.release();
67: }
68: }
|