wasSharp – Diff between revs 5 and 7
?pathlinks?
Rev 5 | Rev 7 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
- | 6 | |
||
- | 7 | using System.Collections.Generic; |
||
- | 8 | using System.Linq; |
||
6 | |
9 | |
|
7 | namespace wasSharp |
10 | namespace wasSharp |
|
8 | { |
11 | { |
|
9 | public class BitTwiddling |
12 | public static class BitTwiddling |
|
10 | { |
13 | { |
|
11 | /////////////////////////////////////////////////////////////////////////// |
14 | /////////////////////////////////////////////////////////////////////////// |
|
12 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
15 | // Copyright (C) 2014 Wizardry and Steamworks - License: GNU GPLv3 // |
|
13 | /////////////////////////////////////////////////////////////////////////// |
16 | /////////////////////////////////////////////////////////////////////////// |
|
14 | /// <summary> |
17 | /// <summary> |
|
15 | /// Swaps two integers passed by reference using XOR. |
18 | /// Swaps two integers passed by reference using XOR. |
|
16 | /// </summary> |
19 | /// </summary> |
|
17 | /// <param name="q">first integer to swap</param> |
20 | /// <param name="q">first integer to swap</param> |
|
18 | /// <param name="p">second integer to swap</param> |
21 | /// <param name="p">second integer to swap</param> |
|
19 | public static void XORSwap(ref int q, ref int p) |
22 | public static void XORSwap(ref int q, ref int p) |
|
20 | { |
23 | { |
|
21 | q ^= p; |
24 | q ^= p; |
|
22 | p ^= q; |
25 | p ^= q; |
|
23 | q ^= p; |
26 | q ^= p; |
|
24 | } |
27 | } |
|
- | 28 | |
||
- | 29 | /////////////////////////////////////////////////////////////////////////// |
||
- | 30 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 31 | /////////////////////////////////////////////////////////////////////////// |
||
- | 32 | /// <summary> |
||
- | 33 | /// Checks whether a flag is set for a bitmask. |
||
- | 34 | /// </summary> |
||
- | 35 | /// <typeparam name="T">a data type</typeparam> |
||
- | 36 | /// <typeparam name="U">a data type</typeparam> |
||
- | 37 | /// <param name="mask">the mask to check the flag for</param> |
||
- | 38 | /// <param name="flag">the flag to check</param> |
||
- | 39 | /// <returns>true in case the flag is set</returns> |
||
- | 40 | public static bool IsMaskFlagSet<T, U>(this T mask, U flag) where T : struct where U : struct |
||
- | 41 | { |
||
- | 42 | return unchecked(mask as dynamic & flag as dynamic) != 0; |
||
- | 43 | } |
||
- | 44 | |
||
- | 45 | /////////////////////////////////////////////////////////////////////////// |
||
- | 46 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 47 | /////////////////////////////////////////////////////////////////////////// |
||
- | 48 | /// <summary> |
||
- | 49 | /// Sets a flag for a bitmask. |
||
- | 50 | /// </summary> |
||
- | 51 | /// <typeparam name="T">a data type</typeparam> |
||
- | 52 | /// <typeparam name="U">a data type</typeparam> |
||
- | 53 | /// <param name="mask">the mask to set the flag on</param> |
||
- | 54 | /// <param name="flag">the flag to set</param> |
||
- | 55 | public static void SetMaskFlag<T, U>(ref T mask, U flag) where T : struct where U : struct |
||
- | 56 | { |
||
- | 57 | mask = unchecked(mask as dynamic | flag as dynamic); |
||
- | 58 | } |
||
- | 59 | |
||
- | 60 | /////////////////////////////////////////////////////////////////////////// |
||
- | 61 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 62 | /////////////////////////////////////////////////////////////////////////// |
||
- | 63 | /// <summary> |
||
- | 64 | /// Create a bitmask from multiple flags. |
||
- | 65 | /// </summary> |
||
- | 66 | /// <typeparam name="T">a data type</typeparam> |
||
- | 67 | /// <param name="flag">the flags to set</param> |
||
- | 68 | /// <returns>a bitmask</returns> |
||
- | 69 | public static T CreateMask<T>(this IEnumerable<T> flag) where T : struct |
||
- | 70 | { |
||
- | 71 | return flag.Aggregate((o, p) => unchecked(o as dynamic | p as dynamic)); |
||
- | 72 | } |
||
- | 73 | |
||
- | 74 | /////////////////////////////////////////////////////////////////////////// |
||
- | 75 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 76 | /////////////////////////////////////////////////////////////////////////// |
||
- | 77 | /// <summary> |
||
- | 78 | /// Unset a flag for a bitmask. |
||
- | 79 | /// </summary> |
||
- | 80 | /// <typeparam name="T">a data type</typeparam> |
||
- | 81 | /// <typeparam name="U">a data type</typeparam> |
||
- | 82 | /// <param name="mask">the mask to unset the flag on</param> |
||
- | 83 | /// <param name="flag">the flag to unset</param> |
||
- | 84 | public static void UnsetMaskFlag<T, U>(ref T mask, U flag) |
||
- | 85 | { |
||
- | 86 | mask = unchecked(mask as dynamic & ~(flag as dynamic)); |
||
- | 87 | } |
||
- | 88 | |
||
- | 89 | /////////////////////////////////////////////////////////////////////////// |
||
- | 90 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
- | 91 | /////////////////////////////////////////////////////////////////////////// |
||
- | 92 | /// <summary> |
||
- | 93 | /// Toggle a flag for a bitmask. |
||
- | 94 | /// </summary> |
||
- | 95 | /// <typeparam name="T">a data type</typeparam> |
||
- | 96 | /// <typeparam name="U">a data type</typeparam> |
||
- | 97 | /// <param name="mask">the mask to toggle the flag on</param> |
||
- | 98 | /// <param name="flag">the flag to toggle</param> |
||
- | 99 | public static void ToggleMaskFlag<T, U>(ref T mask, U flag) |
||
- | 100 | { |
||
- | 101 | mask = unchecked(mask as dynamic ^ flag as dynamic); |
||
- | 102 | } |
||
- | 103 | |
||
- | 104 | /// <summary> |
||
- | 105 | /// Computes the previous power of two. |
||
- | 106 | /// </summary> |
||
- | 107 | /// <param name="x">the integer</param> |
||
- | 108 | /// <returns>the previous power of two</returns> |
||
- | 109 | /// <remarks>Adapted from Hacker's Delight ISBN-10:0201914654</remarks> |
||
- | 110 | public static T PreviousPowerOfTwo<T>(this T x) |
||
- | 111 | { |
||
- | 112 | var y = x as dynamic; |
||
- | 113 | unchecked |
||
- | 114 | { |
||
- | 115 | y = y | (y >> 1); |
||
- | 116 | y = y | (y >> 2); |
||
- | 117 | y = y | (y >> 4); |
||
- | 118 | y = y | (y >> 8); |
||
- | 119 | y = y | (y >> 16); |
||
- | 120 | return y - (y >> 1); |
||
- | 121 | } |
||
- | 122 | } |
||
- | 123 | |
||
- | 124 | /// <summary> |
||
- | 125 | /// Determines if a number is a power of two. |
||
- | 126 | /// </summary> |
||
- | 127 | /// <typeparam name="T">the number type</typeparam> |
||
- | 128 | /// <param name="x">the number</param> |
||
- | 129 | /// <returns>true of the number is a power of two</returns> |
||
- | 130 | public static bool IsPowerOfTwo<T>(this T x) |
||
- | 131 | { |
||
- | 132 | var y = x as dynamic; |
||
- | 133 | return (y != 0) && ((y & (y - 1)) == 0); |
||
- | 134 | } |
||
25 | } |
135 | } |
|
26 | } |
136 | } |
|
27 | |
137 | |
|
28 |
|
138 |
|
|
29 | |
139 | |
|
30 | |
140 | |
|
31 | |
141 | |