-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from joelverhagen/stackalloc
Add Span, minimal MemoryExtensions, and stackalloc handling
- Loading branch information
Showing
5 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--[[ | ||
Copyright 2017 YANG Huan ([email protected]). | ||
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 Span = System.Span | ||
|
||
System.MemoryExtensions = { | ||
AsSpan = function (array) | ||
local SpanT = Span(array.__genericT__) | ||
return SpanT(array) | ||
end, | ||
AsBoundedSpan = function (array, start, length) | ||
local SpanT = Span(array.__genericT__) | ||
return SpanT(array, start, length) | ||
end | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
--[[ | ||
Copyright 2017 YANG Huan ([email protected]). | ||
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, ...) | ||
if type(input) == "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 | ||
else | ||
this._array = System.Array.new(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters