1 11 package org.eclipse.jdt.internal.compiler.util; 12 13 import org.eclipse.jdt.core.compiler.CharOperation; 14 15 20 public final class SimpleSetOfCharArray implements Cloneable { 21 22 public char[][] values; 24 public int elementSize; public int threshold; 26 27 public SimpleSetOfCharArray() { 28 this(13); 29 } 30 31 public SimpleSetOfCharArray(int size) { 32 if (size < 3) size = 3; 33 this.elementSize = 0; 34 this.threshold = size + 1; this.values = new char[2 * size + 1][]; 36 } 37 38 public Object add(char[] object) { 39 int length = this.values.length; 40 int index = (CharOperation.hashCode(object) & 0x7FFFFFFF) % length; 41 char[] current; 42 while ((current = this.values[index]) != null) { 43 if (CharOperation.equals(current, object)) return this.values[index] = object; 44 if (++index == length) index = 0; 45 } 46 this.values[index] = object; 47 48 if (++this.elementSize > this.threshold) rehash(); 50 return object; 51 } 52 53 public void asArray(Object [] copy) { 54 if (this.elementSize != copy.length) 55 throw new IllegalArgumentException (); 56 int index = this.elementSize; 57 for (int i = 0, l = this.values.length; i < l && index > 0; i++) 58 if (this.values[i] != null) 59 copy[--index] = this.values[i]; 60 } 61 62 public void clear() { 63 for (int i = this.values.length; --i >= 0;) 64 this.values[i] = null; 65 this.elementSize = 0; 66 } 67 68 public Object clone() throws CloneNotSupportedException { 69 SimpleSetOfCharArray result = (SimpleSetOfCharArray) super.clone(); 70 result.elementSize = this.elementSize; 71 result.threshold = this.threshold; 72 73 int length = this.values.length; 74 result.values = new char[length][]; 75 System.arraycopy(this.values, 0, result.values, 0, length); 76 return result; 77 } 78 79 public char[] get(char[] object) { 80 int length = this.values.length; 81 int index = (CharOperation.hashCode(object) & 0x7FFFFFFF) % length; 82 char[] current; 83 while ((current = this.values[index]) != null) { 84 if (CharOperation.equals(current, object)) return current; 85 if (++index == length) index = 0; 86 } 87 this.values[index] = object; 88 89 if (++this.elementSize > this.threshold) rehash(); 91 return object; 92 } 93 94 public boolean includes(char[] object) { 95 int length = values.length; 96 int index = (CharOperation.hashCode(object) & 0x7FFFFFFF) % length; 97 char[] current; 98 while ((current = values[index]) != null) { 99 if (CharOperation.equals(current, object)) return true; 100 if (++index == length) index = 0; 101 } 102 return false; 103 } 104 105 public char[] remove(char[] object) { 106 int length = values.length; 107 int index = (CharOperation.hashCode(object) & 0x7FFFFFFF) % length; 108 char[] current; 109 while ((current = values[index]) != null) { 110 if (CharOperation.equals(current, object)) { 111 elementSize--; 112 char[] oldValue = values[index]; 113 values[index] = null; 114 if (values[index + 1 == length ? 0 : index + 1] != null) 115 rehash(); return oldValue; 117 } 118 if (++index == length) index = 0; 119 } 120 return null; 121 } 122 123 private void rehash() { 124 SimpleSetOfCharArray newSet = new SimpleSetOfCharArray(elementSize * 2); char[] current; 126 for (int i = values.length; --i >= 0;) 127 if ((current = values[i]) != null) 128 newSet.add(current); 129 130 this.values = newSet.values; 131 this.elementSize = newSet.elementSize; 132 this.threshold = newSet.threshold; 133 } 134 135 public String toString() { 136 String s = ""; char[] object; 138 for (int i = 0, l = values.length; i < l; i++) 139 if ((object = values[i]) != null) 140 s += new String (object) + "\n"; return s; 142 } 143 } 144 | Popular Tags |