X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2Fbase_uint.cs;h=502d871c01a506d94aa36d3fdd9b60dfa3d8df0f;hb=0fe762d6eee8a8a23033f813217c1675a34f2d6a;hp=83ef1edd101d95e5236794344f40c691357cd3d8;hpb=80b9c15a0c2d1b87180824a36948e37894cebaca;p=NovacoinLibrary.git diff --git a/Novacoin/base_uint.cs b/Novacoin/base_uint.cs index 83ef1ed..502d871 100644 --- a/Novacoin/base_uint.cs +++ b/Novacoin/base_uint.cs @@ -18,6 +18,7 @@ using System; +using System.Collections.Generic; using System.Diagnostics.Contracts; namespace Novacoin @@ -25,7 +26,7 @@ namespace Novacoin /// /// Base class for uint256 and uint160. /// - public class base_uint : IComparable, IEquatable + public class base_uint : IComparable, IEquatable, IEqualityComparer { protected int nWidth; protected uint[] pn; @@ -54,11 +55,37 @@ namespace Novacoin return pn[0]; } + /// + /// Total size in bytes. + /// public int Size { get { - return nWidth; + return nWidth * sizeof(uint); + } + } + + /// + /// Zero or the position of highest non-zero bit plus one. + /// + protected int bits + { + get + { + for (int pos = nWidth - 1; pos >= 0; pos--) + { + if (pn[pos]!=0) + { + for (int bits = 31; bits > 0; bits--) + { + if ((pn[pos] & 1 << bits)!=0) + return 32 * pos + bits + 1; + } + return 32 * pos + 1; + } + } + return 0; } } @@ -75,6 +102,7 @@ namespace Novacoin } + #region Comparison operations public static bool operator <(base_uint a, base_uint b) { for (int i = a.nWidth - 1; i >= 0; i--) @@ -138,7 +166,9 @@ namespace Novacoin } return true; } + #endregion + #region Equality operators public static bool operator ==(base_uint a, base_uint b) { if (object.ReferenceEquals(a, b)) @@ -187,22 +217,45 @@ namespace Novacoin { return (!(a == b)); } - + #endregion + + #region Cast oerations + /// + /// True cast operator + /// + /// + /// public static bool operator true(base_uint a) { return (a != 0); } + /// + /// False cast operator. + /// + /// Value + /// Boolean result public static bool operator false(base_uint a) { return (a == 0); } + /// + /// Imlicit byte[] cast operator. + /// + /// Value public static implicit operator byte[] (base_uint a) { return Interop.LEBytes(a.pn); } - + #endregion + + /// + /// Arrays equality checking helper method. + /// + /// Array 1 + /// Array 2 + /// Result. private static bool ArraysEqual(uint[] a, uint[] b) { Contract.Requires(a.Length == b.Length, "Array length mismatch."); @@ -217,19 +270,26 @@ namespace Novacoin return true; } - public override int GetHashCode() + + #region IEqualityComparer + public bool Equals(base_uint a, base_uint b) { - int hash = 17; - unchecked + if (object.ReferenceEquals(a, b)) { - foreach (var element in pn) - { - hash = hash * 31 + element.GetHashCode(); - } + return true; } - return hash; + + return ArraysEqual(a.pn, b.pn); + } + + public int GetHashCode(base_uint a) + { + return a.GetHashCode(); } + #endregion + + #region IComparable public int CompareTo(base_uint item) { if (this > item) @@ -243,7 +303,9 @@ namespace Novacoin return 0; } + #endregion + #region IEquatable public bool Equals(base_uint a) { if (a == null) @@ -254,11 +316,29 @@ namespace Novacoin return ArraysEqual(pn, a.pn); } + public override int GetHashCode() + { + int hash = 17; + unchecked + { + foreach (var element in pn) + { + hash = hash * 31 + element.GetHashCode(); + } + } + return hash; + } + public override bool Equals(object o) { return Equals(o as base_uint); } + #endregion + /// + /// Serialize to string. + /// + /// public override string ToString() { return Interop.ToHex(Interop.ReverseBytes(this));