![]() |
Quadcap Embeddable Database |
00001 package com.quadcap.sql.lock; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.util.Comparator; 00042 00043 import com.quadcap.util.Debug; 00044 00045 /** 00046 * 00047 * 00048 * @author Stan Bailes 00049 */ 00050 public class SortedArray { 00051 Comparator compare; 00052 int size; 00053 Object[] array = new Object[32]; 00054 00055 public SortedArray(Comparator compare) { 00056 this.compare = compare; 00057 } 00058 00059 public final int find(Object obj) { 00060 int lo = 0; 00061 int hi = size - 1; 00062 00063 while (lo <= hi) { 00064 int mid = (lo + hi) / 2; 00065 Object m = array[mid]; 00066 int cmp = compare.compare(m, obj); 00067 if (cmp < 0) { 00068 lo = mid + 1; 00069 } else if (cmp > 0) { 00070 hi = mid - 1; 00071 } else { 00072 return mid; 00073 } 00074 } 00075 return 0 - (lo + 1); 00076 } 00077 00078 //#ifdef PARANOID 00079 //- final void check() { 00080 //- for (int i = 0; i < size; i++) { 00081 //- //Debug.println(" check " + i + ": " + array[i]); 00082 //- if (i < size - 1) { 00083 //- int cmp = compare.compare(array[i], array[i+1]); 00084 //- if (cmp != -1) { 00085 //- Debug.println("CHECK ERROR: " + i + ": " + array[i] + " vs " + 00086 //- array[i+1] + " = " + cmp); 00087 //- Debug.println("Array = " + this); 00088 //- throw new RuntimeException("CHECK Failed"); 00089 //- } 00090 //- } 00091 //- } 00092 //- } 00093 //#endif 00094 00095 public void add(Object obj) { 00096 //#ifdef PARANOID 00097 //- if (obj instanceof HeldLock) { 00098 //- HeldLock lock = (HeldLock)obj; 00099 //- if (lock.trans == null) { 00100 //- try { 00101 //- throw new RuntimeException("lock.trans null: " + lock); 00102 //- } catch (Throwable t) { 00103 //- Debug.print(t); 00104 //- } 00105 //- } 00106 //- } 00107 //#endif 00108 int pos = find(obj); 00109 if (pos < 0) { 00110 pos = 0 - (pos + 1); 00111 checkSizeForAdd(); 00112 00113 if (pos < size) { 00114 System.arraycopy(array, pos, array, pos+1, size - pos); 00115 } 00116 array[pos] = obj; 00117 size++; 00118 } 00119 } 00120 00121 public void remove(Object obj) { 00122 removeAt(find(obj)); 00123 } 00124 00125 public void removeAt(int pos) { 00126 if (pos >= 0) { 00127 int next = pos + 1; 00128 if (next < size) { 00129 System.arraycopy(array, next, array, pos, size - next); 00130 } 00131 size--; 00132 } 00133 } 00134 00135 public int size() { 00136 return size; 00137 } 00138 00139 public final Object get(int i) { 00140 return array[i]; 00141 } 00142 00143 final void checkSizeForAdd() { 00144 if (size >= array.length) { 00145 Object[] old = array; 00146 array = new Object[old.length + (old.length >> 2)]; 00147 System.arraycopy(old, 0, array, 0, old.length); 00148 } 00149 } 00150 00151 public String toString() { 00152 StringBuffer sb = new StringBuffer(""); 00153 for (int i = 0; i < size; i++) { 00154 sb.append("\n"); 00155 sb.append(String.valueOf(i)); 00156 sb.append(": "); 00157 sb.append(array[i]); 00158 } 00159 sb.append("\n"); 00160 return sb.toString(); 00161 } 00162 00163 }