Skip to content

Commit 3667f9a

Browse files
author
Florian Krabbenhoeft
committed
Migradoc/DocumentObjectModel: Replaced some SortedList/Comparer with generic versions.
NOTE: Part of table rendering speedup.
1 parent 842992f commit 3667f9a

File tree

4 files changed

+28
-74
lines changed

4 files changed

+28
-74
lines changed

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: 2 additions & 50 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>

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
}

MigraDocCore.Rendering/MigraDoc.Rendering/TableRenderer.cs

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

3131
using System;
3232
using System.Collections;
33+
using System.Collections.Generic;
3334
using PdfSharpCore.Drawing;
3435
using MigraDocCore.DocumentObjectModel;
3536
using MigraDocCore.DocumentObjectModel.Visitors;
@@ -278,7 +279,7 @@ void InitFormat(Area area, FormatInfo previousFormatInfo)
278279

279280
void FormatCells()
280281
{
281-
this.formattedCells = new SortedList(new CellComparer());
282+
this.formattedCells = new SortedList<Cell, FormattedCell>(new CellComparer());
282283
foreach (Cell cell in this.mergedCells)
283284
{
284285
FormattedCell formattedCell = new FormattedCell(cell, this.documentRenderer, this.mergedCells.GetEffectiveBorders(cell), this.fieldInfos, 0, 0);
@@ -491,7 +492,7 @@ void CalcLastHeaderRow()
491492

492493
void CreateConnectedRows()
493494
{
494-
this.connectedRowsMap = new SortedList();
495+
this.connectedRowsMap = new SortedList<int, int>();
495496
foreach (Cell cell in this.mergedCells)
496497
{
497498
if (!this.connectedRowsMap.ContainsKey(cell.Row.Index))
@@ -504,7 +505,7 @@ void CreateConnectedRows()
504505

505506
void CreateConnectedColumns()
506507
{
507-
this.connectedColumnsMap = new SortedList();
508+
this.connectedColumnsMap = new SortedList<int, int>();
508509
foreach (Cell cell in this.mergedCells)
509510
{
510511
if (!this.connectedColumnsMap.ContainsKey(cell.Column.Index))
@@ -517,7 +518,7 @@ void CreateConnectedColumns()
517518

518519
void CreateBottomBorderMap()
519520
{
520-
this.bottomBorderMap = new SortedList();
521+
this.bottomBorderMap = new SortedList<int, XUnit>();
521522
this.bottomBorderMap.Add(0, XUnit.FromPoint(0));
522523
while (!this.bottomBorderMap.ContainsKey(this.table.Rows.Count))
523524
{
@@ -562,8 +563,8 @@ XUnit CalcMaxTopBorderWidth(int row)
562563
void CreateNextBottomBorderPosition()
563564
{
564565
int lastIdx = bottomBorderMap.Count - 1;
565-
int lastBorderRow = (int)bottomBorderMap.GetKey(lastIdx);
566-
XUnit lastPos = (XUnit)bottomBorderMap.GetByIndex(lastIdx);
566+
int lastBorderRow = (int)bottomBorderMap.Keys[lastIdx];
567+
XUnit lastPos = (XUnit)bottomBorderMap.Values[lastIdx];
567568
Cell minMergedCell = GetMinMergedCell(lastBorderRow);
568569
FormattedCell minMergedFormattedCell = (FormattedCell)this.formattedCells[minMergedCell];
569570
XUnit maxBottomBorderPosition = lastPos + minMergedFormattedCell.InnerHeight;
@@ -686,10 +687,10 @@ int CalcLastConnectedColumn(int column)
686687

687688
Table table;
688689
MergedCellList mergedCells;
689-
SortedList formattedCells;
690-
SortedList bottomBorderMap;
691-
SortedList connectedRowsMap;
692-
SortedList connectedColumnsMap;
690+
SortedList<Cell, FormattedCell> formattedCells;
691+
SortedList<int, XUnit> bottomBorderMap;
692+
SortedList<int, int> connectedRowsMap;
693+
SortedList<int, int> connectedColumnsMap;
693694

694695
int lastHeaderRow;
695696
int lastHeaderColumn;

0 commit comments

Comments
 (0)