-
Notifications
You must be signed in to change notification settings - Fork 0
/
GlifMap.cs
41 lines (37 loc) · 845 Bytes
/
GlifMap.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
namespace TuckBytesInCode
{
//GlifMap creates a reverse lookup; character -> index
public class GlifMap
{
public GlifMap(ICharLookup map)
{
Init(map);
}
void Init(ICharLookup map)
{
int @base = map.Base();
lookup = new Dictionary<char, int>(@base);
for(int b=0; b<@base; b++) {
lookup.Add(map.Map(b),b);
}
if (map.IncludePadding) {
Padding = map.Padding;
}
}
//lookup the index for a given character
public int Map(char glif)
{
if (Padding.HasValue && glif == Padding.Value) {
return 0;
}
if (!lookup.TryGetValue(glif,out int index)) {
throw new KeyNotFoundException("Failed to reverse map "+glif);
}
return index;
}
Dictionary<char,int> lookup;
char? Padding = null;
}
}