Allow in UintMap for any key to hold int and object values simultaneously, as the old code could only detect that the key is allowed to hold an object value only if there are no other keys with int values. In the new version, if the key is present, it always has both int and object values, but they default to 0 or int if not specified in put. It is effectively the old behavior when the map contained at least one int key, but at least it is documented.

This commit is contained in:
igor%mir2.org 2002-05-01 14:10:41 +00:00
Родитель 1fde3e9b21
Коммит 416882ec3a
2 изменённых файлов: 72 добавлений и 86 удалений

Просмотреть файл

@ -316,47 +316,49 @@ public class Node implements Cloneable {
LEFT = 1, LEFT = 1,
RIGHT = 2; RIGHT = 2;
private static String propNames[];
private static final String propToString(int propType) { private static final String propToString(int propType) {
if (Context.printTrees && propNames == null) { if (Context.printTrees) {
// If Context.printTrees is false, the compiler // If Context.printTrees is false, the compiler
// can remove all these strings. // can remove all these strings.
String[] a = { switch (propType) {
"target", case TARGET_PROP: return "target";
"break", case BREAK_PROP: return "break";
"continue", case CONTINUE_PROP: return "continue";
"enum", case ENUM_PROP: return "enum";
"function", case FUNCTION_PROP: return "function";
"temp", case TEMP_PROP: return "temp";
"local", case LOCAL_PROP: return "local";
"codeoffset", case CODEOFFSET_PROP: return "codeoffset";
"fixups", case FIXUPS_PROP: return "fixups";
"vars", case VARS_PROP: return "vars";
"uses", case USES_PROP: return "uses";
"regexp", case REGEXP_PROP: return "regexp";
"cases", case CASES_PROP: return "cases";
"default", case DEFAULT_PROP: return "default";
"casearray", case CASEARRAY_PROP: return "casearray";
"sourcename", case SOURCENAME_PROP: return "sourcename";
"source", case SOURCE_PROP: return "source";
"type", case TYPE_PROP: return "type";
"special_prop", case SPECIAL_PROP_PROP: return "special_prop";
"label", case LABEL_PROP: return "label";
"finally", case FINALLY_PROP: return "finally";
"localcount", case LOCALCOUNT_PROP: return "localcount";
"targetblock",
"variable", case TARGETBLOCK_PROP: return "targetblock";
"lastuse", case VARIABLE_PROP: return "variable";
"isnumber", case LASTUSE_PROP: return "lastuse";
"directcall", case ISNUMBER_PROP: return "isnumber";
"base_lineno", case DIRECTCALL_PROP: return "directcall";
"end_lineno",
"specialcall" case BASE_LINENO_PROP: return "base_lineno";
}; case END_LINENO_PROP: return "end_lineno";
propNames = a; case SPECIALCALL_PROP: return "specialcall";
case DEBUGSOURCE_PROP: return "debugsource";
default: Context.codeBug();
} }
return propNames[propType-1]; }
return null;
} }
public Object getProp(int propType) { public Object getProp(int propType) {
@ -475,10 +477,10 @@ public class Node implements Cloneable {
sb.append("last use property"); sb.append("last use property");
break; break;
default : default :
if (props.isObjectType(key)) { Object obj = props.getObject(key);
sb.append(props.getObject(key).toString()); if (obj != null) {
} sb.append(obj.toString());
else { }else {
sb.append(props.getExistingInt(key)); sb.append(props.getExistingInt(key));
} }
break; break;
@ -526,7 +528,7 @@ public class Node implements Cloneable {
protected Node next; // next sibling protected Node next; // next sibling
protected Node first; // first element of a linked list of children protected Node first; // first element of a linked list of children
protected Node last; // last element of a linked list of children protected Node last; // last element of a linked list of children
protected UintMap props; private Object datum; // encapsulated data; depends on type
protected Object datum; // encapsulated data; depends on type private UintMap props;
} }

Просмотреть файл

@ -82,22 +82,9 @@ class UintMap implements Serializable {
return 0 <= findIndex(key); return 0 <= findIndex(key);
} }
public boolean isObjectType(int key) {
if (key < 0) Context.codeBug();
int index = findIndex(key);
return index >= 0 && isObjectTypeValue(index);
}
public boolean isIntType(int key) {
if (key < 0) Context.codeBug();
int index = findIndex(key);
return index >= 0 && !isObjectTypeValue(index);
}
/** /**
* Get object value assigned with key. * Get object value assigned with key.
* @return key object value or null if key is absent or does * @return key object value or null if key is absent
* not have object value
*/ */
public Object getObject(int key) { public Object getObject(int key) {
if (key < 0) Context.codeBug(); if (key < 0) Context.codeBug();
@ -112,18 +99,16 @@ class UintMap implements Serializable {
/** /**
* Get integer value assigned with key. * Get integer value assigned with key.
* @return key integer value or defaultValue if key is absent or does * @return key integer value or defaultValue if key is absent
* not have int value
*/ */
public int getInt(int key, int defaultValue) { public int getInt(int key, int defaultValue) {
if (key < 0) Context.codeBug(); if (key < 0) Context.codeBug();
if (ivaluesShift != 0) {
int index = findIndex(key); int index = findIndex(key);
if (0 <= index) { if (0 <= index) {
if (!isObjectTypeValue(index)) { if (ivaluesShift != 0) {
return keys[ivaluesShift + index]; return keys[ivaluesShift + index];
} }
} return 0;
} }
return defaultValue; return defaultValue;
} }
@ -132,26 +117,28 @@ class UintMap implements Serializable {
* Get integer value assigned with key. * Get integer value assigned with key.
* @return key integer value or defaultValue if key does not exist or does * @return key integer value or defaultValue if key does not exist or does
* not have int value * not have int value
* @throws RuntimeException if key does not exist or does * @throws RuntimeException if key does not exist
* not have int value
*/ */
public int getExistingInt(int key) { public int getExistingInt(int key) {
if (key < 0) Context.codeBug(); if (key < 0) Context.codeBug();
if (ivaluesShift != 0) {
int index = findIndex(key); int index = findIndex(key);
if (0 <= index) { if (0 <= index) {
if (!isObjectTypeValue(index)) { if (ivaluesShift != 0) {
return keys[ivaluesShift + index]; return keys[ivaluesShift + index];
} }
} return 0;
} }
// Key must exist // Key must exist
Context.codeBug(); Context.codeBug();
return 0; return 0;
} }
/**
* Set object value of the key.
* If key does not exist, also set its int value to 0.
*/
public void put(int key, Object value) { public void put(int key, Object value) {
if (!(key >= 0 && value != null)) Context.codeBug(); if (key < 0) Context.codeBug();
int index = ensureIndex(key, false); int index = ensureIndex(key, false);
if (values == null) { if (values == null) {
values = new Object[1 << power]; values = new Object[1 << power];
@ -159,12 +146,16 @@ class UintMap implements Serializable {
values[index] = value; values[index] = value;
} }
/**
* Set int value of the key.
* If key does not exist, also set its object value to null.
*/
public void put(int key, int value) { public void put(int key, int value) {
if (key < 0) Context.codeBug(); if (key < 0) Context.codeBug();
int index = ensureIndex(key, true); int index = ensureIndex(key, true);
if (ivaluesShift == 0) { if (ivaluesShift == 0) {
int N = 1 << power; int N = 1 << power;
// keys.length cam be N * 2 after clear which set ivaluesShift to 0 // keys.length can be N * 2 after clear which set ivaluesShift to 0
if (keys.length != N * 2) { if (keys.length != N * 2) {
int[] tmp = new int[N * 2]; int[] tmp = new int[N * 2];
System.arraycopy(keys, 0, tmp, 0, N); System.arraycopy(keys, 0, tmp, 0, N);
@ -173,7 +164,6 @@ class UintMap implements Serializable {
ivaluesShift = N; ivaluesShift = N;
} }
keys[ivaluesShift + index] = value; keys[ivaluesShift + index] = value;
if (values != null) { values[index] = null; }
} }
public void remove(int key) { public void remove(int key) {
@ -370,12 +360,6 @@ class UintMap implements Serializable {
return index; return index;
} }
private boolean isObjectTypeValue(int index) {
if (check && !(index >= 0 && index < (1 << power)))
Context.codeBug();
return values != null && values[index] != null;
}
private void writeObject(ObjectOutputStream out) private void writeObject(ObjectOutputStream out)
throws IOException throws IOException
{ {