Remove Maps; move _fillMapWith* methods to MapBase

Fixes #31843

Bug: https://github.com/dart-lang/sdk/issues/31843
Change-Id: I02c544c921951f4a50421205dc1f25997cbecd6e
Reviewed-on: https://dart-review.googlesource.com/39880
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
This commit is contained in:
Sam Rawlins 2018-03-26 21:23:21 +00:00 committed by commit-bot@chromium.org
parent 742ddd104a
commit 301b5a1f16
11 changed files with 60 additions and 113 deletions

View file

@ -6,6 +6,10 @@
### Core library changes
* `dart:collection`
* Removed `Maps` class. Extend `MapBase` or mix in `MapMixin` instead to
provide map method implementations for a class.
### Tool Changes
## 2.0.0-dev.42.0

View file

@ -8,7 +8,7 @@ import 'dart:_js_helper' show argumentErrorValue, patch;
import 'dart:_foreign_helper' show JS;
import 'dart:_interceptors' show JSExtendableArray;
import 'dart:_internal' show MappedIterable, ListIterable;
import 'dart:collection' show Maps, LinkedHashMap, MapBase;
import 'dart:collection' show LinkedHashMap, MapBase;
import 'dart:_native_typed_data' show NativeUint8List;
/**

View file

@ -133,7 +133,7 @@ abstract class HashMap<K, V> implements Map<K, V> {
factory HashMap.fromIterable(Iterable iterable,
{K key(element), V value(element)}) {
Map<K, V> map = new HashMap<K, V>();
Maps._fillMapWithMappedIterable(map, iterable, key, value);
MapBase._fillMapWithMappedIterable(map, iterable, key, value);
return map;
}
@ -150,7 +150,7 @@ abstract class HashMap<K, V> implements Map<K, V> {
*/
factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
Map<K, V> map = new HashMap<K, V>();
Maps._fillMapWithIterables(map, keys, values);
MapBase._fillMapWithIterables(map, keys, values);
return map;
}
}

View file

@ -121,7 +121,7 @@ abstract class LinkedHashMap<K, V> implements Map<K, V> {
factory LinkedHashMap.fromIterable(Iterable iterable,
{K key(element), V value(element)}) {
LinkedHashMap<K, V> map = new LinkedHashMap<K, V>();
Maps._fillMapWithMappedIterable(map, iterable, key, value);
MapBase._fillMapWithMappedIterable(map, iterable, key, value);
return map;
}
@ -138,7 +138,7 @@ abstract class LinkedHashMap<K, V> implements Map<K, V> {
*/
factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values) {
LinkedHashMap<K, V> map = new LinkedHashMap<K, V>();
Maps._fillMapWithIterables(map, keys, values);
MapBase._fillMapWithIterables(map, keys, values);
return map;
}
}

View file

@ -49,6 +49,48 @@ abstract class MapBase<K, V> extends MapMixin<K, V> {
return result.toString();
}
static _id(x) => x;
/**
* Fills a [Map] with key/value pairs computed from [iterable].
*
* This method is used by [Map] classes in the named constructor
* `fromIterable`.
*/
static void _fillMapWithMappedIterable(
Map map, Iterable iterable, key(element), value(element)) {
key ??= _id;
value ??= _id;
for (var element in iterable) {
map[key(element)] = value(element);
}
}
/**
* Fills a map by associating the [keys] to [values].
*
* This method is used by [Map] classes in the named constructor
* `fromIterables`.
*/
static void _fillMapWithIterables(Map map, Iterable keys, Iterable values) {
Iterator keyIterator = keys.iterator;
Iterator valueIterator = values.iterator;
bool hasNextKey = keyIterator.moveNext();
bool hasNextValue = valueIterator.moveNext();
while (hasNextKey && hasNextValue) {
map[keyIterator.current] = valueIterator.current;
hasNextKey = keyIterator.moveNext();
hasNextValue = valueIterator.moveNext();
}
if (hasNextKey || hasNextValue) {
throw new ArgumentError("Iterables do not have same length.");
}
}
}
/**
@ -347,109 +389,3 @@ class UnmodifiableMapView<K, V> extends MapView<K, V>
Map<RK, RV> retype<RK, RV>() =>
new UnmodifiableMapView<RK, RV>(_map.retype<RK, RV>());
}
/**
* Helper class which implements complex [Map] operations
* in term of basic ones ([Map.keys], [Map.[]],
* [Map.[]=] and [Map.remove].) Not all methods are
* necessary to implement each particular operation.
*
* Deprecated. Will be removed in Dart 2.
*/
class Maps {
static bool containsValue(Map map, Object value) {
for (final v in map.values) {
if (v == value) {
return true;
}
}
return false;
}
static bool containsKey(Map map, Object key) {
for (final k in map.keys) {
if (k == key) {
return true;
}
}
return false;
}
static putIfAbsent(Map map, key, ifAbsent()) {
if (map.containsKey(key)) {
return map[key];
}
final v = ifAbsent();
map[key] = v;
return v;
}
static clear(Map map) {
for (final k in map.keys.toList()) {
map.remove(k);
}
}
static forEach(Map map, void f(key, value)) {
for (final k in map.keys) {
f(k, map[k]);
}
}
static Iterable getValues(Map map) {
return map.keys.map((key) => map[key]);
}
static int length(Map map) => map.keys.length;
static bool isEmpty(Map map) => map.keys.isEmpty;
static bool isNotEmpty(Map map) => map.keys.isNotEmpty;
/**
* Do not use. This entire class will be removed.
*
* Use [MapBase.mapToString] instead.
*/
static String mapToString(Map m) => MapBase.mapToString(m);
static _id(x) => x;
/**
* Fills a map with key/value pairs computed from [iterable].
*
* This method is used by Map classes in the named constructor fromIterable.
*/
static void _fillMapWithMappedIterable(
Map map, Iterable iterable, key(element), value(element)) {
if (key == null) key = _id;
if (value == null) value = _id;
for (var element in iterable) {
map[key(element)] = value(element);
}
}
/**
* Fills a map by associating the [keys] to [values].
*
* This method is used by Map classes in the named constructor fromIterables.
*/
static void _fillMapWithIterables(Map map, Iterable keys, Iterable values) {
Iterator keyIterator = keys.iterator;
Iterator valueIterator = values.iterator;
bool hasNextKey = keyIterator.moveNext();
bool hasNextValue = valueIterator.moveNext();
while (hasNextKey && hasNextValue) {
map[keyIterator.current] = valueIterator.current;
hasNextKey = keyIterator.moveNext();
hasNextValue = valueIterator.moveNext();
}
if (hasNextKey || hasNextValue) {
throw new ArgumentError("Iterables do not have same length.");
}
}
}

