Jypeli 10
The simple game programming library
OperationHelper.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26
27#if UseDouble
28using Scalar = System.Double;
29#else
30using Scalar = System.Single;
31#endif
32using System;
33
34
35namespace AdvanceMath
36{
37 public delegate void RefOperation<TLeft, TRight, TResult>(ref TLeft left, ref TRight right, out TResult result);
38 public delegate TResult ValOperation<TLeft, TRight, TResult>(TLeft left, TRight right);
39
40
41 public static class OperationHelper
42 {
43 public static TResult[] ArrayRefOp<TLeft, TRight, TResult>(TLeft[] leftArray, ref TRight right, RefOperation<TLeft, TRight, TResult> operation)
44 {
45 TResult[] result = new TResult[leftArray.Length];
46 ArrayRefOp<TLeft, TRight, TResult>(leftArray, ref right, result, operation);
47 return result;
48 }
49 public static TResult[] ArrayRefOp<TLeft, TRight, TResult>(ref TLeft left, TRight[] rightArray, RefOperation<TLeft, TRight, TResult> operation)
50 {
51 TResult[] result = new TResult[rightArray.Length];
52 ArrayRefOp<TLeft, TRight, TResult>(ref left, rightArray, result, operation);
53 return result;
54 }
55 public static TResult[] ArrayRefOp<TLeft, TRight, TResult>(TLeft[] leftArray, TRight[] rightArray, RefOperation<TLeft, TRight, TResult> operation)
56 {
57 TResult[] result = new TResult[leftArray.Length];
58 ArrayRefOp<TLeft, TRight, TResult>(leftArray, rightArray, result, operation);
59 return result;
60 }
61
62 public static void ArrayRefOp<TLeft, TRight, TResult>(TLeft[] leftArray, ref TRight right, TResult[] result, RefOperation<TLeft, TRight, TResult> operation)
63 {
64 if (leftArray == null) { throw new ArgumentNullException("leftArray"); }
65 if (result == null) { throw new ArgumentNullException("result"); }
66 if (leftArray.Length != result.Length) { throw new ArgumentException("The Arrays Passed must equal in size."); }
67 for (int pos = 0; pos < result.Length; ++pos)
68 {
69 operation(ref leftArray[pos], ref right, out result[pos]);
70 }
71 }
72 public static void ArrayRefOp<TLeft, TRight, TResult>(ref TLeft left, TRight[] rightArray, TResult[] result, RefOperation<TLeft, TRight, TResult> operation)
73 {
74 if (rightArray == null) { throw new ArgumentNullException("rightArray"); }
75 if (result == null) { throw new ArgumentNullException("result"); }
76 if (rightArray.Length != result.Length) { throw new ArgumentException("The Arrays Passed must equal in size."); }
77 for (int pos = 0; pos < result.Length; ++pos)
78 {
79 operation(ref left, ref rightArray[pos],out result[pos]);
80 }
81 }
82 public static void ArrayRefOp<TLeft, TRight, TResult>(TLeft[] leftArray, TRight[] rightArray, TResult[] result, RefOperation<TLeft, TRight, TResult> operation)
83 {
84 if (leftArray == null) { throw new ArgumentNullException("leftArray"); }
85 if (rightArray == null) { throw new ArgumentNullException("rightArray"); }
86 if (result == null) { throw new ArgumentNullException("result"); }
87 if (leftArray.Length != rightArray.Length || rightArray.Length != result.Length) { throw new ArgumentException("The Arrays Passed must equal in size."); }
88 for (int pos = 0; pos < result.Length; ++pos)
89 {
90 operation(ref leftArray[pos], ref rightArray[pos], out result[pos]);
91 }
92 }
93
94 public static TResult[] ArrayValOp<TLeft, TRight, TResult>(TLeft[] leftArray, TRight right, ValOperation<TLeft, TRight, TResult> operation)
95 {
96 TResult[] result = new TResult[leftArray.Length];
97 ArrayValOp<TLeft, TRight, TResult>(leftArray, right, result, operation);
98 return result;
99 }
100 public static TResult[] ArrayValOp<TLeft, TRight, TResult>(TLeft left, TRight[] rightArray, ValOperation<TLeft, TRight, TResult> operation)
101 {
102 TResult[] result = new TResult[rightArray.Length];
103 ArrayValOp<TLeft, TRight, TResult>(left, rightArray, result, operation);
104 return result;
105 }
106 public static TResult[] ArrayValOp<TLeft, TRight, TResult>(TLeft[] leftArray, TRight[] rightArray, ValOperation<TLeft, TRight, TResult> operation)
107 {
108 TResult[] result = new TResult[leftArray.Length];
109 ArrayValOp<TLeft, TRight, TResult>(leftArray, rightArray, result, operation);
110 return result;
111 }
112
113 public static void ArrayValOp<TLeft, TRight, TResult>(TLeft[] leftArray, TRight right, TResult[] result, ValOperation<TLeft, TRight, TResult> operation)
114 {
115 if (leftArray == null) { throw new ArgumentNullException("leftArray"); }
116 if (result == null) { throw new ArgumentNullException("result"); }
117 if (leftArray.Length != result.Length) { throw new ArgumentException("The Arrays Passed must equal in size."); }
118 for (int pos = 0; pos < result.Length; ++pos)
119 {
120 result[pos] = operation(leftArray[pos], right);
121 }
122 }
123 public static void ArrayValOp<TLeft, TRight, TResult>(TLeft left, TRight[] rightArray, TResult[] result, ValOperation<TLeft, TRight, TResult> operation)
124 {
125 if (rightArray == null) { throw new ArgumentNullException("rightArray"); }
126 if (result == null) { throw new ArgumentNullException("result"); }
127 if (rightArray.Length != result.Length) { throw new ArgumentException("The Arrays Passed must equal in size."); }
128 for (int pos = 0; pos < result.Length; ++pos)
129 {
130 result[pos] = operation(left, rightArray[pos]);
131 }
132 }
133 public static void ArrayValOp<TLeft, TRight, TResult>(TLeft[] leftArray, TRight[] rightArray, TResult[] result, ValOperation<TLeft, TRight, TResult> operation)
134 {
135 if (leftArray == null) { throw new ArgumentNullException("leftArray"); }
136 if (rightArray == null) { throw new ArgumentNullException("rightArray"); }
137 if (result == null) { throw new ArgumentNullException("result"); }
138 if (leftArray.Length != rightArray.Length || rightArray.Length != result.Length) { throw new ArgumentException("The Arrays Passed must equal in size."); }
139 for (int pos = 0; pos < result.Length; ++pos)
140 {
141 result[pos] = operation(leftArray[pos], rightArray[pos]);
142 }
143 }
144 }
145}
System.Single Scalar
Definition: Clamped.cs:29
static TResult[] ArrayValOp< TLeft, TRight, TResult >(TLeft[] leftArray, TRight right, ValOperation< TLeft, TRight, TResult > operation)
static TResult[] ArrayRefOp< TLeft, TRight, TResult >(TLeft[] leftArray, ref TRight right, RefOperation< TLeft, TRight, TResult > operation)
delegate TResult ValOperation< TLeft, TRight, TResult >(TLeft left, TRight right)
delegate void RefOperation< TLeft, TRight, TResult >(ref TLeft left, ref TRight right, out TResult result)