1 19 24 25 package org.netbeans.core.output2; 26 27 import java.util.Arrays ; 28 import org.openide.util.Exceptions; 29 30 42 final class IntMap { 43 private int[] keys = new int[] { Integer.MAX_VALUE, Integer.MAX_VALUE, 44 Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE}; 45 46 private Object [] vals = new Object [5]; 47 private int last = -1; 48 49 50 IntMap() { 51 } 52 53 public int first() { 54 return isEmpty() ? -1 : keys[0]; 55 } 56 57 public int nearest (int line, boolean backward) { 58 if (isEmpty()) { 59 return -1; 60 } 61 if (last == 0) { 62 return keys[last]; 63 } 64 if (line < keys[0]) { 65 return backward ? keys[last] : keys[0]; 66 } 67 if (line > keys[last]) { 68 return backward ? keys[last] : keys[0]; 69 } 70 int idx = Arrays.binarySearch (keys, line); 71 if (idx < 0 && last > 0) { 72 for (int i=1; i <= last; i++) { 73 if (keys[i-1] < line && keys[i] > line) { 74 idx = i; 75 break; 76 } 77 } 78 return backward ? keys[idx-1] : keys[idx]; 79 } else { 80 if (backward) { 81 idx = idx == 0 ? last : idx - 1; 82 } else { 83 idx = idx == last ? 0 : idx + 1; 84 } 85 return keys[idx]; 86 } 87 } 88 89 public int[] getKeys () { 90 if (last == -1) { 91 return new int[0]; 92 } 93 if (last == keys.length -1) { 94 growArrays(); 95 } 96 int[] result = new int[last+1]; 97 try { 98 System.arraycopy (keys, 0, result, 0, last+1); 99 return result; 100 } catch (ArrayIndexOutOfBoundsException aioobe) { ArrayIndexOutOfBoundsException e = new ArrayIndexOutOfBoundsException ( 102 "AIOOBE in IntMap.getKeys() - last = " + last + " keys: " + 103 i2s(keys) + " vals: " + Arrays.asList(vals) + " result length " 104 + result.length); 105 Exceptions.printStackTrace(e); 106 return new int[0]; 107 } 108 } 109 110 111 private static String i2s (int[] arr) { 112 StringBuffer sb = new StringBuffer (arr.length * 3); 113 sb.append ('['); 114 for (int i=0; i < arr.length; i++) { 115 if (arr[i] != Integer.MAX_VALUE) { 116 sb.append (arr[i]); 117 sb.append (','); 118 } 119 } 120 sb.append (']'); 121 return sb.toString(); 122 } 123 124 public Object get (int key) { 125 int idx = Arrays.binarySearch (keys, key); 126 if (idx > -1 && idx <= last) { 127 return vals[idx]; 128 } 129 return null; 130 } 131 132 public void put (int key, Object val) { 133 if (last > 0) { 134 assert key > keys[last]: "key=" + key + " last=" + keys[last]; 135 } 136 if (last == keys.length - 1) { 137 growArrays(); 138 } 139 last++; 140 keys[last] = key; 141 vals[last] = val; 142 } 143 144 private void growArrays() { 145 int newSize = last * 2; 146 int[] newKeys = new int[newSize]; 147 Object [] newVals = new Object [newSize]; 148 Arrays.fill (newKeys, Integer.MAX_VALUE); System.arraycopy (keys, 0, newKeys, 0, keys.length); 150 System.arraycopy (vals, 0, newVals, 0, vals.length); 151 keys = newKeys; 152 vals = newVals; 153 } 154 155 158 public int nextEntry (int entry) { 159 int result = -1; 160 if (!isEmpty()) { 161 int idx = Arrays.binarySearch (keys, entry); 162 if (idx >= 0) { 163 result = idx == keys.length -1 ? keys[0] : keys[idx+1]; 164 } 165 } 166 return result; 167 } 168 169 172 public int prevEntry (int entry) { 173 int result = -1; 174 if (!isEmpty()) { 175 int idx = Arrays.binarySearch (keys, entry); 176 if (idx >= 0) { 177 result = idx == 0 -1 ? keys[keys.length-1] : keys[idx-1]; 178 } 179 } 180 return result; 181 } 182 183 184 public boolean isEmpty() { 185 return last == -1; 186 } 187 188 public int size() { 189 return last + 1; 190 } 191 192 public String toString() { 193 StringBuffer sb = new StringBuffer ("IntMap@") .append(System.identityHashCode(this)); 195 196 for (int i=0; i < size(); i++) { 197 sb.append ("["); sb.append (keys[i]); 199 sb.append (":"); sb.append (vals[i]); 201 sb.append ("]"); } 203 if (size() == 0) { 204 sb.append ("empty"); } 206 return sb.toString(); 207 } 208 } 209 | Popular Tags |