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

Side by Side Diff: sdk/lib/_internal/compiler/js_lib/constant_map.dart

Issue 1127533002: Add Map.unmodifiable constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of _js_helper; 5 part of _js_helper;
6 6
7 class ConstantMapView<K, V> extends UnmodifiableMapView
8 implements ConstantMap {
9 ConstantMapView(Map base) : super(base);
sra1 2015/05/07 19:24:42 indentation
10 }
11
7 abstract class ConstantMap<K, V> implements Map<K, V> { 12 abstract class ConstantMap<K, V> implements Map<K, V> {
13 // Used to create unmodifiable maps from other maps.
14 factory ConstantMap.from(Map other) {
sra1 2015/05/07 19:24:41 Under what conditions can you return 'other'? Or c
15 List keys = other.keys.toList();
16 bool allStrings = true;
17 for (var k in keys) {
18 if (k is! String) {
19 allStrings = false;
20 break;
21 }
22 }
23 if (allStrings) {
24 bool containsProto = false;
25 var protoValue = null;
26 var object = JS('=Object', '{}');
27 int length = 0;
28 for (var k in keys) {
29 var v = other[k];
30 if (k != "__proto__") {
sra1 2015/05/07 19:24:42 Use single quotes for consistency with rest of fil
31 if (!jsHasOwnProperty(object, k)) length++;
32 JS("void", "#[#] = #", object, k, v);
sra1 2015/05/07 19:24:42 ditto
33 } else {
34 containsProto = true;
35 protoValue = v;
36 }
37 }
38 if (containsProto) {
39 length++;
40 return new ConstantProtoMap<K, V>._(length, object, keys, protoValue);
41 }
42 return new ConstantStringMap<K, V>._(length, object, keys);
43 }
44 // TODO(lrn): Make a proper unmodifiable map implementation.
45 return new ConstantMapView<K, V>(new Map.from(other));
46 }
47
8 const ConstantMap._(); 48 const ConstantMap._();
9 49
10 bool get isEmpty => length == 0; 50 bool get isEmpty => length == 0;
11 51
12 bool get isNotEmpty => !isEmpty; 52 bool get isNotEmpty => !isEmpty;
13 53
14 String toString() => Maps.mapToString(this); 54 String toString() => Maps.mapToString(this);
15 55
16 _throwUnmodifiable() { 56 static _throwUnmodifiable() {
17 throw new UnsupportedError("Cannot modify unmodifiable Map"); 57 throw new UnsupportedError("Cannot modify unmodifiable Map");
18 } 58 }
19 void operator []=(K key, V val) => _throwUnmodifiable(); 59 void operator []=(K key, V val) => _throwUnmodifiable();
20 V putIfAbsent(K key, V ifAbsent()) => _throwUnmodifiable(); 60 V putIfAbsent(K key, V ifAbsent()) => _throwUnmodifiable();
21 V remove(K key) => _throwUnmodifiable(); 61 V remove(K key) => _throwUnmodifiable();
22 void clear() => _throwUnmodifiable(); 62 void clear() => _throwUnmodifiable();
23 void addAll(Map<K, V> other) => _throwUnmodifiable(); 63 void addAll(Map<K, V> other) => _throwUnmodifiable();
24 } 64 }
25 65
26 class ConstantStringMap<K, V> extends ConstantMap<K, V> 66 class ConstantStringMap<K, V> extends ConstantMap<K, V>
27 implements _symbol_dev.EfficientLength { 67 implements _symbol_dev.EfficientLength {
28 68
29 // This constructor is not used. The instantiation is shortcut by the 69 // This constructor is not used for actual compile-time constants.
30 // compiler. It is here to make the uninitialized final fields legal. 70 // The instantiation of constant maps is shortcut by the compiler.
31 const ConstantStringMap._(this.length, this._jsObject, this._keys) 71 const ConstantStringMap._(this.length, this._jsObject, this._keys)
32 : super._(); 72 : super._();
33 73
34 final int length; 74 final int length;
35 // A constant map is backed by a JavaScript object. 75 // A constant map is backed by a JavaScript object.
36 final _jsObject; 76 final _jsObject;
37 final List<K> _keys; 77 final List<K> _keys;
38 78
39 bool containsValue(V needle) { 79 bool containsValue(V needle) {
40 return values.any((V value) => value == needle); 80 return values.any((V value) => value == needle);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 Iterable<K> get keys { 180 Iterable<K> get keys {
141 return _getMap().keys; 181 return _getMap().keys;
142 } 182 }
143 183
144 Iterable<V> get values { 184 Iterable<V> get values {
145 return _getMap().values; 185 return _getMap().values;
146 } 186 }
147 187
148 int get length => _getMap().length; 188 int get length => _getMap().length;
149 } 189 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698