@@ -37,54 +37,14 @@ export class StaticArray<T> {
3737 return out ;
3838 }
3939
40+ /** @deprecated Please use source.concat<StaticArray<T>> instead. */
4041 static concat < T > ( source : StaticArray < T > , other : StaticArray < T > ) : StaticArray < T > {
41- var sourceLen = source . length ;
42- var otherLen = other . length ;
43- var outLen = sourceLen + otherLen ;
44- if ( < u32 > outLen > < u32 > BLOCK_MAXSIZE >>> alignof < T > ( ) ) throw new Error ( E_INVALIDLENGTH ) ;
45- var out = changetype < StaticArray < T > > ( __new ( < usize > outLen << alignof < T > ( ) , idof < StaticArray < T > > ( ) ) ) ;
46- var outStart = changetype < usize > ( out ) ;
47- var sourceSize = < usize > sourceLen << alignof < T > ( ) ;
48- if ( isManaged < T > ( ) ) {
49- for ( let offset : usize = 0 ; offset < sourceSize ; offset += sizeof < T > ( ) ) {
50- let ref = load < usize > ( changetype < usize > ( source ) + offset ) ;
51- store < usize > ( outStart + offset , ref ) ;
52- __link ( changetype < usize > ( out ) , ref , true ) ;
53- }
54- outStart += sourceSize ;
55- let otherSize = < usize > otherLen << alignof < T > ( ) ;
56- for ( let offset : usize = 0 ; offset < otherSize ; offset += sizeof < T > ( ) ) {
57- let ref = load < usize > ( changetype < usize > ( other ) + offset ) ;
58- store < usize > ( outStart + offset , ref ) ;
59- __link ( changetype < usize > ( out ) , ref , true ) ;
60- }
61- } else {
62- memory . copy ( outStart , changetype < usize > ( source ) , sourceSize ) ;
63- memory . copy ( outStart + sourceSize , changetype < usize > ( other ) , < usize > otherLen << alignof < T > ( ) ) ;
64- }
65- return out ;
42+ return source . concat < StaticArray < T > > ( other ) ;
6643 }
6744
45+ /** @deprecated Please use source.slice<StaticArray<T>> instead. */
6846 static slice < T > ( source : StaticArray < T > , start : i32 = 0 , end : i32 = i32 . MAX_VALUE ) : StaticArray < T > {
69- var length = source . length ;
70- start = start < 0 ? max ( start + length , 0 ) : min ( start , length ) ;
71- end = end < 0 ? max ( end + length , 0 ) : min ( end , length ) ;
72- length = max ( end - start , 0 ) ;
73- var sliceSize = < usize > length << alignof < T > ( ) ;
74- var slice = changetype < StaticArray < T > > ( __new ( sliceSize , idof < StaticArray < T > > ( ) ) ) ;
75- var sourcePtr = changetype < usize > ( source ) + ( < usize > start << alignof < T > ( ) ) ;
76- if ( isManaged < T > ( ) ) {
77- let off : usize = 0 ;
78- while ( off < sliceSize ) {
79- let ref = load < usize > ( sourcePtr + off ) ;
80- store < usize > ( changetype < usize > ( slice ) + off , ref ) ;
81- __link ( changetype < usize > ( slice ) , ref , true ) ;
82- off += sizeof < usize > ( ) ;
83- }
84- } else {
85- memory . copy ( changetype < usize > ( slice ) , sourcePtr , sliceSize ) ;
86- }
87- return slice ;
47+ return source . slice < StaticArray < T > > ( start , end ) ;
8848 }
8949
9050 constructor ( length : i32 ) {
@@ -210,57 +170,112 @@ export class StaticArray<T> {
210170 return - 1 ;
211171 }
212172
213- concat ( other : Array < T > ) : Array < T > {
214- var thisLen = this . length ;
215- var otherLen = other . length ;
216- var outLen = thisLen + otherLen ;
217- if ( < u32 > outLen > < u32 > BLOCK_MAXSIZE >>> alignof < T > ( ) ) throw new Error ( E_INVALIDLENGTH ) ;
218- var out = changetype < Array < T > > ( __newArray ( outLen , alignof < T > ( ) , idof < Array < T > > ( ) ) ) ;
219- var outStart = out . dataStart ;
220- var thisSize = < usize > thisLen << alignof < T > ( ) ;
221- if ( isManaged < T > ( ) ) {
173+ concat < U extends ArrayLike < T > = Array < T > > ( other : U ) : U {
174+ let sourceLen = this . length ;
175+ let otherLen = other . length ;
176+ let outLen = sourceLen + otherLen ;
177+ if ( < u32 > outLen > < u32 > BLOCK_MAXSIZE >>> alignof < T > ( ) ) {
178+ throw new Error ( E_INVALIDLENGTH ) ;
179+ }
180+ let sourceSize = < usize > sourceLen << alignof < T > ( ) ;
181+ let out ! : U ;
182+
183+ if ( out instanceof Array < T > ) {
184+ out = changetype < U > ( __newArray ( outLen , alignof < T > ( ) , idof < Array < T > > ( ) ) ) ;
185+ let outStart = changetype < Array < T > > ( out ) . dataStart ;
186+ let otherStart = changetype < Array < T > > ( other ) . dataStart ;
222187 let thisStart = changetype < usize > ( this ) ;
223- for ( let offset : usize = 0 ; offset < thisSize ; offset += sizeof < T > ( ) ) {
224- let ref = load < usize > ( thisStart + offset ) ;
225- store < usize > ( outStart + offset , ref ) ;
226- __link ( changetype < usize > ( out ) , ref , true ) ;
188+
189+ if ( isManaged < T > ( ) ) {
190+ for ( let offset : usize = 0 ; offset < sourceSize ; offset += sizeof < T > ( ) ) {
191+ let ref = load < usize > ( thisStart + offset ) ;
192+ store < usize > ( outStart + offset , ref ) ;
193+ __link ( changetype < usize > ( out ) , ref , true ) ;
194+ }
195+ outStart += sourceSize ;
196+ let otherSize = < usize > otherLen << alignof < T > ( ) ;
197+ for ( let offset : usize = 0 ; offset < otherSize ; offset += sizeof < T > ( ) ) {
198+ let ref = load < usize > ( otherStart + offset ) ;
199+ store < usize > ( outStart + offset , ref ) ;
200+ __link ( changetype < usize > ( out ) , ref , true ) ;
201+ }
202+ } else {
203+ memory . copy ( outStart , thisStart , sourceSize ) ;
204+ memory . copy ( outStart + sourceSize , otherStart , < usize > otherLen << alignof < T > ( ) ) ;
227205 }
228- outStart += thisSize ;
229- let otherStart = other . dataStart ;
230- let otherSize = < usize > otherLen << alignof < T > ( ) ;
231- for ( let offset : usize = 0 ; offset < otherSize ; offset += sizeof < T > ( ) ) {
232- let ref = load < usize > ( otherStart + offset ) ;
233- store < usize > ( outStart + offset , ref ) ;
234- __link ( changetype < usize > ( out ) , ref , true ) ;
206+ } else if ( out instanceof StaticArray < T > ) {
207+ out = changetype < U > ( __new ( < usize > outLen << alignof < T > ( ) , idof < StaticArray < T > > ( ) ) ) ;
208+ let outStart = changetype < usize > ( out ) ;
209+ let otherStart = changetype < usize > ( other ) ;
210+ let thisStart = changetype < usize > ( this ) ;
211+
212+ if ( isManaged < T > ( ) ) {
213+ for ( let offset : usize = 0 ; offset < sourceSize ; offset += sizeof < T > ( ) ) {
214+ let ref = load < usize > ( thisStart + offset ) ;
215+ store < usize > ( outStart + offset , ref ) ;
216+ __link ( outStart , ref , true ) ;
217+ }
218+ outStart += sourceSize ;
219+ let otherSize = < usize > otherLen << alignof < T > ( ) ;
220+ for ( let offset : usize = 0 ; offset < otherSize ; offset += sizeof < T > ( ) ) {
221+ let ref = load < usize > ( otherStart + offset ) ;
222+ store < usize > ( outStart + offset , ref ) ;
223+ __link ( outStart , ref , true ) ;
224+ }
225+ } else {
226+ memory . copy ( outStart , thisStart , sourceSize ) ;
227+ memory . copy ( outStart + sourceSize , otherStart , < usize > otherLen << alignof < T > ( ) ) ;
235228 }
236229 } else {
237- memory . copy ( outStart , changetype < usize > ( this ) , thisSize ) ;
238- memory . copy ( outStart + thisSize , other . dataStart , < usize > otherLen << alignof < T > ( ) ) ;
230+ ERROR ( "Only Array<T> and StaticArray<T> accept for 'U' parameter" ) ;
239231 }
240232 return out ;
241233 }
242234
243- slice ( start : i32 = 0 , end : i32 = i32 . MAX_VALUE ) : Array < T > {
235+ slice < U extends ArrayLike < T > = Array < T > > ( start : i32 = 0 , end : i32 = i32 . MAX_VALUE ) : U {
244236 var length = this . length ;
245237 start = start < 0 ? max ( start + length , 0 ) : min ( start , length ) ;
246- end = end < 0 ? max ( end + length , 0 ) : min ( end , length ) ;
238+ end = end < 0 ? max ( end + length , 0 ) : min ( end , length ) ;
247239 length = max ( end - start , 0 ) ;
248- var slice = changetype < Array < T > > ( __newArray ( length , alignof < T > ( ) , idof < Array < T > > ( ) ) ) ;
249- var sliceBase = slice . dataStart ;
250- var thisBase = changetype < usize > ( this ) + ( < usize > start << alignof < T > ( ) ) ;
251- if ( isManaged < T > ( ) ) {
252- let off = < usize > 0 ;
253- let end = < usize > length << alignof < usize > ( ) ;
254- while ( off < end ) {
255- let ref = load < usize > ( thisBase + off ) ;
256- store < usize > ( sliceBase + off , ref ) ;
257- __link ( changetype < usize > ( slice ) , ref , true ) ;
258- off += sizeof < usize > ( ) ;
240+
241+ var sourceStart = changetype < usize > ( this ) + ( < usize > start << alignof < T > ( ) ) ;
242+ var size = < usize > length << alignof < T > ( ) ;
243+ let out ! : U ;
244+
245+ if ( out instanceof Array < T > ) {
246+ // return Array
247+ out = changetype < U > ( __newArray ( length , alignof < T > ( ) , idof < Array < T > > ( ) ) ) ;
248+ let outStart = changetype < Array < T > > ( out ) . dataStart ;
249+ if ( isManaged < T > ( ) ) {
250+ let off : usize = 0 ;
251+ while ( off < size ) {
252+ let ref = load < usize > ( sourceStart + off ) ;
253+ store < usize > ( outStart + off , ref ) ;
254+ __link ( changetype < usize > ( out ) , ref , true ) ;
255+ off += sizeof < usize > ( ) ;
256+ }
257+ } else {
258+ memory . copy ( outStart , sourceStart , size ) ;
259+ }
260+ } else if ( out instanceof StaticArray < T > ) {
261+ // return StaticArray
262+ out = changetype < U > ( __new ( size , idof < StaticArray < T > > ( ) ) ) ;
263+ let outStart = changetype < usize > ( out ) ;
264+ if ( isManaged < T > ( ) ) {
265+ let off : usize = 0 ;
266+ while ( off < size ) {
267+ let ref = load < usize > ( sourceStart + off ) ;
268+ store < usize > ( outStart + off , ref ) ;
269+ __link ( outStart , ref , true ) ;
270+ off += sizeof < usize > ( ) ;
271+ }
272+ } else {
273+ memory . copy ( outStart , sourceStart , size ) ;
259274 }
260275 } else {
261- memory . copy ( sliceBase , thisBase , length << alignof < T > ( ) ) ;
276+ ERROR ( "Only Array<T> and StaticArray<T> accept for 'U' parameter" ) ;
262277 }
263- return slice ;
278+ return out ;
264279 }
265280
266281 findIndex ( fn : ( value : T , index : i32 , array : StaticArray < T > ) => bool ) : i32 {
0 commit comments