Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(420)

Side by Side Diff: packages/charted/lib/core/utils/lists.dart

Issue 1521693002: Roll Observatory deps (charted -> ^0.3.0) (Closed) Base URL: https://chromium.googlesource.com/external/github.com/dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // 1 //
2 // Copyright 2014 Google Inc. All rights reserved. 2 // Copyright 2014 Google Inc. All rights reserved.
3 // 3 //
4 // Use of this source code is governed by a BSD-style 4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at 5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd 6 // https://developers.google.com/open-source/licenses/bsd
7 // 7 //
8 8
9 part of charted.core.utils; 9 part of charted.core.utils;
10 10
11 /// Returns a sum of all values in the given list of values 11 /// Returns a sum of all values in the given list of values
12 num sum(List values) => 12 num sum(List values) => values == null || values.isEmpty
13 values == null || values.isEmpty ? 13 ? 0
14 0: values.fold(0.0, (old, next) => old + next); 14 : values.fold(0.0, (old, next) => old + next);
15 15
16 /// Returns the smallest number in the given list of values 16 /// Returns the smallest number in the given list of values
17 num min(Iterable values) => 17 num min(Iterable values) => values == null || values.isEmpty
18 values == null || values.isEmpty ? 18 ? null
19 null : values.fold(values.elementAt(0), math.min); 19 : values.fold(values.elementAt(0), math.min);
20 20
21 /// Returns the largest number in the given list of values 21 /// Returns the largest number in the given list of values
22 num max(Iterable values) => 22 num max(Iterable values) => values == null || values.isEmpty
23 values == null || values.isEmpty ? 23 ? null
24 null : values.fold(values.elementAt(0), math.max); 24 : values.fold(values.elementAt(0), math.max);
25 25
26 /// Represents a constant pair of values 26 /// Represents a constant pair of values
27 class Pair<T1, T2> { 27 class Pair<T1, T2> {
28 final T1 first; 28 final T1 first;
29 final T2 last; 29 final T2 last;
30 30
31 const Pair(this.first, this.last); 31 const Pair(this.first, this.last);
32 32
33 bool operator==(other) => 33 bool operator ==(other) =>
34 other is Pair && first == other.first && last == other.last; 34 other is Pair && first == other.first && last == other.last;
35 35
36 int get hashCode => hash2(first, last); 36 int get hashCode => hash2(first, last);
37 } 37 }
38 38
39 /// Represents a pair of mininum and maximum values in a List. 39 /// Represents a pair of mininum and maximum values in a List.
40 class Extent<T> extends Pair<T, T> { 40 class Extent<T> extends Pair<T, T> {
41 final T min; 41 final T min;
42 final T max; 42 final T max;
43 43
44 factory Extent.items(Iterable<T> items, 44 factory Extent.items(Iterable<T> items,
45 [ Comparator compare = Comparable.compare ]) { 45 [Comparator compare = Comparable.compare]) {
46 if (items.length == 0) return new Extent(null, null); 46 if (items.length == 0) return new Extent(null, null);
47 var max = items.first, 47 var max = items.first, min = items.first;
48 min = items.first;
49 for (var value in items) { 48 for (var value in items) {
50 if (compare(max, value) < 0) max = value; 49 if (compare(max, value) < 0) max = value;
51 if (compare(min, value) > 0) min = value; 50 if (compare(min, value) > 0) min = value;
52 } 51 }
53 return new Extent(min, max); 52 return new Extent(min, max);
54 } 53 }
55 54
56 const Extent(T min, T max) : min = min, max = max, super(min, max); 55 const Extent(T min, T max)
56 : min = min,
57 max = max,
58 super(min, max);
57 } 59 }
58 60
59 /// Iterable representing a range of values containing the start, stop 61 /// Iterable representing a range of values containing the start, stop
60 /// and each of the step values between them. 62 /// and each of the step values between them.
61 class Range extends DelegatingList<num> { 63 class Range extends DelegatingList<num> {
62 final num start; 64 final num start;
63 final num stop; 65 final num stop;
64 final num step; 66 final num step;
65 67
66 factory Range.integers(num start, [num stop, num step = 1]) => 68 factory Range.integers(num start, [num stop, num step = 1]) =>
67 new Range(start, stop, step, true); 69 new Range(start, stop, step, true);
68 70
69 factory Range(num start, [num stop, num step = 1, bool integers = false]) { 71 factory Range(num start, [num stop, num step = 1, bool integers = false]) {
70 List<num> values = <num>[]; 72 List<num> values = <num>[];
71 73
72 if (stop == null) { 74 if (stop == null) {
73 stop = start; 75 stop = start;
74 start = 0; 76 start = 0;
75 } 77 }
76 78
77 if (step == 0 || start < stop && step < 0 || start > stop && step > 0) { 79 if (step == 0 || start < stop && step < 0 || start > stop && step > 0) {
78 throw new ArgumentError('Invalid range.'); 80 throw new ArgumentError('Invalid range.');
79 } 81 }
80 82
81 var k = _integerConversionFactor(step.abs()), 83 var k = _integerConversionFactor(step.abs()), i = -1, j;
82 i = -1,
83 j;
84 84
85 start *= k; 85 start *= k;
86 stop *= k; 86 stop *= k;
87 step *= k; 87 step *= k;
88 88
89 if (step < 0) { 89 if (step < 0) {
90 while ((j = start + step * ++i) > stop) { 90 while ((j = start + step * ++i) > stop) {
91 values.add(integers ? j ~/ k : j / k); 91 values.add(integers ? j ~/ k : j / k);
92 } 92 }
93 } else { 93 } else {
94 while ((j = start + step * ++i) < stop) { 94 while ((j = start + step * ++i) < stop) {
95 values.add(integers ? j ~/ k : j / k); 95 values.add(integers ? j ~/ k : j / k);
96 } 96 }
97 } 97 }
98 98
99 return new Range._internal(start, stop, step, values); 99 return new Range._internal(start, stop, step, values);
100 } 100 }
101 101
102 Range._internal(this.start, this.stop, this.step, List values) 102 Range._internal(this.start, this.stop, this.step, List values)
103 : super(values); 103 : super(values);
104 104
105 static int _integerConversionFactor(num val) { 105 static int _integerConversionFactor(num val) {
106 int k = 1; 106 int k = 1;
107 while (val * k % 1 > 0) { 107 while (val * k % 1 > 0) {
108 k *= 10; 108 k *= 10;
109 } 109 }
110 return k; 110 return k;
111 } 111 }
112 } 112 }
OLDNEW
« no previous file with comments | « packages/charted/lib/core/utils/color.dart ('k') | packages/charted/lib/core/utils/namespace.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698