1 11 12 package org.eclipse.core.internal.localstore; 13 14 import java.util.Arrays ; 15 16 47 public class PrefixPool { 48 private String [] pool; 49 private int size; 50 51 57 public PrefixPool(int initialCapacity) { 58 if (initialCapacity <= 0) 59 throw new IllegalArgumentException ("Illegal Capacity: " + initialCapacity); pool = new String [initialCapacity]; 61 size = 0; 62 } 63 64 69 publicvoid clear() { 70 Arrays.fill(pool, 0, size, null); 71 size = 0; 72 } 73 74 78 publicint size() { 79 return size; 80 } 81 82 85 private void checkCapacity() { 86 if (size + 1 >= pool.length) { 87 String [] newprefixList = new String [2 * pool.length]; 88 System.arraycopy(pool, 0, newprefixList, 0, pool.length); 89 Arrays.fill(pool, null); pool = newprefixList; 91 } 92 } 93 94 109 publicvoid insertLonger(String s) { 110 for (int i = size - 1; i >= 0; i--) { 112 if (pool[i].startsWith(s)) { 113 return; 115 } else if (s.startsWith(pool[i])) { 116 pool[i] = s; 118 return; 119 } 120 } 121 checkCapacity(); 122 pool[size] = s; 123 size++; 124 } 125 126 144 publicboolean insertShorter(String s) { 145 boolean replaced = false; 146 for (int i = size - 1; i >= 0; i--) { 148 if (s.startsWith(pool[i])) { 149 return false; 151 } else if (pool[i].startsWith(s)) { 152 if (replaced) { 153 System.arraycopy(pool, i + 1, pool, i, size - i - 1); 156 size--; 157 pool[size] = null; 158 } else { 159 pool[i] = s; 161 replaced = true; 162 } 163 } 164 } 165 if (!replaced) { 166 checkCapacity(); 168 pool[size] = s; 169 size++; 170 } 171 return replaced; 172 } 173 174 181 publicboolean containsAsPrefix(String s) { 182 for (int i = size - 1; i >= 0; i--) { 184 if (pool[i].startsWith(s)) { 185 return true; 186 } 187 } 188 return false; 189 } 190 191 198 publicboolean hasPrefixOf(String s) { 199 for (int i = size - 1; i >= 0; i--) { 200 if (s.startsWith(pool[i])) { 201 return true; 202 } 203 } 204 return false; 205 } 206 207 } 208 | Popular Tags |