You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Source/DFPSR/api/algorithmAPI_List.h
+282-3Lines changed: 282 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ namespace dsr {
30
30
31
31
// Returns true iff a and b are equal in length and content according to T's equality operator.
32
32
template<typename T>
33
-
booloperator==(const List<T>& a, const List<T>& b) {
33
+
staticbooloperator==(const List<T>& a, const List<T>& b) {
34
34
if (a.length() != b.length()) returnfalse;
35
35
for (intptr_t i = 0; i < a.length(); i++) {
36
36
if (!(a[i] == b[i])) returnfalse;
@@ -39,14 +39,14 @@ bool operator==(const List<T>& a, const List<T>& b) {
39
39
}
40
40
41
41
// Returns false iff a and b are equal in length and content according to T's equality operator.
42
-
template<typename T> booloperator!=(const List<T>& a, const List<T>& b) { return !(a == b); }
42
+
template<typename T> staticbooloperator!=(const List<T>& a, const List<T>& b) { return !(a == b); }
43
43
44
44
// Printing a generic List of elements for easy debugging.
45
45
// A new line is used after each element, because the element type might print using multiple lines and the list might be very long.
46
46
// No new line at the end, because the caller might want to add a comma before breaking the line.
47
47
// If string_toStreamIndented is defined for lists of a specific element type, this template function will be overridden by the more specific function.
// TODO: Having insert union without append is a bit strange, so implement a basic append function as well.
217
+
218
+
// TODO: Create a varargs version starting with nothing and adding unique elements from all lists before returning by value, so that duplicates in the first list are also reduced.
219
+
// TODO: Take a function for equality.
220
+
// Pre-conditions:
221
+
// All elements in targetList must be unique, or else they will remain duplicated.
222
+
// targetList and sourceList may not refer to the same list.
223
+
// Pushes all elements in sourceList that does not already exist in targetList.
224
+
// Returns true iff any element was pushed to targetList.
// TODO: Create a varargs version starting with nothing and adding unique elements from all lists before returning by value, so that duplicates in the first list are also reduced.
237
+
// TODO: Take functions for both equality and sorting.
238
+
// TODO: Assert that the original list is sorted in debug mode.
239
+
// Pre-conditions:
240
+
// All elements in targetList must be unique, or else they will remain duplicated.
241
+
// targetList must be sorted in ascending order.
242
+
// targetList and sourceList may not refer to the same list.
243
+
// Pushes all elements in sourceList that does not already exist in targetList.
244
+
// Returns true iff any element was pushed to targetList.
staticvoidimpl_list_heapify(dsr::List<T>& targetList, intptr_t n, intptr_t i, const TemporaryCallback<bool(const T &leftSide, const T &rightSide)> &compare) {
259
+
intptr_t largest = i;
260
+
intptr_t l = 2 * i + 1;
261
+
intptr_t r = 2 * i + 2;
262
+
if (l < n && !compare(targetList[l], targetList[largest])) {
263
+
largest = l;
264
+
}
265
+
if (r < n && !compare(targetList[r], targetList[largest])) {
266
+
largest = r;
267
+
}
268
+
if (largest != i) {
269
+
targetList.swap(i, largest);
270
+
impl_list_heapify(targetList, n, largest, compare);
271
+
}
272
+
}
273
+
274
+
// Apply the heap-sort algorithm to targetList.
275
+
// The compare function should return true when leftSide and rightSide are sorted correctly.
276
+
// Pre-condition:
277
+
// The compare function must return true when leftSide and rightSide are equal, because elements in the list might be identical.
278
+
// Side-effects:
279
+
// Overwrites the input with the result, by sorting it in-place.
280
+
// The elements returned by reference in targetList is a permutation of the original elements,
281
+
// where each neighboring pair of elements satisfy the compare condition.
282
+
template <typename T>
283
+
staticvoidlist_heapSort(List<T>& targetList, const TemporaryCallback<bool(const T &leftSide, const T &rightSide)> &compare) {
284
+
intptr_t n = targetList.length();
285
+
for (intptr_t i = n / 2 - 1; i >= 0; i--) {
286
+
dsr::impl_list_heapify(targetList, n, i, compare);
287
+
}
288
+
for (intptr_t i = n - 1; i > 0; i--) {
289
+
targetList.swap(0, i);
290
+
dsr::impl_list_heapify(targetList, i, 0, compare);
291
+
}
292
+
}
293
+
294
+
// Using the < operator in T to heap-sort in-place with ascending order.
295
+
// Useful for basic types where you don't want to write a custom comparison.
296
+
// Side-effects:
297
+
// Overwrites the input with the result, by sorting it in-place.
298
+
// The elements returned by reference in targetList is a permutation of the original elements,
299
+
// where each following element is larger or equal to the previous element.
0 commit comments