|
| 1 | +# Sort `LIST` in ascending order using the insertion sorting algorithm. |
| 2 | +%define INSERTION_SORT(LIST) \ |
| 3 | + local i = 2; \ |
| 4 | + until i > length(LIST) { \ |
| 5 | + local x = LIST[i]; \ |
| 6 | + local j = i; \ |
| 7 | + until j <= 1 or LIST[j - 1] <= x { \ |
| 8 | + LIST[j] = LIST[j - 1]; \ |
| 9 | + j--; \ |
| 10 | + } \ |
| 11 | + LIST[j] = x; \ |
| 12 | + i++; \ |
| 13 | + } |
| 14 | + |
| 15 | +# Sort `LIST` of type `TYPE` in ascending order of the value of `FIELD` using the |
| 16 | +# insertion sorting algorithm. |
| 17 | +%define INSERTION_SORT_BY_FIELD(TYPE,LIST,FIELD) \ |
| 18 | + local i = 2; \ |
| 19 | + until i > length(LIST) { \ |
| 20 | + local TYPE x = LIST[i]; \ |
| 21 | + local j = i; \ |
| 22 | + until j <= 1 or LIST[j - 1].FIELD <= x.FIELD { \ |
| 23 | + LIST[j] = LIST[j - 1]; \ |
| 24 | + j--; \ |
| 25 | + } \ |
| 26 | + LIST[j] = x; \ |
| 27 | + i++; \ |
| 28 | + } |
| 29 | + |
| 30 | +# Count the number of elements in `LIST` that satisfy `CMP`, and store the result in |
| 31 | +# `STORE`. local `i` is the index of the current element. |
| 32 | +%define COUNT(LIST,CMP,STORE) \ |
| 33 | + local STORE = 0; \ |
| 34 | + local i = 1; \ |
| 35 | + repeat length(LIST) { \ |
| 36 | + if CMP { \ |
| 37 | + STORE += 1; \ |
| 38 | + } \ |
| 39 | + i++; \ |
| 40 | + } |
| 41 | + |
| 42 | +# Sum the elements in `LIST` that satisfy `CMP`, and store the result in `STORE`. |
| 43 | +# local `i` is the index of the current element. |
| 44 | +%define SUM(LIST,CMP,STORE) \ |
| 45 | + local STORE = 0; \ |
| 46 | + local i = 1; \ |
| 47 | + repeat length(LIST) { \ |
| 48 | + if CMP { \ |
| 49 | + STORE += LIST[i]; \ |
| 50 | + } \ |
| 51 | + i++; \ |
| 52 | + } |
| 53 | + |
| 54 | +# Find the largest element in `LIST` that satisfies `CMP`, and store the result in |
| 55 | +# `STORE`. local `i` is the index of the current element. |
| 56 | +%define FINDMAX(LIST,CMP,STORE) \ |
| 57 | + local STORE = LIST[1]; \ |
| 58 | + local i = 1; \ |
| 59 | + repeat length(LIST) { \ |
| 60 | + if CMP { \ |
| 61 | + if LIST[i] > STORE { \ |
| 62 | + STORE = LIST[i]; \ |
| 63 | + } \ |
| 64 | + } \ |
| 65 | + i++; \ |
| 66 | + } |
| 67 | + |
| 68 | +# Find the smallest element in `LIST` that satisfies `CMP`, and store the result in |
| 69 | +# `STORE`. local `i` is the index of the current element. |
| 70 | +%define FINDMIN(LIST,CMP,STORE) \ |
| 71 | + local STORE = LIST[1]; \ |
| 72 | + local i = 1; \ |
| 73 | + repeat length(LIST) { \ |
| 74 | + if CMP { \ |
| 75 | + if LIST[i] < STORE { \ |
| 76 | + STORE = LIST[i]; \ |
| 77 | + } \ |
| 78 | + } \ |
| 79 | + i++; \ |
| 80 | + } |
| 81 | + |
| 82 | +# Reverse `LIST` in place. |
| 83 | +%define REVERSE(LIST) \ |
| 84 | + local i = 1; \ |
| 85 | + local j = length(LIST); \ |
| 86 | + repeat length(LIST) / 2 { \ |
| 87 | + local x = LIST[i]; \ |
| 88 | + LIST[i] = LIST[j]; \ |
| 89 | + LIST[j] = x; \ |
| 90 | + i++; \ |
| 91 | + j--; \ |
| 92 | + } |
| 93 | + |
| 94 | +# Copy `SRC` to `DEST`. |
| 95 | +%define COPY(SRC,DEST) \ |
| 96 | + delete DEST; \ |
| 97 | + local i = 1; \ |
| 98 | + repeat length(SRC) { \ |
| 99 | + add SRC[i] to DEST; \ |
| 100 | + i++; \ |
| 101 | + } |
| 102 | + |
| 103 | +# Remove duplicate elements from `LIST`. |
| 104 | +%define UNIQUE(LIST) \ |
| 105 | + local i = 1; \ |
| 106 | + until i > length(LIST) { \ |
| 107 | + local j = i + 1; \ |
| 108 | + until j > length(LIST) { \ |
| 109 | + if LIST[i] == LIST[j] { \ |
| 110 | + delete LIST[j]; \ |
| 111 | + } else { \ |
| 112 | + j++; \ |
| 113 | + } \ |
| 114 | + } \ |
| 115 | + i++; \ |
| 116 | + } |
| 117 | + |
| 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) \ |
| 121 | + local STORE = 0; \ |
| 122 | + local i = 1; \ |
| 123 | + repeat length(LIST) { \ |
| 124 | + if CMP { \ |
| 125 | + STORE += LIST[i].FIELD; \ |
| 126 | + } \ |
| 127 | + i++; \ |
| 128 | + } |
| 129 | + |
| 130 | +# Find the largest `FIELD` value in `LIST` of type `TYPE` that satisfies `CMP` and store |
| 131 | +# the result in `STORE`. |
| 132 | +%define FINDMAX_BY_FIELD(TYPE,LIST,FIELD,CMP,STORE) \ |
| 133 | + local TYPE STORE = LIST[1]; \ |
| 134 | + local i = 1; \ |
| 135 | + repeat length(LIST) { \ |
| 136 | + if CMP { \ |
| 137 | + if LIST[i].FIELD > STORE.FIELD { \ |
| 138 | + STORE = LIST[i]; \ |
| 139 | + } \ |
| 140 | + } \ |
| 141 | + i++; \ |
| 142 | + } |
| 143 | + |
| 144 | +# Find the smallest `FIELD` value in `LIST` of type `TYPE` that satisfies `CMP` and |
| 145 | +# store the result in `STORE`. |
| 146 | +%define FINDMIN_BY_FIELD(TYPE,LIST,FIELD,CMP,STORE) \ |
| 147 | + local TYPE STORE = LIST[1]; \ |
| 148 | + local i = 1; \ |
| 149 | + repeat length(LIST) { \ |
| 150 | + if CMP { \ |
| 151 | + if LIST[i].FIELD < STORE.FIELD { \ |
| 152 | + STORE = LIST[i]; \ |
| 153 | + } \ |
| 154 | + } \ |
| 155 | + i++; \ |
| 156 | + } |
| 157 | + |
| 158 | +# Remove duplicate elements from `LIST` by field `FIELD`. |
| 159 | +%define UNIQUE_BY_FIELD(LIST,FIELD) \ |
| 160 | + local i = 1; \ |
| 161 | + until i > length(LIST) { \ |
| 162 | + local j = i + 1; \ |
| 163 | + until j > length(LIST) { \ |
| 164 | + if LIST[i].FIELD == LIST[j].FIELD { \ |
| 165 | + delete LIST[j]; \ |
| 166 | + } else { \ |
| 167 | + j++; \ |
| 168 | + } \ |
| 169 | + } \ |
| 170 | + i++; \ |
| 171 | + } |
0 commit comments