/
Vn.cs
227 lines (168 loc) · 5.53 KB
/
Vn.cs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright Lamont Granquist, Sebastien Gaggini and the MechJeb contributors
* SPDX-License-Identifier: LicenseRef-PD-hp OR Unlicense OR CC0-1.0 OR 0BSD OR MIT-0 OR MIT OR LGPL-2.1+
*/
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using MechJebLib.Utils;
using static MechJebLib.Utils.Statics;
using static System.Math;
// ReSharper disable InconsistentNaming
namespace MechJebLib.Primitives
{
public class Vn : IList<double>, IReadOnlyList<double>, IDisposable
{
private int _n;
private static readonly ObjectPool<Vn> _pool = new ObjectPool<Vn>(New, Clear);
private readonly List<double> _values = new List<double>();
public Vn Abs()
{
Vn abs = Rent(_n);
for (int i = 0; i < _n; i++)
abs[i] = Math.Abs(this[i]);
return abs;
}
public double AbsMax()
{
double absmax = double.NegativeInfinity;
for (int i = 0; i < _n; i++)
if (Math.Abs(this[i]) > absmax)
absmax = this[i];
return absmax;
}
public Vn Dup()
{
Vn dup = Rent(_n);
for (int i = 0; i < _n; i++)
dup[i] = this[i];
return dup;
}
public Vn MutAdd(double rhs)
{
for (int i = 0; i < _n; i++)
this[i] += rhs;
return this;
}
public Vn MutAdd(IReadOnlyList<double> rhs)
{
for (int i = 0; i < _n; i++)
this[i] += rhs[i];
return this;
}
public Vn MutSub(double rhs)
{
for (int i = 0; i < _n; i++)
this[i] -= rhs;
return this;
}
public Vn MutSub(IReadOnlyList<double> rhs)
{
for (int i = 0; i < _n; i++)
this[i] -= rhs[i];
return this;
}
public Vn MutTimes(double rhs)
{
for (int i = 0; i < _n; i++)
this[i] *= rhs;
return this;
}
public Vn MutTimes(IReadOnlyList<double> rhs)
{
for (int i = 0; i < _n; i++)
this[i] *= rhs[i];
return this;
}
public Vn MutDiv(double rhs)
{
for (int i = 0; i < _n; i++)
this[i] /= rhs;
return this;
}
public Vn MutDiv(IReadOnlyList<double> rhs)
{
for (int i = 0; i < _n; i++)
this[i] /= rhs[i];
return this;
}
public double magnitude
{
get
{
// FIXME: overflow
double sumsq = 0;
for (int i = 0; i < _n; i++)
sumsq += this[i] * this[i];
return Sqrt(sumsq);
}
}
private Vn()
{
}
private static Vn New() => new Vn();
public Vn CopyFrom(IReadOnlyList<double> other)
{
for (int i = 0; i < _n; i++)
this[i] = other[i];
return this;
}
public void CopyTo(IList<double> other)
{
for (int i = 0; i < _n; i++)
other[i] = this[i];
}
public void Set(int index, V3 v)
{
this[index] = v.x;
this[index + 1] = v.y;
this[index + 2] = v.z;
}
public V3 Get(int index) => new V3(this[index], this[index + 1], this[index + 2]);
public static Vn Rent(int n)
{
Vn list = _pool.Borrow();
while (list.Count < n)
list.Add(0);
if (list.Count > n)
list.RemoveRange(n, list.Count - n);
list._n = n;
return list;
}
private void RemoveRange(int i, int listCount) => _values.RemoveRange(i, listCount);
public static Vn Rent(double[] other)
{
Vn list = Rent(other.Length);
for (int i = 0; i < other.Length; i++)
list[i] = other[i];
return list;
}
private static void Clear(Vn obj)
{
// do not resize the array to zero to avoid creating garbage
for (int i = 0; i < obj.Count; i++)
obj[i] = 0;
}
public static void Return(Vn obj) => _pool.Release(obj);
public void Dispose() => _pool.Release(this);
public IEnumerator<double> GetEnumerator() => _values.GetEnumerator();
public override string ToString() => DoubleArrayString(this);
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)_values).GetEnumerator();
public void Add(double item) => _values.Add(item);
public void Clear() => _values.Clear();
public bool Contains(double item) => _values.Contains(item);
public void CopyTo(double[] array, int arrayIndex) => _values.CopyTo(array, arrayIndex);
public bool Remove(double item) => _values.Remove(item);
public int Count => _values.Count;
public bool IsReadOnly => ((ICollection<double>)_values).IsReadOnly;
public int IndexOf(double item) => _values.IndexOf(item);
public void Insert(int index, double item) => _values.Insert(index, item);
public void RemoveAt(int index) => _values.RemoveAt(index);
public double this[int index]
{
get => _values[index];
set => _values[index] = value;
}
}
}