Move binarySearch implementation in animated_list to foundation/collection.dart. (#29860)
This commit is contained in:
@@ -45,3 +45,26 @@ bool listEquals<T>(List<T> a, List<T> b) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Returns the position of `value` in the `sortedList`, if it exists.
|
||||
///
|
||||
/// Returns `-1` if the `value` is not in the list. Requires the list items
|
||||
/// to implement [Comparable] and the `sortedList` to already be ordered.
|
||||
int binarySearch<T extends Comparable<Object>>(List<T> sortedList, T value) {
|
||||
int min = 0;
|
||||
int max = sortedList.length;
|
||||
while (min < max) {
|
||||
final int mid = min + ((max - min) >> 1);
|
||||
final T element = sortedList[mid];
|
||||
final int comp = element.compareTo(value);
|
||||
if (comp == 0) {
|
||||
return mid;
|
||||
}
|
||||
if (comp < 0) {
|
||||
min = mid + 1;
|
||||
} else {
|
||||
max = mid;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -74,13 +74,17 @@ class _TaskEntry<T> {
|
||||
Completer<T> completer;
|
||||
|
||||
void run() {
|
||||
Timeline.timeSync(
|
||||
debugLabel ?? 'Scheduled Task',
|
||||
() {
|
||||
completer.complete(task());
|
||||
},
|
||||
flow: flow != null ? Flow.step(flow.id) : null,
|
||||
);
|
||||
if (!kReleaseMode) {
|
||||
Timeline.timeSync(
|
||||
debugLabel ?? 'Scheduled Task',
|
||||
() {
|
||||
completer.complete(task());
|
||||
},
|
||||
flow: flow != null ? Flow.step(flow.id) : null,
|
||||
);
|
||||
} else {
|
||||
completer.complete(task());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:collection/collection.dart' show binarySearch;
|
||||
|
||||
import 'package:flutter/animation.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import 'basic.dart';
|
||||
import 'framework.dart';
|
||||
|
||||
17
packages/flutter/test/foundation/binary_search_test.dart
Normal file
17
packages/flutter/test/foundation/binary_search_test.dart
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter/src/foundation/collections.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
test('binarySearch', () {
|
||||
final List<int> items = <int>[1, 2, 3];
|
||||
|
||||
expect(binarySearch(items, 1), 0);
|
||||
expect(binarySearch(items, 2), 1);
|
||||
expect(binarySearch(items, 3), 2);
|
||||
expect(binarySearch(items, 12), -1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user