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