summaryrefslogtreecommitdiff
path: root/NW4RTools/Misc.cs
blob: 118f16454f8a01efa64dd12ad4def2712733af5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
namespace NW4RTools {
	public static class Misc {
		public static int AlignUp(int val, int to) {
			return (val + (to - 1)) & ~(to - 1);
		}

		public static uint AlignUp(uint val, uint to) {
			return (val + (to - 1)) & ~(to - 1);
		}

		public static int AlignDown(int val, int to) {
			return val & ~(to - 1);
		}

		public static uint AlignDown(uint val, uint to) {
			return val & ~(to - 1);
		}


		public static void Assert(bool condition) {
			if (!condition)
				throw new Exception("Assert failed");
		}
		public static void Assert(bool condition, string format, params object[] args) {
			if (!condition)
				throw new Exception(String.Format(format, args));
		}


		public static bool ArrayCompare<T>(T[] one, T[] two) {
			if (one == two)
				return true;
			if (one == null || two == null)
				return false;
			if (one.Length != two.Length)
				return false;

			for (int i = 0; i < one.Length; i++) {
				if (!one[i].Equals(two[i]))
					return false;
			}

			return true;
		}


		public static int ArrayHash<T>(T[] arr) {
			if (arr == null)
				return 0xEA7BEEF;

			int hash = 0;

			for (int i = 0; i < arr.Length; i++)
				hash ^= arr[i].GetHashCode();

			return hash;
		}
	}
}