Fastlist
const fastlist = require("@vectarine/fastlist")A module for writing high performance code by storing data in a compact way
Principles of fastlists
Section titled “Principles of fastlists”- A fastlist contains a list of Vec2
- Any function that takes a Vec2 and outputs another Vec2 also exists for Fastlist with the same syntax
- You can index a fastlist using .get
- You can draw the content of a fastlist quickly using functions starting with draw
Functions
Section titled “Functions”fastlist.newLinspace
Section titled “fastlist.newLinspace”fastlist.newLinspace(min: Vec.Vec2, max: Vec.Vec2, step: Vec.Vec2): FastListReturn a list of Vec2 that are linearly spaced between min and max in a 2D grid
fastlist.zeros
Section titled “fastlist.zeros”fastlist.zeros(size: number): FastListReturn a list of Vec2 of size size filled with Vec2.ZERO
fastlist.fromValue
Section titled “fastlist.fromValue”fastlist.fromValue(value: Vec.Vec2, size: number): FastListReturn a list of Vec2 of size size filled with value
fastlist.fromTable
Section titled “fastlist.fromTable”fastlist.fromTable(tbl: { Vec.Vec2 }): FastListConvert a table of Vec2 into a FastList
FastList
Section titled “FastList”export type FastList = ...Operators
#a — (self: FastList): number
Returns the number of Vec2 in the fast list
#fastlist is equivalent to fastlist.__len(fastlist)
a + b — (self: FastList, other: FastListOrVec): FastList
Element-wise addition of the fastlist and another fastlist
a - b — (self: FastList, other: FastListOrVec): FastList
Element-wise subtraction of the fastlist by another fastlist
-a — (self: FastList): FastList
Element-wise negation of the fastlist
a * b — (self: FastList, other: FastListOrVec): FastList
Element-wise multiplication of the fastlist by another fastlist
a / b — (self: FastList, other: FastListOrVec): FastList
Element-wise division of the fastlist by another fastlist
FastList:get
Section titled “FastList:get”FastList:get(index: number): Vec.Vec2Returns the Vec2 at the given index
FastList:concat
Section titled “FastList:concat”FastList:concat(other: FastList): FastListReturns a new fast list that is the concatenation of the two fast lists If one fastlist is ABC and the other is XYZ, the result is ABCXYZ
FastList:toTable
Section titled “FastList:toTable”FastList:toTable(): { Vec.Vec2 }Returns a table of Vec2 from the fast list
FastList:toNumberTableX
Section titled “FastList:toNumberTableX”FastList:toNumberTableX(): { number }Returns a table of numbers from the fast list, by taking the x component of each Vec2
FastList:forEach
Section titled “FastList:forEach”FastList:forEach(fn: (Vec.Vec2, number) -> ()): FastListApply a function to each Vec2 in the fast list. You can modify the vector in-place in the function to modify elements of the fast list efficiently This function is not as efficient that the other as it calls Lua code for each element.
FastList:componentRepeated
Section titled “FastList:componentRepeated”FastList:componentRepeated(count: number): FastListCreate a new FastList where every Vec2 is repeated count times
A list like XYZ with a count of 2 becomes XXYYZZ
FastList:repeated
Section titled “FastList:repeated”FastList:repeated(count: number): FastListCreate a new FastList where the list is repeated count times
A list like XYZ with a count of 2 becomes XYZXYZ
FastList:weave
Section titled “FastList:weave”FastList:weave(others: { FastList }): FastListCreate a new Fastlist by weaving elements from multiple fastlists If the fastlists are ABC, DEF and GHI, the result is ADGBCEFH
FastList:unweave
Section titled “FastList:unweave”FastList:unweave(groupCount: number): { FastList }Unweave a fastlist into groupCount fastlists. This is the opposite of weave.
A fastlist like ADGBCEFH with a group count of 3 becomes ABC, DEF and GHI
ADGBCEFH with a group count of 2 becomes AGCF and DBEH
The resulting fastlists might not be of the same size if the original fastlist size is not a multiple of groupCount.
FastList:filterBetweenX
Section titled “FastList:filterBetweenX”FastList:filterBetweenX( mask: FastList, min: number | FastList, max: number | FastList,): FastListFilter the Fastlist on the X component, keeping only the Vec2s for which the corresponding Vec2 in the mask has a x component between min and max. The mask and self and expected to be of the same size.
The indices outside the range are replaced with GAPs. If there were already gaps in the mask or fastlist, they are preserved.
The resulting fastlist has the same length as the original one.
Conside the following non-fastlist code:
local filtered = {}for i, value in pairs(mask): if min <= value.x <= max then filtered[i] = fastlist[I] endendThe equivalent fastlist code is:
local filtered = fastlist:filterBetweenX(mask, min, max)You can use math.inf for max or -math.inf for min for taking greater or smaller values.
FastList:filterBetweenY
Section titled “FastList:filterBetweenY”FastList:filterBetweenY( mask: FastList, min: number | FastList, max: number | FastList,): FastListFilter the Fastlist on the Y component, keeping only the Vec2s for which the corresponding Vec2 in the mask has a y component between min and max. Gaps are preserved.
The resulting fastlist has the same length as the original one. See filterBetweenX for more information
FastList:replaceGaps
Section titled “FastList:replaceGaps”FastList:replaceGaps(replacement: Vec.Vec2): FastListReplace gaps in the Fastlist with the replacement value
FastList:filterGaps
Section titled “FastList:filterGaps”FastList:filterGaps(): FastListRemove gaps from the Fastlist, resulting in a shorter list
FastList:otherwise
Section titled “FastList:otherwise”FastList:otherwise(other: FastList): FastListReturns the elements of the fastlist at indices where there are no gaps, otherwise, the gap is replaced with the value from other.
Self and other are expected to be of the same size.
This is similar to how or works for numbers: 4 or 5 is 4, but nil or 5 is 5
FastList:also
Section titled “FastList:also”FastList:also(other: FastList): FastListReturns the elements of other at indices where there is no gaps inside both fastlists. If there is a gap in one of the fastlists, a gap will be there in the result.
This is similar to how and works for numbers: 4 and 5 is 5, but nil and 5 is nil
FastList:countGaps
Section titled “FastList:countGaps”FastList:countGaps(): numberReturn the number of gaps in the fastlist
FastList:scale
Section titled “FastList:scale”FastList:scale(k: number): FastListScales the fastlist by the given factor
FastList:cmul
Section titled “FastList:cmul”FastList:cmul(other: FastListOrVec): FastListMultiply (as complex numbers) the fastlist by another fastlist, element-wise
FastList:dot
Section titled “FastList:dot”FastList:dot(other: FastListOrVec): FastListCompute the dot product of the fastlist and another fastlist, element-wise
FastList:normalized
Section titled “FastList:normalized”FastList:normalized(): FastListNormalize each element in the fastlist
FastList:round
Section titled “FastList:round”FastList:round(digits_of_precision: number?): FastListRound each element in the fastlist to the given number of digits of precision
If digits_of_precision is not provided, it defaults to 0 (round to nearest integer)
FastList:floor
Section titled “FastList:floor”FastList:floor(): FastListRound down each element in the fastlist
FastList:ceil
Section titled “FastList:ceil”FastList:ceil(): FastListRound up each element in the fastlist
FastList:max
Section titled “FastList:max”FastList:max(other: FastListOrVec): FastListCompute the component-wise maximum of the fastlist and another fastlist
FastList:min
Section titled “FastList:min”FastList:min(other: FastListOrVec): FastListCompute the component-wise minimum of the fastlist and another fastlist
FastList:lerp
Section titled “FastList:lerp”FastList:lerp(other: FastListOrVec, k: number): FastListLinearly interpolate between the fastlist and another fastlist, element-wise
FastList:sign
Section titled “FastList:sign”FastList:sign(): FastListCompute the sign of every element in the fastlist
FastList:noise
Section titled “FastList:noise”FastList:noise(): FastListCompute simplex noise for each Vec2 in the fast list and return a new fast list where the Vec2 both contain the noise value.
FastList:worleyNoise
Section titled “FastList:worleyNoise”FastList:worleyNoise(): FastListCompute worley noise for each Vec2 in the fast list and return a new fast list
FastList:toPolar
Section titled “FastList:toPolar”FastList:toPolar(): FastListConvert each Vec2 in the fastlist to polar coordinates, element-wise So, the x component becomes the length and the y component becomes the angle
FastList:toCartesian
Section titled “FastList:toCartesian”FastList:toCartesian(): FastListConvert each Vec2 in the fastlist to Cartesian coordinates, element-wise So, the first component (the length) becomes x and the second component (the angle) becomes the y.
FastList:drawRects
Section titled “FastList:drawRects”FastList:drawRects()Take the fastlist by groups of 4 Vec2 (pos, size and 2 Vec2 for the color which is a Vec4). For each of these groups, perform the same operation as Graphics.drawRect() This is a lot faster than calling Graphics.drawRect() in a loop
FastList:drawQuads
Section titled “FastList:drawQuads”FastList:drawQuads()Take the fastlist by groups of 6 Vec2 (p1, p2, p3, p4 and 2 Vec2 for the color which is a Vec4).
For each of these groups, perform the same operation as Graphics.drawPolygon({p1, p2, p3, p4}, color)
This is a lot faster than calling Graphics.drawPolygon({p1, p2, p3, p4}, color) in a loop
FastList:drawImages
Section titled “FastList:drawImages”FastList:drawImages(image: Image.ImageResource, color: Vec4.Vec4?)Take the fastlist by groups of 2 Vec2 (pos and size).
For each of these groups, perform the same operation as image:drawImage(pos, size)
This is a lot faster than calling image:drawImage(pos, size) in a loop
FastList:drawImageParts
Section titled “FastList:drawImageParts”FastList:drawImageParts(image: Image.ImageResource, color: Vec4.Vec4?)Take the fastlist by groups of 6 Vec2 (p1, p2, p3, p4, src_pos and src_size).
For each of these groups, perform the same operation as image:drawImagePart(p1, p2, p3, p4, src_pos, src_size)
This is a lot faster than calling image:drawImagePart(p1, p2, p3, p4, src_pos, src_size) in a loop
FastList:drawTiles
Section titled “FastList:drawTiles”FastList:drawTiles(imageWithTileset: Image.ImageWithTileset, color: Vec4.Vec4?)Take the fastlist by groups of 3 Vec2 ( (tileId, empty), pos, size).
For each of these groups, perform the same operation as imageWithTileset:drawTile
This is a lot faster than calling imageWithTileset:drawTile in a loop
FastList:drawTileQuads
Section titled “FastList:drawTileQuads”FastList:drawTileQuads(imageWithTileset: Image.ImageWithTileset, color: Vec4.Vec4?)Take the fastlist by groups of 5 Vec2 ( (tileId, empty), p1, p2, p3, p4).
For each of these groups, perform the same operation as imageWithTileset:drawTileQuad
This is a lot faster than calling imageWithTileset:drawTileQuad in a loop
FastListOrVec
Section titled “FastListOrVec”type FastListOrVec = ...Constants
Section titled “Constants”fastlist.GAP
Section titled “fastlist.GAP”fastlist.GAP = ...A gap is a special value used to represent empty indices in a fastlist, similar to a nil in a table. When you filter a fastlist, you get a new fastlist with gaps.
Gaps are ‘contagious’, meaning that adding/substracting/multiplying, etc… a gap to a number will result in a gap.
You can remove gaps using the replaceGaps or otherwise function.
