Skip to content
This repository
Browse code

Remove more unneeded words from NFA.Node and NFA.Edge

  • Loading branch information...
commit 4c7a3f05bdb07ed47e10830df11d5eb7206aa3bb 1 parent e9fbe21
Stefan O'Rear authored

Showing 1 changed file with 30 additions and 23 deletions. Show diff stats Hide diff stats

  1. +30 23 lib/Cursor.cs
53 lib/Cursor.cs
@@ -1135,25 +1135,24 @@ public sealed class CC : IFreeze {
1135 1135 // are used to refer to NFAs that haven't been assigned to numbers yet;
1136 1136 // the form is -{1,2}-2*N
1137 1137 public sealed class NFA {
1138   - public sealed class Node {
1139   - NFA owner;
  1138 + struct Node {
1140 1139 public int fate;
1141   - public bool final;
1142   - public int nedges;
1143 1140 public int first_edge;
1144   - public Node(NFA owner, int curfate) { this.owner = owner; fate = curfate; }
1145 1141
1146   - public override string ToString() {
  1142 + public string Describe(NFA owner, int ownix) {
1147 1143 List<Edge> le = new List<Edge>();
1148   - for (int i = 0; i < nedges; i++)
1149   - le.Add(owner.edges[first_edge + i]);
1150   - return "(" + fate + ")" + (final ? "+ " : " ") +
  1144 + for (int i = first_edge; i < owner.edges.Length; i++) {
  1145 + if (ownix + 1 < owner.nodes.Length &&
  1146 + i >= owner.nodes[ownix+1].first_edge)
  1147 + break;
  1148 + le.Add(owner.edges[i]);
  1149 + }
  1150 + return (fate > 0 ? "(" + fate + ") " : " ") +
1151 1151 Kernel.JoinS(", ", le);
1152 1152 }
1153 1153 }
1154 1154
1155 1155 public struct Edge {
1156   - public int from;
1157 1156 public int to;
1158 1157 public int when; // -2=epsilon -1=use CC
1159 1158 public CC when_cc;
@@ -1164,6 +1163,8 @@ public struct Edge {
1164 1163 }
1165 1164
1166 1165 List<Node> nodes_l = new List<Node>();
  1166 + List<int> outgoing = new List<int>();
  1167 + List<int> fromv = new List<int>();
1167 1168 Edge[] edges = new Edge[8];
1168 1169 int nedges;
1169 1170 Node[] nodes;
@@ -1182,11 +1183,14 @@ public struct Edge {
1182 1183 //int end_node;
1183 1184
1184 1185 public int AddNode() {
1185   - nodes_l.Add(new Node(this, curfate));
  1186 + nodes_l.Add(new Node());
  1187 + outgoing.Add(0);
1186 1188 return nodes_l.Count - 1;
1187 1189 }
1188 1190 public void SetFinal(int node) {
1189   - nodes_l[node].final = true;
  1191 + Node n = nodes_l[node];
  1192 + n.fate = curfate + 1;
  1193 + nodes_l[node] = n;
1190 1194 }
1191 1195 public void AddEdge(int from, int to, int when) {
1192 1196 AddEdge(from, to, when, null);
@@ -1196,30 +1200,30 @@ public struct Edge {
1196 1200 }
1197 1201 void AddEdge(int from, int to, int when, CC when_cc) {
1198 1202 Edge e;
1199   - e.from = from;
  1203 + fromv.Add(from);
1200 1204 e.to = to;
1201 1205 e.when = when;
1202 1206 e.when_cc = when_cc;
1203 1207 if (nedges == edges.Length) {
1204 1208 Array.Resize(ref edges, nedges * 2);
1205 1209 }
1206   - nodes_l[from].nedges++;
  1210 + ++outgoing[from];
1207 1211 edges[nedges++] = e;
1208 1212 }
1209   - public int NodeCount { get { return nodes_l.Count; } }
  1213 + public int NodeCount { get { return nodes.Length; } }
1210 1214 public Edge[] EdgesOf(int i, ref int index, ref int imax) {
1211 1215 index = nodes[i].first_edge;
1212   - imax = nodes[i].nedges + index;
  1216 + imax = (i == nodes.Length - 1) ? edges.Length : nodes[i+1].first_edge;
1213 1217 return edges;
1214 1218 }
1215 1219 public int FateOf(int i) {
1216 1220 var n = nodes[i];
1217   - return n.final ? n.fate : -1;
  1221 + return n.fate - 1;
1218 1222 }
1219 1223
1220 1224 public void Dump() {
1221   - for (int ix = 0; ix < nodes_l.Count; ix++) {
1222   - Console.WriteLine(ix + ": " + nodes_l[ix].ToString());
  1225 + for (int ix = 0; ix < nodes.Length; ix++) {
  1226 + Console.WriteLine(ix + ": " + nodes[ix].Describe(this, ix));
1223 1227 }
1224 1228 }
1225 1229
@@ -1237,8 +1241,8 @@ public struct Edge {
1237 1241
1238 1242 while (ngrey != 0) {
1239 1243 int val = greybuf[--ngrey];
1240   - int eix = nodes[val].first_edge;
1241   - int lix = nodes[val].nedges + eix;
  1244 + int eix = 0, lix = 0;
  1245 + EdgesOf(val, ref eix, ref lix);
1242 1246 while (eix != lix) {
1243 1247 Edge e = edges[eix++];
1244 1248 if (e.when == -2) {
@@ -1255,6 +1259,7 @@ public struct Edge {
1255 1259
1256 1260 public void Complete() {
1257 1261 nodes = nodes_l.ToArray();
  1262 + nodes_l = null;
1258 1263 info_stack = null;
1259 1264 outer_stack = null;
1260 1265 greybuf = new int[nodes.Length];
@@ -1265,13 +1270,15 @@ public struct Edge {
1265 1270 int i = 0;
1266 1271 // put the edges in correct order
1267 1272 for (int j = 0; j < nodes.Length; j++) {
1268   - i += nodes[j].nedges;
  1273 + i += outgoing[j];
1269 1274 nodes[j].first_edge = i;
1270 1275 }
1271 1276 for (i = 0; i < nedges; i++) {
1272 1277 Edge e = oedges[i];
1273   - edges[--nodes[e.from].first_edge] = e;
  1278 + edges[--nodes[fromv[i]].first_edge] = e;
1274 1279 }
  1280 + outgoing = null;
  1281 + fromv = null;
1275 1282 }
1276 1283
1277 1284 public Dictionary<LexerState,LexerState> dfashare

0 comments on commit 4c7a3f0

Please sign in to comment.
Something went wrong with that request. Please try again.