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