-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathSpan.lua
More file actions
107 lines (98 loc) · 2.92 KB
/
Copy pathSpan.lua
File metadata and controls
107 lines (98 loc) · 2.92 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
--[[
Copyright 2017 YANG Huan (sy.yanghuan@gmail.com).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local System = System
local throw = System.throw
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
local IndexOutOfRangeException = System.IndexOutOfRangeException
local Span = {
__ctor__ = function (this, input, ...)
local t = type(input)
if t == "table" then
local argsLen = select("#", ...)
local maxLength = input:getLength()
local start, length
if argsLen == 2 then
start, length = ...
if start >= maxLength then
throw(ArgumentOutOfRangeException("start"))
end
if start + length > maxLength then
throw(ArgumentOutOfRangeException("length"))
end
else
start, length = 0, maxLength
end
this._array = input
this._min = start
this._max = start + length - 1
elseif t == "number" then
this._array = System.Array(this.__genericT__)(input)
this._min = 0
this._max = input
else
this._array = System.Array(this.__genericT__)(1)
this._array:set(0, input)
this._min = 0
this._max = 0
end
end,
get = function (this, index)
local i = this._min + index
if i > this._max then
throw(IndexOutOfRangeException("index"))
end
return this._array:get(i)
end,
set = function (this, index, value)
local i = this._min + index
if i > this._max then
throw(IndexOutOfRangeException("index"))
end
return this._array:set(i, value)
end,
getIsEmpty = function (this)
return this:getLength() <= 0
end,
getLength = function (this)
return this._max - this._min + 1
end,
Slice = function (this, start, ...)
local newMin = this._min + start
if newMin > this._max then
throw(ArgumentOutOfRangeException("start"))
end
local newMax
local argsLen = select("#", ...)
if argsLen == 1 then
length = ...
newMax = newMin + length - 1
if newMax > this._max then
throw(ArgumentOutOfRangeException("length"))
end
else
newMax = this._max
end
local ctor = System.Span(this.__genericT__)
return ctor(this._array, newMin, newMax - newMin + 1)
end,
ctorArray = function (array)
local ctor = System.Span(array.__genericT__)
return ctor(array)
end
}
local SpanFn = System.defStc("System.Span", function (T)
return {
__genericT__ = T
}
end, Span, 1)
System.Span = SpanFn