/// <summary> /// Convert the UUID to a byte array of 32 bytes. /// </summary> /// <returns> /// The UUID data as a byte array of 32 bytes. /// </returns> public byte[] ToByteArray() { EnsureConstructed(); byte[] byteArray = new byte[32]; long datetime = m_dateTime; int addr1 = m_addr1; int addr2 = m_addr2; int addr3 = m_addr3; int addr4 = m_addr4; int port = m_port; int count = m_count; byteArray[0] = (byte)(NumberUtils.URShift(datetime, 56)); byteArray[1] = (byte)(NumberUtils.URShift(datetime, 48)); byteArray[2] = (byte)(NumberUtils.URShift(datetime, 40)); byteArray[3] = (byte)(NumberUtils.URShift(datetime, 32)); byteArray[4] = (byte)(NumberUtils.URShift(datetime, 24)); byteArray[5] = (byte)(NumberUtils.URShift(datetime, 16)); byteArray[6] = (byte)(NumberUtils.URShift(datetime, 8)); byteArray[7] = (byte)(datetime); byteArray[8] = (byte)(NumberUtils.URShift(addr1, 24)); byteArray[9] = (byte)(NumberUtils.URShift(addr1, 16)); byteArray[10] = (byte)(NumberUtils.URShift(addr1, 8)); byteArray[11] = (byte)(addr1); byteArray[12] = (byte)(NumberUtils.URShift(addr2, 24)); byteArray[13] = (byte)(NumberUtils.URShift(addr2, 16)); byteArray[14] = (byte)(NumberUtils.URShift(addr2, 8)); byteArray[15] = (byte)(addr2); byteArray[16] = (byte)(NumberUtils.URShift(addr3, 24)); byteArray[17] = (byte)(NumberUtils.URShift(addr3, 16)); byteArray[18] = (byte)(NumberUtils.URShift(addr3, 8)); byteArray[19] = (byte)(addr3); byteArray[20] = (byte)(NumberUtils.URShift(addr4, 24)); byteArray[21] = (byte)(NumberUtils.URShift(addr4, 16)); byteArray[22] = (byte)(NumberUtils.URShift(addr4, 8)); byteArray[23] = (byte)(addr4); byteArray[24] = (byte)(NumberUtils.URShift(port, 24)); byteArray[25] = (byte)(NumberUtils.URShift(port, 16)); byteArray[26] = (byte)(NumberUtils.URShift(port, 8)); byteArray[27] = (byte)(port); byteArray[28] = (byte)(NumberUtils.URShift(count, 24)); byteArray[29] = (byte)(NumberUtils.URShift(count, 16)); byteArray[30] = (byte)(NumberUtils.URShift(count, 8)); byteArray[31] = (byte)(count); return(byteArray); }
/// <summary> /// Update the current thread gate status, without changing the /// active count. /// </summary> /// <remarks> /// The caller must hold synchronization on the ThreadGate. /// </remarks> /// <param name="status"> /// The new status. /// </param> /// <returns> /// The old status. /// </returns> protected virtual ThreadGateState UpdateStatus(ThreadGateState status) { AtomicCounter atomicState = m_atomicState; long offsettedStatus = ((long)status) << STATUS_OFFSET; while (true) { long current = atomicState.GetCount(); long newValue = offsettedStatus | (current & ACTIVE_COUNT_MASK); if (atomicState.SetCount(current, newValue)) { return((ThreadGateState)(NumberUtils.URShift(current, STATUS_OFFSET))); } } }
public void DecimalTest() { decimal d = 0.000000M; Assert.AreEqual(d, NumberUtils.GetUnscaledValue(d)); d = 12345.6789M; decimal expected = 123456789M; Assert.AreEqual(expected, NumberUtils.GetUnscaledValue(d)); d = 1.0001M; expected = 10001M; Assert.AreEqual(expected, NumberUtils.GetUnscaledValue(d)); }
/// <summary> /// Finish construction or deserialization. /// </summary> /// <remarks> /// The UUID's internally cached hashcode value is zero until /// construction is completed, or until deserialization is /// completed, and never zero otherwise. Every constructor, /// except for the deserialization constructor, must call this /// method. /// </remarks> private void InitHashcode() { int hash = (int)(NumberUtils.URShift(m_dateTime, 32)) ^ (int)m_dateTime ^ m_addr1 ^ m_addr2 ^ m_addr3 ^ m_addr4 ^ m_port ^ m_count; if (hash == 0) { hash = 2147483647; // Integer.MAX_VALUE is actually a prime ;-) } m_hash = hash; }
/// <summary> /// Reorder elements of a type T array in a random way. /// </summary> /// <typeparam name="T"> /// The type of the element. /// </typeparam> /// <param name="array"> /// The array of type T objects to randomize. /// </param> /// <returns> /// The array of elements ordered in a random manner. /// </returns> public static T[] Randomize <T>(T[] array) { int c; if (array == null || (c = array.Length) <= 1) { return(array); } Random rnd = NumberUtils.GetRandom(); for (int i1 = 0; i1 < c; i1++) { int i2 = rnd.Next(c); T t = array[i2]; array[i2] = array[i1]; array[i1] = t; } return(array); }
/// <summary> /// Initializes a new Binary instance from /// <see cref="BinaryMemoryStream"/>. /// </summary> /// <param name="stream"> /// A <b>BinaryMemoryStream</b> instance. /// </param> internal Binary(BinaryMemoryStream stream) { byte[] ab = stream.GetInternalByteArray(); int cbData = (int)stream.Length; int cbTotal = ab.Length; int cbWaste = cbTotal - cbData; // tolerate up to 12.5% waste if (cbWaste <= Math.Max(16, NumberUtils.URShift(cbTotal, 3))) { m_ab = ab; } else { byte[] abNew = new byte[cbData]; Array.Copy(ab, 0, abNew, 0, cbData); m_ab = abNew; } m_cb = cbData; }
public void BitsToSingleTest() { Single s = 121345.22256f; Assert.AreEqual(s, NumberUtils.Int32BitsToSingle(NumberUtils.SingleToInt32Bits(s))); Assert.AreEqual(Single.NaN, NumberUtils.Int32BitsToSingle(NumberUtils.SingleToInt32Bits(Single.NaN))); Assert.AreEqual(Single.MaxValue, NumberUtils.Int32BitsToSingle(NumberUtils.SingleToInt32Bits(Single.MaxValue))); Assert.AreEqual(Single.NegativeInfinity, NumberUtils.Int32BitsToSingle(NumberUtils.SingleToInt32Bits(Single.NegativeInfinity))); Double d = 1234533.00009; Assert.AreEqual(d, NumberUtils.Int64BitsToDouble(NumberUtils.DoubleToInt64Bits(d))); Assert.AreEqual(Double.NaN, NumberUtils.Int64BitsToDouble(NumberUtils.DoubleToInt64Bits(Double.NaN))); Assert.AreEqual(Double.MaxValue, NumberUtils.Int64BitsToDouble(NumberUtils.DoubleToInt64Bits(Double.MaxValue))); Assert.AreEqual(Double.NegativeInfinity, NumberUtils.Int64BitsToDouble(NumberUtils.DoubleToInt64Bits(Double.NegativeInfinity))); }
public void ParseHexTest() { Assert.AreEqual(0, NumberUtils.ParseHex("").Length); Assert.AreEqual(0, NumberUtils.ParseHex(null).Length); String number = "0xafff42"; byte[] res = NumberUtils.ParseHex(number); Assert.AreEqual(0xaf, res[0]); Assert.AreEqual(0xff, res[1]); Assert.AreEqual(0x42, res[2]); Assert.AreEqual("0x0A", NumberUtils.ToHexEscape(10)); Assert.AreEqual("0xAFFF42", NumberUtils.ToHexEscape(res)); number = "123E3A"; res = NumberUtils.ParseHex(number); Assert.AreEqual(0x12, res[0]); Assert.AreEqual(0x3e, res[1]); Assert.AreEqual(0x3a, res[2]); }
public void ChangeEndian() { Int16 i16 = 1234; Assert.AreEqual(i16, NumberUtils.ChangeEndian(NumberUtils.ChangeEndian(i16))); Int32 i32 = -123487; Assert.AreEqual(i32, NumberUtils.ChangeEndian(NumberUtils.ChangeEndian(i32))); Int64 i64 = -122234536L; Assert.AreEqual(i64, NumberUtils.ChangeEndian(NumberUtils.ChangeEndian(i64))); UInt16 ui16 = 11113; Assert.AreEqual(ui16, NumberUtils.ChangeEndian(NumberUtils.ChangeEndian(ui16))); UInt32 ui32 = 123487; Assert.AreEqual(ui32, NumberUtils.ChangeEndian(NumberUtils.ChangeEndian(ui32))); UInt64 ui64 = 122234536L; Assert.AreEqual(ui64, NumberUtils.ChangeEndian(NumberUtils.ChangeEndian(ui64))); }
/// <summary> /// Enter the thread gate. /// </summary> /// <remarks> /// A thread uses this method to obtain non-exclusive access to the /// resource represented by the thread gate. Each invocation of this /// method must ultimately have a corresponding invocation of the /// Exit method. /// </remarks> /// <param name="millis"> /// Maximum number of milliseconds to wait; pass -1 for forever or 0 /// for no wait. /// </param> /// <returns> /// <b>true</b> iff the calling thread successfully entered the gate. /// </returns> public virtual bool Enter(long millis) { AtomicCounter atomicState = m_atomicState; if (IncrementThreadLocalCount(m_slotThreadEnterCount) > 1 || ClosingThread == Thread.CurrentThread) { // we were already in the gate, or are the one which has closed it // thus we must get in regardless of the state if ((atomicState.Increment() & ACTIVE_COUNT_MASK) > int.MaxValue) { // the gate has been entered way too many times, we must // have a cut-off somewhere, it is here as this could only // be possible if a thread keeps reentering the gate atomicState.Decrement(); DecrementThreadLocalCount(m_slotThreadEnterCount); throw new InvalidOperationException("The ThreadGate is full."); } // no need to check m_slotThreadEnterVersion, to get here we must be up to date return(true); } bool isSuccess = false; try { while (true) { long status = atomicState.GetCount(); switch ((ThreadGateState)(NumberUtils.URShift(status, STATUS_OFFSET))) { case ThreadGateState.Open: if (atomicState.SetCount(status, status + 1)) { // atomic set succeeded confirming that the gate // remained open and that we made it in long version = Version; if (version > GetThreadLocalCount(m_slotThreadEnterVersion)) { // the gate has been closed/opened since we // last entered, flush to get up to date Thread.MemoryBarrier(); SetThreadLocalCount(m_slotThreadEnterVersion, version); } return(isSuccess = true); } // we failed to atomically enter an open gate, which // can happen if either the gate closed just as we entered // or if another thread entered at the same time break; // retry case ThreadGateState.Closing: case ThreadGateState.Closed: // we know that we were not already in the gate, and are // not the one closing the gate; wait for it to open lock (this) { ThreadGateState state = Status; if (state == ThreadGateState.Closing || state == ThreadGateState.Closed) { // wait for the gate to open millis = (int)DoWait(millis); if (millis == 0L) { return(false); } } // version must be set from within sync since // we have not yet entered the gate. SetThreadLocalCount(m_slotThreadEnterVersion, Version); } break; // retry case ThreadGateState.Destroyed: DecrementThreadLocalCount(m_slotThreadEnterCount); throw new InvalidOperationException("ThreadGate.Enter: ThreadGate has been destroyed."); default: DecrementThreadLocalCount(m_slotThreadEnterCount); throw new InvalidOperationException("ThreadGate.Enter: ThreadGate has an invalid status. " + this); } } } finally { if (!isSuccess) { DecrementThreadLocalCount(m_slotThreadEnterCount); } } }
/// <summary> /// Convert the UUID to a printable String. /// </summary> /// <returns> /// The UUID data as a 0x-prefixed hex string. /// </returns> public override string ToString() { return(NumberUtils.ToHexEscape(ToByteArray())); }
/// <summary> /// Construct a UUID from a string. /// </summary> /// <param name="s"> /// A string as would be returned from UUID.ToString() /// </param> public UUID(string s) : this(NumberUtils.ParseHex(s)) { }
/// <summary> /// Return a human-readable description for this <b>Binary</b>. /// </summary> /// <returns> /// A string description of the object. /// </returns> public override string ToString() { return("Binary(length=" + m_cb + ", value=" + NumberUtils.ToHexEscape(m_ab, m_of, m_cb) + ')'); }