PageRenderTime 113ms CodeModel.GetById 40ms RepoModel.GetById 8ms app.codeStats 0ms

/src/ZebraFlickr/Core/ByteArrayComparer.cs

https://bitbucket.org/garethl/zebraflickr
C# | 149 lines | 73 code | 22 blank | 54 comment | 21 complexity | c813960f7df43811c83c2da4957dd849 MD5 | raw file
  1. #region License
  2. // Copyright (c) 2012 Gareth Lennox (garethl@dwakn.com)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Gareth Lennox nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections.Generic;
  30. namespace ZebraFlickr.Core
  31. {
  32. /// <summary>
  33. /// Byte array equality comparer
  34. /// </summary>
  35. public class ByteArrayComparer : IEqualityComparer<byte[]>, IComparer<byte[]>
  36. {
  37. private static readonly ByteArrayComparer _instance = new ByteArrayComparer();
  38. public static ByteArrayComparer Instance
  39. {
  40. get { return _instance; }
  41. }
  42. #region IComparer<byte[]> Members
  43. /// <summary>
  44. /// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
  45. /// </summary>
  46. /// <returns>
  47. /// Less than zero <paramref name="x"/> is less than <paramref name="y"/>.
  48. /// Zero <paramref name="x"/> equals <paramref name="y"/>.
  49. /// Greater than zero <paramref name="x"/> is greater than <paramref name="y"/>.
  50. /// </returns>
  51. /// <param name="x">The first object to compare.</param>
  52. /// <param name="y">The second object to compare.</param>
  53. public int Compare(byte[] x, byte[] y)
  54. {
  55. for (int i = 0; i < x.Length && i < y.Length; ++i)
  56. {
  57. int c = x[i] - y[i];
  58. if (c != 0)
  59. return c;
  60. }
  61. if (x.Length < y.Length)
  62. return -1;
  63. if (x.Length > y.Length)
  64. return 1;
  65. return 0;
  66. }
  67. #endregion
  68. #region IEqualityComparer<byte[]> Members
  69. /// <summary>
  70. /// Determines whether the specified objects are equal.
  71. /// </summary>
  72. /// <returns>
  73. /// true if the specified objects are equal; otherwise, false.
  74. /// </returns>
  75. /// <param name="x">The first objectto compare.</param>
  76. /// <param name="y">The second object to compare.</param>
  77. public bool Equals(byte[] x, byte[] y)
  78. {
  79. if (x == null || y == null || x.Length != y.Length)
  80. return false;
  81. for (var i = 0; i < x.Length; i++)
  82. {
  83. if (x[i] != y[i])
  84. return false;
  85. }
  86. return true;
  87. }
  88. /// <summary>
  89. /// Returns a hash code for the specified object.
  90. /// </summary>
  91. /// <returns>
  92. /// A hash code for the specified object.
  93. /// </returns>
  94. /// <param name="obj">The <see cref="T:System.Object"/> for which a hash code is to be returned.</param>
  95. /// <exception cref="T:System.ArgumentNullException">The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.</exception>
  96. public int GetHashCode(byte[] obj)
  97. {
  98. unchecked
  99. {
  100. const int p = 16777619;
  101. var hash = (int) 2166136261;
  102. for (var i = 0; i < obj.Length; i++)
  103. hash = (hash ^ obj[i])*p;
  104. hash += hash << 13;
  105. hash ^= hash >> 7;
  106. hash += hash << 3;
  107. hash ^= hash >> 17;
  108. hash += hash << 5;
  109. return hash;
  110. }
  111. }
  112. #endregion
  113. public int Compare(byte[] x, int offset, int count, byte[] y)
  114. {
  115. if (count != y.Length)
  116. throw new ArgumentOutOfRangeException("Count does not match second argument's length");
  117. var end = offset + count;
  118. for (int i = offset; i < end; ++i)
  119. {
  120. int c = x[i] - y[i - offset];
  121. if (c != 0)
  122. return c;
  123. }
  124. return 0;
  125. }
  126. }
  127. }