Skip to content

Fastlist

const fastlist = require("@vectarine/fastlist")

A module for writing high performance code by storing data in a compact way

  • 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
fastlist.newLinspace(min: Vec.Vec2, max: Vec.Vec2, step: Vec.Vec2): FastList

Return a list of Vec2 that are linearly spaced between min and max in a 2D grid

fastlist.zeros(size: number): FastList

Return a list of Vec2 of size size filled with Vec2.ZERO

fastlist.fromValue(value: Vec.Vec2, size: number): FastList

Return a list of Vec2 of size size filled with value

fastlist.fromTable(tbl: { Vec.Vec2 }): FastList

Convert a table of Vec2 into a 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(index: number): Vec.Vec2

Returns the Vec2 at the given index

FastList:concat(other: FastList): FastList

Returns 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(): { Vec.Vec2 }

Returns a table of Vec2 from the fast list

FastList:toNumberTableX(): { number }

Returns a table of numbers from the fast list, by taking the x component of each Vec2

FastList:forEach(fn: (Vec.Vec2, number) -> ()): FastList

Apply 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(count: number): FastList

Create a new FastList where every Vec2 is repeated count times A list like XYZ with a count of 2 becomes XXYYZZ

FastList:repeated(count: number): FastList

Create a new FastList where the list is repeated count times A list like XYZ with a count of 2 becomes XYZXYZ

FastList:weave(others: { FastList }): FastList

Create a new Fastlist by weaving elements from multiple fastlists If the fastlists are ABC, DEF and GHI, the result is ADGBCEFH

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(
mask: FastList,
min: number | FastList,
max: number | FastList,
): FastList

Filter 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]
end
end

The 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(
mask: FastList,
min: number | FastList,
max: number | FastList,
): FastList

Filter 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(replacement: Vec.Vec2): FastList

Replace gaps in the Fastlist with the replacement value

FastList:filterGaps(): FastList

Remove gaps from the Fastlist, resulting in a shorter list

FastList:otherwise(other: FastList): FastList

Returns 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(other: FastList): FastList

Returns 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(): number

Return the number of gaps in the fastlist

FastList:scale(k: number): FastList

Scales the fastlist by the given factor

FastList:cmul(other: FastListOrVec): FastList

Multiply (as complex numbers) the fastlist by another fastlist, element-wise

FastList:dot(other: FastListOrVec): FastList

Compute the dot product of the fastlist and another fastlist, element-wise

FastList:normalized(): FastList

Normalize each element in the fastlist

FastList:round(digits_of_precision: number?): FastList

Round 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(): FastList

Round down each element in the fastlist

FastList:ceil(): FastList

Round up each element in the fastlist

FastList:max(other: FastListOrVec): FastList

Compute the component-wise maximum of the fastlist and another fastlist

FastList:min(other: FastListOrVec): FastList

Compute the component-wise minimum of the fastlist and another fastlist

FastList:lerp(other: FastListOrVec, k: number): FastList

Linearly interpolate between the fastlist and another fastlist, element-wise

FastList:sign(): FastList

Compute the sign of every element in the fastlist

FastList:noise(): FastList

Compute 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(): FastList

Compute worley noise for each Vec2 in the fast list and return a new fast list

FastList:toPolar(): FastList

Convert 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(): FastList

Convert 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()

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()

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(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(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(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(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

type FastListOrVec = ...
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.