Skip to content

Commit c31d8cf

Browse files
authored
Merge pull request #184 from OregonGhost/PerformanceImprovements
Table performance improvements
2 parents 02ff702 + b2d4a63 commit c31d8cf

File tree

7 files changed

+85
-115
lines changed

7 files changed

+85
-115
lines changed

MigraDocCore.DocumentObjectModel/MigraDoc.DocumentObjectModel.Tables/Cell.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,22 +338,22 @@ public Shading Shading
338338
/// </summary>
339339
public int MergeRight
340340
{
341-
get { return this.mergeRight.Value; }
342-
set { this.mergeRight.Value = value; }
341+
get { return this.mergeRight ?? 0; }
342+
set { this.mergeRight = value; }
343343
}
344344
[DV]
345-
internal NInt mergeRight = NInt.NullValue;
345+
internal int? mergeRight;
346346

347347
/// <summary>
348348
/// Gets or sets the number of cells to be merged down.
349349
/// </summary>
350350
public int MergeDown
351351
{
352-
get { return this.mergeDown.Value; }
353-
set { this.mergeDown.Value = value; }
352+
get { return this.mergeDown ?? 0; }
353+
set { this.mergeDown = value; }
354354
}
355355
[DV]
356-
internal NInt mergeDown = NInt.NullValue;
356+
internal int? mergeDown;
357357

358358
/// <summary>
359359
/// Gets the collection of document objects that defines the cell.
@@ -405,10 +405,10 @@ internal override void Serialize(Serializer serializer)
405405
if (!this.IsNull("Format"))
406406
this.format.Serialize(serializer, "Format", null);
407407

408-
if (!this.mergeDown.IsNull)
408+
if (this.mergeDown.HasValue)
409409
serializer.WriteSimpleAttribute("MergeDown", this.MergeDown);
410410

411-
if (!this.mergeRight.IsNull)
411+
if (this.mergeRight.HasValue)
412412
serializer.WriteSimpleAttribute("MergeRight", this.MergeRight);
413413

414414
if (!this.verticalAlignment.IsNull)

MigraDocCore.DocumentObjectModel/MigraDoc.DocumentObjectModel.Tables/Column.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,20 @@ public int Index
115115
{
116116
get
117117
{
118-
if (IsNull("Index"))
118+
if (!index.HasValue)
119119
{
120-
Columns clms = this.Parent as Columns;
121-
SetValue("Index", clms.IndexOf(this));
120+
Columns clms = (Columns)Parent;
121+
// One for all and all for one.
122+
for (int i = 0; i < clms.Count; ++i)
123+
{
124+
clms[i].index = i;
125+
}
122126
}
123-
return index;
127+
return index ?? 0;
124128
}
125129
}
126130
[DV]
127-
internal NInt index = NInt.NullValue;
131+
internal int? index;
128132

129133
/// <summary>
130134
/// Gets a cell by its row index. The first cell has index 0.
@@ -134,7 +138,7 @@ public Cell this[int index]
134138
get
135139
{
136140
//Check.ArgumentOutOfRange(index >= 0 && index < table.Rows.Count, "index");
137-
return Table.Rows[index][this.index];
141+
return Table.Rows[index][this.index ?? 0];
138142
}
139143
}
140144

@@ -230,11 +234,11 @@ public Borders Borders
230234
/// </summary>
231235
public int KeepWith
232236
{
233-
get { return this.keepWith.Value; }
234-
set { this.keepWith.Value = value; }
237+
get { return this.keepWith ?? 0; }
238+
set { this.keepWith = value; }
235239
}
236240
[DV]
237-
internal NInt keepWith = NInt.NullValue;
241+
internal int? keepWith;
238242

239243
/// <summary>
240244
/// Gets or sets a value which define whether the column is a header.
@@ -309,7 +313,7 @@ internal override void Serialize(Serializer serializer)
309313
if (!this.width.IsNull)
310314
serializer.WriteSimpleAttribute("Width", this.Width);
311315

312-
if (!this.keepWith.IsNull)
316+
if (this.keepWith.HasValue)
313317
serializer.WriteSimpleAttribute("KeepWith", this.KeepWith);
314318

