• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15declare function print(arg:any):void;
16let F : number[] = [0, 1, 2]
17let G : number[] = [0, 1, 2]
18
19function tryLoopOpt(f: number[], v : number): void {
20    let idx = 1 - 1;
21    let ret = f[idx];
22    for (let i = 0; i < f.length; ++i) {
23        f[i] += 1;
24        f[i] += v;
25    }
26    print(ret);
27    print(f[idx]);
28    print(f[idx + 1]);
29}
30
31tryLoopOpt(F, <number><Object>'a');
32
33function tryMergeOpt(g: number[], v : number): void {
34    let idx = 1 - 1;
35    let ret = g[idx];
36    if (g[idx] < 10) {
37        g[idx] -= 10;
38    } else {
39        v++;
40    }
41    print(ret + v);
42    print(g[idx]);
43}
44
45tryMergeOpt(G, <number><Object>'b');
46
47function binarySearch(array: number[], target: number): number {
48    let low : number = 0;
49    let high : number = array.length - 1;
50
51    while (low <= high) {
52        let mid: number = (low + high) >>> 1;
53        if (array[mid] == target) {
54            return mid;
55        } else if (array[mid] < target) {
56            low = mid + 1;
57        } else if (array[mid] > target) {
58            high = mid - 1;
59        }
60    }
61    return -1;
62}
63
64let array: number[] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
65let ret = binarySearch(array, 3);
66print(ret);
67
68function foo(arr : number[]) {
69    let t = 0 + 0;
70    let sum = arr[t];
71    for (let i = 0; i < 2; ++i) {
72        sum += arr[t];
73        arr[t] = 999;
74    }
75    return sum;
76}
77print(foo(array));
78