1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Added toString method and generics.

This commit is contained in:
Sergey Prigogin 2008-02-10 07:05:43 +00:00
parent d64fdf7905
commit 8111176e94
2 changed files with 77 additions and 49 deletions

View file

@ -25,7 +25,7 @@ public class HashTable implements Cloneable{
return currEntry == -1;
}
final public int size(){
final public int size() {
return currEntry + 1;
}
@ -47,20 +47,20 @@ public class HashTable implements Cloneable{
}
}
public Object clone(){
public Object clone() {
HashTable newTable = null;
try {
newTable = (HashTable) super.clone();
} catch ( CloneNotSupportedException e ) {
} catch (CloneNotSupportedException e) {
//shouldn't happen because object supports clone.
return null;
}
int size = capacity();
if( hashTable != null ){
newTable.hashTable = new int[ size*2 ];
newTable.nextTable = new int[ size ];
if (hashTable != null) {
newTable.hashTable = new int[size * 2];
newTable.nextTable = new int[size];
System.arraycopy(hashTable, 0, newTable.hashTable, 0, hashTable.length);
System.arraycopy(nextTable, 0, newTable.nextTable, 0, nextTable.length);
}
@ -79,7 +79,7 @@ public class HashTable implements Cloneable{
if (hashTable == null)
return;
for( int i = 0; i < capacity(); i++ ){
for (int i = 0; i < capacity(); i++) {
hashTable[2*i] = 0;
hashTable[2*i+1] = 0;
nextTable[i] = 0;
@ -90,11 +90,11 @@ public class HashTable implements Cloneable{
return;
// clear the table (don't call clear() or else the subclasses stuff will be cleared too)
for( int i = 0; i < capacity(); i++ ){
for (int i = 0; i < capacity(); i++) {
hashTable[2*i] = 0;
hashTable[2*i+1] = 0;
nextTable[i] = 0;
}
}
// Need to rehash everything
for (int i = 0; i <= currEntry; ++i) {
linkIntoHashTable(i, hash(i));
@ -128,7 +128,7 @@ public class HashTable implements Cloneable{
// need to link
int j = hashTable[hash] - 1;
while (nextTable[j] != 0) {
// if(nextTable[j] - 1 == j) {
// if (nextTable[j] - 1 == j) {
// break;
// }
j = nextTable[j] - 1;
@ -144,15 +144,15 @@ public class HashTable implements Cloneable{
}
protected void removeEntry(int i, int hash) {
if (nextTable == null){
if (nextTable == null) {
--currEntry;
return;
}
// Remove the hash entry
if (hashTable[hash] == i + 1)
if (hashTable[hash] == i + 1) {
hashTable[hash] = nextTable[i];
else {
} else {
// find entry pointing to me
int j = hashTable[hash] - 1;
while (nextTable[j] != 0 && nextTable[j] != i + 1)
@ -165,13 +165,15 @@ public class HashTable implements Cloneable{
System.arraycopy(nextTable, i + 1, nextTable, i, currEntry - i);
// adjust hash and next entries for things that moved
for (int j = 0; j < hashTable.length; ++j)
for (int j = 0; j < hashTable.length; ++j) {
if (hashTable[j] > i + 1)
--hashTable[j];
}
for (int j = 0; j < nextTable.length; ++j)
for (int j = 0; j < nextTable.length; ++j) {
if (nextTable[j] > i + 1)
--nextTable[j];
}
}
// last entry is now free
@ -179,21 +181,21 @@ public class HashTable implements Cloneable{
--currEntry;
}
final public void sort( Comparator c ) {
if( size() > 1 ){
quickSort( c, 0, size() - 1 );
final public void sort(Comparator<Object> c) {
if (size() > 1) {
quickSort(c, 0, size() - 1);
rehash();
}
}
final private void quickSort( Comparator c, int p, int r ){
if( p < r ){
int q = partition( c, p, r );
if( p < q ) quickSort( c, p, q );
if( ++q < r ) quickSort( c, q, r );
final private void quickSort(Comparator<Object> c, int p, int r) {
if (p < r) {
int q = partition(c, p, r);
if (p < q) quickSort(c, p, q);
if (++q < r) quickSort(c, q, r);
}
}
protected int partition( Comparator c, int p, int r ) {
protected int partition(Comparator<Object> c, int p, int r) {
throw new UnsupportedOperationException();
}

View file

@ -23,29 +23,29 @@ import java.util.List;
* @author aniefer
*/
public class ObjectMap extends ObjectTable {
public static final ObjectMap EMPTY_MAP = new ObjectMap( 0 ){
public Object clone() { return this; }
public List toList() { return Collections.EMPTY_LIST; }
public Object put( Object key, Object value ) { throw new UnsupportedOperationException(); }
public static final ObjectMap EMPTY_MAP = new ObjectMap(0) {
public Object clone() { return this; }
public List<Object> toList() { return Collections.emptyList(); }
public Object put(Object key, Object value) { throw new UnsupportedOperationException(); }
};
private Object[] valueTable;
public ObjectMap(int initialSize) {
super( initialSize );
valueTable = new Object[ capacity() ];
super(initialSize);
valueTable = new Object[capacity()];
}
public Object clone(){
public Object clone() {
ObjectMap newMap = (ObjectMap) super.clone();
newMap.valueTable = new Object[ capacity() ];
newMap.valueTable = new Object[capacity()];
System.arraycopy(valueTable, 0, newMap.valueTable, 0, valueTable.length);
return newMap;
}
final public void clear(){
final public void clear() {
super.clear();
for( int i = 0; i < valueTable.length; i++ ){
for(int i = 0; i < valueTable.length; i++) {
valueTable[i] = null;
}
}
@ -54,7 +54,7 @@ public class ObjectMap extends ObjectTable {
Object[] oldValueTable = valueTable;
valueTable = new Object[size];
System.arraycopy(oldValueTable, 0, valueTable, 0, oldValueTable.length);
super.resize( size );
super.resize(size);
}
public Object put(Object key, Object value) {
@ -71,15 +71,15 @@ public class ObjectMap extends ObjectTable {
return null;
}
final public Object getAt( int i ){
if( i < 0 || i > currEntry )
final public Object getAt(int i) {
if (i < 0 || i > currEntry)
return null;
return valueTable[i];
}
final public Object remove( Object key ) {
if( key == null )
final public Object remove(Object key) {
if (key == null)
return null;
int i = lookup(key);
if (i < 0)
@ -101,18 +101,24 @@ public class ObjectMap extends ObjectTable {
super.removeEntry(i);
}
protected int partition( Comparator c, int p, int r ){
Object x = keyTable[ p ];
@Override
protected int partition(Comparator<Object> c, int p, int r) {
Object x = keyTable[p];
Object temp = null;
int i = p;
int j = r;
while( true ){
while( c.compare( keyTable[ j ], x ) > 0 ){ j--; }
if( i < j )
while( c.compare( keyTable[ i ], x ) < 0 ){ i++; }
while (true) {
while (c.compare(keyTable[j], x) > 0) {
j--;
}
if (i < j) {
while (c.compare(keyTable[i], x) < 0) {
i++;
}
}
if( i < j ){
if (i < j) {
temp = keyTable[j];
keyTable[j] = keyTable[i];
keyTable[i] = temp;
@ -126,9 +132,29 @@ public class ObjectMap extends ObjectTable {
}
}
public Object [] valueArray(){
Object [] vals = new Object[ size() ];
System.arraycopy( valueTable, 0, vals, 0, vals.length );
public Object[] valueArray() {
Object[] vals = new Object[size()];
System.arraycopy(valueTable, 0, vals, 0, vals.length);
return vals;
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size(); i++) {
Object key = keyAt(i);
if (key != null) {
if (sb.length() == 0) {
sb.append("{");
} else {
sb.append(", ");
}
Object value = valueTable[i];
sb.append(String.valueOf(key));
sb.append(": ");
sb.append(String.valueOf(value));
}
}
sb.append("}");
return sb.toString();
}
}