315319
if (!this.IsNull("Borders"))

MigraDocCore.DocumentObjectModel/MigraDoc.DocumentObjectModel.Tables/Row.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,20 @@ public int Index
121121
{
122122
get
123123
{
124-
if (IsNull("index"))
124+
if (!index.HasValue)
125125
{
126-
Rows rws = this.parent as Rows;
127-
SetValue("Index", rws.IndexOf(this));
126+
Rows rws = (Rows)parent;
127+
// One for all and all for one.
128+
for (int i = 0; i < rws.Count; ++i)
129+
{
130+
rws[i].index = i;
131+
}
128132
}
129-
return index;
133+
return index ?? 0;
130134
}
131135
}
132136
[DV]
133-
internal NInt index = NInt.NullValue;
137+
internal int? index;
134138

135139
/// <summary>
136140
/// Gets a cell by its column index. The first cell has index 0.
@@ -286,11 +290,11 @@ public Shading Shading
286290
/// </summary>
287291
public int KeepWith
288292
{
289-
get { return this.keepWith.Value; }
290-
set { this.keepWith.Value = value; }
293+
get { return this.keepWith ?? 0; }
294+
set { this.keepWith = value; }
291295
}
292296
[DV]
293-
internal NInt keepWith = NInt.NullValue;
297+
internal int? keepWith;
294298

295299
/// <summary>
296300
/// Gets the Cells collection of the table.
@@ -360,7 +364,7 @@ internal override void Serialize(Serializer serializer)
360364
if (!this.verticalAlignment.IsNull)
361365
serializer.WriteSimpleAttribute("VerticalAlignment", this.VerticalAlignment);
362366

363-
if (!this.keepWith.IsNull)
367+
if (this.keepWith.HasValue)
364368
serializer.WriteSimpleAttribute("KeepWith", this.KeepWith);
365369

366370
//Borders & Shading

MigraDocCore.DocumentObjectModel/MigraDoc.DocumentObjectModel.Visitors/CellComparer.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#endregion
3232

3333
using System;
34-
using System.Collections;
34+
using System.Collections.Generic;
3535
using MigraDocCore.DocumentObjectModel.Tables;
3636
using MigraDocCore.DocumentObjectModel.MigraDoc.DocumentObjectModel.Resources;
3737

@@ -41,23 +41,21 @@ namespace MigraDocCore.DocumentObjectModel.Visitors
4141
/// Comparer for the cell positions within a table.
4242
/// It compares the cell positions from top to bottom and left to right.
4343
/// </summary>
44-
public class CellComparer : IComparer
44+
public class CellComparer : IComparer<Cell>
4545
{
46-
public int Compare(object lhs, object rhs)
46+
public int Compare(Cell lhs, Cell rhs)
4747
{
48-
if (!(lhs is Cell))
49-
throw new ArgumentException(AppResources.CompareJustCells, "lhs");
48+
if (ReferenceEquals(lhs, null))
49+
throw new ArgumentNullException(nameof(lhs));
5050

51-
if (!(rhs is Cell))
52-
throw new ArgumentException(AppResources.CompareJustCells, "rhs");
51+
if (ReferenceEquals(rhs, null))
52+
throw new ArgumentNullException(nameof(rhs));
5353

54-
Cell cellLhs = lhs as Cell;
55-
Cell cellRhs = rhs as Cell;
56-
int rowCmpr = cellLhs.Row.Index - cellRhs.Row.Index;
54+
int rowCmpr = lhs.Row.Index - rhs.Row.Index;
5755
if (rowCmpr != 0)
5856
return rowCmpr;
5957

60-
return cellLhs.Column.Index - cellRhs.Column.Index;
58+
return lhs.Column.Index - rhs.Column.Index;
6159
}
6260
}
6361
}

