diff --git a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj index 2a68e33..ea59871 100644 --- a/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML/ClosedXML/ClosedXML.csproj @@ -77,6 +77,7 @@ + diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellCollection.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellCollection.cs index 5e4fe6a..e671b3b 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellCollection.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Cells/XLCellCollection.cs @@ -5,12 +5,12 @@ namespace ClosedXML.Excel { - internal class XLCellCollection : IDictionary + internal class XLCellCollection : IDictionary { - private Dictionary dictionary = new Dictionary(); + private Dictionary dictionary = new Dictionary(); - private Dictionary deleted = new Dictionary(); - public Dictionary Deleted + private Dictionary deleted = new Dictionary(); + public Dictionary Deleted { get { @@ -18,7 +18,7 @@ } } - public void Add(IXLAddress key, XLCell value) + public void Add(XLAddressLight key, XLCell value) { if (deleted.ContainsKey(key)) deleted.Remove(key); @@ -26,17 +26,17 @@ dictionary.Add(key, value); } - public bool ContainsKey(IXLAddress key) + public bool ContainsKey(XLAddressLight key) { return dictionary.ContainsKey(key); } - public ICollection Keys + public ICollection Keys { get { return dictionary.Keys; } } - public bool Remove(IXLAddress key) + public bool Remove(XLAddressLight key) { if (!deleted.ContainsKey(key)) deleted.Add(key, dictionary[key]); @@ -44,7 +44,7 @@ return dictionary.Remove(key); } - public bool TryGetValue(IXLAddress key, out XLCell value) + public bool TryGetValue(XLAddressLight key, out XLCell value) { return dictionary.TryGetValue(key, out value); } @@ -54,7 +54,7 @@ get { return dictionary.Values; } } - public XLCell this[IXLAddress key] + public XLCell this[XLAddressLight key] { get { @@ -66,7 +66,7 @@ } } - public void Add(KeyValuePair item) + public void Add(KeyValuePair item) { if (deleted.ContainsKey(item.Key)) deleted.Remove(item.Key); @@ -83,12 +83,12 @@ dictionary.Clear(); } - public bool Contains(KeyValuePair item) + public bool Contains(KeyValuePair item) { return dictionary.Contains(item); } - public void CopyTo(KeyValuePair[] array, int arrayIndex) + public void CopyTo(KeyValuePair[] array, int arrayIndex) { throw new NotImplementedException(); } @@ -103,7 +103,7 @@ get { return false; } } - public bool Remove(KeyValuePair item) + public bool Remove(KeyValuePair item) { if (!deleted.ContainsKey(item.Key)) deleted.Add(item.Key, dictionary[item.Key]); @@ -111,7 +111,7 @@ return dictionary.Remove(item.Key); } - public IEnumerator> GetEnumerator() + public IEnumerator> GetEnumerator() { return dictionary.GetEnumerator(); } @@ -130,8 +130,9 @@ { foreach (var kp in dictionary.Values.Where(predicate).Select(c=>c)) { - if (!deleted.ContainsKey(kp.Address)) - deleted.Add(kp.Address, kp); + var key = new XLAddressLight(kp.Address.RowNumber, kp.Address.ColumnNumber); + if (!deleted.ContainsKey(key)) + deleted.Add(key, kp); } dictionary.RemoveAll(predicate); diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Public.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Public.cs index 0673bf1..8346162 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Public.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/Colors/XLColor_Public.cs @@ -146,23 +146,18 @@ public override int GetHashCode() { - unchecked // Overflow is fine, just wrap + if (ColorType == XLColorType.Theme) { - if (ColorType == XLColorType.Theme) - { - int hash = 17; - hash = hash * 23 + ThemeColor.GetHashCode(); - hash = hash * 23 + ThemeTint.GetHashCode(); - return hash; - } - else if (ColorType == XLColorType.Indexed) - { - return Indexed; - } - else - { - return Color.GetHashCode(); - } + return ThemeColor.GetHashCode() + ^ ThemeTint.GetHashCode(); + } + else if (ColorType == XLColorType.Indexed) + { + return Indexed; + } + else + { + return Color.GetHashCode(); } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs index 5277cf7..98cc8c5 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLAlignment.cs @@ -250,20 +250,15 @@ public override int GetHashCode() { - unchecked // Overflow is fine, just wrap - { - int hash = 17; - hash = hash * 23 + Horizontal.GetHashCode(); - hash = hash * 23 + Vertical.GetHashCode(); - hash = hash * 23 + Indent.GetHashCode(); - hash = hash * 23 + JustifyLastLine.GetHashCode(); - hash = hash * 23 + ReadingOrder.GetHashCode(); - hash = hash * 23 + RelativeIndent.GetHashCode(); - hash = hash * 23 + ShrinkToFit.GetHashCode(); - hash = hash * 23 + TextRotation.GetHashCode(); - hash = hash * 23 + WrapText.GetHashCode(); - return hash; - } + return (Int32)Horizontal + ^ (Int32)Vertical + ^ Indent + ^ JustifyLastLine.GetHashCode() + ^ (Int32)ReadingOrder + ^ RelativeIndent + ^ ShrinkToFit.GetHashCode() + ^ TextRotation + ^ WrapText.GetHashCode(); } public bool Equals(IXLAlignment other) diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLBorder.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLBorder.cs index eedf8b1..893783e 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLBorder.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLBorder.cs @@ -277,23 +277,18 @@ public override int GetHashCode() { - unchecked // Overflow is fine, just wrap - { - int hash = 17; - hash = hash * 23 + LeftBorder.GetHashCode(); - hash = hash * 23 + LeftBorderColor.GetHashCode(); - hash = hash * 23 + RightBorder.GetHashCode(); - hash = hash * 23 + RightBorderColor.GetHashCode(); - hash = hash * 23 + TopBorder.GetHashCode(); - hash = hash * 23 + TopBorderColor.GetHashCode(); - hash = hash * 23 + BottomBorder.GetHashCode(); - hash = hash * 23 + BottomBorderColor.GetHashCode(); - hash = hash * 23 + DiagonalBorder.GetHashCode(); - hash = hash * 23 + DiagonalBorderColor.GetHashCode(); - hash = hash * 23 + DiagonalUp.GetHashCode(); - hash = hash * 23 + DiagonalDown.GetHashCode(); - return hash; - } + return (Int32)LeftBorder + ^ LeftBorderColor.GetHashCode() + ^ (Int32)RightBorder + ^ RightBorderColor.GetHashCode() + ^ (Int32)TopBorder + ^ TopBorderColor.GetHashCode() + ^ (Int32)BottomBorder + ^ BottomBorderColor.GetHashCode() + ^ (Int32)DiagonalBorder + ^ DiagonalBorderColor.GetHashCode() + ^ DiagonalUp.GetHashCode() + ^ DiagonalDown.GetHashCode(); } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFill.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFill.cs index e9c4077..5062393 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFill.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFill.cs @@ -132,14 +132,9 @@ public override int GetHashCode() { - unchecked // Overflow is fine, just wrap - { - int hash = 17; - hash = hash * 23 + BackgroundColor.GetHashCode(); - hash = hash * 23 + PatternType.GetHashCode(); - hash = hash * 23 + PatternColor.GetHashCode(); - return hash; - } + return BackgroundColor.GetHashCode() + ^ (Int32)PatternType + ^ PatternColor.GetHashCode(); } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFont.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFont.cs index f8902b7..d0e2e12 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFont.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLFont.cs @@ -277,21 +277,16 @@ public override int GetHashCode() { - unchecked // Overflow is fine, just wrap - { - int hash = 17; - hash = hash * 23 + Bold.GetHashCode(); - hash = hash * 23 + Italic.GetHashCode(); - hash = hash * 23 + Underline.GetHashCode(); - hash = hash * 23 + Strikethrough.GetHashCode(); - hash = hash * 23 + VerticalAlignment.GetHashCode(); - hash = hash * 23 + Shadow.GetHashCode(); - hash = hash * 23 + FontSize.GetHashCode(); - hash = hash * 23 + FontColor.GetHashCode(); - hash = hash * 23 + FontName.GetHashCode(); - hash = hash * 23 + FontFamilyNumbering.GetHashCode(); - return hash; - } + return Bold.GetHashCode() + ^ Italic.GetHashCode() + ^ (Int32)Underline + ^ Strikethrough.GetHashCode() + ^ (Int32)VerticalAlignment + ^ Shadow.GetHashCode() + ^ FontSize.GetHashCode() + ^ FontColor.GetHashCode() + ^ FontName.GetHashCode() + ^ (Int32)FontFamilyNumbering; } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLNumberFormat.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLNumberFormat.cs index 641c4f3..b20a900 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLNumberFormat.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLNumberFormat.cs @@ -93,13 +93,8 @@ public override int GetHashCode() { - unchecked // Overflow is fine, just wrap - { - int hash = 17; - hash = hash * 23 + NumberFormatId.GetHashCode(); - hash = hash * 23 + Format.GetHashCode(); - return hash; - } + return NumberFormatId + ^ Format.GetHashCode(); } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLStyle.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLStyle.cs index bad11b6..42a2dc8 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLStyle.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/Style/XLStyle.cs @@ -87,16 +87,11 @@ public override int GetHashCode() { - unchecked // Overflow is fine, just wrap - { - int hash = 17; - hash = hash * 23 + Font.GetHashCode(); - hash = hash * 23 + Fill.GetHashCode(); - hash = hash * 23 + Border.GetHashCode(); - hash = hash * 23 + NumberFormat.GetHashCode(); - hash = hash * 23 + Alignment.GetHashCode(); - return hash; - } + return Font.GetHashCode() + ^ Fill.GetHashCode() + ^ Border.GetHashCode() + ^ NumberFormat.GetHashCode() + ^ Alignment.GetHashCode(); } } } diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLAddress.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLAddress.cs index a030f15..32c7dbf 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLAddress.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLAddress.cs @@ -302,9 +302,13 @@ public static Boolean operator ==(XLAddress xlCellAddressLeft, XLAddress xlCellAddressRight) { - return - xlCellAddressLeft.RowNumber == xlCellAddressRight.RowNumber - && xlCellAddressLeft.ColumnNumber == xlCellAddressRight.ColumnNumber; + if (xlCellAddressLeft.rowNumber == xlCellAddressRight.rowNumber) + if (xlCellAddressRight.columnNumber > 0) + return xlCellAddressLeft.columnNumber == xlCellAddressRight.columnNumber; + else + return xlCellAddressLeft.columnLetter == xlCellAddressRight.columnLetter; + else + return false; } public static Boolean operator !=(XLAddress xlCellAddressLeft, XLAddress xlCellAddressRight) @@ -359,12 +363,15 @@ public Int32 GetHashCode(Object obj) { - return obj.GetHashCode(); + return ((XLAddress)obj).GetHashCode(); } public override Int32 GetHashCode() { - return this.ToString().GetHashCode(); + if (columnNumber > 0 ) + return rowNumber ^ columnNumber; + else + return rowNumber ^ columnLetter.GetHashCode(); } #endregion diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLAddressLight.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLAddressLight.cs new file mode 100644 index 0000000..68c2fd0 --- /dev/null +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLAddressLight.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace ClosedXML.Excel +{ + internal struct XLAddressLight + { + public XLAddressLight(Int32 rowNumber, Int32 columnNumber) + { + RowNumber = rowNumber; + ColumnNumber = columnNumber; + } + public Int32 RowNumber; + public Int32 ColumnNumber; + } +} diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index 1e3838c..cbd38a5 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -582,11 +582,11 @@ if (alignment.Horizontal != null) xlStylized.InnerStyle.Alignment.Horizontal = alignmentHorizontalValues.Single(a => a.Value == alignment.Horizontal).Key; if (alignment.Indent != null) - xlStylized.InnerStyle.Alignment.Indent = Int32.Parse(alignment.Indent.ToString()); + xlStylized.InnerStyle.Alignment.Indent = (Int32)alignment.Indent.Value; if (alignment.JustifyLastLine != null) xlStylized.InnerStyle.Alignment.JustifyLastLine = alignment.JustifyLastLine; if (alignment.ReadingOrder != null) - xlStylized.InnerStyle.Alignment.ReadingOrder = (XLAlignmentReadingOrderValues)Int32.Parse(alignment.ReadingOrder.ToString()); + xlStylized.InnerStyle.Alignment.ReadingOrder = (XLAlignmentReadingOrderValues)(Int32)alignment.ReadingOrder.Value ; if (alignment.RelativeIndent != null) xlStylized.InnerStyle.Alignment.RelativeIndent = alignment.RelativeIndent; if (alignment.ShrinkToFit != null) @@ -672,17 +672,18 @@ if (fontColor.HasValue) xlStylized.InnerStyle.Font.FontColor = fontColor; - if (font.FontFamilyNumbering != null && ((FontFamilyNumbering)font.FontFamilyNumbering).Val != null) - xlStylized.InnerStyle.Font.FontFamilyNumbering = (XLFontFamilyNumberingValues)Int32.Parse(((FontFamilyNumbering)font.FontFamilyNumbering).Val.ToString()); + if (font.FontFamilyNumbering != null) + if (font.FontFamilyNumbering.Val.HasValue) + xlStylized.InnerStyle.Font.FontFamilyNumbering = (XLFontFamilyNumberingValues)font.FontFamilyNumbering.Val.Value; if (font.FontName != null) { - if (((FontName)font.FontName).Val != null) - xlStylized.InnerStyle.Font.FontName = ((FontName)font.FontName).Val; + if (font.FontName.Val != null) + xlStylized.InnerStyle.Font.FontName = font.FontName.Val; } if (font.FontSize != null) { - if (((FontSize)font.FontSize).Val != null) - xlStylized.InnerStyle.Font.FontSize = ((FontSize)font.FontSize).Val; + if (font.FontSize.Val != null) + xlStylized.InnerStyle.Font.FontSize = font.FontSize.Val; } xlStylized.InnerStyle.Font.Italic = GetBoolean(font.Italic); diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs index 96c5df6..2c4d59b 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs @@ -46,14 +46,15 @@ static void Main(string[] args) { - List running = new List(); + FillStyles(); + List runningSave = new List(); + List runningLoad = new List(); + List runningSavedBack = new List(); foreach (Int32 r in Enumerable.Range(1, 1)) { var startTotal = DateTime.Now; - - FillStyles(); var wb = new XLWorkbook(); - foreach (var i in Enumerable.Range(1, 1)) + foreach (var i in Enumerable.Range(1, 5)) { var ws = wb.Worksheets.Add("Sheet" + i); foreach (var ro in Enumerable.Range(1, 2000)) @@ -77,25 +78,32 @@ var start = DateTime.Now; wb.SaveAs(@"C:\Excel Files\ForTesting\Benchmark.xlsx"); var end = DateTime.Now; - Console.WriteLine("Saved in {0} secs.", (end - start).TotalSeconds); + var saved = (end - start).TotalSeconds; + runningSave.Add(saved); + Console.WriteLine("Saved in {0} secs.", saved); var start1 = DateTime.Now; var wb1 = new XLWorkbook(@"C:\Excel Files\ForTesting\Benchmark.xlsx"); - var end1 = DateTime.Now; - Console.WriteLine("Loaded in {0} secs.", (end1 - start1).TotalSeconds); + var loaded = (end1 - start1).TotalSeconds; + runningLoad.Add(loaded); + Console.WriteLine("Loaded in {0} secs.", loaded); + var start2 = DateTime.Now; //wb1.SaveAs(@"C:\Excel Files\ForTesting\Benchmark_Saved.xlsx"); var end2 = DateTime.Now; - Console.WriteLine("Saved back in {0} secs.", (end2 - start2).TotalSeconds); + var savedBack = (end2 - start2).TotalSeconds; + runningSavedBack.Add(savedBack); + Console.WriteLine("Saved back in {0} secs.", savedBack); var endTotal = DateTime.Now; Console.WriteLine("It all took {0} secs.", (endTotal - startTotal).TotalSeconds); - running.Add((endTotal - startTotal).TotalSeconds); } Console.WriteLine("-------"); - Console.WriteLine("Avg total time: {0}", running.Average()); - Console.ReadKey(); + Console.WriteLine("Avg Save time: {0}", runningSave.Average()); + Console.WriteLine("Avg Load time: {0}", runningLoad.Average()); + Console.WriteLine("Avg Save Back time: {0}", runningSavedBack.Average()); + //Console.ReadKey(); } private static IXLStyle style1;