View file

@ -334,7 +334,7 @@ class SplayTreeMap<K, V> extends _SplayTree<K, _SplayTreeMapNode<K, V>>
int compare(K key1, K key2),
bool isValidKey(potentialKey)}) {
SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey);
Maps._fillMapWithMappedIterable(map, iterable, key, value);
MapBase._fillMapWithMappedIterable(map, iterable, key, value);
return map;
}
@ -352,7 +352,7 @@ class SplayTreeMap<K, V> extends _SplayTree<K, _SplayTreeMapNode<K, V>>
factory SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values,
[int compare(K key1, K key2), bool isValidKey(potentialKey)]) {
SplayTreeMap<K, V> map = new SplayTreeMap<K, V>(compare, isValidKey);
Maps._fillMapWithIterables(map, keys, values);
MapBase._fillMapWithIterables(map, keys, values);
return map;
}

View file

@ -116,6 +116,7 @@ LibTest/collection/LinkedHashSet/LinkedHashSet.from_A03_t01: RuntimeError # Plea
LibTest/collection/LinkedList/add_A01_t01: Pass, Slow # Slow tests that needs extra time to finish.
LibTest/collection/ListBase/ListBase_class_A01_t01: RuntimeError # Please triage this failure
LibTest/collection/ListMixin/ListMixin_class_A01_t01: RuntimeError # Please triage this failure
LibTest/collection/Maps/*: Skip # Maps class no longer exists
LibTest/convert/JsonCodec/encode_A01_t01: RuntimeError # code.google.com/p/co19/issues/detail?id=735
LibTest/convert/JsonCodec/encode_A01_t02: RuntimeError # code.google.com/p/co19/issues/detail?id=735
LibTest/convert/JsonDecoder/fuse_A01_t01: RuntimeError # co19-roll r667: Please triage this failure

View file

@ -7,6 +7,7 @@ Language/Classes/definition_t24: MissingCompileTimeError
Language/Types/Type_Void/syntax_t08: MissingCompileTimeError
Language/Types/Type_Void/syntax_t09: MissingCompileTimeError
LayoutTests/*: Skip # TODO(ahe): Make dart:html available.
LibTest/collection/Maps/*: Skip # Maps class no longer exists.
LibTest/html/*: Skip # TODO(ahe): Make dart:html available.
WebPlatformTest/*: Skip # TODO(ahe): Make dart:html available.

View file

@ -303,6 +303,7 @@ Language/Types/Type_Void/syntax_t09: MissingCompileTimeError # Issue co19/30264
LayoutTests/fast/*: SkipByDesign # DOM not supported on VM.
LibTest/collection/ListBase/ListBase_class_A01_t01: RuntimeError # Large integers
LibTest/collection/ListMixin/ListMixin_class_A01_t01: RuntimeError # Large integers
LibTest/collection/Maps/*: Skip # Maps class no longer exists
LibTest/core/Duration/inDays_A01_t01: RuntimeError # Large integers
LibTest/core/Duration/inHours_A01_t01: RuntimeError # Large integers
LibTest/core/Duration/inMilliseconds_A01_t01: RuntimeError # Large integers

View file

@ -2,6 +2,8 @@
# for details. All rights reserved. Use of this source code is governed by a
# BSD-style license that can be found in the LICENSE file.
maps_test: Skip # Maps class no longer exists
[ $compiler == dart2analyzer ]
*: Skip

View file

@ -3,6 +3,8 @@
# BSD-style license that can be found in the LICENSE file.
iterable_where_type_test: RuntimeError # Disabled. Issue 32463
maps_test: Skip # Maps class no longer exists
[ $compiler == dart2analyzer ]
bigint_from_test: CompileTimeError # Issue 32585
compare_to2_test: CompileTimeError # invalid test