diff Implab/Automaton/IndexedAlphabetBase.cs @ 171:0f70905b4652 ref20160224

Working on regular DFA
author cin
date Thu, 10 Mar 2016 01:19:33 +0300
parents 96681e9d0cea
children 92d5278d1b10
line wrap: on
line diff
--- a/Implab/Automaton/IndexedAlphabetBase.cs	Fri Mar 04 01:56:31 2016 +0300
+++ b/Implab/Automaton/IndexedAlphabetBase.cs	Thu Mar 10 01:19:33 2016 +0300
@@ -17,16 +17,13 @@
         int m_nextId = 1;
         readonly int[] m_map;
 
-        public int Count {
-            get { return m_nextId; }
-        }
-
         protected IndexedAlphabetBase(int mapSize) {
             m_map = new int[mapSize];
         }
 
         protected IndexedAlphabetBase(int[] map) {
-            Debug.Assert(map != null);
+            Debug.Assert(map != null && map.Length > 0);
+            Debug.Assert(map.All(x => x >= 0));
 
             m_map = map;
             m_nextId = map.Max() + 1;
@@ -39,60 +36,41 @@
             return m_map[index];
         }
 
+        public int DefineSymbol(T symbol, int cls) {
+            var index = GetSymbolIndex(symbol);
+            m_map[index] = cls;
+            m_nextId = Math.Max(cls + 1, m_nextId);
+            return cls;
+        }
+
         public int DefineClass(IEnumerable<T> symbols) {
+            return DefineClass(symbols, m_nextId);
+        }
+
+        public int DefineClass(IEnumerable<T> symbols, int cls) {
             Safe.ArgumentNotNull(symbols, "symbols");
             symbols = symbols.Distinct();
 
-            foreach (var symbol in symbols) {
-                var index = GetSymbolIndex(symbol);
-                if (m_map[index] == DFAConst.UNCLASSIFIED_INPUT)
-                    m_map[GetSymbolIndex(symbol)] = m_nextId;
-                else
-                    throw new InvalidOperationException(String.Format("Symbol '{0}' already in use", symbol));
-            }
-            return m_nextId++;
-        }
-
-        public List<T>[] CreateReverseMap() {
-            return
-                Enumerable.Range(0, Count)
-                    .Select(
-                        i => InputSymbols
-                        .Where(x => i != DFAConst.UNCLASSIFIED_INPUT && m_map[GetSymbolIndex(x)] == i)
-                            .ToList()
-                    )
-                    .ToArray();
-        }
+            foreach (var symbol in symbols)
+                m_map[GetSymbolIndex(symbol)] = cls;
+            
+            m_nextId = Math.Max(cls + 1, m_nextId);
 
-        public int[] Reclassify(IAlphabetBuilder<T> newAlphabet, IEnumerable<IEnumerable<int>> classes) {
-            Safe.ArgumentNotNull(newAlphabet, "newAlphabet");
-            Safe.ArgumentNotNull(classes, "classes");
-            var reverseMap = CreateReverseMap();
-
-            var translationMap = new int[Count];
-
-            foreach (var scl in classes) {
-                // skip if the supper class contains the unclassified element
-                if (scl.Contains(DFAConst.UNCLASSIFIED_INPUT))
-                    continue;
-                var range = new List<T>();
-                foreach (var cl in scl) {
-                    if (cl < 0 || cl >= reverseMap.Length)
-                        throw new ArgumentOutOfRangeException(String.Format("Class {0} is not valid for the current alphabet", cl));
-                    range.AddRange(reverseMap[cl]);
-                }
-                var newClass = newAlphabet.DefineClass(range);
-                foreach (var cl in scl)
-                    translationMap[cl] = newClass;
-            }
-
-            return translationMap;
+            return cls;
         }
 
         public virtual int Translate(T symbol) {
             return m_map[GetSymbolIndex(symbol)];
         }
 
+        public int Count {
+            get { return m_nextId; }
+        }
+
+        public bool Contains(T symbol) {
+            return true;
+        }
+
         public abstract int GetSymbolIndex(T symbol);
 
         public abstract IEnumerable<T> InputSymbols { get; }