1%# Copyright (c) 2021-2025 Huawei Device Co., Ltd. 2%# Licensed under the Apache License, Version 2.0 (the "License"); 3%# you may not use this file except in compliance with the License. 4%# You may obtain a copy of the License at 5%# 6%# http://www.apache.org/licenses/LICENSE-2.0 7%# 8%# Unless required by applicable law or agreed to in writing, software 9%# distributed under the License is distributed on an "AS IS" BASIS, 10%# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11%# See the License for the specific language governing permissions and 12%# limitations under the License. 13% 14% require_relative 'Array_code' 15 16/** 17 * Takes an integer value and returns the item at that index, 18 * allowing for positive and negative integers. Negative integers count back 19 * from the last item in the array. 20 * 21 * @param index Zero-based index of the array element to be returned. 22 * Negative index counts back from the end of the array — if `index` < 0, index + `array.length()` is accessed. 23 * 24 * @returns The element in the array matching the given index. 25 * Returns undefined if `index` < `-length()` or `index` >= `length()`. 26 */ 27<%= access_public %> <%= override %> at<%= this_generic %>(<%= this_arg %>index: number): <%= el_type_boxed %> | undefined { 28 return <%= this_call.('at') %>index as int) 29} 30 31/** 32 * Creates a new `Array` from this `Array` instance and given `Array` instance. 33 * 34 * @param other to concatenate into a new array. 35 * 36 * @returns New `Array` instance, constructed from `this` and given `other` instances of `Array` class. 37 */ 38// <%= access_public %> <%= override %> concat<%= this_generic %>(<%= this_arg %>...items: (<%= el_type %> | Concat<%= this_type %>)[]): <%= this_type %> { 39// throw new Error("not implemented") 40// } 41 42<%= access_public %> concat<%= this_generic %>(<%= this_arg %>...items: FixedArray<ConcatArray<<%= el_type %>>>): <%= this_type %> { 43 let totalAdd = <%= this_len_int %>; 44 for (let i = 0; i < items.length; i++) { 45 totalAdd += items[i].length as int 46 } 47 48 const buf : <%= make_fixed_array %> = <%= make_buffer.('totalAdd') %>; 49 50 for (let i = 0; i < <%= this_len_int %>; i++) { 51 buf[i] = <%= get_unsafe.(this, 'i') %>; 52 } 53 54 let insertTo = <%= this_len_int %>; 55 for (let i = 0; i < items.length; i++) { 56 const arr = items[i] 57 const len = arr.length as int 58 for (let j = 0; j < len; j++) { 59 buf[insertTo++] = arr.$_get(j) 60 } 61 } 62 63 return <%= from_buffer.('buf') %>; 64} 65 66/** 67 * Takes an integer value and returns the item at that index, 68 * allowing for positive and negative integers. Negative integers count back 69 * from the last item in the array. 70 * 71 * @param index Zero-based index of the array element to be returned. 72 * Negative index counts back from the end of the array — if `index` < 0, index + `array.length()` is accessed. 73 * 74 * @returns The element in the array matching the given index. 75 * Returns undefined if `index` < `-length()` or `index` >= `length()`. 76 */ 77<%= access_public %> at<%= this_generic %>(<%= this_arg %>index: int): <%= el_type_boxed %> | undefined { 78 let len = <%= this_len_int %>; 79 let k: int; 80 if (index >= 0) { 81 k = index; 82 } else { 83 k = len + index; 84 } 85 86 if (k < 0 || k >= len) { 87 return undefined; 88 } 89 90 return <%= get_unsafe.(this, 'k') %>; 91} 92 93/** 94 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length. 95 * 96 * @param target index at which to copy the sequence 97 * 98 * @param start index at which to start copying elements from 99 * 100 * @param end index at which to end copying elements from 101 * 102 * @returns this array after transformation 103 */ 104<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: number, start: number, end?: Number): <%= this_return_type %> { 105 <%= this_call.('copyWithin') %>target as int, start as int, asIntOrDefault(end, <%= this_len_int %>)); 106 return <%= this %>; 107} 108 109/** 110 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length. 111 * 112 * @param target index at which to copy the sequence 113 * 114 * @param start index at which to start copying elements from 115 * 116 * @param end index at which to end copying elements from 117 * 118 * @returns this array after transformation 119 */ 120<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: int, start: int, end: int): <%= this_return_type %> { 121 target = normalizeIndex(target, <%= this_len_int %>) 122 start = normalizeIndex(start, <%= this_len_int %>) 123 end = normalizeIndex(end, <%= this_len_int %>) 124 125 if (end <= start) { 126 return <%= this %>; 127 } 128 129 if (target <= start) { 130 while (start < end) { 131 const read = <%= get_unsafe.(this, 'start++') %>; 132 <%= set_unsafe.(this, 'target++', 'read') %>; 133 } 134 } else { 135 let len = end - start; 136 if (target + len > <%= this_len_int %>) { 137 len = <%= this_len_int %> - target 138 } 139 for (let i = 0; i < len; i++) { 140 const read = <%= get_unsafe.(this, 'start + len - 1 - i') %>; 141 <%= set_unsafe.(this, 'target + len - 1 - i', 'read') %>; 142 } 143 } 144 145 return <%= this %>; 146} 147 148/** 149 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length. 150 * 151 * @param target index at which to copy the sequence 152 * 153 * @param start index at which to start copying elements from 154 * 155 * @returns this array after transformation 156 */ 157<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: int, start: int): <%= this_return_type %> { 158 <%= this_call.('copyWithin') %>target, start, <%= this_len_int %>); 159 return <%= this %>; 160} 161 162/** 163 * Makes a shallow copy of the Array part to another location in the same Array and returns it without modifying its length. 164 * 165 * @param target index at which to copy the sequence 166 * 167 * @returns this array after transformation 168 */ 169<%= access_public %> copyWithin<%= this_generic %>(<%= this_arg %>target: int): <%= this_return_type %> { 170 <%= this_call.('copyWithin') %>target, 0, <%= this_len_int %>); 171 return <%= this %>; 172} 173 174/** 175 * Changes all elements in the Array to a static value, from a start index to an end index 176 * 177 * @param value to fill the array with 178 * 179 * @param start index at which to start filling 180 * 181 * @param end index at which to end filling, but not including 182 * 183 * @returns this array after transformation 184 */ 185<%= access_public %> fill<%= this_generic %>(<%= this_arg %>value: <%= el_type %>, start?: Number, end?: Number): <%= this_return_type %> { 186 <%= this_call.('fill') %>value, asIntOrDefault(start, 0), asIntOrDefault(end, <%= this_len_int %>)); 187 return <%= this %>; 188} 189 190/** 191 * Changes all elements in the Array to a static value, from a start index to an end index 192 * 193 * @param value to fill the array with 194 * 195 * @param start index at which to start filling 196 * 197 * @param end index at which to end filling, but not including 198 * 199 * @returns this array after transformation 200 */ 201% if function_type != 'has_native' 202<%= access_public %> fill<%= this_generic %>(<%= this_arg %>value: <%= el_type %>, start: int, end: int): <%= this_return_type %> { 203 start = normalizeIndex(start, <%= this_len_int %>); 204 end = normalizeIndex(end, <%= this_len_int %>) 205 206 for (let i = start; i < end; i++) { 207 <%= set_unsafe.(this, 'i', 'value') %>; 208 } 209 210 return <%= this %>; 211} 212 213% else 214<%= access_public %> native fill<%= this_generic %>(<%= this_arg %>value: <%= el_type %>, start: int, end: int): <%= this_return_type %>; 215% end 216 217% TemplateData::get_lambda_data.each { |lambda_args_params| 218% lambda_params_num, lambda_args, lambda_params, lambda_args2 = lambda_args_params 219/** 220 * Returns the value of the first element in the array where predicate is true, and undefined 221 * otherwise. 222 * 223 * @param predicate find calls predicate once for each element of the array, in ascending 224 * order, until it finds one where predicate returns true. If such an element is found, find 225 * immediately returns that element value. Otherwise, find returns undefined. 226 * 227 * @returns the value of the first element in the array or undefined 228 */ 229<%= access_public %> <%= override %> find<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): <%= el_type_boxed %> | undefined { 230 const res = <%= this_call.('findIndex') %>predicate) 231 if (res == -1) { 232 return undefined 233 } 234 return <%= get_unsafe.(this, 'res as int') %>; 235} 236 237/** 238 * Returns the index of the first element in the array where predicate is true, and -1 239 * otherwise. 240 * 241 * @param predicate find calls predicate once for each element of the array, in ascending 242 * order, until it finds one where predicate returns true. If such an element is found, 243 * findIndex immediately returns that element index. Otherwise, findIndex returns -1. 244 * 245 * @returns found element index or -1 otherwise 246 */ 247<%= access_public %> <%= override %> findIndex<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): number { 248 for (let i = 0; i < <%= this_len_int %>; i++) { 249 if (predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) { 250 return i; 251 } 252 } 253 return -1; 254} 255 256/** 257 * Iterates the array in reverse order and returns the value of the first element 258 * that satisfies the provided testing function 259 * 260 * @param predicate testing function 261 * 262 * @returns found element or undefined otherwise 263 */ 264<%= access_public %> <%= override %> findLast<%= this_generic %>(<%= this_arg %>predicate: (elem: <%= el_type %><%= lambda_params %>) => boolean): <%= el_type_boxed %> | undefined { 265 for (let i = <%= this_len_int %> - 1; i >= 0; i--) { 266 const val = <%= get_unsafe.(this, 'i') %>; 267 if (predicate(val<%= lambda_args %>)) { 268 return val; 269 } 270 } 271 return undefined; 272} 273 274/** 275 * Determines whether all the members of an array satisfy the specified test. 276 * 277 * @param predicate A function that accepts up to three arguments. The every method calls 278 * the predicate function for each element in the array until the predicate returns a value 279 * which is coercible to the Boolean value false, or until the end of the array. 280 * 281 * @returns `true` if `predicate` returns a `true` value for every array element. Otherwise, `false`. 282 */ 283<%= access_public %> <%= override %> every<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): boolean {<% if this_arg == "" %> 284 let curArrLength = <%= this_len_int %> 285 for (let i = 0; i < curArrLength && i < <%= this_len_int %>; i++) {<% else %> 286 for (let i = 0; i < <%= this_len_int %>; i++) {<% end %> 287 if (!predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) { 288 return false 289 } 290 } 291 return true; 292} 293 294/** 295 * Determines whether the specified callback function returns true for any element of an array. 296 * 297 * @param predicate A function that accepts up to three arguments. The some method calls 298 * the predicate function for each element in the array until the predicate returns a value 299 * which is coercible to the Boolean value true, or until the end of the array. 300 * 301 * @returns `true` if `predicate` returns a `true` value for at least one array element. Otherwise, `false`. 302 */ 303<%= access_public %> <%= override %> some<%= this_generic %>(<%= this_arg %>predicate: (value: <%= el_type %><%= lambda_params %>) => boolean): boolean { 304 for (let i = 0; i < <%= this_len_int %>; i++) { 305 if (predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) { 306 return true 307 } 308 } 309 return false 310} 311 312/** 313 * Iterates the array in reverse order and returns the index of 314 * the first element that satisfies the provided testing function. 315 * If no elements satisfy the testing function, -1 is returned. 316 * 317 * @param predicate testing function 318 * 319 * @returns index of first element satisfying to predicate, -1 if no such element 320 */ 321<%= access_public %> <%= override %> findLastIndex<%= this_generic %>(<%= this_arg %>predicate: (element: <%= el_type %><%= lambda_params %>) => boolean): number { 322 for (let i = <%= this_len_int %> - 1; i >= 0; i--) { 323 if (predicate(<%= get_unsafe.(this, 'i') %><%= lambda_args %>)) { 324 return i 325 } 326 } 327 return -1 328} 329 330/** 331 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. 332 * 333 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. 334 * 335 * @returns a result after applying callbackfn over all elements of the Array 336 */ 337<%= access_public %> <%= override %> reduce<%= this_generic %>(<%= this_arg %>callbackfn: (previousValue: <%= el_type %>, currentValue: <%= el_type %><%= lambda_params %>) => <%= el_type %>): <%= el_type %> { 338 if (<%= this_len_int %> == 0) { 339 throw new TypeError("Reduce of empty array with no initial value") 340 } 341 let acc: <%= el_type %> = <%= get_unsafe.(this, '0') %>; 342 for (let i = 1; i < <%= this_len_int %>; i++) { 343 acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>) 344 } 345 return acc 346} 347 348/** 349 * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. 350 * 351 * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. 352 * 353 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. 354 * 355 * @returns a result after applying callbackfn over all elements of the Array 356 */ 357<%= access_public %> <%= override %> reduce<<%= this_generic_one %>U = <%= el_type %>>(<%= this_arg %>callbackfn: (previousValue: U, currentValue: <%= el_type %><%= lambda_params %>) => U, initialValue: U): U { 358 let acc = initialValue 359 for (let i = 0; i < <%= this_len_int %>; i++) { 360 acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>) 361 } 362 return acc 363} 364 365/** 366 * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. 367 * 368 * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. 369 * 370 * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. 371 * 372 * @returns a result after applying callbackfn over all elements of the Array 373 */ 374<%= access_public %> <%= override %> reduceRight<<%= this_generic_one %>U>(<%= this_arg %>callbackfn: (previousValue: U, currentValue: <%= el_type %><%= lambda_params %>) => U, initialValue: U): U { 375 let acc = initialValue 376 for (let i = <%= this_len_int %> - 1; i >= 0; i--) { 377 acc = callbackfn(acc, <%= get_unsafe.(this, 'i') %><%= lambda_args %>) 378 } 379 return acc 380} 381 382/** 383 * Performs the specified action for each element in an array. 384 * 385 * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. 386 */ 387<%= access_public %> <%= override %> forEach<%= this_generic %>(<%= this_arg %>callbackfn: (value: <%= el_type %><%= lambda_params %>) => void): void { 388 const len0 = <%= this_len_int %>; 389 for (let i = 0; i < len0; i++) { 390 callbackfn(<%= get_unsafe.(this, 'i') %><%= lambda_args %>) 391 } 392} 393 394% } 395 396/** 397 * Creates a new `Array` object and populates it with elements of `this` instance of `Array` class 398 * selected from `start` to `end` (`end` not included) where `start` and `end` represent the index of items in that array. 399 * 400 * @param start zero-based index at which to start extraction 401 * 402 * @param end zero-based index at which to end extraction. `slice()` extracts up to but not including end. 403 * 404 * @returns `Array` instance, constructed from extracted elements of `this` instance. 405 */ 406<%= access_public %> <%= override %> slice<%= this_generic %>(<%= this_arg %>start?: Number, end?: Number): <%= this_type %> { 407 const len: int = <%= this_len_int %>; 408 return <%= this_call.('slice') %>asIntOrDefault(start, 0), asIntOrDefault(end, len)) 409} 410 411/** 412 * Creates a new `Array` object and populates it with elements of `this` instance of `Array` class 413 * selected from `start` to `end` (`end` not included) where `start` and `end` represent the index of items in that array. 414 * 415 * @param start zero-based index at which to start extraction 416 * 417 * @param end zero-based index at which to end extraction. `slice()` extracts up to but not including end. 418 * 419 * @returns `Array` instance, constructed from extracted elements of `this` instance. 420 */ 421<%= access_public %> slice<%= this_generic %>(<%= this_arg %>start: int, end: int): <%= this_type %> { 422 const len: int = <%= this_len_int %>; 423 const relStart = normalizeIndex(start, len) 424 const relEnd = normalizeIndex(end, len) 425 426 let count = relEnd - relStart; 427 if (count < 0) { 428 count = 0; 429 } 430 let res : <%= make_fixed_array %> = <%= make_buffer.('count') %> 431 for (let i = 0; i < count; i++) { 432 res[i] = <%= get_unsafe.(this, 'relStart + i') %>; 433 } 434 435 return <%= from_buffer.('res') %> 436} 437 438/** 439 * Creates a new `Array` object and populates it with elements of `this` instance of `Array` class 440 * selected from `start` to `Int.MAX_VALUE`, which means 'to the end of an array'. 441 * 442 * @param start zero-based index at which to start extraction 443 * 444 * @returns `Array` instance, constructed from extracted elements of `this` instance. 445 */ 446<%= access_public %> slice<%= this_generic %>(<%= this_arg %>start: int): <%= this_type %> { 447 return <%= this_call.('slice') %>start, Int.MAX_VALUE as int); 448} 449 450<% if access_public == "export function" %><% #generate code for BuiltinArray.ets %> 451/** 452 * Returns the last index at which a given element can be found in the array, 453 * or -1 if it is not present. The array is searched backwards, starting at fromIndex. 454 * 455 * @param element element to locate in the array. 456 * @param fromIndex zero-based index at which to start searching backwards. 457 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used. 458 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned. 459 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched. 460 * 461 * @returns The last index of the element in the array; -1 if not found. 462 */ 463<%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>element: <%= el_type %>, fromIndex: int): int { 464 if (<%= this_len_int %> == 0) { 465 return -1; 466 } 467 let n = fromIndex; 468 let k: int; 469 if (n >= 0) { 470 k = min(<%= this_len_int %> - 1, n); 471 } else { 472 k = <%= this_len_int %> + n; 473 } 474 475 while (k >= 0) { 476 if (__runtimeEquals(<%= get_unsafe.(this, 'k') %>, element)) { 477 return k; 478 } 479 k--; 480 } 481 return -1; 482} 483 484/** 485 * Returns the last index at which a given element can be found in the array, 486 * or -1 if it is not present. The array is searched backwards, starting at fromIndex. 487 * 488 * @param element element to locate in the array. 489 * @param fromIndex zero-based index at which to start searching backwards. 490 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used. 491 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned. 492 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched. 493 * 494 * @returns The last index of the element in the array; -1 if not found. 495 */ 496<%= access_public %> <%= override %> lastIndexOf<%= this_generic %>(<%= this_arg %>element: <%= el_type %>, fromIndex?: Number): number { 497 return <%= this_call.('lastIndexOf') %>element, asIntOrDefault(fromIndex, <%= this_len_int %> - 1)); 498} 499<% else %><% #generate code for Array.ets %> 500/** 501 * Returns the last index at which a given element can be found in the array, 502 * or -1 if it is not present. The array is searched backwards, starting at fromIndex. 503 * 504 * @param searchElement element to locate in the array. 505 * 506 * @param fromIndex zero-based index at which to start searching backwards. 507 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used. 508 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned. 509 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched. 510 * If `fromIndex` is undefined then `fromIndex = 0`. 511 * If `fromIndex` is ommited then `fromIndex = length()-1`. 512 * 513 * @returns The last index of the element in the array; -1 if not found. 514 */ 515 <%= access_public %> native lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: T, fromIndex: int): int; 516 517/** 518 * Returns the last index at which a given element can be found in the array, 519 * or -1 if it is not present. The array is searched backwards, starting at fromIndex. 520 * 521 * @param searchElement element to locate in the array. 522 * 523 * @param fromIndex zero-based index at which to start searching backwards. 524 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used. 525 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned. 526 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched. 527 * If `fromIndex` is undefined then `fromIndex = 0`. 528 * If `fromIndex` is ommited then `fromIndex = length()-1`. 529 * 530 * @returns The last index of the element in the array; -1 if not found. 531 */ 532<%= access_public %> lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: <%= el_type %>, fromIndex: number|undefined): number { 533 return <%= this_call.('lastIndexOf') %>searchElement, asIntOrDefault(fromIndex, 0)); 534} 535 536/** 537 * Returns the last index at which a given element can be found in the array, 538 * or -1 if it is not present. The array is searched backwards, starting at fromIndex. 539 * 540 * @param searchElement element to locate in the array. 541 * 542 * @param fromIndex zero-based index at which to start searching backwards. 543 * Negative index counts back from the end of the array — if `fromIndex` < 0, `fromIndex` + `length()` is used. 544 * If `fromIndex` < `-length()`, the array is not searched and -1 is returned. 545 * If `fromIndex` >= `length()` then `array.length - 1` is used, causing the entire array to be searched. 546 * If `fromIndex` is undefined then `fromIndex = 0`. 547 * If `fromIndex` is ommited then `fromIndex = length()-1`. 548 * 549 * @returns The last index of the element in the array; -1 if not found. 550 */ 551<%= access_public %> native lastIndexOf<%= this_generic %>(<%= this_arg %>searchElement: <%= el_type %>): number; 552<% end %> 553 554/** 555 * Creates and returns a new string by concatenating all of the elements in an `Array`, 556 * separated by a specified separator string. 557 * If the array has only one item, then that item will be returned without using the separator. 558 * 559 * @param sep specifies a separator 560 * 561 * @returns A string with all array elements joined. If `length()` is 0, the empty string is returned. 562 */ 563<%= access_public %> <%= override %> join<%= this_generic %>(<%= this_arg %>sep?: String): string { 564 if (<%= this_len_int %> == 0) { 565 return "" 566 } 567 const sepReal = sep === undefined ? "," : sep! 568 <% if el_type == "T" %>// NOTE(aleksander-sotov) We can't call String constructor here due to internal issue 18583 569 const first_el = <%= get_unsafe.(this, '0') %> 570 let first_str = "" 571 if (first_el !== undefined && first_el !== null) { 572 first_str = new String(first_el) 573 } 574 let sb = new StringBuilder(first_str)<% else %>let sb = new StringBuilder(new String(<%= get_unsafe.(this, '0') %>))<% end %> 575 for (let i: int = 1; i < <%= this_len_int %>; i++) { 576 const tmp = <%= get_unsafe.(this, 'i') %> 577 sb.append(sepReal); 578 <% if el_type == "T" %>// NOTE(aleksander-sotov) We can't call String constructor here due to internal issue 18583 579 if (tmp !== undefined && tmp !== null) { 580 sb.append(new String(tmp)) 581 } else { 582 sb.append("") 583 }<% else %>sb.append(tmp)<% end %> 584 } 585 586 return sb.toString(); 587} 588 589/** 590 * Returns a string representing the specified array and its elements. 591 * 592 * @returns string representation 593 */ 594<%= access_public %> <%= override %> toString<%= this_generic %>(<%= this_arg.chomp(', ') %>): string { 595 return <%= this_call.('join') %>","); 596} 597 598/** 599 * Returns a locale string representing the specified array and its elements. 600 * 601 * @param locales 602 * 603 * @param options 604 * 605 * @returns string representation 606 */ 607<%= access_public %> toLocaleString<%= this_generic %>(<%= this_arg %>locales: Object, options: Object): string { 608 throw new Error("Array.toLocaleString: not implemented") 609} 610 611/** 612 * Returns a locale string representing the specified array and its elements. 613 * 614 * @param options 615 * 616 * @returns string representation 617 */ 618<%= access_public %> toLocaleString<%= this_generic %>(<%= this_arg %>locales: Object): string { 619 return <%= this_call.('toLocaleString') %>new Object(), new Object()) 620} 621 622/** 623 * Returns a locale string representing the specified array and its elements. 624 * 625 * @returns string representation 626 */ 627<%= access_public %> <%= override %> toLocaleString<%= this_generic %>(<%= this_arg.chomp(', ') %>): string { 628 const sb = new StringBuilder() 629 const len = <%= this_len_int %>; 630 for (let i = 0; i < len; i++) { 631 if (i != 0) { 632 sb.append(",") 633 } 634 let x = <%= get_unsafe.(this, 'i') %> as NullishType; 635 if ((null !== x) && (undefined !== x)) { 636 sb.append((x! as object).toLocaleString()) // #26217 637 } 638 } 639 return sb.toString() 640} 641 642/** 643 * Copying version of the splice() method. 644 * 645 * @param start index 646 * 647 * @param delete number of items after start index 648 * 649 * @returns a new Array with some elements removed and/or replaced at a given index. 650 */ 651<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start?: Number, delete?: Number): <%= this_type %> { 652 const len = <%= this_len_int %>; 653 return <%= this_call.('toSpliced') %>asIntOrDefault(start, len), asIntOrDefault(delete, len)) 654} 655 656/** 657 * Copying version of the splice() method. 658 * 659 * @param start index 660 * 661 * @param delete number of items after start index 662 * 663 * @returns a new Array with some elements removed and/or replaced at a given index. 664 */ 665<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: number, delete: number, ...items: FixedArray<<%= el_type %>>): <%= this_type %> { 666 const len = <%= this_len_int %>; 667 return <%= this_call.('toSpliced') %>start as int, delete as int, ...items) 668} 669 670/** 671 * Copying version of the splice() method. 672 * 673 * @param start index 674 * 675 * @param delete number of items after start index 676 * 677 * @returns a new Array with some elements removed and/or replaced at a given index. 678 */ 679<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: int, delete: int, ...items: FixedArray<<%= el_type %>>): <%= this_type %> { 680 const len = <%= this_len_int %>; 681 start = normalizeIndex(start, len); 682 if (delete < 0) { 683 delete = 0; 684 } else if (delete > len) { 685 delete = len; 686 } 687 if (start > len - delete) { 688 delete = len - start 689 } 690 const res : <%= make_fixed_array %> = <%= make_buffer.('len - delete + items.length') %>; 691 for (let i = 0; i < start; i++) { 692 res[i] = <%= get_unsafe.(this, 'i') %> 693 } 694 for (let i = 0; i < items.length; i++) { 695 res[start + i] = items[i] 696 } 697 for (let i = start + delete; i < len; i++) { 698 res[i - delete + items.length] = <%= get_unsafe.(this, 'i') %> 699 } 700 return <%= from_buffer.('res') %>; 701} 702 703/** 704 * Copying version of the splice() method. 705 * 706 * @param start index 707 * 708 * @returns a new Array with some elements removed and/or replaced at a given index. 709 */ 710<%= access_public %> toSpliced<%= this_generic %>(<%= this_arg %>start: int): <%= this_type %> { 711 return <%= this_call.('toSpliced') %>start, <%= this_len_int %>) 712} 713 714/** 715 * Checks whether an Array includes a certain value among its entries, 716 * returning true or false as appropriate. 717 * 718 * @param val value to search 719 * 720 * @param fromIndex start index 721 * 722 * @returns true if val is in Array 723 */ 724<%= access_public %> <%= override %> includes<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex?: Number): boolean { 725 const len = <%= this_len_int %>; 726 const fi = normalizeIndex(asIntOrDefault(fromIndex, 0), len); 727% if ['float', 'double', 'number'].include?(el_type) 728 if (isNaN(val)) { 729 for (let i = fi; i < len; i++) { 730 if (isNaN(<%= get_unsafe.(this, 'i') %>)) { 731 return true; 732 } 733 } 734 return false; 735 } 736% end 737% if ['boolean', 'byte', 'short', 'int', 'long', 'char', 'float', 'double', 'number'].include?(el_type) 738 for (let i = fi; i < len; i++) { 739 if (val == <%= get_unsafe.(this, 'i') %>) { 740 return true; 741 } 742 } 743 return false; 744% elsif el_type == 'T' 745 if (val instanceof String) { 746 return this.searchString(val, fi, len) 747 } else if (val instanceof Number) { 748 return this.searchNumber(val, fi, len) 749 } else if (val instanceof Float) { 750 return this.searchFloat(val, fi, len) 751 } else if (val instanceof Long) { 752 return this.searchLong(val, fi, len) 753 } else if (val instanceof Int) { 754 return this.searchInt(val, fi, len) 755 } else if (val === undefined) { 756 return this.searchUndefined(fi, len) 757 } else if (val == null) { 758 return this.searchNull(fi, len) 759 } else { 760 return this.searchCommon(val, fi, len) 761 } 762% else 763 for (let i = fi; i < len; i++) { 764 if (__runtimeSameValueZero(val, <%= get_unsafe.(this, 'i') %>)) { 765 return true; 766 } 767 } 768 return false; 769% end 770} 771 772/** 773 * Returns the first index at which a given element 774 * can be found in the array, or -1 if it is not present. 775 * 776 * @param val value to search 777 * 778 * @param fromIndex index to search from 779 * 780 * @returns index of val, -1 otherwise 781 */ 782% if el_type != 'T' 783<%= access_public %> indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex: int): int { 784 fromIndex = normalizeIndex(fromIndex, <%= this_len_int %>) 785 for (let i = fromIndex; i < <%= this_len_int %>; i++) { 786 if (__runtimeEquals(val, <%= get_unsafe.(this, 'i') %>)) { 787 return i 788 } 789 } 790 return -1 791} 792% else 793<%= access_public %> native indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex: int): int; 794% end 795 796/** 797 * Returns the first index at which a given element 798 * can be found in the array, or -1 if it is not present. 799 * 800 * @param val value to search 801 * 802 * @param fromIndex index to search from 803 * 804 * @returns index of val, -1 otherwise 805 */ 806<%= access_public %> <%= override %> indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>, fromIndex?: Number): number { 807 return <%= this_call.('indexOf') %>val, asIntOrDefault(fromIndex, 0)) 808} 809 810% if this_array == 'escompat_array' 811<%= access_public %> native indexOf<%= this_generic %>(<%= this_arg %>val: <%= el_type %>): number; 812%end 813 814/** 815 * Copying version of the sort() method. 816 * It returns a new array with the elements sorted in ascending order. 817 * 818 * @returns sorted copy of hte current instance using default comparator 819 */ 820<%= access_public %> toSorted<%= this_generic %>(<%= this_arg.chomp(', ') %>): <%= this_type %> { 821 let arr = <%= clone_this %>; 822 <%= arr_method_call.('arr', 'sort') %> 823 return arr 824} 825 826/** 827 * Copying version of the sort() method. 828 * It returns a new array with the elements sorted in ascending order. 829 * 830 * @param comparator function to compare to elements of the Array 831 * 832 * @returns sorted copy of the current instance comparator 833 */ 834<%= access_public %> toSorted<%= this_generic %>(<%= this_arg %>comparator: (a: <%= el_type %>, b: <%= el_type %>) => number): <%= this_type %> { 835 let arr = <%= clone_this %>; 836 <%= arr_method_call.('arr', 'sort', 'comparator') %> 837 return arr 838} 839 840/** 841 * Modifies `this` instance of `Array` class and populates 842 * it with same elements ordered towards the direction opposite to that previously stated. 843 * 844 * @note Mutating method 845 */ 846<%= access_public %> reverse<%= this_generic %>(<%= this_arg.chomp(', ') %>): <%= this_return_type %> { 847 for (let i = 0; i < <%= this_len_int %> / 2; i++) { 848 const tmp = <%= get_unsafe.(this, 'i') %>; 849 const idx_r = <%= this_len_int %> - 1 - i; 850 const val_r = <%= get_unsafe.(this, 'idx_r') %>; 851 <%= set_unsafe.(this, 'i', 'val_r') %>; 852 <%= set_unsafe.(this, 'idx_r', 'tmp') %>; 853 } 854 return <%= this %>; 855} 856 857/** 858 * Copying version of the reverse() method. 859 * It returns a new array with the elements in reversed order. 860 * 861 * @returns reversed copy of the current Array 862 */ 863<%= access_public %> toReversed<%= this_generic %>(<%= this_arg.chomp(', ') %>): <%= this_type %> { 864 let arr : <%= make_fixed_array %> = <%= make_buffer.(this_len_int) %> 865 for (let i = 0; i < <%= this_len_int %>; i++) { 866 arr[<%= this_len_int %> - 1 - i] = <%= get_unsafe.(this, 'i') %> 867 } 868 return <%= from_buffer.('arr') %> 869} 870 871/** 872 * Copying version of using the bracket notation to change the value of a given index. 873 * It returns a new Array with the element at the given index replaced with the given value. 874 * 875 * @param index to replace 876 * 877 * @param value new value 878 * 879 * @returns a new Array with the element at the given index replaced with the given value 880 */ 881<%= access_public %> with<%= this_generic %>(<%= this_arg %>index: number, value: <%= el_type %>): <%= this_type %> { 882 return <%= this_call.('with') %>index as int, value) 883} 884 885/** 886 * Copying version of using the bracket notation to change the value of a given index. 887 * It returns a new Array with the element at the given index replaced with the given value. 888 * 889 * @param index to replace 890 * 891 * @param value new value 892 * 893 * @returns a new Array with the element at the given index replaced with the given value 894 */ 895<%= access_public %> with<%= this_generic %>(<%= this_arg %>index: int, value: <%= el_type %>): <%= this_type %> { 896 if (index < 0) { 897 index += <%= this_len_int %>; 898 } 899 if (index >= <%= this_len_int %>) { 900 throw new RangeError("Invalid index") 901 } 902 let arr = <%= clone_this %>; 903 <%= set_unsafe.('arr', 'index', 'value') %>; 904 return arr 905} 906 907/** 908 * Returns an iterator over all values 909 */ 910<%= access_public %> <%= override %> values<%= this_generic %>(<%= this_arg.chomp(', ') %>): IterableIterator<<%= el_type %>> { 911 return new ArrayValuesIterator_<%= el_type %><%= this_iterator_generic || this_generic %>(<%= this %>); 912} 913 914/** 915 * Returns an iterable of key, value pairs for every entry in the array 916 */ 917<%= access_public %> <%= override %> entries<%= this_generic %>(<%= this_arg.chomp(', ') %>): IterableIterator<[number, <%= el_type %>]> { 918 return new ArrayEntriesIterator_<%= el_type %><%= this_iterator_generic || this_generic %>(<%= this %>); 919} 920