@@ -13,18 +13,18 @@ List of functionality:
1313 - Interleaving (Interleave)
1414 - Patterned rearrangement (Twirl, Swap)
1515 - Fill: Fill with single value or pattern
16- - Aggregation: Sum, Min, Max with inverse step
16+ - Aggregation: Sum, Min, Max with inverse step, Average
1717 - Clamp/Clip: Bound all elements to a range
18+ - Chunking/Splitting: Split into fixed-size blocks
1819
1920TODO:
2021A test To know which one is the faster:
2122 - AssignIff(Max(Varptr(this[n]),Varptr(result))[0]<>this[n],Varptr(result))
2223 - c=a[n];out = c < out ? c Else out
2324
2425Missing or possible extensions:
25- - Aggregation: Average, complete min/max
2626 - Set operations: Union, Intersection, Difference, Unique/Distinct
27- - Chunking/Splitting: Split into fixed-size blocks, group by predicate
27+ - Chunking/Splitting: group by predicate
2828 - Flatten/Concat: For arrays-of-arrays
2929 - Sliding window: Windowed views for DSP, stats, ML
3030 - Reorganize the file, it's crappy actually
@@ -37,6 +37,129 @@ Completion:
3737 - For full "batteries-included" status, add functional, searching, set, and chunking utilities.
3838#end
3939
40+ 'TO TEST:
41+ Function SlidingRead<T,ChunkFormat>( this:T[],
42+
43+ 'data to write into:
44+ data:T Ptr,
45+
46+ 'dimension x,y of the unidimensional array:
47+ length:ChunkFormat Ptr,
48+ height:ChunkFormat Ptr,
49+
50+ 'position of the window:
51+ offsetX:UInt,
52+ offsetY:UInt,
53+ offsetLength:ChunkFormat,
54+ offsetHeight:ChunkFormat,
55+
56+ 'Pointer in the window:
57+ ptrX:UInt,
58+ ptrY:UInt )
59+
60+ 'Return the content
61+ data[0]=this[ SlidingGetPtr( Varptr(length),
62+ Varptr(height),
63+
64+ Varptr(offsetX),
65+ Varptr(offsetY),
66+ Varptr(offsetLength),
67+ Varptr(offsetHeight),
68+
69+ Varptr(ptrX),
70+ Varptr(ptrY)) ]
71+ End
72+
73+ 'TO TEST:
74+ Function SlidingWrite<T,ChunkFormat>( this:T[],
75+
76+ 'data to write/set:
77+ data:T Ptr,
78+
79+ 'dimension x,y of the unidimensional array:
80+ length:ChunkFormat Ptr,
81+ height:ChunkFormat Ptr,
82+
83+ 'position of the window:
84+ offsetX:UInt,
85+ offsetY:UInt,
86+ offsetLength:ChunkFormat,
87+ offsetHeight:ChunkFormat,
88+
89+ 'Pointer in the window:
90+ ptrX:UInt,
91+ ptrY:UInt )
92+
93+ 'Return the content
94+ data[0]=this[ SlidingGetPtr( Varptr(length),
95+ Varptr(height),
96+
97+ Varptr(offsetX),
98+ Varptr(offsetY),
99+ Varptr(offsetLength),
100+ Varptr(offsetHeight),
101+
102+ Varptr(ptrX),
103+ Varptr(ptrY)) ]
104+ End
105+
106+ Private 'Get a pointer for read/write in a virtual 2d array from a sliding window
107+ Function SlidingGetPtr<T,ChunkFormat>:ChunkFormat Ptr( this:T[],
108+
109+ 'dimension x,y of the unidimensional array:
110+ length:ChunkFormat Ptr,
111+ height:ChunkFormat Ptr,
112+
113+ 'position of the window:
114+ offsetX:UInt Ptr,
115+ offsetY:UInt Ptr,
116+ offsetLength:ChunkFormat Ptr,
117+ offsetHeight:ChunkFormat Ptr,
118+
119+ 'Pointer in the window:
120+ ptrX:UInt Ptr,
121+ ptrY:UInt Ptr )
122+
123+ 'The pointer is inside the window
124+ ptrX[0]=ptrX[0]>offsetLength[0] ? offsetLength[0] Else ptrX[0] 'guard
125+ ptrY[0]=ptrY[0]>offsetHeight[0] ? offsetHeight[0] Else ptrY[0] 'guard
126+
127+ 'The window is inside the 2d array
128+ offsetX[0]=offsetX[0]<=length[0]-offsetLength[0] ? offsetX[0] Else length[0]-offsetLength[0] 'guard, here <= avoids sub (edge case)
129+ offsetY[0]=offsetY[0]<=height[0]-offsetHeight[0] ? offsetY[0] Else height[0]-offsetHeight[0] 'guard, here <= avoids sub (edge case)
130+
131+ 'Shift the pointer inside the 2d array
132+ ptrX[0]+=offsetX[0]
133+ ptrY[0]+=offsetY[0]
134+
135+ 'Pointer from 2d array to 1d array
136+ ptrY[0]*=length[0]-1
137+ ptrX[0]+=ptrY[0]
138+
139+ 'Return the content
140+ Return ptrX
141+ End
142+ Public
143+
144+ 'TO TEST:
145+ Function Splitting<T>:T[][]( this:T[],chunksLength:Int )
146+ 'ToTest
147+ Local thisLength:=this.Length
148+ Local chunckNb:=Ceil(thisLength/chunksLength)
149+ Local result:T[]=T[chunckNb]
150+ Local toEnd:Int
151+ Local limEnd:Int
152+ For Local n:=0 Until chunckNb
153+ T[n]=T[chunksLength]
154+ limEnd=n*chunksLength+chunksLength
155+ toEnd=limEnd>thisLength ? thisLength Else limEnd
156+ this.CopyTo(result[n],n*chunksLength,0,toEnd)
157+ End
158+ Return result
159+ End
160+
161+ '---
162+
40163Function CopyTo<T>:T[]( this:T[],out:T[],srcOffset:Int=0,dstOffset:Int=0,count:Int=Null )
41164 '
42165 'A CopyTo who can be inlined
@@ -53,7 +176,7 @@ Function CopyTo<T>:T[]( this:T[],out:T[],srcOffset:Int=0,dstOffset:Int=0,count:I
53176 Return out
54177End
55178
56- #rem monkeydoc @pro Return a copy of the array is needed
179+ #rem wonkeydoc @pro Return a copy of the array is needed
57180#end
58181Function Cpynd<T>:T[]( data:T[],needed:Bool )
59182 'Both useful and elegant^^
95218
96219' --- Aggregations:
97220
98- Function Min<T>:T( this:T[],minlim:T=0 ) Where T Implements INumeric
99- Local thisSize:=this.Size
100- Local result:T=minlim 'implicit casting
101- For Local n:=0 Until thisSize
102- AssignIff(Min(Varptr(this[n]),Varptr(result))[0]<>this[n],Varptr(result))
103- End
104- End
105-
106- Function Max<T>:T( this:T[],maxlim:T=LongMax ) Where T Implements INumeric
107- Local thisSize:=this.Size
108- Local result:T=maxlim 'auto and implicit casting
109- For Local n:=0 Until thisSize
110- AssignIff(Max(Varptr(this[n]),Varptr(result))[0]<>this[n],Varptr(result))
111- End
112- End
113-
114- Function Min<T>:T(a:T[],inverse:Bool8) Where T Implements INumeric
221+ Function Min<T,Boolean>:T(a:T[],inverse:Boolean=False8,minlim:T=0) Where T Implements INumeric and
222+ Boolean=Bool Or Boolean=Bool8
115223
116224 'iDkP from GaragePixel
117- '2025-02-18
225+ '2025-02-18 - 2025-07-19
118226
119227 'Order n
120228
121- Local out:T
229+ Local out:T=minlim 'implicit casting
122230 Local len:=T.Length
123- Local c:=out
124231 Local starts:Int, ends:Int, shift:Int
125232
126233 If inverse=False8
@@ -134,54 +241,28 @@ Function Min<T>:T(a:T[],inverse:Bool8) Where T Implements INumeric
134241 End
135242
136243 For Local n:=starts Until ends Step shift
137- c=a[n];out = c < out ? c Else out
244+ ' c=a[n];out = c < out ? c Else out
245+ AssignIff(Min(Varptr(this[n]),Varptr(out))[0]<this[n],Varptr(out))
138246 End
139247
140248 Return out
141249End
142250
143- Function Max<T>:T(a:T[],inverse:Bool8) Where T Implements INumeric
144-
251+ Function Max<T,Boolean>:T( this:T[],inverse:Boolean=False8,maxlim:T=LongMax ) Where T Implements INumeric And
252+ Boolean=Bool Or Boolean=Bool8
253+
145254 'iDkP from GaragePixel
146- '2025-02-18
255+ '2025-02-18 - 2025-07-19
147256
148257 'Order n
149-
150- Local out:T
151- Local len:=T.Length
152- Local c:=out
153- Local starts:Int, ends:Int, shift:Int
154-
155- If last=False8
156- starts=len
157- ends=0
158- shift=-1
159- Else
160- starts=0
161- ends=len
162- shift=1
163- End
164-
165- For Local n:=starts Until ends Step shift
166- c=a[n];out = c>=out ? c Else out
167- End
168-
169- Return out
170- End
171258
172- Function Min<T>:T(a:T[],inverse:Bool) Where T Implements INumeric
259+ Local out:T=maxlim 'auto and implicit casting
173260
174- 'iDkP from GaragePixel
175- '2025-02-18
176-
177- 'Order n
178-
179- Local out:T
261+ 'Local out:T
180262 Local len:=T.Length
181- Local c:=out
182263 Local starts:Int, ends:Int, shift:Int
183264
184- If inverse=False
265+ If inverse=False8
185266 starts=len
186267 ends=0
187268 shift=-1
@@ -190,42 +271,14 @@ Function Min<T>:T(a:T[],inverse:Bool) Where T Implements INumeric
190271 ends=len
191272 shift=1
192273 End
193-
194- For Local n:=starts Until ends Step shift
195- c=a[n];out = c < out ? c Else out
196- End
197-
198- Return out
199- End
200274
201- Function Max<T>:T(a:T[],inverse:Bool) Where T Implements INumeric
202-
203- 'iDkP from GaragePixel
204- '2025-02-18
205-
206- 'Order n
207-
208- Local out:T
209- Local len:=T.Length
210- Local c:=out
211- Local starts:Int, ends:Int, shift:Int
212-
213- If last=False
214- starts=len
215- ends=0
216- shift=-1
217- Else
218- starts=0
219- ends=len
220- shift=1
221- End
222-
223275 For Local n:=starts Until ends Step shift
224- c=a[n];out = c>=out ? c Else out
276+ 'c=a[n];out = c>=out ? c Else out
277+ AssignIff(Max(Varptr(this[n]),Varptr(out))[0]<>this[n],Varptr(out))
225278 End
226279
227280 Return out
228- End
281+ End
229282
230283Function Sum<T>:T( this:T[] ) Where T Implements INumeric
231284 'Unsafe version, potentially faster
@@ -254,7 +307,20 @@ Function SumSafe<T>:T( this:T[] ) Where T Implements INumeric
254307 Return Null
255308End
256309
257- #rem monkeydoc @expert The length of the array.
310+ #rem wonkeydoc Average the values from a stack.
311+ @author iDkP from GaragePixel
312+ @since 2025-07-19
313+ #end
314+ Function Avr<T>:T( this:T[] ) Where T Implements INumeric And T<>Bool
315+ Local s:Double=0 'Sure that we'll fall in the larger range
316+ Local len:=this.Size
317+ For Local n:T=0 Until len
318+ s+=this[n]
319+ End
320+ Return s/len
321+ End
322+
323+ #rem wonkeydoc @expert The length of the array.
258324In the case of multi-dimensional arrays, the length of the array is the product
259325of the sizes of all dimensions.
260326#end
319385
320386'------------
321387
322- #rem monkeydoc hidden Merges two sorted arrays into a single sorted array.
388+ #rem wonkeydoc hidden Merges two sorted arrays into a single sorted array.
323389Merges the elements of arrays a and b, preserving order and stability.
324390
325391On elements:
@@ -364,7 +430,7 @@ Function Merge<T>:T[]( a:T[],b:T[] )
364430 Return res
365431End
366432
367- #rem monkeydoc Reverses an array in place.
433+ #rem wonkeydoc Reverses an array in place.
368434Swaps elements from both ends toward the center, modifying the original array.
369435Used for reversing descending runs in TimSort_g and similar algorithms.
370436
@@ -392,7 +458,7 @@ Function Reverse<T>:T[]( arr:T[],copy:Bool=True )
392458 Return out
393459End
394460
395- #rem monkeydoc Reverses a subrange of an array in place.
461+ #rem wonkeydoc Reverses a subrange of an array in place.
396462Swaps elements between the specified start and end indices, reversing the segment.
397463Useful for in-place reversal of partial runs or subsequences (e.g., in TimSort_g).
398464
@@ -630,7 +696,7 @@ Function Tile<T>:T[]( this:T[],factor:Int=0,copy:Bool=True )
630696 Return this
631697End
632698
633- #rem monkeydoc Rot - Cyclically rotates an array left/right by shift
699+ #rem wonkeydoc Rot - Cyclically rotates an array left/right by shift
634700@author: iDkP for GaragePixel,
635701@since 2021?
636702#end
0 commit comments