From 313b006f8359024144d3005e17f362943a57f0fc Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Thu, 17 Jul 2008 21:19:41 +0000 Subject: [PATCH] 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 --- Nuclex.Support (x86).csproj | 4 ++ Source/Collections/ItemEventArgs.cs | 20 +++++++ Source/Collections/ReadOnlyCollection.cs | 22 ++++++- Source/Collections/ReadOnlyDictionary.cs | 22 ++++++- Source/Collections/ReadOnlyList.cs | 22 ++++++- Source/Collections/ReverseComparer.cs | 22 ++++++- Source/IntegerHelper.Test.cs | 75 ++++++++++++++++++++++++ Source/IntegerHelper.cs | 61 +++++++++++++++++++ 8 files changed, 244 insertions(+), 4 deletions(-) create mode 100644 Source/IntegerHelper.Test.cs create mode 100644 Source/IntegerHelper.cs diff --git a/Nuclex.Support (x86).csproj b/Nuclex.Support (x86).csproj index aabecb2..d3bcbc8 100644 --- a/Nuclex.Support (x86).csproj +++ b/Nuclex.Support (x86).csproj @@ -118,6 +118,10 @@ FloatHelper.cs + + + IntegerHelper.cs + LicenseKey.cs diff --git a/Source/Collections/ItemEventArgs.cs b/Source/Collections/ItemEventArgs.cs index 0ddc4e4..5ef91e7 100644 --- a/Source/Collections/ItemEventArgs.cs +++ b/Source/Collections/ItemEventArgs.cs @@ -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; diff --git a/Source/Collections/ReadOnlyCollection.cs b/Source/Collections/ReadOnlyCollection.cs index 56caa9f..c7d296e 100644 --- a/Source/Collections/ReadOnlyCollection.cs +++ b/Source/Collections/ReadOnlyCollection.cs @@ -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; diff --git a/Source/Collections/ReadOnlyDictionary.cs b/Source/Collections/ReadOnlyDictionary.cs index 8ec555a..c4bf9d6 100644 --- a/Source/Collections/ReadOnlyDictionary.cs +++ b/Source/Collections/ReadOnlyDictionary.cs @@ -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; diff --git a/Source/Collections/ReadOnlyList.cs b/Source/Collections/ReadOnlyList.cs index 45a623c..3a72cd1 100644 --- a/Source/Collections/ReadOnlyList.cs +++ b/Source/Collections/ReadOnlyList.cs @@ -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; diff --git a/Source/Collections/ReverseComparer.cs b/Source/Collections/ReverseComparer.cs index 12076c2..8fa51ce 100644 --- a/Source/Collections/ReverseComparer.cs +++ b/Source/Collections/ReverseComparer.cs @@ -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 { diff --git a/Source/IntegerHelper.Test.cs b/Source/IntegerHelper.Test.cs new file mode 100644 index 0000000..de6b9f0 --- /dev/null +++ b/Source/IntegerHelper.Test.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; + +using NUnit.Framework; + +namespace Nuclex.Support { + + /// Contains unit tests for the integer helper class + [TestFixture] + public class IntegerHelperTest { + + /// + /// Verifies that the next power of 2 calculation works for long integers + /// + [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) + ); + } + + /// + /// Verifies that the next power of 2 calculation works for long integers + /// + [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)); + } + + /// + /// Verifies that the next power of 2 calculation works for integers + /// + [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)); + } + + /// + /// Verifies that the next power of 2 calculation works for integers + /// + [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 diff --git a/Source/IntegerHelper.cs b/Source/IntegerHelper.cs new file mode 100644 index 0000000..a2c9973 --- /dev/null +++ b/Source/IntegerHelper.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; + +namespace Nuclex.Support { + + public static class IntegerHelper { + + /// Returns the next highest power of 2 from the specified value + /// Value of which to return the next highest power of 2 + /// The next highest power of 2 to the value + public static long NextPowerOf2(long value) { + return (long)NextPowerOf2((ulong)value); + } + + /// Returns the next highest power of 2 from the specified value + /// Value of which to return the next highest power of 2 + /// The next highest power of 2 to the value + 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; + } + + /// Returns the next highest power of 2 from the specified value + /// Value of which to return the next highest power of 2 + /// The next highest power of 2 to the value + public static int NextPowerOf2(int value) { + return (int)NextPowerOf2((uint)value); + } + + /// Returns the next highest power of 2 from the specified value + /// Value of which to return the next highest power of 2 + /// The next highest power of 2 to the value + 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