Transpiled version (10287L) is out of date.
1 | sclass MountainsAndValleys { |
2 | settable double[] values; |
3 | gettable new IntBuffer highs; |
4 | gettable new IntBuffer lows; |
5 | |
6 | *() {} |
7 | *(double[] *values) {} |
8 | |
9 | run { |
10 | int n = l(values); |
11 | if (n == 0) ret; |
12 | highs.add(0); |
13 | lows.add(0); |
14 | |
15 | int i = 1; |
16 | while (i < n) { |
17 | // find plateau |
18 | |
19 | var value = values[i]; |
20 | int j = i+1; |
21 | while (j < n && values[j] == value) ++j; |
22 | |
23 | // are we going up or down? |
24 | |
25 | var last = values[i-1]; |
26 | var list = value > last ? highs : lows; |
27 | |
28 | //if (list.last() == i-1) |
29 | if (values[list.last()] == last) |
30 | list.popLast(); |
31 | list.add(i); |
32 | |
33 | i = j; |
34 | } |
35 | } |
36 | |
37 | double get(int idx) { ret values[idx]; } |
38 | |
39 | // returns position of previous high/low |
40 | // or -1 if not found |
41 | int prev(IntBuffer highsOrLows, int idx) { |
42 | int j = intBufferBinarySearch(highsOrLows, idx-1); |
43 | if (j >= 0) ret idx-1; |
44 | j = -j-1; |
45 | j--; |
46 | ret j >= 0 ? highsOrLows.get(j) : -1; |
47 | } |
48 | |
49 | // returns position of next high/low |
50 | // or -1 if not found |
51 | int next(IntBuffer highsOrLows, int idx) { |
52 | int j = intBufferBinarySearch(highsOrLows, idx+1); |
53 | if (j >= 0) ret idx+1; |
54 | j = -j-1; |
55 | ret j < l(highsOrLows) ? highsOrLows.get(j) : -1; |
56 | } |
57 | |
58 | // direction = -1 or 1 |
59 | int prevLowerOrHigher(IntBuffer highsOrLows, int idx, int direction) { |
60 | double value = get(idx); |
61 | while (true) { |
62 | int j = prev(highsOrLows, idx); |
63 | if (j < 0 || cmp(get(j), value) == direction) ret j; |
64 | idx = j; |
65 | } |
66 | } |
67 | |
68 | // direction = -1 or 1 |
69 | int nextLowerOrHigher(IntBuffer highsOrLows, int idx, int direction) { |
70 | double value = get(idx); |
71 | while (true) { |
72 | int j = next(highsOrLows, idx); |
73 | if (j < 0 || cmp(get(j), value) == direction) ret j; |
74 | idx = j; |
75 | } |
76 | } |
77 | |
78 | // all the prev/next methods return -1 if not found |
79 | |
80 | int prevHigh(int idx) { ret prev(highs, idx); } |
81 | int nextHigh(int idx) { ret next(highs, idx); } |
82 | int prevLow(int idx) { ret prev(lows, idx); } |
83 | int nextLow(int idx) { ret next(lows, idx); } |
84 | |
85 | int prevLowerHigh(int idx) { |
86 | ret prevLowerOrHigher(highs, idx, 1); |
87 | } |
88 | |
89 | int prevLowerLow(int idx) { |
90 | ret prevLowerOrHigher(lows, idx, 1); |
91 | } |
92 | |
93 | int nextLowerHigh(int idx) { |
94 | ret nextLowerOrHigher(highs, idx, 1); |
95 | } |
96 | |
97 | int nextLowerLow(int idx) { |
98 | ret nextLowerOrHigher(lows, idx, 1); |
99 | } |
100 | |
101 | int nextLowOrHigh(int idx) { |
102 | int low = nextLow(idx), high = nextHigh(idx); |
103 | ret high < 0 || low < high ? low : high; |
104 | } |
105 | } |
download show line numbers debug dex old transpilations
Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1036455 |
Snippet name: | MountainsAndValleys - finds and connects local maxima and minima |
Eternal ID of this version: | #1036455/14 |
Text MD5: | de020543b2c15330ba8d3127e3095a2b |
Author: | stefan |
Category: | javax / maths |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2023-01-21 15:53:21 |
Source code size: | 2613 bytes / 105 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 176 / 281 |
Version history: | 13 change(s) |
Referenced in: | [show references] |