mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-09-01 12:43:26 +02:00
This commit was manufactured by cvs2svn to create branch 'cdt_2_0'.
Cherrypick from master 2004-08-11 17:56:01 UTC Andrew Niefer <aniefer@ca.ibm.com> 'from a patch originally from Dave Daoust, consolidate the Scanner2 data': core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/CharTable.java core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/ObjectTable.java
This commit is contained in:
parent
04b3eec1c6
commit
6c18d73efc
2 changed files with 310 additions and 0 deletions
|
@ -0,0 +1,153 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2004 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Common Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/cpl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author ddaoust
|
||||
*
|
||||
*/
|
||||
public class CharTable extends HashTable {
|
||||
protected char[][] keyTable;
|
||||
|
||||
protected CharTable(int initialSize) {
|
||||
super(initialSize);
|
||||
keyTable = new char[capacity()][];
|
||||
}
|
||||
|
||||
protected void resize(int size) {
|
||||
char[][] oldKeyTable = keyTable;
|
||||
keyTable = new char[size][];
|
||||
System.arraycopy(oldKeyTable, 0, keyTable, 0, oldKeyTable.length);
|
||||
super.resize(size);
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
super.clear();
|
||||
for( int i = 0; i < capacity(); i++ )
|
||||
keyTable[i] = null;
|
||||
}
|
||||
protected final int hash(char[] source, int start, int length) {
|
||||
return CharArrayUtils.hash(source, start, length) & ((keyTable.length * 2) - 1);
|
||||
}
|
||||
protected final int hash( int pos ){
|
||||
return hash(keyTable[pos], 0, keyTable[pos].length);
|
||||
}
|
||||
|
||||
protected final int hash( char[] obj ){
|
||||
return hash( obj, 0, obj.length );
|
||||
}
|
||||
|
||||
protected final int addIndex(char[] buffer ) {
|
||||
return addIndex(buffer, 0, buffer.length);
|
||||
}
|
||||
protected final int addIndex(char[] buffer, int start, int len) {
|
||||
if (hashTable != null)
|
||||
{
|
||||
int hash = hash(buffer, start, len);
|
||||
int pos = lookup(buffer, start, len, hash);
|
||||
if (pos != -1)
|
||||
return pos;
|
||||
|
||||
// key is not here, add it.
|
||||
if( (currEntry + 1) >= capacity()) {
|
||||
resize();
|
||||
hash = hash(buffer, start, len);
|
||||
}
|
||||
currEntry++;
|
||||
keyTable[currEntry] = CharArrayUtils.extract(buffer, start, len);
|
||||
linkIntoHashTable(currEntry, hash);
|
||||
}
|
||||
else
|
||||
{
|
||||
int pos = lookup(buffer, start, len);
|
||||
if (pos != -1)
|
||||
return pos;
|
||||
// key is not here, add it.
|
||||
if( (currEntry + 1) >= capacity()) {
|
||||
resize();
|
||||
if( capacity() > minHashSize ){
|
||||
//if we grew from list to hash, then recurse and add as a hashtable
|
||||
return addIndex( buffer, start, len );
|
||||
}
|
||||
}
|
||||
currEntry++;
|
||||
keyTable[currEntry] = CharArrayUtils.extract(buffer, start, len);
|
||||
}
|
||||
return currEntry;
|
||||
|
||||
}
|
||||
|
||||
protected void removeEntry(int i) {
|
||||
// Remove the entry from the keyTable, shifting everything over if necessary
|
||||
int hash = hash(keyTable[i]);
|
||||
if (i < currEntry)
|
||||
System.arraycopy(keyTable, i + 1, keyTable, i, currEntry - i);
|
||||
|
||||
keyTable[currEntry] = null;
|
||||
|
||||
// Make sure you remove the value before calling super where currEntry will change
|
||||
removeEntry(i, hash);
|
||||
}
|
||||
|
||||
public List toList(){
|
||||
List list = new ArrayList( size() );
|
||||
int size = size();
|
||||
for( int i = 0; i < size; i++ ){
|
||||
list.add( keyAt( i ) );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
final public char[] keyAt( int i ){
|
||||
if( i < 0 || i > currEntry )
|
||||
return null;
|
||||
|
||||
return keyTable[ i ];
|
||||
}
|
||||
|
||||
final public boolean containsKey( char[] key ){
|
||||
return lookup( key ) != -1;
|
||||
}
|
||||
|
||||
protected int lookup(char[] buffer ){
|
||||
return lookup(buffer, 0, buffer.length);
|
||||
}
|
||||
|
||||
protected final int lookup(char[] buffer, int start, int len) {
|
||||
if (hashTable != null)
|
||||
return lookup(buffer, start, len, hash(buffer, start, len) );
|
||||
for (int i = 0; i <= currEntry; i++) {
|
||||
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
protected final int lookup(char[] buffer, int start, int len, int hash) {
|
||||
if (hashTable[hash] == 0)
|
||||
return -1;
|
||||
|
||||
int i = hashTable[hash] - 1;
|
||||
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||
return i;
|
||||
|
||||
// Follow the next chain
|
||||
for (i = nextTable[i] - 1; i >= 0; i = nextTable[i] - 1)
|
||||
if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2004 IBM Corporation and others.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Common Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/cpl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* IBM Corporation - initial API and implementation
|
||||
*******************************************************************************/
|
||||
|
||||
/*
|
||||
* For use by the Parser Symbol Table
|
||||
* Created on Jul 15, 2004
|
||||
*/
|
||||
package org.eclipse.cdt.internal.core.parser.scanner2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* @author aniefer
|
||||
*/
|
||||
public abstract class ObjectTable extends HashTable implements Cloneable{
|
||||
protected Object[] keyTable;
|
||||
|
||||
public ObjectTable(int initialSize) {
|
||||
super(initialSize);
|
||||
keyTable = new Object[capacity()];
|
||||
}
|
||||
|
||||
public Object clone(){
|
||||
ObjectTable newTable = null;
|
||||
try {
|
||||
newTable = (ObjectTable) super.clone();
|
||||
} catch ( CloneNotSupportedException e ) {
|
||||
//shouldn't happen because object supports clone.
|
||||
return null;
|
||||
}
|
||||
|
||||
int size = capacity();
|
||||
newTable.keyTable = new Object[ size ];
|
||||
System.arraycopy(keyTable, 0, newTable.keyTable, 0, keyTable.length);
|
||||
|
||||
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);
|
||||
}
|
||||
newTable.currEntry = currEntry;
|
||||
return newTable;
|
||||
}
|
||||
|
||||
public List toList(){
|
||||
List list = new ArrayList( size() );
|
||||
int size = size();
|
||||
for( int i = 0; i < size; i++ ){
|
||||
list.add( keyAt( i ) );
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public Object keyAt( int i ){
|
||||
if( i < 0 || i > currEntry )
|
||||
return null;
|
||||
|
||||
return keyTable[ i ];
|
||||
}
|
||||
|
||||
public void clear(){
|
||||
super.clear();
|
||||
for( int i = 0; i < keyTable.length; i++ )
|
||||
keyTable[i] = null;
|
||||
}
|
||||
|
||||
protected final int hash( int pos ){
|
||||
return hash(keyTable[pos]);
|
||||
}
|
||||
|
||||
private int hash(Object obj) {
|
||||
return obj.hashCode() & ((capacity() * 2) - 1);
|
||||
}
|
||||
|
||||
protected void resize(int size) {
|
||||
Object[] oldKeyTable = keyTable;
|
||||
keyTable = new Object[size];
|
||||
System.arraycopy(oldKeyTable, 0, keyTable, 0, oldKeyTable.length);
|
||||
|
||||
super.resize(size);
|
||||
}
|
||||
|
||||
protected final int add(Object obj) {
|
||||
int pos = lookup(obj);
|
||||
if (pos != -1)
|
||||
return pos;
|
||||
|
||||
if ( (currEntry + 1) >= capacity()) {
|
||||
resize();
|
||||
}
|
||||
currEntry++;
|
||||
keyTable[currEntry] = obj;
|
||||
linkIntoHashTable(currEntry, hash(obj));
|
||||
return currEntry;
|
||||
}
|
||||
|
||||
protected void removeEntry(int i) {
|
||||
// Remove the entry from the keyTable, shifting everything over if necessary
|
||||
|
||||
int hash = hash(keyTable[i]);
|
||||
if (i < currEntry)
|
||||
System.arraycopy(keyTable, i + 1, keyTable, i, currEntry - i);
|
||||
|
||||
keyTable[currEntry] = null;
|
||||
|
||||
// Make sure you remove the value before calling super where currEntry will change
|
||||
removeEntry(i, hash);
|
||||
|
||||
}
|
||||
|
||||
protected final int lookup(Object buffer ){
|
||||
|
||||
if (hashTable != null) {
|
||||
int hash = hash(buffer);
|
||||
|
||||
if (hashTable[hash] == 0)
|
||||
return -1;
|
||||
|
||||
int i = hashTable[hash] - 1;
|
||||
if (buffer.equals( keyTable[i] ) )
|
||||
return i;
|
||||
|
||||
// Follow the next chain
|
||||
for (i = nextTable[i] - 1; i >= 0; i = nextTable[i] - 1)
|
||||
if ( buffer.equals( keyTable[i] ))
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i <= currEntry; i++) {
|
||||
if (buffer.equals( keyTable[i] ) )
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean containsKey( Object key ){
|
||||
return lookup( key ) != -1;
|
||||
}
|
||||
|
||||
public Object [] keyArray(){
|
||||
Object [] keys = new Object[ size() ];
|
||||
System.arraycopy( keyTable, 0, keys, 0, keys.length );
|
||||
return keys;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue