Added license tags in the files where they were amiss; added IntegerHelper class with a method to quickly determine the next power of 2 to any number
git-svn-id: file:///srv/devel/repo-conversion/nusu@82 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
a666885598
commit
313b006f83
|
@ -118,6 +118,10 @@
|
|||
<Compile Include="Source\FloatHelper.Test.cs">
|
||||
<DependentUpon>FloatHelper.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\IntegerHelper.cs" />
|
||||
<Compile Include="Source\IntegerHelper.Test.cs">
|
||||
<DependentUpon>IntegerHelper.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Source\Licensing\LicenseKey.cs" />
|
||||
<Compile Include="Source\Licensing\LicenseKey.Test.cs">
|
||||
<DependentUpon>LicenseKey.cs</DependentUpon>
|
||||
|
|
|
@ -1,3 +1,23 @@
|
|||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
|
@ -1,4 +1,24 @@
|
|||
using System;
|
||||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
|
@ -1,4 +1,24 @@
|
|||
using System;
|
||||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections;
|
||||
using System.Collections.ObjectModel;
|
||||
|
|
|
@ -1,4 +1,24 @@
|
|||
using System;
|
||||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
|
|
@ -1,4 +1,24 @@
|
|||
using System;
|
||||
#region CPL License
|
||||
/*
|
||||
Nuclex Framework
|
||||
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the IBM Common Public License as
|
||||
published by the IBM Corporation; either version 1.0 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
IBM Common Public License for more details.
|
||||
|
||||
You should have received a copy of the IBM Common Public
|
||||
License along with this library
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Nuclex.Support.Collections {
|
||||
|
|
75
Source/IntegerHelper.Test.cs
Normal file
75
Source/IntegerHelper.Test.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace Nuclex.Support {
|
||||
|
||||
/// <summary>Contains unit tests for the integer helper class</summary>
|
||||
[TestFixture]
|
||||
public class IntegerHelperTest {
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the next power of 2 calculation works for long integers
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestNextPowerOf2ULong() {
|
||||
Assert.AreEqual(1UL, IntegerHelper.NextPowerOf2(0UL));
|
||||
Assert.AreEqual(1UL, IntegerHelper.NextPowerOf2(1UL));
|
||||
Assert.AreEqual(2UL, IntegerHelper.NextPowerOf2(2UL));
|
||||
Assert.AreEqual(4UL, IntegerHelper.NextPowerOf2(3UL));
|
||||
Assert.AreEqual(4UL, IntegerHelper.NextPowerOf2(4UL));
|
||||
Assert.AreEqual(
|
||||
9223372036854775808UL, IntegerHelper.NextPowerOf2(9223372036854775807UL)
|
||||
);
|
||||
Assert.AreEqual(
|
||||
9223372036854775808UL, IntegerHelper.NextPowerOf2(9223372036854775808UL)
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the next power of 2 calculation works for long integers
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestNextPowerOf2Long() {
|
||||
Assert.AreEqual(1L, IntegerHelper.NextPowerOf2(0L));
|
||||
Assert.AreEqual(1L, IntegerHelper.NextPowerOf2(1L));
|
||||
Assert.AreEqual(2L, IntegerHelper.NextPowerOf2(2L));
|
||||
Assert.AreEqual(4L, IntegerHelper.NextPowerOf2(3L));
|
||||
Assert.AreEqual(4L, IntegerHelper.NextPowerOf2(4L));
|
||||
Assert.AreEqual(4611686018427387904L, IntegerHelper.NextPowerOf2(4611686018427387903L));
|
||||
Assert.AreEqual(4611686018427387904L, IntegerHelper.NextPowerOf2(4611686018427387904L));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the next power of 2 calculation works for integers
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestNextPowerOf2UInt() {
|
||||
Assert.AreEqual(1U, IntegerHelper.NextPowerOf2(0U));
|
||||
Assert.AreEqual(1U, IntegerHelper.NextPowerOf2(1U));
|
||||
Assert.AreEqual(2U, IntegerHelper.NextPowerOf2(2U));
|
||||
Assert.AreEqual(4U, IntegerHelper.NextPowerOf2(3U));
|
||||
Assert.AreEqual(4U, IntegerHelper.NextPowerOf2(4U));
|
||||
Assert.AreEqual(2147483648U, IntegerHelper.NextPowerOf2(2147483647U));
|
||||
Assert.AreEqual(2147483648U, IntegerHelper.NextPowerOf2(2147483648U));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the next power of 2 calculation works for integers
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TestNextPowerOf2Int() {
|
||||
Assert.AreEqual(1, IntegerHelper.NextPowerOf2(0));
|
||||
Assert.AreEqual(1, IntegerHelper.NextPowerOf2(1));
|
||||
Assert.AreEqual(2, IntegerHelper.NextPowerOf2(2));
|
||||
Assert.AreEqual(4, IntegerHelper.NextPowerOf2(3));
|
||||
Assert.AreEqual(4, IntegerHelper.NextPowerOf2(4));
|
||||
Assert.AreEqual(1073741824, IntegerHelper.NextPowerOf2(1073741823));
|
||||
Assert.AreEqual(1073741824, IntegerHelper.NextPowerOf2(1073741824));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support
|
61
Source/IntegerHelper.cs
Normal file
61
Source/IntegerHelper.cs
Normal file
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Nuclex.Support {
|
||||
|
||||
public static class IntegerHelper {
|
||||
|
||||
/// <summary>Returns the next highest power of 2 from the specified value</summary>
|
||||
/// <param name="value">Value of which to return the next highest power of 2</param>
|
||||
/// <returns>The next highest power of 2 to the value</returns>
|
||||
public static long NextPowerOf2(long value) {
|
||||
return (long)NextPowerOf2((ulong)value);
|
||||
}
|
||||
|
||||
/// <summary>Returns the next highest power of 2 from the specified value</summary>
|
||||
/// <param name="value">Value of which to return the next highest power of 2</param>
|
||||
/// <returns>The next highest power of 2 to the value</returns>
|
||||
public static ulong NextPowerOf2(ulong value) {
|
||||
if(value == 0)
|
||||
return 1;
|
||||
|
||||
--value;
|
||||
value |= value >> 1;
|
||||
value |= value >> 2;
|
||||
value |= value >> 4;
|
||||
value |= value >> 8;
|
||||
value |= value >> 16;
|
||||
value |= value >> 32;
|
||||
++value;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>Returns the next highest power of 2 from the specified value</summary>
|
||||
/// <param name="value">Value of which to return the next highest power of 2</param>
|
||||
/// <returns>The next highest power of 2 to the value</returns>
|
||||
public static int NextPowerOf2(int value) {
|
||||
return (int)NextPowerOf2((uint)value);
|
||||
}
|
||||
|
||||
/// <summary>Returns the next highest power of 2 from the specified value</summary>
|
||||
/// <param name="value">Value of which to return the next highest power of 2</param>
|
||||
/// <returns>The next highest power of 2 to the value</returns>
|
||||
public static uint NextPowerOf2(uint value) {
|
||||
if(value == 0)
|
||||
return 1;
|
||||
|
||||
--value;
|
||||
value |= value >> 1;
|
||||
value |= value >> 2;
|
||||
value |= value >> 4;
|
||||
value |= value >> 8;
|
||||
value |= value >> 16;
|
||||
++value;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support
|
Loading…
Reference in New Issue
Block a user