提交 56eac437 编写于 作者: S Skylot

Fix errors in AttributesList, refactoring

上级 b4d08bdc
...@@ -8,12 +8,10 @@ public enum AttributeType { ...@@ -8,12 +8,10 @@ public enum AttributeType {
// blocks // blocks
LOOP(false), LOOP(false),
CATCH_BLOCK(false), CATCH_BLOCK(false),
EXC_HANDLER(true),
EXC_HANDLER(true),
SPLITTER_BLOCK(true), SPLITTER_BLOCK(true),
FORCE_RETURN(true), FORCE_RETURN(true),
// fields // fields
...@@ -32,8 +30,25 @@ public enum AttributeType { ...@@ -32,8 +30,25 @@ public enum AttributeType {
DECLARE_VARIABLE(true); DECLARE_VARIABLE(true);
private static final int notUniqCount;
private final boolean uniq; private final boolean uniq;
static {
// place all not unique attributes at first
int last = -1;
AttributeType[] vals = AttributeType.values();
for (int i = 0; i < vals.length; i++) {
AttributeType type = vals[i];
if (type.notUniq())
last = i;
}
notUniqCount = last + 1;
}
public static int getNotUniqCount() {
return notUniqCount;
}
private AttributeType(boolean isUniq) { private AttributeType(boolean isUniq) {
this.uniq = isUniq; this.uniq = isUniq;
} }
......
...@@ -14,6 +14,12 @@ import java.util.List; ...@@ -14,6 +14,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
/**
* Storage for different attribute types:
* 1. flags - boolean attribute (set or not)
* 2. attribute - class instance associated for attribute type,
* only one attached to node for unique attributes, multiple for others
*/
public class AttributesList { public class AttributesList {
private final Set<AttributeFlag> flags; private final Set<AttributeFlag> flags;
...@@ -24,16 +30,11 @@ public class AttributesList { ...@@ -24,16 +30,11 @@ public class AttributesList {
public AttributesList() { public AttributesList() {
flags = EnumSet.noneOf(AttributeFlag.class); flags = EnumSet.noneOf(AttributeFlag.class);
uniqAttr = new EnumMap<AttributeType, IAttribute>(AttributeType.class); uniqAttr = new EnumMap<AttributeType, IAttribute>(AttributeType.class);
attributes = new ArrayList<IAttribute>(1); attributes = new ArrayList<IAttribute>(0);
attrCount = new int[AttributeType.values().length]; attrCount = new int[AttributeType.getNotUniqCount()];
} }
public void add(IAttribute attr) { // Flags
if (attr.getType().isUniq())
uniqAttr.put(attr.getType(), attr);
else
addMultiAttribute(attr);
}
public void add(AttributeFlag flag) { public void add(AttributeFlag flag) {
flags.add(flag); flags.add(flag);
...@@ -47,12 +48,21 @@ public class AttributesList { ...@@ -47,12 +48,21 @@ public class AttributesList {
flags.remove(flag); flags.remove(flag);
} }
// Attributes
public void add(IAttribute attr) {
if (attr.getType().isUniq())
uniqAttr.put(attr.getType(), attr);
else
addMultiAttribute(attr);
}
private void addMultiAttribute(IAttribute attr) { private void addMultiAttribute(IAttribute attr) {
attributes.add(attr); attributes.add(attr);
attrCount[attr.getType().ordinal()]++; attrCount[attr.getType().ordinal()]++;
} }
private int getCountInternal(AttributeType type) { private int getMultiCountInternal(AttributeType type) {
return attrCount[type.ordinal()]; return attrCount[type.ordinal()];
} }
...@@ -67,15 +77,14 @@ public class AttributesList { ...@@ -67,15 +77,14 @@ public class AttributesList {
if (type.isUniq()) if (type.isUniq())
return uniqAttr.containsKey(type); return uniqAttr.containsKey(type);
else else
return getCountInternal(type) != 0; return getMultiCountInternal(type) != 0;
} }
public IAttribute get(AttributeType type) { public IAttribute get(AttributeType type) {
if (type.isUniq()) { if (type.isUniq()) {
return uniqAttr.get(type); return uniqAttr.get(type);
} else { } else {
int count = getCountInternal(type); if (getMultiCountInternal(type) != 0) {
if (count != 0) {
for (IAttribute attr : attributes) for (IAttribute attr : attributes)
if (attr.getType() == type) if (attr.getType() == type)
return attr; return attr;
...@@ -86,9 +95,9 @@ public class AttributesList { ...@@ -86,9 +95,9 @@ public class AttributesList {
public int getCount(AttributeType type) { public int getCount(AttributeType type) {
if (type.isUniq()) { if (type.isUniq()) {
return 0; return uniqAttr.containsKey(type) ? 1 : 0;
} else { } else {
return getCountInternal(type); return getMultiCountInternal(type);
} }
} }
...@@ -103,7 +112,7 @@ public class AttributesList { ...@@ -103,7 +112,7 @@ public class AttributesList {
public List<IAttribute> getAll(AttributeType type) { public List<IAttribute> getAll(AttributeType type) {
assert type.notUniq(); assert type.notUniq();
int count = getCountInternal(type); int count = getMultiCountInternal(type);
if (count == 0) { if (count == 0) {
return Collections.emptyList(); return Collections.emptyList();
} else { } else {
...@@ -129,6 +138,26 @@ public class AttributesList { ...@@ -129,6 +138,26 @@ public class AttributesList {
} }
} }
public void remove(IAttribute attr) {
AttributeType type = attr.getType();
if (type.isUniq()) {
IAttribute a = uniqAttr.get(type);
if (a == attr)
uniqAttr.remove(type);
} else {
if (getMultiCountInternal(type) == 0)
return;
for (Iterator<IAttribute> it = attributes.iterator(); it.hasNext();) {
IAttribute a = it.next();
if (a == attr) {
it.remove();
attrCount[type.ordinal()]--;
}
}
}
}
public void clear() { public void clear() {
flags.clear(); flags.clear();
uniqAttr.clear(); uniqAttr.clear();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册