MigraDocCore.DocumentObjectModel/MigraDoc.DocumentObjectModel.Visitors/MergedCellList.cs

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
using System;
3434
using System.Collections;
35+
using System.Collections.Generic;
3536
using MigraDocCore.DocumentObjectModel.IO;
3637
using MigraDocCore.DocumentObjectModel.Tables;
3738
using MigraDocCore.DocumentObjectModel.Visitors;
@@ -42,49 +43,8 @@ namespace MigraDocCore.DocumentObjectModel.Visitors
4243
/// <summary>
4344
/// Represents a merged list of cells of a table.
4445
/// </summary>
45-
public class MergedCellList : ArrayList
46+
public class MergedCellList : List<Cell>
4647
{
47-
48-
/// <summary>
49-
/// Enumerator that can iterate through the MergedCellList.
50-
/// </summary>
51-
public class Enumerator : IEnumerator
52-
{
53-
internal Enumerator(MergedCellList list)
54-
{
55-
this.list = list;
56-
}
57-
58-
#region IEnumerator Members
59-
60-
public void Reset()
61-
{
62-
this.index = -1;
63-
}
64-
65-
public Cell Current
66-
{
67-
get
68-
{
69-
return (Cell)this.list[this.index];
70-
}
71-
}
72-
73-
object IEnumerator.Current
74-
{
75-
get { return Current; }
76-
}
77-
78-
public bool MoveNext()
79-
{
80-
return ++index < this.list.Count;
81-
}
82-
#endregion
83-
84-
MergedCellList list;
85-
int index = -1;
86-
}
87-
8848
/// <summary>
8949
/// Enumeration of neighbor positions of cells in a table.
9050
/// </summary>
@@ -144,14 +104,6 @@ private bool IsAlreadyCovered(Cell cell)
144104
return false;
145105
}
146106

147-
/// <summary>
148-
/// Gets the Enumerator for this list.
149-
/// </summary>
150-
public override IEnumerator GetEnumerator()
151-
{
152-
return new Enumerator(this);
153-
}
154-
155107
/// <summary>
156108
/// Gets the cell at the specified position.
157109
/// </summary>
@@ -189,7 +141,7 @@ public Borders GetEffectiveBorders(Cell cell)
189141

190142
if (cell.mergeRight > 0)
191143
{
192-
Cell rightBorderCell = cell.Table[cell.Row.Index, cell.Column.Index + cell.mergeRight];
144+
Cell rightBorderCell = cell.Table[cell.Row.Index, cell.Column.Index + (cell.mergeRight ?? 0)];
193145
if (rightBorderCell.borders != null && rightBorderCell.borders.right != null)
194146
borders.Right = rightBorderCell.borders.right.Clone();
195147
else
@@ -198,7 +150,7 @@ public Borders GetEffectiveBorders(Cell cell)
198150

199151
if (cell.mergeDown > 0)
200152
{
201-
Cell bottomBorderCell = cell.Table[cell.Row.Index + cell.mergeDown, cell.Column.Index];
153+
Cell bottomBorderCell = cell.Table[cell.Row.Index + (cell.mergeDown ?? 0), cell.Column.Index];
202154
if (bottomBorderCell.borders != null && bottomBorderCell.borders.bottom != null)
203155
borders.Bottom = bottomBorderCell.borders.bottom.Clone();
204156
else

MigraDocCore.Rendering/MigraDoc.Rendering/TableFormatInfo.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030

3131
using System;
3232
using System.Collections;
33+
using System.Collections.Generic;
3334
using MigraDocCore.DocumentObjectModel;
35+
using MigraDocCore.DocumentObjectModel.Tables;
3436
using MigraDocCore.DocumentObjectModel.Visitors;
37+
using PdfSharpCore.Drawing;
3538

3639
namespace MigraDocCore.Rendering
3740
{
@@ -86,9 +89,9 @@ internal override bool IsStarting
8689
internal int endRow = -1;
8790

8891
internal int lastHeaderRow = -1;
89-
internal SortedList formattedCells;
92+
internal SortedList<Cell, FormattedCell> formattedCells;
9093
internal MergedCellList mergedCells;
91-
internal SortedList bottomBorderMap;
92-
internal SortedList connectedRowsMap;
94+
internal SortedList<int, XUnit> bottomBorderMap;
95+
internal SortedList<int, int> connectedRowsMap;
9396
}
9497
}

0 commit comments

Comments
 (0)