Permalink
Browse files

Streamline the methods used for finding a bit more

  • Loading branch information...
1 parent 3e172ae commit 586bc62284be8f16fba7157a0502bbe9b003abc6 @olabini committed May 4, 2011
@@ -76,7 +76,7 @@ public Object activate(IokeObject method, IokeObject context, IokeObject message
String name = m1.getName();
if(m1.getArgumentCount() == 0) {
- Object val = IokeObject.findCell(on, message, context, name);
+ Object val = IokeObject.findCell(IokeObject.as(on, context), name);
if(val == context.runtime.nul || !IokeObject.isTrue(val)) {
return Interpreter.send(context.runtime.setValue, context, on, m1, Message.getArg2(message));
} else {
@@ -113,7 +113,7 @@ public Object activate(IokeObject method, IokeObject context, IokeObject message
String name = m1.getName();
if(m1.getArgumentCount() == 0) {
- Object val = IokeObject.findCell(on, message, context, name);
+ Object val = IokeObject.findCell(IokeObject.as(on, context), name);
if(val == context.runtime.nul || !IokeObject.isTrue(val)) {
return val;
} else {
@@ -385,7 +385,7 @@ public Object activate(IokeObject method, IokeObject context, IokeObject message
getArguments().getEvaluatedArguments(context, message, on, args, new HashMap<String, Object>());
String name = Text.getText(Interpreter.send(context.runtime.asText, context, args.get(0)));
- return IokeObject.findCell(on, message, context, name) != context.runtime.nul ? context.runtime._true : context.runtime._false;
+ return IokeObject.findCell(on, context, name) != context.runtime.nul ? context.runtime._true : context.runtime._false;
}
}));
@@ -28,38 +28,34 @@
int flags;
- public final void put(String name, int index, Object value) {
- Cell cell = getCell(name, index, false);
+ public final void put(String name, Object value) {
+ Cell cell = getCell(name, false);
cell.value = value;
}
public final boolean has(String name) {
- return null != getCell(name, 0, true);
+ return null != getCell(name, true);
}
- public final boolean has(int index) {
- return null != getCell(null, index, true);
- }
-
- public final Object get(String name, int index) {
- Cell cell = getCell(name, index, true);
+ public final Object get(String name) {
+ Cell cell = getCell(name, true);
if(cell == null) {
return cell;
}
return cell.value;
}
- public Object remove(String name, int index) {
- int indexOrHash = (name != null ? name.hashCode() : index);
+ public Object remove(String name) {
+ int hash = name.hashCode();
Cell[] cellsLocalRef = cells;
if(count != 0) {
int tableSize = cells.length;
- int cellIndex = getCellIndex(tableSize, indexOrHash);
+ int cellIndex = getCellIndex(tableSize, hash);
Cell prev = cellsLocalRef[cellIndex];
Cell cell = prev;
while(cell != null) {
- if(cell.indexOrHash == indexOrHash && (cell.name == name || (name != null && name.equals(cell.name)))) {
+ if(cell.name == name || name.equals(cell.name)) {
break;
}
prev = cell;
@@ -108,31 +104,31 @@ public Object remove(String name, int index) {
public static class Cell {
String name;
- int indexOrHash;
+ int hash;
public Object value;
Cell next; // next in hash table bucket
Cell orderedNext; // next in linked list
- Cell(String name, int indexOrHash) {
+ Cell(String name, int hash) {
this.name = name;
- this.indexOrHash = indexOrHash;
+ this.hash = hash;
}
}
- private Cell getCell(String name, int index, boolean query) {
+ private Cell getCell(String name, boolean query) {
Cell[] cellsLocalRef = cells;
if(cellsLocalRef == null && query) {
return null;
}
- int indexOrHash = (name != null ? name.hashCode() : index);
+ int hash = name.hashCode();
if(cellsLocalRef != null) {
Cell cell;
- int cellIndex = getCellIndex(cellsLocalRef.length, indexOrHash);
+ int cellIndex = getCellIndex(cellsLocalRef.length, hash);
for(cell = cellsLocalRef[cellIndex];
cell != null;
cell = cell.next) {
Object sname = cell.name;
- if(indexOrHash == cell.indexOrHash && (sname == name || (name != null && name.equals(sname)))) {
+ if(sname == name || name.equals(sname)) {
break;
}
}
@@ -141,27 +137,27 @@ private Cell getCell(String name, int index, boolean query) {
}
}
- return createCell(name, indexOrHash, query);
+ return createCell(name, hash, query);
}
- private static int getCellIndex(int tableSize, int indexOrHash) {
- return indexOrHash & (tableSize - 1);
+ private static int getCellIndex(int tableSize, int hash) {
+ return hash & (tableSize - 1);
}
- private Cell createCell(String name, int indexOrHash, boolean query) {
+ private Cell createCell(String name, int hash, boolean query) {
Cell[] cellsLocalRef = cells;
int insertPos;
if(count == 0) {
cellsLocalRef = new Cell[INITIAL_CELL_SIZE];
cells = cellsLocalRef;
- insertPos = getCellIndex(cellsLocalRef.length, indexOrHash);
+ insertPos = getCellIndex(cellsLocalRef.length, hash);
} else {
int tableSize = cellsLocalRef.length;
- insertPos = getCellIndex(tableSize, indexOrHash);
+ insertPos = getCellIndex(tableSize, hash);
Cell prev = cellsLocalRef[insertPos];
Cell cell = prev;
while(cell != null) {
- if(cell.indexOrHash == indexOrHash && (cell.name == name || (name != null && name.equals(cell.name)))) {
+ if(cell.name == name || name.equals(cell.name)) {
break;
}
prev = cell;
@@ -175,11 +171,11 @@ private Cell createCell(String name, int indexOrHash, boolean query) {
cellsLocalRef = new Cell[cellsLocalRef.length * 2];
copyTable(cells, cellsLocalRef, count);
cells = cellsLocalRef;
- insertPos = getCellIndex(cellsLocalRef.length, indexOrHash);
+ insertPos = getCellIndex(cellsLocalRef.length, hash);
}
}
}
- Cell newCell = new Cell(name, indexOrHash);
+ Cell newCell = new Cell(name, hash);
++count;
if(lastAdded != null)
lastAdded.orderedNext = newCell;
@@ -197,7 +193,7 @@ private static void copyTable(Cell[] cells, Cell[] newCells, int count) {
--i;
Cell cell = cells[i];
while(cell != null) {
- int insertPos = getCellIndex(tableSize, cell.indexOrHash);
+ int insertPos = getCellIndex(tableSize, cell.hash);
Cell next = cell.next;
addKnownAbsentCell(newCells, cell, insertPos);
cell.next = null;
@@ -254,7 +254,7 @@ public int getEvaluatedArguments(final IokeObject context, final IokeObject mess
for(Map.Entry<Object, Object> me : keys.entrySet()) {
givenKeywords.put(Text.getText(IokeObject.convertToText(me.getKey(), message, context, true)) + ":", me.getValue());
}
- } else if(IokeObject.findCell(result, message, context, "asTuple") != runtime.nul) {
+ } else if(IokeObject.findCell(IokeObject.as(result, context), "asTuple") != runtime.nul) {
Object tupledValue = Interpreter.send(runtime.asTuple, context, result);
Object[] values = Tuple.getElements(tupledValue);
argumentsWithoutKeywords.addAll(Arrays.asList(values));
@@ -61,7 +61,7 @@ public void assignArgumentValues(final IokeObject locals, final IokeObject conte
for(Map.Entry<Object, Object> me : keys.entrySet()) {
givenKeywords.put(Text.getText(IokeObject.convertToText(me.getKey(), message, context, true)) + ":", me.getValue());
}
- } else if(IokeObject.findCell(result, message, context, "asTuple") != runtime.nul) {
+ } else if(IokeObject.findCell(IokeObject.as(result, context), "asTuple") != runtime.nul) {
Object tupledValue = Interpreter.send(runtime.asTuple, context, result);
Object[] values = Tuple.getElements(tupledValue);
argumentsWithoutKeywords.addAll(Arrays.asList(values));
@@ -24,21 +24,21 @@ public static void init(IokeObject obj) throws ControlFlow {
IokeObject baseBehavior = new IokeObject(runtime, "contains behavior copied from Base");
baseBehavior.setKind("DefaultBehavior BaseBehavior");
- baseBehavior.setCell("=", runtime.base.body.get("=", 0));
- baseBehavior.setCell("==", runtime.base.body.get("==", 0));
- baseBehavior.setCell("cell", runtime.base.body.get("cell", 0));
- baseBehavior.setCell("cell?", runtime.base.body.get("cell?", 0));
- baseBehavior.setCell("cell=", runtime.base.body.get("cell=", 0));
- baseBehavior.setCell("cells", runtime.base.body.get("cells", 0));
- baseBehavior.setCell("cellNames", runtime.base.body.get("cellNames", 0));
- baseBehavior.setCell("removeCell!", runtime.base.body.get("removeCell!", 0));
- baseBehavior.setCell("undefineCell!", runtime.base.body.get("undefineCell!", 0));
- baseBehavior.setCell("cellOwner?", runtime.base.body.get("cellOwner?", 0));
- baseBehavior.setCell("cellOwner", runtime.base.body.get("cellOwner", 0));
- baseBehavior.setCell("documentation", runtime.base.body.get("documentation", 0));
- baseBehavior.setCell("identity", runtime.base.body.get("identity", 0));
- baseBehavior.setCell("activatable", runtime.base.body.get("activatable", 0));
- baseBehavior.setCell("activatable=", runtime.base.body.get("activatable=", 0));
+ baseBehavior.setCell("=", runtime.base.body.get("="));
+ baseBehavior.setCell("==", runtime.base.body.get("=="));
+ baseBehavior.setCell("cell", runtime.base.body.get("cell"));
+ baseBehavior.setCell("cell?", runtime.base.body.get("cell?"));
+ baseBehavior.setCell("cell=", runtime.base.body.get("cell="));
+ baseBehavior.setCell("cells", runtime.base.body.get("cells"));
+ baseBehavior.setCell("cellNames", runtime.base.body.get("cellNames"));
+ baseBehavior.setCell("removeCell!", runtime.base.body.get("removeCell!"));
+ baseBehavior.setCell("undefineCell!", runtime.base.body.get("undefineCell!"));
+ baseBehavior.setCell("cellOwner?", runtime.base.body.get("cellOwner?"));
+ baseBehavior.setCell("cellOwner", runtime.base.body.get("cellOwner"));
+ baseBehavior.setCell("documentation", runtime.base.body.get("documentation"));
+ baseBehavior.setCell("identity", runtime.base.body.get("identity"));
+ baseBehavior.setCell("activatable", runtime.base.body.get("activatable"));
+ baseBehavior.setCell("activatable=", runtime.base.body.get("activatable="));
obj.mimicsWithoutCheck(baseBehavior);
obj.registerCell("BaseBehavior", baseBehavior);
@@ -110,9 +110,12 @@ public DefaultArgumentsDefinition getArguments() {
@Override
public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
- Object superCell = IokeObject.findSuperCellOn(out_on, out_self, out_message, out_context, out_name);
+ Object superCell = context.runtime.nul;
+ if(out_name != null) {
+ superCell = IokeObject.findSuperCellOn(out_on, out_self, out_context, out_name);
+ }
if(superCell == context.runtime.nul) {
- superCell = IokeObject.findSuperCellOn(out_on, out_self, out_message, out_context, Message.name(out_message));
+ superCell = IokeObject.findSuperCellOn(out_on, out_self, out_context, Message.name(out_message));
}
if(superCell != context.runtime.nul) {
@@ -202,9 +202,9 @@ private static boolean shouldActivate(IokeObject io, IokeObject message) throws
private static Object findCell(IokeObject message, IokeObject ctx, Object obj, String name, IokeObject recv) throws ControlFlow {
Runtime runtime = ctx.runtime;
- Object cell = IokeObject.findCell(recv, message, ctx, name);
+ Object cell = IokeObject.findCell(recv, name);
Object passed = null;
- while(cell == runtime.nul && ((cell = passed = IokeObject.findCell(recv, message, ctx, "pass")) != runtime.nul) && !isApplicable(passed, message, ctx)) {
+ while(cell == runtime.nul && ((cell = passed = IokeObject.findCell(recv, "pass")) != runtime.nul) && !isApplicable(passed, message, ctx)) {
cell = signalNoSuchCell(message, ctx, obj, name, cell, recv);
}
return cell;
@@ -216,7 +216,7 @@ public static Object perform(Object obj, IokeObject recv, IokeObject ctx, IokeOb
}
private static boolean isApplicable(Object pass, IokeObject message, IokeObject ctx) throws ControlFlow {
- if(pass != null && pass != ctx.runtime.nul && IokeObject.findCell(IokeObject.as(pass, ctx), message, ctx, "applicable?") != ctx.runtime.nul) {
+ if(pass != null && pass != ctx.runtime.nul && IokeObject.findCell(IokeObject.as(pass, ctx), "applicable?") != ctx.runtime.nul) {
return IokeObject.isTrue(Interpreter.send(ctx.runtime.isApplicableMessage, ctx, pass, ctx.runtime.createMessage(Message.wrap(message))));
}
return true;
@@ -126,7 +126,7 @@ public IokeObject negate(IokeObject obj) {
}
public final boolean isEqualTo(IokeObject self, Object other) throws ControlFlow {
- Object cell = IokeObject.findCell(self, self.runtime.eqMessage, self.runtime.ground, "==");
+ Object cell = IokeObject.findCell(self, "==");
if(cell == self.runtime.nul) {
boolean result = (other instanceof IokeObject) && (self.body == IokeObject.as(other, self).body);
@@ -138,7 +138,7 @@ public final boolean isEqualTo(IokeObject self, Object other) throws ControlFlow
}
public final int hashCode(IokeObject self) throws ControlFlow {
- Object cell = IokeObject.findCell(self, self.runtime.hashMessage, self.runtime.ground, "hash");
+ Object cell = IokeObject.findCell(self, "hash");
if(cell == self.runtime.nul) {
return System.identityHashCode(self.body);
@@ -461,7 +461,7 @@ private static void report(Object self, IokeObject context, IokeObject message,
}
public static Object activateFixed(IokeObject self, IokeObject context, IokeObject message, Object on) throws ControlFlow {
- Object cell = IokeObject.findCell(self, message, context, "activate");
+ Object cell = IokeObject.findCell(self, "activate");
if(cell == context.runtime.nul) {
report(self, context, message, "activate");
return context.runtime.nil;
Oops, something went wrong.

0 comments on commit 586bc62

Please sign in to comment.