Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/sablecc/sablecc/LR0ItemSet.java


1   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2    * This file is part of SableCC.                             *
3    * See the file "LICENSE" for copyright information and the  *
4    * terms and conditions for copying, distribution and        *
5    * modification of SableCC.                                  *
6    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7   
8   package org.sablecc.sablecc;
9   
10  import java.util.*;
11  import com.sun.java.util.collections.*;
12  
13  import java.util.Vector;
14  
15  final class LR0ItemSet implements Cloneable, Comparable
16  {
17      private final TreeMap items;
18      private int hashCode;
19  
20      LR0ItemSet()
21      {
22          items = new TreeMap();
23      }
24  
25      private LR0ItemSet(LR0ItemSet set)
26      {
27          items = (TreeMap) set.items.clone();
28      }
29  
30      void set(LR0Item item)
31      {
32          if(items.put(item, item) == null)
33          {
34              hashCode += item.hashCode();
35              modified_ = true;
36          }
37      }
38  
39      boolean get(LR0Item item)
40      {
41          return items.get(item) != null;
42      }
43  
44      LR0Item[] items_;
45      boolean modified_ = true;
46  
47      private void computeArray()
48      {
49          Vector itemVector = new Vector(0);
50  
51          for(Iterator e = items.keySet().iterator(); e.hasNext();)
52          {
53              itemVector.addElement(e.next());
54          }
55  
56          items_ = new LR0Item[itemVector.size()];
57          itemVector.copyInto(items_);
58          modified_ = false;
59      }
60  
61      LR0Item[] items()
62      {
63          if(modified_)
64          {
65              computeArray();
66          }
67  
68          return items_;
69      }
70  
71      public String toString()
72      {
73          StringBuffer result = new StringBuffer();
74          result.append("{");
75  
76          Production[] productions = Production.productions();
77          boolean space = false;
78          for(int i = 0; i < productions.length; i++)
79          {
80              int rightsideLength = productions[i].rightside().length;
81  
82              for(int j = 0; j <= rightsideLength; j++)
83              {
84                  LR0Item item = new LR0Item(productions[i].index, j);
85                  if(get(item))
86                  {
87                      if(space)
88                      {
89                          result.append(",");
90                      }
91                      else
92                      {
93                          space = true;
94                      }
95  
96                      result.append(item);
97                  }
98              }
99  
100         }
101 
102         result.append("}");
103         return result.toString();
104     }
105 
106     public Object clone()
107     {
108         return new LR0ItemSet(this);
109     }
110 
111     public boolean equals(Object obj)
112     {
113         if((obj == null) ||
114             (obj.getClass() != this.getClass()))
115         {
116             return false;
117         }
118 
119         LR0ItemSet set = (LR0ItemSet) obj;
120 
121         if(set.items.size() != items.size())
122         {
123             return false;
124         }
125 
126         for(Iterator e = items.keySet().iterator(); e.hasNext();)
127         {
128             if(!set.get((LR0Item) e.next()))
129             {
130                 return false;
131             }
132         }
133 
134         return true;
135     }
136 
137     public int hashCode()
138     {
139         return hashCode;
140     }
141 
142     public int compareTo(Object object)
143     {
144         LR0ItemSet set = (LR0ItemSet) object;
145 
146         int result = items.size() - set.items.size();
147 
148         if(result == 0)
149         {
150             Iterator e = items.keySet().iterator();
151             Iterator f = set.items.keySet().iterator();
152 
153             while(e.hasNext() && f.hasNext() && (result == 0))
154             {
155                 result = ((LR0Item) e.next()).compareTo(f.next());
156             }
157 
158             if(result == 0)
159             {
160                 if(e.hasNext())
161                 {
162                     return 1;
163                 }
164 
165                 if(f.hasNext())
166                 {
167                     return -1;
168                 }
169             }
170         }
171 
172         return result;
173     }
174 
175 }
176 
177