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

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

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

Powered by Google App Engine
This is Rietveld 408576698