public String toString() { if (PrintLow.shouldPrintAtCurrentLevel()) { final SubLObject oldLevel = PrintLow.maybeIncreasePrintLevel(); try { if (this == SubLNil.NIL) { return "NIL"; } SubLListListIterator iter = null; Resourcer resourcer = Resourcer.getInstance(); try { final StringBuilder buf = new StringBuilder("("); final boolean checkLength = PrintLow.controlPrintLength(); final int printLength = PrintLow.maxPrintLength(); int counter = 0; boolean terminatedEarly = false; iter = resourcer.acquireSubLListListIterator(this); while (iter.hasNext() && (!iter.isNextImproperElement())) { buf.append(iter.next()); if (checkLength) { counter++; if (counter >= printLength) { buf.append(" ... "); terminatedEarly = true; break; } } if (iter.hasNext()) { buf.append(" "); } } if (!terminatedEarly && iter.isNextImproperElement()) { buf.append(" . ").append(iter.getDottedElement()); } buf.append(")"); return buf.toString(); } finally { resourcer.releaseSubLListListIterator(iter); } } finally { PrintLow.maybeDecreasePrintLevel(oldLevel); } } else { return "( # )"; } }
public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } SubLObject sublObj = (SubLObject) obj; if (!sublObj.isList()) { return false; } SubLList otherCons = sublObj.toList(); SubLListListIterator iter1 = null; SubLListListIterator iter2 = null; Resourcer resourcer = Resourcer.getInstance(); try { iter1 = resourcer.acquireSubLListListIterator(this); iter2 = resourcer.acquireSubLListListIterator(otherCons); while (iter1.hasNext() && (!iter1.isNextImproperElement())) { if ((!iter2.hasNext()) || (iter2.isNextImproperElement())) { return false; } if (!iter1.next().equals(iter2.next())) { return false; } } if (iter1.isNextImproperElement()) { if (iter2.isNextImproperElement()) { return iter1.getDottedElement().equals(iter2.getDottedElement()); } return false; } else if (iter2.hasNext()) { // iter1 is prefix of iter2 case return false; } // they should be equal at this point return true; } finally { resourcer.releaseSubLListListIterator(iter1); resourcer.releaseSubLListListIterator(iter2); } }
public SubLList assoc(SubLObject item, BinaryFunction test, UnaryFunction key) { SubLObject currentItem = null; SubLList result = SubLNil.NIL; SubLListListIterator iter = null; Resourcer resourcer = Resourcer.getInstance(); SubLList curAssoc; try { iter = resourcer.acquireSubLListListIterator(this); while (iter.hasNext()) { curAssoc = iter.nextSubLObject().toList(); if (SubLNil.NIL != test.processItem(item, key.processItem(curAssoc.first()))) { result = curAssoc; break; } } } finally { resourcer.releaseSubLListListIterator(iter); } return result; }
private final SubLList mapResult( SubLFunction function, SubLObject[] moreLists, boolean returnConsedResults) { ArrayList<SubLObject> results = new ArrayList<SubLObject>(); SubLListListIterator[] iterators = new SubLListListIterator[1 + moreLists.length]; // @todo get rid of call to new -APB int iteratorCount = iterators.length; SubLObject[] args = null; Resourcer resourcer = Resourcer.getInstance(); try { args = resourcer.acquireSubLObjectArray(iteratorCount); iterators[0] = resourcer.acquireSubLListListIterator(this); for (int i = 0, size = moreLists.length; i < size; i++) { iterators[i + 1] = resourcer.acquireSubLListListIterator(moreLists[i].toList()); } SubLListListIterator iter = null; SubLObject curResult = null; while (true) { for (int i = 0; i < iteratorCount; i++) { iter = iterators[i]; if (!iter.hasNext()) { return SubLObjectFactory.makeList(results); } if (iter.isNextImproperElement()) { Errors.error("Unexpected improper list: " + ((i == 0) ? this : moreLists[i - 1])); } args[i] = iter.nextSubLObject(); } curResult = Functions.funcall(function, args); if (results != null) { results.add(curResult); } } } finally { resourcer.releaseSubLObjectArray(args); for (int i = 0, size = iterators.length; i < size; i++) { resourcer.releaseSubLListListIterator(iterators[i]); } } }
public int hashCode(int currentDepth) { if (currentDepth < MAX_HASH_DEPTH) { SubLListListIterator iter = null; Resourcer resourcer = Resourcer.getInstance(); int hashCode = 0; try { int index = 0; iter = resourcer.acquireSubLListListIterator(this); while (iter.hasNext() && (!iter.isNextImproperElement()) && (index++ < MAX_HASH_LENGTH)) { hashCode ^= iter.nextSubLObject().hashCode(currentDepth + 1); index++; } if (iter.isNextImproperElement()) { hashCode ^= iter.getDottedElement().hashCode(currentDepth + 1); } return hashCode; } finally { resourcer.releaseSubLListListIterator(iter); } } else { return DEFAULT_EXCEEDED_HASH_VALUE; } }