comparison Implab/Parsing/AlphabetBase.cs @ 55:c0bf853aa04f

Added initial JSON support +JSONParser +JSONWriter
author cin
date Sun, 15 Jun 2014 19:39:11 +0400
parents
children 21611344d366
comparison
equal deleted inserted replaced
51:2c332a9c64c0 55:c0bf853aa04f
1 using Implab;
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8
9 namespace Implab.Parsing {
10 public abstract class AlphabetBase<T> : IAlphabet<T> {
11 public const int UNCLASSIFIED = 0;
12
13 int m_nextId = 1;
14 int[] m_map;
15
16 public int Count {
17 get { return m_nextId; }
18 }
19
20 protected AlphabetBase() {
21 m_map = new int[MapSize];
22 }
23
24 protected AlphabetBase(int[] map) {
25 Debug.Assert(map != null);
26 Debug.Assert(map.Length == MapSize);
27
28 m_map = map;
29 m_nextId = map.Max() + 1;
30 }
31
32 public int DefineSymbol(T symbol) {
33 var index = GetSymbolIndex(symbol);
34 if (m_map[index] == UNCLASSIFIED)
35 m_map[index] = m_nextId++;
36 return m_map[index];
37 }
38
39 public int DefineClass(IEnumerable<T> symbols) {
40 Safe.ArgumentNotNull(symbols, "symbols");
41 symbols = symbols.Distinct();
42
43 foreach (var symbol in symbols) {
44 var index = GetSymbolIndex(symbol);
45 if (m_map[index] == UNCLASSIFIED)
46 m_map[GetSymbolIndex(symbol)] = m_nextId;
47 else
48 throw new InvalidOperationException(String.Format("Symbol '{0}' already in use", symbol));
49 }
50 return m_nextId++;
51 }
52
53 public List<T>[] CreateReverseMap() {
54 return
55 Enumerable.Range(UNCLASSIFIED, Count)
56 .Select(
57 i => InputSymbols
58 .Where(x => i != UNCLASSIFIED && m_map[GetSymbolIndex(x)] == i)
59 .ToList()
60 )
61 .ToArray();
62 }
63
64 public int[] Reclassify(IAlphabet<T> newAlphabet, IEnumerable<ICollection<int>> classes) {
65 Safe.ArgumentNotNull(newAlphabet, "newAlphabet");
66 Safe.ArgumentNotNull(classes, "classes");
67 var reverseMap = CreateReverseMap();
68
69 int[] translationMap = new int[Count];
70
71 foreach (var scl in classes) {
72 // skip if the supper class contains the unclassified element
73 if (scl.Contains(UNCLASSIFIED))
74 continue;
75 var range = new List<T>();
76 foreach (var cl in scl) {
77 if (cl < 0 || cl >= reverseMap.Length)
78 throw new ArgumentOutOfRangeException(String.Format("Class {0} is not valid for the current alphabet", cl));
79 range.AddRange(reverseMap[cl]);
80 }
81 var newClass = newAlphabet.DefineClass(range);
82 foreach (var cl in scl)
83 translationMap[cl] = newClass;
84 }
85
86 return translationMap;
87 }
88
89 public int Translate(T symbol) {
90 return m_map[GetSymbolIndex(symbol)];
91 }
92
93 public abstract int GetSymbolIndex(T symbol);
94
95 public abstract IEnumerable<T> InputSymbols { get; }
96
97 protected abstract int MapSize { get; }
98
99 public int[] GetTranslationMap() {
100 return m_map;
101 }
102 }
103 }