00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package com.sleepycat.db.rpcserver;
00011
00012 import java.util.*;
00013
00018 class FreeList {
00019 class FreeIndex {
00020 int index;
00021 FreeIndex(int index) { this.index = index; }
00022 int getIndex() { return index; }
00023 }
00024
00025 Vector items = new Vector();
00026 FreeIndex free_head = null;
00027
00028 public synchronized int add(Object obj) {
00029 int pos;
00030 if (free_head == null) {
00031 pos = items.size();
00032 items.addElement(obj);
00033 if (pos + 1 % 1000 == 0)
00034 Server.err.println(this + " grew to size " + (pos + 1));
00035 } else {
00036 pos = free_head.getIndex();
00037 free_head = (FreeIndex)items.elementAt(pos);
00038 items.setElementAt(obj, pos);
00039 }
00040 return pos;
00041 }
00042
00043 public synchronized void del(int pos) {
00044 Object obj = items.elementAt(pos);
00045 if (obj != null && obj instanceof FreeIndex)
00046 throw new NoSuchElementException("index " + pos + " has already been freed");
00047 items.setElementAt(free_head, pos);
00048 free_head = new FreeIndex(pos);
00049 }
00050
00051 public void del(Object obj) {
00052 del(items.indexOf(obj));
00053 }
00054
00055 public Object get(int pos) {
00056 Object obj = items.elementAt(pos);
00057 if (obj instanceof FreeIndex)
00058 obj = null;
00059 return obj;
00060 }
00061
00062 public LocalIterator iterator() {
00063 return new FreeListIterator();
00064 }
00065
00071 class FreeListIterator implements LocalIterator {
00072 int current;
00073
00074 FreeListIterator() { current = findNext(-1); }
00075
00076 private int findNext(int start) {
00077 int next = start;
00078 while (++next < items.size()) {
00079 Object obj = items.elementAt(next);
00080 if (obj == null || !(obj instanceof FreeIndex))
00081 break;
00082 }
00083 return next;
00084 }
00085
00086 public boolean hasNext() {
00087 return (findNext(current) < items.size());
00088 }
00089
00090 public Object next() {
00091 current = findNext(current);
00092 if (current == items.size())
00093 throw new NoSuchElementException("enumerated past end of FreeList");
00094 return items.elementAt(current);
00095 }
00096
00097 public void remove() {
00098 del(current);
00099 }
00100 }
00101 }