|
| 1 | + |
| 2 | +Namespace stdlib.syntax |
| 3 | + |
| 4 | +#rem MiniLibrary: Arrays slicing window |
| 5 | +Since 2025-07-20 (Aida 4) |
| 6 | +Author: iDkP from GaragePixel |
| 7 | +#end |
| 8 | + |
| 9 | +'Extract (copy) and virtual 2d array window |
| 10 | +Function SlidingExtract<T,ChunkFormat>( src:T[], 'The source |
| 11 | + srcLength:ChunkFormat Ptr, |
| 12 | + srcHeight:ChunkFormat Ptr, |
| 13 | + |
| 14 | + 'position of the window to extract: |
| 15 | + srcOffsetX:UInt, |
| 16 | + srcOffsetY:UInt, |
| 17 | + srcOffsetLength:ChunkFormat, |
| 18 | + srcOffsetHeight:ChunkFormat, |
| 19 | + |
| 20 | + 'destination array reference |
| 21 | + dst:T[] ) |
| 22 | + |
| 23 | + #rem |
| 24 | + |
| 25 | + - Example: |
| 26 | + scr: |
| 27 | + ****** length: 6 |
| 28 | + ***12* height: 3 |
| 29 | + ***34* window: |
| 30 | + offsetx: 4 |
| 31 | + offsety: 2 |
| 32 | + offsetLength: 2 |
| 33 | + offsetHeight: 2 |
| 34 | + result: |
| 35 | + 1234 |
| 36 | + #end |
| 37 | +End |
| 38 | + |
| 39 | +'Insert (paste) a virtual 2d array window |
| 40 | +Function SlidingInsert<T,ChunkFormat>( src:T[], 'The source |
| 41 | + srcLength:ChunkFormat Ptr, |
| 42 | + srcHeight:ChunkFormat Ptr, |
| 43 | + |
| 44 | + 'destination array reference |
| 45 | + dst:T[], |
| 46 | + |
| 47 | + 'position of the window to extract: |
| 48 | + dstOffsetX:UInt, |
| 49 | + dstOffsetY:UInt, |
| 50 | + dstOffsetLength:ChunkFormat, |
| 51 | + dstOffsetHeight:ChunkFormat ) |
| 52 | + |
| 53 | + #rem |
| 54 | + |
| 55 | + - Example: |
| 56 | + scr: |
| 57 | + 1234 -> 12 srcLength: 2 |
| 58 | + 34 srcHeight: 2 |
| 59 | + |
| 60 | + result (dest): |
| 61 | + ****** dstLength: 6 |
| 62 | + ***12* dstHeight: 3 |
| 63 | + ***34* window: |
| 64 | + dstOffsetx: 4 |
| 65 | + dstOffsety: 2 |
| 66 | + dstOffsetLength: 2 |
| 67 | + dstOffsetHeight: 2 |
| 68 | + #end |
| 69 | +End |
| 70 | + |
| 71 | +'Copy a virtual 2d array to another virtual 2d array |
| 72 | +Function SlidingCopyTo<T,ChunkFormat>( src:T[], 'The source |
| 73 | + srcLength:ChunkFormat Ptr, |
| 74 | + srcHeight:ChunkFormat Ptr, |
| 75 | + |
| 76 | + 'position of the scr window: |
| 77 | + srcOffsetX:UInt, |
| 78 | + srcOffsetY:UInt, |
| 79 | + srcOffsetLength:ChunkFormat, |
| 80 | + srcOffsetHeight:ChunkFormat, |
| 81 | + |
| 82 | + dest:T[], 'the destination array |
| 83 | + dstLength:ChunkFormat Ptr, |
| 84 | + dstHeight:ChunkFormat Ptr, |
| 85 | + |
| 86 | + 'position of the dest window: |
| 87 | + dstOffsetX:UInt, |
| 88 | + dstOffsetY:UInt ) |
| 89 | + #rem |
| 90 | + |
| 91 | + - Example: |
| 92 | + scr: |
| 93 | + ****** length: 6 |
| 94 | + ***00* height: 3 |
| 95 | + ***00* window: |
| 96 | + offsetx: 4 |
| 97 | + offsety: 2 |
| 98 | + offsetLength: 2 |
| 99 | + offsetHeight: 2 |
| 100 | + dest |
| 101 | + |
| 102 | + ****** length: 6 |
| 103 | + **X*** height: 4 |
| 104 | + ****** window: |
| 105 | + ****** offsetx: 3 |
| 106 | + offsety: 2 |
| 107 | + |
| 108 | + result: |
| 109 | + ****** |
| 110 | + **00** |
| 111 | + **00** |
| 112 | + ****** |
| 113 | + #end |
| 114 | +End |
| 115 | + |
| 116 | +'Read a window from a virtual 2d array to another virtual 2d array |
| 117 | +Function SlidingRead<T,ChunkFormat>( this:T[], |
| 118 | + |
| 119 | + 'data to write into: |
| 120 | + data:T Ptr, |
| 121 | + |
| 122 | + 'dimension x,y of the unidimensional array: |
| 123 | + length:ChunkFormat Ptr, |
| 124 | + height:ChunkFormat Ptr, |
| 125 | + |
| 126 | + 'position of the window: |
| 127 | + offsetX:UInt, |
| 128 | + offsetY:UInt, |
| 129 | + offsetLength:ChunkFormat, |
| 130 | + offsetHeight:ChunkFormat, |
| 131 | + |
| 132 | + 'Pointer in the window: |
| 133 | + ptrX:UInt, |
| 134 | + ptrY:UInt ) |
| 135 | + |
| 136 | + 'Return the content |
| 137 | + data[0]=this[ SlidingGetPtr( this, |
| 138 | + |
| 139 | + Varptr(length), |
| 140 | + Varptr(height), |
| 141 | + |
| 142 | + Varptr(offsetX), |
| 143 | + Varptr(offsetY), |
| 144 | + Varptr(offsetLength), |
| 145 | + Varptr(offsetHeight), |
| 146 | + |
| 147 | + Varptr(ptrX), |
| 148 | + Varptr(ptrY)) ] |
| 149 | +End |
| 150 | + |
| 151 | +'Write a window from a virtual 2d array to another virtual 2d array |
| 152 | +Function SlidingWrite<T,ChunkFormat>( this:T[], |
| 153 | + |
| 154 | + 'data to write/set: |
| 155 | + data:T Ptr, |
| 156 | + |
| 157 | + 'dimension x,y of the unidimensional array: |
| 158 | + length:ChunkFormat Ptr, |
| 159 | + height:ChunkFormat Ptr, |
| 160 | + |
| 161 | + 'position of the window: |
| 162 | + offsetX:UInt, |
| 163 | + offsetY:UInt, |
| 164 | + offsetLength:ChunkFormat, |
| 165 | + offsetHeight:ChunkFormat, |
| 166 | + |
| 167 | + 'Pointer in the window: |
| 168 | + ptrX:UInt, |
| 169 | + ptrY:UInt ) |
| 170 | + |
| 171 | + 'Return the content |
| 172 | + data[0]=this[ SlidingGetPtr( this, |
| 173 | + |
| 174 | + Varptr(length), |
| 175 | + Varptr(height), |
| 176 | + |
| 177 | + Varptr(offsetX), |
| 178 | + Varptr(offsetY), |
| 179 | + Varptr(offsetLength), |
| 180 | + Varptr(offsetHeight), |
| 181 | + |
| 182 | + Varptr(ptrX), |
| 183 | + Varptr(ptrY)) ] |
| 184 | +End |
| 185 | + |
| 186 | +'Get a pointer for read/write in a virtual 2d array from a sliding window |
| 187 | +Function SlidingGetPtr<T,ChunkFormat>:ChunkFormat Ptr( this:T[], |
| 188 | + |
| 189 | + 'dimension x,y of the unidimensional array: |
| 190 | + length:ChunkFormat Ptr, |
| 191 | + height:ChunkFormat Ptr, |
| 192 | + |
| 193 | + 'position of the window: |
| 194 | + offsetX:UInt Ptr, |
| 195 | + offsetY:UInt Ptr, |
| 196 | + offsetLength:ChunkFormat Ptr, |
| 197 | + offsetHeight:ChunkFormat Ptr, |
| 198 | + |
| 199 | + 'Pointer in the window: |
| 200 | + ptrX:UInt Ptr, |
| 201 | + ptrY:UInt Ptr ) |
| 202 | + |
| 203 | + 'The pointer is inside the window |
| 204 | + ptrX[0]=ptrX[0]>offsetLength[0] ? offsetLength[0] Else ptrX[0] 'guard |
| 205 | + ptrY[0]=ptrY[0]>offsetHeight[0] ? offsetHeight[0] Else ptrY[0] 'guard |
| 206 | + |
| 207 | + 'The window is inside the 2d array |
| 208 | + offsetX[0]=offsetX[0]<=length[0]-offsetLength[0] ? offsetX[0] Else length[0]-offsetLength[0] 'guard, here <= avoids sub (edge case) |
| 209 | + offsetY[0]=offsetY[0]<=height[0]-offsetHeight[0] ? offsetY[0] Else height[0]-offsetHeight[0] 'guard, here <= avoids sub (edge case) |
| 210 | + |
| 211 | + 'Shift the pointer inside the 2d array |
| 212 | + ptrX[0]+=offsetX[0] |
| 213 | + ptrY[0]+=offsetY[0] |
| 214 | + |
| 215 | + 'Pointer from 2d array to 1d array |
| 216 | + ptrY[0]*=length[0]-1 |
| 217 | + ptrX[0]+=ptrY[0] |
| 218 | + |
| 219 | + 'Return the content |
| 220 | + Return ptrX |
| 221 | +End |
0 commit comments