-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaps.cs
138 lines (119 loc) · 4.77 KB
/
Maps.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Lic:
// Maps.cs
// Alternate Maps
// version: 22.10.27
// Copyright (C) Alternate maps Jeroen P. Broks
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
// EndLic
using System;
using System.Text;
using System.Diagnostics;
using System.Collections.Generic;
namespace TrickyUnits {
delegate void MapError(string Message);
class StringMap {
public readonly SortedDictionary<string, string> Map;
public bool MustExist = false;
MapError Err = delegate (string Msg) { Console.WriteLine(Msg); Debug.WriteLine(Msg); };
public MapError Error {
set {
Err = value ?? throw new Exception("NULL not allowed for error handler!");
}
get {
throw new Exception("Error field is write-only!");
}
}
public string this[string key] {
get {
if (!Map.ContainsKey(key)) {
if (MustExist)
Err?.Invoke($"StringMap does not contain key {key}");
return "";
}
return Map[key];
}
set => Map[key] = value;
}
public SortedDictionary<string, string>.KeyCollection Keys => Map.Keys;
public SortedDictionary<string, string>.ValueCollection Values => Map.Values;
public StringMap() { Map = new SortedDictionary<string, string>(); }
public StringMap(SortedDictionary<string, string> M) { Map = M; }
public StringMap(Dictionary<string, string> M) {
Map = new SortedDictionary<string, string>();
foreach (string k in M.Keys) Map[k] = M[k];
}
}
class TMap<MapKey, MapValue> {
public readonly SortedDictionary<MapKey, MapValue> Map;
public bool MustExist = false;
MapError Err = delegate (string Msg) { Console.WriteLine(Msg); Debug.WriteLine(Msg); };
public MapError Error {
set {
Err = value ?? throw new Exception("NULL not allowed for error handler!");
}
get {
throw new Exception("Error field is write-only!");
}
}
public MapValue this[MapKey key] {
set { Map[key] = value; }
get {
try {
if (!Map.ContainsKey(key)) {
if (MustExist)
Err?.Invoke($"StringMap does not contain key {key.ToString()}");
return default(MapValue);
}
} catch(Exception huh) {
Debug.WriteLine($"{huh.Message}");
Debug.WriteLine($"{huh.StackTrace}");
Err?.Invoke($"ERROR! {huh.Message}");
return default(MapValue);
}
return Map[key];
}
}
public SortedDictionary<MapKey, MapValue>.KeyCollection Keys => Map.Keys;
public SortedDictionary<MapKey, MapValue>.ValueCollection Values => Map.Values;
public int Count => Map.Count;
public TMap() {
Map = new SortedDictionary<MapKey, MapValue>();
}
public TMap(SortedDictionary<MapKey,MapValue> WithDict) {
Map = WithDict;
}
public void Clear() => Map.Clear();
public void Kill(MapKey key) => Map.Remove(key);
}
class SBMap {
readonly TMap<string, StringBuilder> Map = new TMap<string, StringBuilder>();
public void Plus(string key,string app) => Map[key].Append(app);
public string this[string k] {
get => Map[k].ToString();
set {
if (value == "")
Map.Map.Remove(k);
else {
if (!Map.Map.ContainsKey(k))
Map[k] = new StringBuilder(value);
else {
Map[k].Remove(0, Map[k].Length);
Map[k].Append(value);
}
}
}
}
}
}