public int Compare(Object x, Object y) { Nfa a = (Nfa)x; Nfa b = (Nfa)y; return(a.GetLabel() - b.GetLabel()); }
/* * Function: sort_states */ public void sort_states() { nfa_set.Sort(0, nfa_set.Count, null); //nfa_set.Sort(0, nfa_set.Count, new NfaComp()); #if OLD_DEBUG Console.Write("NFA vector indices: "); for (int index = 0; index < nfa_set.Count; index++) { Nfa elem = (Nfa)nfa_set[index]; Console.Write(Int32.ToString(elem.GetLabel()) + " "); } Console.Write("\n"); #endif return; }
/* * function: Print_Set */ public static void Print_Set(ArrayList nfa_set) { int size; int elem; size = nfa_set.Count; if (size == 0) { Console.Write("empty "); } for (elem = 0; elem < size; ++elem) { Nfa nfa = (Nfa)nfa_set[elem]; Console.Write(Int32.ToString(nfa.GetLabel()) + " "); } }
/* * Function: e_closure * Description: Alters input set. */ public void e_closure() { Nfa state = null; /* * Debug checks */ #if DEBUG Utility.assert(null != nfa_set); Utility.assert(null != nfa_bit); #endif accept = null; anchor = Spec.NONE; accept_index = Utility.INT_MAX; /* * Create initial stack. */ Stack nfa_stack = new Stack(); int size = nfa_set.Count; for (int i = 0; i < size; i++) { state = (Nfa)nfa_set[i]; #if DEBUG Utility.assert(nfa_bit.Get(state.GetLabel())); #endif nfa_stack.Push(state); } /* * Main loop. */ while (nfa_stack.Count > 0) { Object o = nfa_stack.Pop(); if (o == null) { break; } state = (Nfa)o; #if OLD_DUMP_DEBUG if (null != state.GetAccept()) { Console.WriteLine("Looking at accepting state " + Int32.ToString(state.GetLabel()) + " with <" + state.GetAccept().action + ">"); } #endif if (null != state.GetAccept() && state.GetLabel() < accept_index) { accept_index = state.GetLabel(); accept = state.GetAccept(); anchor = state.GetAnchor(); #if OLD_DUMP_DEBUG Console.WriteLine("Found accepting state " + Int32.ToString(state.GetLabel()) + " with <" + state.GetAccept().action + ">"); #endif #if DEBUG Utility.assert(null != accept); Utility.assert(Spec.NONE == anchor || 0 != (anchor & Spec.END) || 0 != (anchor & Spec.START)); #endif } if (Nfa.EPSILON == state.GetEdge()) { if (state.GetNext() != null) { if (false == nfa_set.Contains(state.GetNext())) { #if DEBUG Utility.assert(false == nfa_bit.Get(state.GetNext().GetLabel())); #endif nfa_bit.Set(state.GetNext().GetLabel(), true); nfa_set.Add(state.GetNext()); nfa_stack.Push(state.GetNext()); } } if (null != state.GetSib()) { if (false == nfa_set.Contains(state.GetSib())) { #if DEBUG Utility.assert(false == nfa_bit.Get(state.GetSib().GetLabel())); #endif nfa_bit.Set(state.GetSib().GetLabel(), true); nfa_set.Add(state.GetSib()); nfa_stack.Push(state.GetSib()); } } } } if (null != nfa_set) { sort_states(); } }