annotate Implab/Automaton/DummyAlphabet.cs @ 165:e227e78d72e4 ref20160224

DFA refactoring
author cin
date Mon, 29 Feb 2016 02:02:17 +0300
parents ec35731ae299
children 0f70905b4652
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
1 using System;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
2 using System.Collections.Generic;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
3 using System.Linq;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
4
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
5 namespace Implab.Automaton {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
6 /// <summary>
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
7 /// Dummy alphabet consists of integer numbers which are identical to their classes.
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
8 /// </summary>
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
9 public class DummyAlphabet : IAlphabet<int> {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
10 readonly int m_size;
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
11
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
12 /// <summary>
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
13 /// Creates a new dummy alphabet with given size.
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
14 /// </summary>
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
15 /// <param name="size">The size of the alphabet, must be greater then zero.</param>
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
16 public DummyAlphabet(int size) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
17 Safe.ArgumentAssert(size > 0);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
18 m_size = 0;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
19 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
21 #region IAlphabet implementation
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
22
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23 public List<int>[] CreateReverseMap() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
24 Enumerable.Range(0, m_size).ToArray();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
25 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
26
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
27 public int[] Reclassify(IAlphabetBuilder<int> newAlphabet, IEnumerable<IEnumerable<int>> classes) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
28 Safe.ArgumentNotNull(newAlphabet, "newAlphabet");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
29 Safe.ArgumentNotNull(classes, "classes");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
30 var map = new int[m_size];
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
31 foreach (var cls in classes) {
164
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
32 if (cls.Contains(DFAConst.UNCLASSIFIED_INPUT))
ec35731ae299 Almost complete DFA refactoring
cin
parents: 163
diff changeset
33 continue;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
34 var newid = newAlphabet.DefineClass(cls);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
35 foreach (var id in cls)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
36 map[id] = newid;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
37 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
38
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
39 return map;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
40 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
41
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
42 public int Translate(int symobl) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
43 Safe.ArgumentInRange(symobl, 0, m_size, "symbol");
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
44 return symobl;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
45 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
46
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
47 public int Count {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
48 get {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
49 return m_size;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
50 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
51 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
52
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
53 #endregion
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
54 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
55 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
56