Repository navigation
Expand file tree
/
Copy pathVector.lua
More file actions
135 lines (128 loc) · 2.87 KB
/
Copy pathVector.lua
File metadata and controls
135 lines (128 loc) · 2.87 KB
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
local System = System
local throw = System.throw
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
local ArgumentNullException = System.ArgumentNullException
local NotSupportedException = System.NotSupportedException
local type = type
local getmetatable = getmetatable
local setmetatable = setmetatable
local counts = {
[System.Byte] = 32,
[System.Int16] = 16,
[System.Int32] = 8,
[System.Single] = 8,
[System.Double] = 4,
}
local function getCount(T)
local count = counts[T]
if not count then
throw(NotSupportedException("Specified type is not supported"))
end
return count
end
local function default(T)
return T(0)
end
local Vector = {
__ctor__ = function (this, values, index)
local count = this:Count()
if index == nil then
if type(values) == "number" then
for i = 1, count do
this[i] = values
end
return
else
index = 0
end
end
if values == nil then
throw(ArgumentNullException())
end
if (index < 0) or (#values - index < count) then
throw(ArgumentOutOfRangeException("IndexMustBeLessOrEqual"))
end
for i = 1, count do
this[i] = values[index + i]
end
end,
get = function (this, index)
local count = this:Count()
if (index < 0) or (index >= count) then
throw(ArgumentOutOfRangeException("index"))
end
return this[index + 1]
end,
Count = function (T)
return getCount(T.__genericT__)
end,
One = function (T)
return T(1)
end,
IsSupported = function (T)
return counts[T.__genericT__] ~= nil
end,
Zero = default,
default = default,
ToString = function (this)
return '<' .. table.concat(this, ', ') .. '>'
end,
__eq = function (a, b)
local n = #a
if n ~= #b then
return false
end
for i = 1, n do
if a[i] ~= b[i] then
return false
end
end
return true
end,
__sub = function (a, b)
local t, n = {}, #a
for i = 1, n do
t[i] = a[i] - b[i]
end
return setmetatable(t, getmetatable(a))
end,
__mul = function (a, b)
if type(a) == "number" then
a, b = b, a
end
local t, n = {}, #a
if type(b) == "number" then
for i = 1, n do
t[i] = a[i] * b
end
else
for i = 1, n do
t[i] = a[i] * b[i]
end
end
return setmetatable(t, getmetatable(a))
end,
__div = function (a, b)
local t, n = {}, #a
if type(b) == "number" then
for i = 1, n do
t[i] = a[i] / b
end
else
for i = 1, n do
t[i] = a[i] / b[i]
end
end
return setmetatable(t, getmetatable(a))
end,
}
System.defStc("System.Numerics.Vector", function(T)
return {
__genericT__ = T,
base = { System.IEquatable_1(T) },
__eq = Vector.__eq,
__sub = Vector.__sub,
__mul = Vector.__mul,
__div = Vector.__div,
}
end, Vector, 1)