001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.process.extended;
010:
011: public class StackNodeSet {
012: private StackNode[] nodes = new StackNode[10];
013: private int position = 0;
014: private int count = 0;
015: private ExtendedParserAutomaton automaton = null;
016:
017: public void push(StackNode stackNode) {
018: for (int i = 0; i < count; i++)
019: if (nodes[i].compare(stackNode)) {
020: if (nodes[i].addAncestor(stackNode.ancestors[0])) {
021: if ((i < position) && (stackNode != nodes[i])) {
022: if (i < (position - 1)) {
023: StackNode dummy = nodes[position - 1];
024: nodes[position - 1] = nodes[i];
025: nodes[i] = dummy;
026: }
027:
028: position--;
029: }
030: }
031:
032: return;
033: }
034:
035: if (nodes.length == (count + 1)) {
036: StackNode[] newNodes = new StackNode[nodes.length + 10];
037: System.arraycopy(nodes, 0, newNodes, 0, nodes.length);
038: nodes = newNodes;
039: }
040:
041: nodes[count++] = stackNode;
042: }
043:
044: public StackNode pop() {
045: if (position >= count)
046: throw new java.util.EmptyStackException();
047:
048: return nodes[position++];
049: }
050:
051: public StackNode get(int index) {
052: if ((0 > index) || (index >= (count - position)))
053: throw new ArrayIndexOutOfBoundsException(index);
054:
055: return nodes[position + index];
056: }
057:
058: public void clear() {
059: position = 0;
060: count = 0;
061: }
062:
063: public boolean isEmpty() {
064: return position >= count;
065: }
066:
067: public int size() {
068: return count - position;
069: }
070:
071: public void setExtendedParserAutomaton(
072: ExtendedParserAutomaton automaton) {
073: this .automaton = automaton;
074: }
075:
076: public String toString() {
077: StringBuffer buffer = new StringBuffer();
078:
079: for (int i = position; i < count; i++) {
080: if (i > position)
081: buffer.append("\n");
082:
083: buffer.append(nodes[i].toString());
084: }
085:
086: return buffer.toString();
087: }
088:
089: public String toCanonicalString() {
090: StringBuffer buffer = new StringBuffer();
091:
092: for (int i = position; i < count; i++) {
093: if (i > position)
094: buffer.append("\n");
095:
096: buffer.append(nodes[i].toCanonicalString(automaton));
097: }
098:
099: return buffer.toString();
100: }
101: }
|