KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > process > extended > StackNodeSet


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.process.extended;
10
11 public class StackNodeSet
12 {
13   private StackNode[] nodes = new StackNode[10];
14   private int position = 0;
15   private int count = 0;
16   public int watchdog = 0;
17
18   public void push(StackNode stackNode)
19   {
20     watchdog++;
21     //System.out.println("push "+stackNode);
22

23     for (int i = 0; i<count; i++)
24     {
25       if (nodes[i].pattern==stackNode.pattern)
26       {
27         //System.out.println("state already exists");
28
for (StackNode node = nodes[i]; node!=null; node = node.sibling)
29         {
30           // if state already exists with identical or equal content
31
if (node.ancestor==stackNode.ancestor)
32           {
33             //System.out.println("ancestor already exists");
34
return;
35           }
36
37           if (node.sibling==null)
38           {
39             node.sibling = stackNode;
40             break;
41           }
42         }
43
44         if (i<position) // move node ontop, for revisiting
45
{
46 /* if (i<(position-1))
47           {
48             StackNode dummy = nodes[position-1];
49             nodes[position-1] = nodes[i];
50             nodes[i] = dummy;
51           }
52
53           position--;*/

54           position=0;
55         }
56
57         return;
58       }
59     }
60
61     if (nodes.length==(count+1))
62     {
63       StackNode[] newNodes = new StackNode[nodes.length+10];
64       System.arraycopy(nodes, 0, newNodes, 0, nodes.length);
65       nodes = newNodes;
66     }
67
68     nodes[count++] = stackNode;
69   }
70
71   public StackNode pop()
72   {
73     if (position>=count)
74       throw new java.util.EmptyStackException JavaDoc();
75
76     return nodes[position++];
77   }
78
79   public void clear()
80   {
81     position = 0;
82     count = 0;
83     watchdog = 0;
84   }
85
86   public boolean isEmpty()
87   {
88     return position>=count;
89   }
90
91   public int size()
92   {
93     return count-position;
94   }
95
96   public String JavaDoc toString()
97   {
98     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
99     buffer.append("{");
100
101     for (int i = 0; i<count; i++)
102     {
103       if (i==position)
104         buffer.append("|");
105
106       buffer.append(nodes[i].toString());
107
108       if (((i+1)!=position) && (i<(count-1)))
109         buffer.append(",");
110     }
111
112     if (count==position)
113       buffer.append("|");
114
115     buffer.append("}\n");
116
117     return buffer.toString();
118   }
119
120   public String JavaDoc dump()
121   {
122     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
123
124     for (int i = 0; i<count; i++)
125       buffer.append(nodes[i].dump());
126
127     return buffer.toString();
128   }
129 }
130
Popular Tags