|
2 | 2 | %define INSERTION_SORT(LIST) \ |
3 | 3 | local i = 2; \ |
4 | 4 | until i > length(LIST) { \ |
5 | | - local x = LIST[i]; \ |
| 5 | + local isx = LIST[i]; \ |
6 | 6 | local j = i; \ |
7 | | - until j <= 1 or LIST[j - 1] <= x { \ |
| 7 | + until j <= 1 or LIST[j - 1] <= isx { \ |
8 | 8 | LIST[j] = LIST[j - 1]; \ |
9 | 9 | j--; \ |
10 | 10 | } \ |
11 | | - LIST[j] = x; \ |
| 11 | + LIST[j] = isx; \ |
12 | 12 | i++; \ |
13 | 13 | } |
14 | 14 |
|
|
17 | 17 | %define INSERTION_SORT_BY_FIELD(TYPE,LIST,FIELD) \ |
18 | 18 | local i = 2; \ |
19 | 19 | until i > length(LIST) { \ |
20 | | - local TYPE x = LIST[i]; \ |
| 20 | + local TYPE isfx = LIST[i]; \ |
21 | 21 | local j = i; \ |
22 | | - until j <= 1 or LIST[j - 1].FIELD <= x.FIELD { \ |
| 22 | + until j <= 1 or LIST[j - 1]FIELD <= isfx FIELD { \ |
23 | 23 | LIST[j] = LIST[j - 1]; \ |
24 | 24 | j--; \ |
25 | 25 | } \ |
26 | | - LIST[j] = x; \ |
| 26 | + LIST[j] = isfx; \ |
27 | 27 | i++; \ |
28 | 28 | } |
29 | 29 |
|
|
54 | 54 | # Find the largest element in `LIST` that satisfies `CMP`, and store the result in |
55 | 55 | # `STORE`. local `i` is the index of the current element. |
56 | 56 | %define FINDMAX(LIST,CMP,STORE) \ |
57 | | - local STORE = LIST[1]; \ |
| 57 | + local STORE = "-Infinity"; \ |
58 | 58 | local i = 1; \ |
59 | 59 | repeat length(LIST) { \ |
60 | 60 | if CMP { \ |
|
68 | 68 | # Find the smallest element in `LIST` that satisfies `CMP`, and store the result in |
69 | 69 | # `STORE`. local `i` is the index of the current element. |
70 | 70 | %define FINDMIN(LIST,CMP,STORE) \ |
71 | | - local STORE = LIST[1]; \ |
| 71 | + local STORE = "Infinity"; \ |
72 | 72 | local i = 1; \ |
73 | 73 | repeat length(LIST) { \ |
74 | 74 | if CMP { \ |
|
100 | 100 | i++; \ |
101 | 101 | } |
102 | 102 |
|
| 103 | +# Extend `DEST` with the elements of `SRC`. |
| 104 | +%define EXTEND(SRC,DEST) \ |
| 105 | + local i = 1; \ |
| 106 | + repeat length(SRC) { \ |
| 107 | + add SRC[i] to DEST; \ |
| 108 | + i++; \ |
| 109 | + } |
| 110 | + |
103 | 111 | # Remove duplicate elements from `LIST`. |
104 | 112 | %define UNIQUE(LIST) \ |
105 | 113 | local i = 1; \ |
|
115 | 123 | i++; \ |
116 | 124 | } |
117 | 125 |
|
118 | | -# Sum the field `FIELD` in `LIST` of type `TYPE` that satisfy `CMP`, and store the |
119 | | -# result in `STORE`. |
120 | | -%define SUM_BY_FIELD(TYPE,LIST,FIELD,CMP,STORE) \ |
| 126 | +# Sum the field `FIELD` in `LIST` that satisfy `CMP`, and store the result in `STORE`. |
| 127 | +%define SUM_BY_FIELD(LIST,FIELD,CMP,STORE) \ |
121 | 128 | local STORE = 0; \ |
122 | 129 | local i = 1; \ |
123 | 130 | repeat length(LIST) { \ |
124 | 131 | if CMP { \ |
125 | | - STORE += LIST[i].FIELD; \ |
| 132 | + STORE += LIST[i]FIELD; \ |
126 | 133 | } \ |
127 | 134 | i++; \ |
128 | 135 | } |
129 | 136 |
|
| 137 | +# TODO [BLOCKED BY]: https://github.com/aspizu/goboscript/issues/71 |
130 | 138 | # Find the largest `FIELD` value in `LIST` of type `TYPE` that satisfies `CMP` and store |
131 | 139 | # the result in `STORE`. |
132 | 140 | %define FINDMAX_BY_FIELD(TYPE,LIST,FIELD,CMP,STORE) \ |
133 | | - local TYPE STORE = LIST[1]; \ |
| 141 | + local TYPE STORE; \ |
| 142 | + STORE FIELD = "-Infinity"; \ |
134 | 143 | local i = 1; \ |
135 | 144 | repeat length(LIST) { \ |
136 | 145 | if CMP { \ |
137 | | - if LIST[i].FIELD > STORE.FIELD { \ |
| 146 | + if LIST[i]FIELD > STORE FIELD { \ |
138 | 147 | STORE = LIST[i]; \ |
139 | 148 | } \ |
140 | 149 | } \ |
141 | 150 | i++; \ |
142 | 151 | } |
143 | 152 |
|
| 153 | +# TODO [BLOCKED BY]: https://github.com/aspizu/goboscript/issues/71 |
144 | 154 | # Find the smallest `FIELD` value in `LIST` of type `TYPE` that satisfies `CMP` and |
145 | 155 | # store the result in `STORE`. |
146 | 156 | %define FINDMIN_BY_FIELD(TYPE,LIST,FIELD,CMP,STORE) \ |
147 | | - local TYPE STORE = LIST[1]; \ |
| 157 | + local TYPE STORE; \ |
| 158 | + STORE FIELD = "Infinity"; \ |
148 | 159 | local i = 1; \ |
149 | 160 | repeat length(LIST) { \ |
150 | 161 | if CMP { \ |
151 | | - if LIST[i].FIELD < STORE.FIELD { \ |
| 162 | + if LIST[i]FIELD < STORE FIELD { \ |
152 | 163 | STORE = LIST[i]; \ |
153 | 164 | } \ |
154 | 165 | } \ |
|
161 | 172 | until i > length(LIST) { \ |
162 | 173 | local j = i + 1; \ |
163 | 174 | until j > length(LIST) { \ |
164 | | - if LIST[i].FIELD == LIST[j].FIELD { \ |
| 175 | + if LIST[i] FIELD == LIST[j] FIELD { \ |
165 | 176 | delete LIST[j]; \ |
166 | 177 | } else { \ |
167 | 178 | j++; \ |
|
0 commit comments