public XmlQueryStaticData(byte[] data, Type[]?ebTypes) { MemoryStream dataStream = new MemoryStream(data, writable: false); XmlQueryDataReader dataReader = new XmlQueryDataReader(dataStream); int length; // Read a format version int formatVersion = dataReader.Read7BitEncodedInt(); // Changes in the major part of version are not supported if ((formatVersion & ~0xff) > CurrentFormatVersion) { throw new NotSupportedException(); } // XmlWriterSettings defaultWriterSettings; _defaultWriterSettings = new XmlWriterSettings(dataReader); // IList<WhitespaceRule> whitespaceRules; length = dataReader.ReadInt32(); if (length != 0) { _whitespaceRules = new WhitespaceRule[length]; for (int idx = 0; idx < length; idx++) { _whitespaceRules[idx] = new WhitespaceRule(dataReader); } } // string[] names; length = dataReader.ReadInt32(); if (length != 0) { _names = new string[length]; for (int idx = 0; idx < length; idx++) { _names[idx] = dataReader.ReadString(); } } // StringPair[][] prefixMappingsList; length = dataReader.ReadInt32(); if (length != 0) { _prefixMappingsList = new StringPair[length][]; for (int idx = 0; idx < length; idx++) { int length2 = dataReader.ReadInt32(); _prefixMappingsList[idx] = new StringPair[length2]; for (int idx2 = 0; idx2 < length2; idx2++) { _prefixMappingsList[idx][idx2] = new StringPair(dataReader.ReadString(), dataReader.ReadString()); } } } // Int32Pair[] filters; length = dataReader.ReadInt32(); if (length != 0) { _filters = new Int32Pair[length]; for (int idx = 0; idx < length; idx++) { _filters[idx] = new Int32Pair(dataReader.Read7BitEncodedInt(), dataReader.Read7BitEncodedInt()); } } // XmlQueryType[] types; length = dataReader.ReadInt32(); if (length != 0) { _types = new XmlQueryType[length]; for (int idx = 0; idx < length; idx++) { _types[idx] = XmlQueryTypeFactory.Deserialize(dataReader); } } // XmlCollation[] collations; length = dataReader.ReadInt32(); if (length != 0) { _collations = new XmlCollation[length]; for (int idx = 0; idx < length; idx++) { _collations[idx] = new XmlCollation(dataReader); } } // string[] globalNames; length = dataReader.ReadInt32(); if (length != 0) { _globalNames = new string[length]; for (int idx = 0; idx < length; idx++) { _globalNames[idx] = dataReader.ReadString(); } } // EarlyBoundInfo[] earlyBound; length = dataReader.ReadInt32(); if (length != 0) { _earlyBound = new EarlyBoundInfo[length]; for (int idx = 0; idx < length; idx++) { _earlyBound[idx] = new EarlyBoundInfo(dataReader.ReadString(), ebTypes ![idx]);
/// <summary> /// Minimizes the given automaton using Hopcroft's algorithm. /// </summary> public static void MinimizeHopcroft(Automaton a) { a.Determinize(); if (a.initial.numTransitions == 1) { Transition t = a.initial.TransitionsArray[0]; if (t.to == a.initial && t.min == Character.MIN_CODE_POINT && t.max == Character.MAX_CODE_POINT) { return; } } a.Totalize(); // initialize data structures int[] sigma = a.GetStartPoints(); State[] states = a.GetNumberedStates(); int sigmaLen = sigma.Length, statesLen = states.Length; List <State>[,] reverse = new List <State> [statesLen, sigmaLen]; ISet <State>[] partition = new EquatableSet <State> [statesLen]; List <State>[] splitblock = new List <State> [statesLen]; int[] block = new int[statesLen]; StateList[,] active = new StateList[statesLen, sigmaLen]; StateListNode[,] active2 = new StateListNode[statesLen, sigmaLen]; LinkedList <Int32Pair> pending = new LinkedList <Int32Pair>(); OpenBitSet pending2 = new OpenBitSet(sigmaLen * statesLen); OpenBitSet split = new OpenBitSet(statesLen), refine = new OpenBitSet(statesLen), refine2 = new OpenBitSet(statesLen); for (int q = 0; q < statesLen; q++) { splitblock[q] = new List <State>(); partition[q] = new EquatableSet <State>(); for (int x = 0; x < sigmaLen; x++) { active[q, x] = new StateList(); } } // find initial partition and reverse edges for (int q = 0; q < statesLen; q++) { State qq = states[q]; int j = qq.accept ? 0 : 1; partition[j].Add(qq); block[q] = j; for (int x = 0; x < sigmaLen; x++) { //List<State>[] r = reverse[qq.Step(sigma[x]).number]; var r = qq.Step(sigma[x]).number; if (reverse[r, x] == null) { reverse[r, x] = new List <State>(); } reverse[r, x].Add(qq); } } // initialize active sets for (int j = 0; j <= 1; j++) { for (int x = 0; x < sigmaLen; x++) { foreach (State qq in partition[j]) { if (reverse[qq.number, x] != null) { active2[qq.number, x] = active[j, x].Add(qq); } } } } // initialize pending for (int x = 0; x < sigmaLen; x++) { int j = (active[0, x].Count <= active[1, x].Count) ? 0 : 1; pending.AddLast(new Int32Pair(j, x)); pending2.Set(x * statesLen + j); } // process pending until fixed point int k = 2; while (pending.Count > 0) { Int32Pair ip = pending.First.Value; pending.Remove(ip); int p = ip.N1; int x = ip.N2; pending2.Clear(x * statesLen + p); // find states that need to be split off their blocks for (StateListNode m = active[p, x].First; m != null; m = m.Next) { List <State> r = reverse[m.Q.number, x]; if (r != null) { foreach (State s in r) { int i = s.number; if (!split.Get(i)) { split.Set(i); int j = block[i]; splitblock[j].Add(s); if (!refine2.Get(j)) { refine2.Set(j); refine.Set(j); } } } } } // refine blocks for (int j = refine.NextSetBit(0); j >= 0; j = refine.NextSetBit(j + 1)) { List <State> sb = splitblock[j]; if (sb.Count < partition[j].Count) { ISet <State> b1 = partition[j]; ISet <State> b2 = partition[k]; foreach (State s in sb) { b1.Remove(s); b2.Add(s); block[s.number] = k; for (int c = 0; c < sigmaLen; c++) { StateListNode sn = active2[s.number, c]; if (sn != null && sn.Sl == active[j, c]) { sn.Remove(); active2[s.number, c] = active[k, c].Add(s); } } } // update pending for (int c = 0; c < sigmaLen; c++) { int aj = active[j, c].Count, ak = active[k, c].Count, ofs = c * statesLen; if (!pending2.Get(ofs + j) && 0 < aj && aj <= ak) { pending2.Set(ofs + j); pending.AddLast(new Int32Pair(j, c)); } else { pending2.Set(ofs + k); pending.AddLast(new Int32Pair(k, c)); } } k++; } refine2.Clear(j); foreach (State s in sb) { split.Clear(s.number); } sb.Clear(); } refine.Clear(0, refine.Length - 1); } // make a new state for each equivalence class, set initial state State[] newstates = new State[k]; for (int n = 0; n < newstates.Length; n++) { State s = new State(); newstates[n] = s; foreach (State q in partition[n]) { if (q == a.initial) { a.initial = s; } s.accept = q.accept; s.number = q.number; // select representative q.number = n; } } // build transitions and set acceptance for (int n = 0; n < newstates.Length; n++) { State s = newstates[n]; s.accept = states[s.number].accept; foreach (Transition t in states[s.number].GetTransitions()) { s.AddTransition(new Transition(t.min, t.max, newstates[t.to.number])); } } a.ClearNumberedStates(); a.RemoveDeadTransitions(); }
/// <summary> /// Deserialize XmlQueryStaticData object from a byte array. /// </summary> public XmlQueryStaticData(byte[] data, Type[] ebTypes) { MemoryStream dataStream = new MemoryStream(data, /*writable:*/false); XmlQueryDataReader dataReader = new XmlQueryDataReader(dataStream); int length; // Read a format version int formatVersion = dataReader.ReadInt32Encoded(); // Changes in the major part of version are not supported if ((formatVersion & ~0xff) > CurrentFormatVersion) throw new NotSupportedException(); // XmlWriterSettings defaultWriterSettings; _defaultWriterSettings = new XmlWriterSettings(dataReader); // IList<WhitespaceRule> whitespaceRules; length = dataReader.ReadInt32(); if (length != 0) { _whitespaceRules = new WhitespaceRule[length]; for (int idx = 0; idx < length; idx++) { _whitespaceRules[idx] = new WhitespaceRule(dataReader); } } // string[] names; length = dataReader.ReadInt32(); if (length != 0) { _names = new string[length]; for (int idx = 0; idx < length; idx++) { _names[idx] = dataReader.ReadString(); } } // StringPair[][] prefixMappingsList; length = dataReader.ReadInt32(); if (length != 0) { _prefixMappingsList = new StringPair[length][]; for (int idx = 0; idx < length; idx++) { int length2 = dataReader.ReadInt32(); _prefixMappingsList[idx] = new StringPair[length2]; for (int idx2 = 0; idx2 < length2; idx2++) { _prefixMappingsList[idx][idx2] = new StringPair(dataReader.ReadString(), dataReader.ReadString()); } } } // Int32Pair[] filters; length = dataReader.ReadInt32(); if (length != 0) { _filters = new Int32Pair[length]; for (int idx = 0; idx < length; idx++) { _filters[idx] = new Int32Pair(dataReader.ReadInt32Encoded(), dataReader.ReadInt32Encoded()); } } // XmlQueryType[] types; length = dataReader.ReadInt32(); if (length != 0) { _types = new XmlQueryType[length]; for (int idx = 0; idx < length; idx++) { _types[idx] = XmlQueryTypeFactory.Deserialize(dataReader); } } // XmlCollation[] collations; length = dataReader.ReadInt32(); if (length != 0) { _collations = new XmlCollation[length]; for (int idx = 0; idx < length; idx++) { _collations[idx] = new XmlCollation(dataReader); } } // string[] globalNames; length = dataReader.ReadInt32(); if (length != 0) { _globalNames = new string[length]; for (int idx = 0; idx < length; idx++) { _globalNames[idx] = dataReader.ReadString(); } } // EarlyBoundInfo[] earlyBound; length = dataReader.ReadInt32(); if (length != 0) { _earlyBound = new EarlyBoundInfo[length]; for (int idx = 0; idx < length; idx++) { _earlyBound[idx] = new EarlyBoundInfo(dataReader.ReadString(), ebTypes[idx]); } } Debug.Assert(formatVersion != CurrentFormatVersion || dataReader.Read() == -1, "Extra data at the end of the stream"); dataReader.Dispose(); }