forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsolidatedTable.cs
More file actions
302 lines (256 loc) · 6.75 KB
/
ConsolidatedTable.cs
File metadata and controls
302 lines (256 loc) · 6.75 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Text;
using Waher.Content.Markdown.Model;
using Waher.Content.Markdown.Model.BlockElements;
namespace Waher.Content.Markdown.Consolidation
{
/// <summary>
/// Represents a consolidated table.
/// </summary>
public class ConsolidatedTable
{
private string caption;
private string id;
private int nrColumns;
private int nrHeaderRows;
private int nrCellRows;
private readonly Dictionary<string, int> columnIndices = new Dictionary<string, int>();
private readonly Dictionary<long, MarkdownElement> headers = new Dictionary<long, MarkdownElement>();
private readonly Dictionary<long, MarkdownElement> cells = new Dictionary<long, MarkdownElement>();
private readonly Dictionary<int, TextAlignment> columnAlignments = new Dictionary<int, TextAlignment>();
private readonly Dictionary<int, string> sources = new Dictionary<int, string>();
private readonly object synchObj = new object();
/// <summary>
/// Represents a consolidated table.
/// </summary>
/// <param name="Source">Source of table.</param>
/// <param name="MarkdownTable">Markdown table.</param>
public ConsolidatedTable(string Source, Table MarkdownTable)
{
int i, j;
this.nrColumns = MarkdownTable.Columns;
this.nrHeaderRows = MarkdownTable.Headers.Length;
this.nrCellRows = MarkdownTable.Rows.Length;
this.caption = MarkdownTable.Caption;
this.id = MarkdownTable.Id;
i = 0;
foreach (TextAlignment Alignment in MarkdownTable.Alignments)
{
this.columnIndices[this.GetHeaderKey(MarkdownTable.Headers, i)] = i;
this.columnAlignments[i++] = Alignment;
}
i = 0;
foreach (MarkdownElement[] Row in MarkdownTable.Headers)
{
j = 0;
foreach (MarkdownElement Element in Row)
this.headers[(((long)i) << 32) + j++] = Element;
i++;
}
i = 0;
foreach (MarkdownElement[] Row in MarkdownTable.Rows)
{
j = 0;
foreach (MarkdownElement Element in Row)
this.cells[(((long)i) << 32) + j++] = Element;
this.sources[i++] = Source;
}
}
private string GetHeaderKey(MarkdownElement[][] Headers, int ColumnIndex)
{
StringBuilder sb = new StringBuilder();
bool First = true;
foreach (MarkdownElement[] Row in Headers)
{
if (First)
First = false;
else
sb.AppendLine();
if (ColumnIndex < Row.Length)
Row[ColumnIndex].GenerateMarkdown(sb);
}
return sb.ToString();
}
/// <summary>
/// Number of columns.
/// </summary>
public int NrColumns
{
get
{
lock (this.synchObj)
{
return this.nrColumns;
}
}
}
/// <summary>
/// Number of header rows.
/// </summary>
public int NrHeaderRows
{
get
{
lock (this.synchObj)
{
return this.nrHeaderRows;
}
}
}
/// <summary>
/// Number of cell rows.
/// </summary>
public int NrCellRows
{
get
{
lock (this.synchObj)
{
return this.nrCellRows;
}
}
}
/// <summary>
/// Gets the alignment of a column.
/// </summary>
/// <param name="Column">Zero-based column index.</param>
/// <returns>Column alignment.</returns>
public TextAlignment GetAlignment(int Column)
{
lock (this.synchObj)
{
if (Column < 0 || Column > this.nrColumns)
throw new ArgumentOutOfRangeException("Invalid column.", nameof(Column));
return this.columnAlignments.TryGetValue(Column, out TextAlignment Alignment) ? Alignment : TextAlignment.Left;
}
}
/// <summary>
/// Adds data from a Markdown table to the consolidated table.
/// </summary>
/// <param name="Source">Source of table.</param>
/// <param name="MarkdownTable">Table to add</param>
public void Add(string Source, Table MarkdownTable)
{
lock (this.synchObj)
{
if (this.caption != MarkdownTable.Caption)
this.caption = string.Empty;
if (this.id != MarkdownTable.Id)
this.id = string.Empty;
if (MarkdownTable.Headers.Length > this.nrHeaderRows)
this.nrHeaderRows = MarkdownTable.Headers.Length;
int[] Indices = new int[MarkdownTable.Columns];
int i, i2, j, j2;
string Key;
i = 0;
foreach (TextAlignment Alignment in MarkdownTable.Alignments)
{
Key = this.GetHeaderKey(MarkdownTable.Headers, i);
if (!this.columnIndices.TryGetValue(Key, out i2))
{
i2 = this.nrColumns++;
this.columnIndices[Key] = i2;
this.columnAlignments[i2] = Alignment;
j = 0;
foreach (MarkdownElement[] Row in MarkdownTable.Headers)
this.headers[(((long)j++) << 32) + i2] = Row[i];
}
Indices[i++] = i2;
}
i = 0;
i2 = this.nrCellRows;
this.nrCellRows += MarkdownTable.Rows.Length;
foreach (MarkdownElement[] Row in MarkdownTable.Rows)
{
j = 0;
foreach (MarkdownElement Element in Row)
{
j2 = Indices[j++];
this.cells[(((long)i2) << 32) + j2] = Element;
}
i++;
this.sources[i2++] = Source;
}
}
}
/// <summary>
/// Exports the consolidated table to Markdown.
/// </summary>
/// <param name="Markdown">Markdown output.</param>
public void Export(StringBuilder Markdown)
{
lock (this.synchObj)
{
int i, j;
for (i = 0; i < this.nrHeaderRows; i++)
{
Markdown.Append("| Source |");
for (j = 0; j < this.nrColumns; j++)
{
if (this.headers.TryGetValue((((long)i) << 32) + j, out MarkdownElement E))
{
if (E is null)
Markdown.Append('|');
else
{
Markdown.Append(' ');
E.GenerateMarkdown(Markdown);
Markdown.Append(" |");
}
}
else
Markdown.Append(" |");
}
Markdown.AppendLine();
}
Markdown.Append("|:--|");
for (j = 0; j < this.nrColumns; j++)
{
if (!this.columnAlignments.TryGetValue(j, out TextAlignment Alignment))
Alignment = TextAlignment.Left;
switch (Alignment)
{
case TextAlignment.Left:
Markdown.Append(":--|");
break;
case TextAlignment.Right:
Markdown.Append("--:|");
break;
case TextAlignment.Center:
Markdown.Append(":-:|");
break;
default:
Markdown.Append("---|");
break;
}
}
Markdown.AppendLine();
for (i = 0; i < this.nrCellRows; i++)
{
Markdown.Append("| `");
if (this.sources.TryGetValue(i, out string Source))
Markdown.Append(Source);
Markdown.Append("` |");
for (j = 0; j < this.nrColumns; j++)
{
if (this.cells.TryGetValue((((long)i) << 32) + j, out MarkdownElement E))
{
if (E is null)
Markdown.Append('|');
else
{
Markdown.Append(' ');
E.GenerateMarkdown(Markdown);
Markdown.Append(" |");
}
}
else
Markdown.Append(" |");
}
Markdown.AppendLine();
}
}
}
}
}