diff --git a/modules/std/syntax/primitives/arrays.wx b/modules/std/syntax/primitives/arrays/arrays.wx similarity index 87% rename from modules/std/syntax/primitives/arrays.wx rename to modules/std/syntax/primitives/arrays/arrays.wx index a118cd30..ab69728f 100644 --- a/modules/std/syntax/primitives/arrays.wx +++ b/modules/std/syntax/primitives/arrays/arrays.wx @@ -4,6 +4,7 @@ Namespace std.syntax #rem MiniLibrary: Arrays Since 2021? - 2025-02-02 - 2025-06-19 - 2025-06-21 (Aida 4) Author: iDkP from GaragePixel +#end List of functionality: - Copying (CopyTo for inlined usage) @@ -16,6 +17,7 @@ List of functionality: - Aggregation: Sum, Min, Max with inverse step, Average - Clamp/Clip: Bound all elements to a range - Chunking/Splitting: Split into fixed-size blocks + - Sliding window: Windowed views for DSP, stats, ML TODO: A test To know which one is the faster: @@ -26,7 +28,6 @@ Missing or possible extensions: - Set operations: Union, Intersection, Difference, Unique/Distinct - Chunking/Splitting: group by predicate - Flatten/Concat: For arrays-of-arrays - - Sliding window: Windowed views for DSP, stats, ML - Reorganize the file, it's crappy actually Possible application fields: @@ -37,110 +38,6 @@ Completion: - For full "batteries-included" status, add functional, searching, set, and chunking utilities. #end -'TO TEST: -Function SlidingRead( this:T[], - - 'data to write into: - data:T Ptr, - - 'dimension x,y of the unidimensional array: - length:ChunkFormat Ptr, - height:ChunkFormat Ptr, - - 'position of the window: - offsetX:UInt, - offsetY:UInt, - offsetLength:ChunkFormat, - offsetHeight:ChunkFormat, - - 'Pointer in the window: - ptrX:UInt, - ptrY:UInt ) - - 'Return the content - data[0]=this[ SlidingGetPtr( Varptr(length), - Varptr(height), - - Varptr(offsetX), - Varptr(offsetY), - Varptr(offsetLength), - Varptr(offsetHeight), - - Varptr(ptrX), - Varptr(ptrY)) ] -End - -'TO TEST: -Function SlidingWrite( this:T[], - - 'data to write/set: - data:T Ptr, - - 'dimension x,y of the unidimensional array: - length:ChunkFormat Ptr, - height:ChunkFormat Ptr, - - 'position of the window: - offsetX:UInt, - offsetY:UInt, - offsetLength:ChunkFormat, - offsetHeight:ChunkFormat, - - 'Pointer in the window: - ptrX:UInt, - ptrY:UInt ) - - 'Return the content - data[0]=this[ SlidingGetPtr( Varptr(length), - Varptr(height), - - Varptr(offsetX), - Varptr(offsetY), - Varptr(offsetLength), - Varptr(offsetHeight), - - Varptr(ptrX), - Varptr(ptrY)) ] -End - -Private 'Get a pointer for read/write in a virtual 2d array from a sliding window -Function SlidingGetPtr:ChunkFormat Ptr( this:T[], - - 'dimension x,y of the unidimensional array: - length:ChunkFormat Ptr, - height:ChunkFormat Ptr, - - 'position of the window: - offsetX:UInt Ptr, - offsetY:UInt Ptr, - offsetLength:ChunkFormat Ptr, - offsetHeight:ChunkFormat Ptr, - - 'Pointer in the window: - ptrX:UInt Ptr, - ptrY:UInt Ptr ) - - 'The pointer is inside the window - ptrX[0]=ptrX[0]>offsetLength[0] ? offsetLength[0] Else ptrX[0] 'guard - ptrY[0]=ptrY[0]>offsetHeight[0] ? offsetHeight[0] Else ptrY[0] 'guard - - 'The window is inside the 2d array - offsetX[0]=offsetX[0]<=length[0]-offsetLength[0] ? offsetX[0] Else length[0]-offsetLength[0] 'guard, here <= avoids sub (edge case) - offsetY[0]=offsetY[0]<=height[0]-offsetHeight[0] ? offsetY[0] Else height[0]-offsetHeight[0] 'guard, here <= avoids sub (edge case) - - 'Shift the pointer inside the 2d array - ptrX[0]+=offsetX[0] - ptrY[0]+=offsetY[0] - - 'Pointer from 2d array to 1d array - ptrY[0]*=length[0]-1 - ptrX[0]+=ptrY[0] - - 'Return the content - Return ptrX -End -Public - 'TO TEST: Function Splitting:T[][]( this:T[],chunksLength:Int ) 'ToTest diff --git a/modules/std/syntax/primitives/arrays/slidings.wx b/modules/std/syntax/primitives/arrays/slidings.wx new file mode 100644 index 00000000..1a1c99bd --- /dev/null +++ b/modules/std/syntax/primitives/arrays/slidings.wx @@ -0,0 +1,221 @@ + +Namespace stdlib.syntax + +#rem MiniLibrary: Arrays slicing window +Since 2025-07-20 (Aida 4) +Author: iDkP from GaragePixel +#end + +'Extract (copy) and virtual 2d array window +Function SlidingExtract( src:T[], 'The source + srcLength:ChunkFormat Ptr, + srcHeight:ChunkFormat Ptr, + + 'position of the window to extract: + srcOffsetX:UInt, + srcOffsetY:UInt, + srcOffsetLength:ChunkFormat, + srcOffsetHeight:ChunkFormat, + + 'destination array reference + dst:T[] ) + + #rem + + - Example: + scr: + ****** length: 6 + ***12* height: 3 + ***34* window: + offsetx: 4 + offsety: 2 + offsetLength: 2 + offsetHeight: 2 + result: + 1234 + #end +End + +'Insert (paste) a virtual 2d array window +Function SlidingInsert( src:T[], 'The source + srcLength:ChunkFormat Ptr, + srcHeight:ChunkFormat Ptr, + + 'destination array reference + dst:T[], + + 'position of the window to extract: + dstOffsetX:UInt, + dstOffsetY:UInt, + dstOffsetLength:ChunkFormat, + dstOffsetHeight:ChunkFormat ) + + #rem + + - Example: + scr: + 1234 -> 12 srcLength: 2 + 34 srcHeight: 2 + + result (dest): + ****** dstLength: 6 + ***12* dstHeight: 3 + ***34* window: + dstOffsetx: 4 + dstOffsety: 2 + dstOffsetLength: 2 + dstOffsetHeight: 2 + #end +End + +'Copy a virtual 2d array to another virtual 2d array +Function SlidingCopyTo( src:T[], 'The source + srcLength:ChunkFormat Ptr, + srcHeight:ChunkFormat Ptr, + + 'position of the scr window: + srcOffsetX:UInt, + srcOffsetY:UInt, + srcOffsetLength:ChunkFormat, + srcOffsetHeight:ChunkFormat, + + dest:T[], 'the destination array + dstLength:ChunkFormat Ptr, + dstHeight:ChunkFormat Ptr, + + 'position of the dest window: + dstOffsetX:UInt, + dstOffsetY:UInt ) + #rem + + - Example: + scr: + ****** length: 6 + ***00* height: 3 + ***00* window: + offsetx: 4 + offsety: 2 + offsetLength: 2 + offsetHeight: 2 + dest + + ****** length: 6 + **X*** height: 4 + ****** window: + ****** offsetx: 3 + offsety: 2 + + result: + ****** + **00** + **00** + ****** + #end +End + +'Read a window from a virtual 2d array to another virtual 2d array +Function SlidingRead( this:T[], + + 'data to write into: + data:T Ptr, + + 'dimension x,y of the unidimensional array: + length:ChunkFormat Ptr, + height:ChunkFormat Ptr, + + 'position of the window: + offsetX:UInt, + offsetY:UInt, + offsetLength:ChunkFormat, + offsetHeight:ChunkFormat, + + 'Pointer in the window: + ptrX:UInt, + ptrY:UInt ) + + 'Return the content + data[0]=this[ SlidingGetPtr( this, + + Varptr(length), + Varptr(height), + + Varptr(offsetX), + Varptr(offsetY), + Varptr(offsetLength), + Varptr(offsetHeight), + + Varptr(ptrX), + Varptr(ptrY)) ] +End + +'Write a window from a virtual 2d array to another virtual 2d array +Function SlidingWrite( this:T[], + + 'data to write/set: + data:T Ptr, + + 'dimension x,y of the unidimensional array: + length:ChunkFormat Ptr, + height:ChunkFormat Ptr, + + 'position of the window: + offsetX:UInt, + offsetY:UInt, + offsetLength:ChunkFormat, + offsetHeight:ChunkFormat, + + 'Pointer in the window: + ptrX:UInt, + ptrY:UInt ) + + 'Return the content + data[0]=this[ SlidingGetPtr( this, + + Varptr(length), + Varptr(height), + + Varptr(offsetX), + Varptr(offsetY), + Varptr(offsetLength), + Varptr(offsetHeight), + + Varptr(ptrX), + Varptr(ptrY)) ] +End + +'Get a pointer for read/write in a virtual 2d array from a sliding window +Function SlidingGetPtr:ChunkFormat Ptr( this:T[], + + 'dimension x,y of the unidimensional array: + length:ChunkFormat Ptr, + height:ChunkFormat Ptr, + + 'position of the window: + offsetX:UInt Ptr, + offsetY:UInt Ptr, + offsetLength:ChunkFormat Ptr, + offsetHeight:ChunkFormat Ptr, + + 'Pointer in the window: + ptrX:UInt Ptr, + ptrY:UInt Ptr ) + + 'The pointer is inside the window + ptrX[0]=ptrX[0]>offsetLength[0] ? offsetLength[0] Else ptrX[0] 'guard + ptrY[0]=ptrY[0]>offsetHeight[0] ? offsetHeight[0] Else ptrY[0] 'guard + + 'The window is inside the 2d array + offsetX[0]=offsetX[0]<=length[0]-offsetLength[0] ? offsetX[0] Else length[0]-offsetLength[0] 'guard, here <= avoids sub (edge case) + offsetY[0]=offsetY[0]<=height[0]-offsetHeight[0] ? offsetY[0] Else height[0]-offsetHeight[0] 'guard, here <= avoids sub (edge case) + + 'Shift the pointer inside the 2d array + ptrX[0]+=offsetX[0] + ptrY[0]+=offsetY[0] + + 'Pointer from 2d array to 1d array + ptrY[0]*=length[0]-1 + ptrX[0]+=ptrY[0] + + 'Return the content + Return ptrX +End diff --git a/modules/std/syntax/syntax.xw b/modules/std/syntax/syntax.xw index 9ed1ad37..90eb6ca7 100644 --- a/modules/std/syntax/syntax.xw +++ b/modules/std/syntax/syntax.xw @@ -5,7 +5,8 @@ Namespace std.syntax #Import "consts/logicals" #Import "consts/strings" -#Import "primitives/arrays" +#Import "primitives/arrays/arrays" +#Import "primitives/arrays/slidings" #Import "primitives/variants/variantcasts" #Import "primitives/variants/meta" #Import "primitives/variants/makevars"