KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > ObjectArray


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Comparator JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import org.h2.engine.Constants;
12
13 /**
14  * @author Thomas
15  */

16
17 public class ObjectArray {
18     private static final int SIZE_INIT = 4, SIZE_SHRINK = 256;
19
20     private Object JavaDoc[] data;
21     private int size;
22
23     public ObjectArray() {
24         this(SIZE_INIT);
25     }
26     
27     public ObjectArray(int size) {
28         data = new Object JavaDoc[size > 1 ? size : 1];
29     }
30
31     public ObjectArray(Object JavaDoc[] data) {
32         this.data = data;
33         size = data.length;
34     }
35
36     public ObjectArray(Collection JavaDoc collection) {
37         // TODO lib: Collection should not be required
38
size = collection.size();
39         data = new Object JavaDoc[size];
40         Iterator JavaDoc it = collection.iterator();
41         for(int i=0; i<size; i++) {
42             data[i] = it.next();
43         }
44     }
45
46     public void add(Object JavaDoc value) {
47         ensureCapacity(size);
48         data[size++] = value;
49     }
50
51     public Object JavaDoc get(int i) {
52         if (Constants.CHECK && i >= size) {
53             throw new ArrayIndexOutOfBoundsException JavaDoc("i=" + i + " size=" + size);
54         }
55         return data[i];
56     }
57
58     public Object JavaDoc remove(int i) {
59         // TODO performance: the app should (where possible) remove from end to start, to avoid O(n^2)
60
if (Constants.CHECK && i >= size) {
61             throw new ArrayIndexOutOfBoundsException JavaDoc("i=" + i + " size=" + size);
62         }
63         Object JavaDoc value = data[i];
64         System.arraycopy(data, i + 1, data, i, size - i - 1);
65         size--;
66         data[size] = null;
67         // TODO optimization / lib: could shrink ObjectArray on element remove
68
return value;
69     }
70     
71     public void removeRange(int from, int to) {
72         if (Constants.CHECK && (to > size || from > to)) {
73             throw new ArrayIndexOutOfBoundsException JavaDoc("to=" + to + " from="+from+" size=" + size);
74         }
75         System.arraycopy(data, to, data, from, size - to);
76         size -= to - from;
77         for(int i=size + (to-from) - 1; i>=size; i--) {
78             data[i] = null;
79         }
80     }
81     
82     public void setSize(int i) {
83         ensureCapacity(i);
84         this.size = i;
85     }
86
87     private void ensureCapacity(int i) {
88         while (i >= data.length) {
89             Object JavaDoc[] d = new Object JavaDoc[data.length * 2];
90             System.arraycopy(data, 0, d, 0, data.length);
91             data = d;
92         }
93     }
94
95     public void add(int i, Object JavaDoc value) {
96         if (Constants.CHECK && i > size) {
97             throw new ArrayIndexOutOfBoundsException JavaDoc("i=" + i + " size=" + size);
98         }
99         ensureCapacity(size);
100         if (i == size) {
101             add(value);
102         } else {
103             System.arraycopy(data, i, data, i + 1, size - i);
104             data[i] = value;
105             size++;
106         }
107     }
108
109     public void set(int i, Object JavaDoc value) {
110         if (Constants.CHECK && i >= size) {
111             throw new ArrayIndexOutOfBoundsException JavaDoc("i=" + i + " size=" + size);
112         }
113         data[i] = value;
114     }
115
116     public int size() {
117         return size;
118     }
119
120     public void toArray(Object JavaDoc[] array) {
121         for(int i=0; i<size; i++) {
122             array[i] = data[i];
123         }
124     }
125
126     public void clear() {
127         if(data.length > SIZE_SHRINK) {
128             data = new Object JavaDoc[SIZE_INIT];
129         } else {
130             for(int i=0; i<size; i++) {
131                 data[i] = null;
132             }
133         }
134         size = 0;
135     }
136
137     public int indexOf(Object JavaDoc o) {
138         for(int i=0; i<size; i++) {
139             if(data[i] == o) {
140                 return i;
141             }
142         }
143         return -1;
144     }
145
146     public void addAll(ObjectArray list) {
147         for(int i=0; i<list.size; i++) {
148             add(list.get(i));
149         }
150     }
151     
152     private void swap(int l, int r) {
153         Object JavaDoc t = data[r];
154         data[r] = data[l];
155         data[l] = t;
156     }
157     
158     public void sort(Comparator JavaDoc comp) {
159         sort(comp, 0, size-1);
160     }
161     
162     private void sort(Comparator JavaDoc comp, int l, int r) {
163         int i, j;
164         while (r - l > 10) {
165             i = (r + l) >> 1;
166             if (comp.compare(get(l), get(r)) > 0) {
167                 swap(l, r);
168             }
169             if (comp.compare(get(i), get(l)) < 0) {
170                 swap(l, i);
171             } else if (comp.compare(get(i), get(r)) > 0) {
172                 swap(i, r);
173             }
174             j = r - 1;
175             swap(i, j);
176             Object JavaDoc p = get(j);
177             i = l;
178             while (true) {
179                 do {
180                     ++i;
181                 } while (comp.compare(get(i), p) < 0);
182                 do {
183                     --j;
184                 } while (comp.compare(get(j), p) > 0);
185                 if (i >= j) {
186                     break;
187                 }
188                 swap(i, j);
189             }
190             swap(i, r - 1);
191             sort(comp, l, i - 1);
192             l = i + 1;
193         }
194         for (i = l + 1; i <= r; i++) {
195             Object JavaDoc t = get(i);
196             for (j = i - 1; j >= l && (comp.compare(get(j), t) > 0); j--) {
197                 set(j + 1, get(j));
198             }
199             set(j + 1, t);
200         }
201     }
202
203 // public void sortInsertion(Comparator comp) {
204
// for (int i = 1, j; i < size(); i++) {
205
// Object t = get(i);
206
// for (j = i - 1; j >= 0 && (comp.compare(get(j), t) < 0); j--) {
207
// set(j + 1, get(j));
208
// }
209
// set(j + 1, t);
210
// }
211
// }
212

213 }
214
Popular Tags