-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathcommon.dart
69 lines (56 loc) · 1.89 KB
/
common.dart
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
import '../heaps/base.dart';
/// Comparator function compared [left] with [right]
typedef Comparator<T extends Comparable> = bool Function(T left, T right);
/// Makes sure left is less than right.
///
/// In a sorting algorithm, this function needs to be true if the
/// sorting needs to return ascending order of lists.
///
/// For example, if we see an ascending ordered list- `[1, 2, 3, 5]`
/// we can see from left to right, all two consecutive elements in
/// this function would return true, in other words, settings this as
/// `reduce` function would yield true for a list sorted in ascending
/// order.
bool ascendingFn<T extends Comparable>(Comparable a, Comparable b) =>
a.compareTo(b) <= 0;
/// Swaps two elements in a [List]
void swap<T extends Comparable>(List<T> list, int i, int j) {
if (i == j) return;
var temp = list[i];
list[i] = list[j];
list[j] = temp;
}
/// Checks if the list is sorted according to [compareFn]
bool isSorted<T extends Comparable>(List<T> list,
[Comparator<T> compareFn = ascendingFn]) {
var i = 0;
while (i < list.length - 1 && list.length > 1) {
if (!compareFn(list[i], list[i + 1])) return false;
++i;
}
return true;
}
/// Checks if the list is sorted reversely.
bool isReverseSorted<T extends Comparable>(List<T> list,
[Comparator<T> compareFn = ascendingFn]) {
var i = 0;
while (i < list.length - 1 && list.length > 1) {
if (compareFn(list[i], list[i + 1])) return false;
i++;
}
return true;
}
/// Returns the minimum and maximum of a [List] as a [Map]
///
/// The minimum and maximum values are stored as "min"
/// and "max" kets in the [Map] returned.
Map<String, num> findMinMax(List<num> list) {
if (list.isEmpty) throw InvalidIndexError();
var min = list[0];
var max = list[0];
for (var item in list) {
if (item < min) min = item;
if (item > max) max = item;
}
return {'min': min, 'max': max};
}