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

Side by Side Diff: pkg/serialization/lib/src/serialization_helpers.dart

Issue 17379025: pkg/serialization: cleanup (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 6 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 /** 5 /**
6 * This contains extra functions and classes useful for implementing 6 * This contains extra functions and classes useful for implementing
7 * serialiation. Some or all of these will be removed once the functionality is 7 * serialiation. Some or all of these will be removed once the functionality is
8 * available in the core library. 8 * available in the core library.
9 */ 9 */
10 library serialization_helpers; 10 library serialization_helpers;
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 void operator []=(K key, V value) { 198 void operator []=(K key, V value) {
199 var index = _indexOf(key); 199 var index = _indexOf(key);
200 if (index == -1) { 200 if (index == -1) {
201 keys.add(key); 201 keys.add(key);
202 values.add(value); 202 values.add(value);
203 } else { 203 } else {
204 values[index] = value; 204 values[index] = value;
205 } 205 }
206 } 206 }
207 207
208 putIfAbsent(K key, Function ifAbsent) { 208 V putIfAbsent(K key, Function ifAbsent) {
209 var index = _indexOf(key); 209 var index = _indexOf(key);
210 if (index == -1) { 210 if (index == -1) {
211 keys.add(key); 211 keys.add(key);
212 values.add(ifAbsent()); 212 values.add(ifAbsent());
213 return values.last; 213 return values.last;
214 } else { 214 } else {
215 return values[index]; 215 return values[index];
216 } 216 }
217 } 217 }
218 218
219 _indexOf(K key) { 219 int _indexOf(K key) {
220 // Go backwards on the guess that we are most likely to access the most 220 // Go backwards on the guess that we are most likely to access the most
221 // recently added. 221 // recently added.
222 // Make strings and primitives unique 222 // Make strings and primitives unique
223 var compareEquality = isPrimitive(key); 223 var compareEquality = isPrimitive(key);
224 for (var i = keys.length - 1; i >= 0; i--) { 224 for (var i = keys.length - 1; i >= 0; i--) {
225 var equal = compareEquality ? key == keys[i] : identical(key, keys[i]); 225 var equal = compareEquality ? key == keys[i] : identical(key, keys[i]);
226 if (equal) return i; 226 if (equal) return i;
227 } 227 }
228 return -1; 228 return -1;
229 } 229 }
(...skipping 15 matching lines...) Expand all
245 int get length => keys.length; 245 int get length => keys.length;
246 void clear() { 246 void clear() {
247 keys.clear(); 247 keys.clear();
248 values.clear(); 248 values.clear();
249 } 249 }
250 bool get isEmpty => keys.isEmpty; 250 bool get isEmpty => keys.isEmpty;
251 bool get isNotEmpty => !isEmpty; 251 bool get isNotEmpty => !isEmpty;
252 252
253 // Note that this is doing an equality comparison. 253 // Note that this is doing an equality comparison.
254 bool containsValue(x) => values.contains(x); 254 bool containsValue(x) => values.contains(x);
255
256 void addAll(Map<K, V> other) {
257 other.forEach((K key, V value) {
258 this[key] = value;
259 });
260 }
255 } 261 }
OLDNEW
« no previous file with comments | « no previous file | pkg/serialization/lib/src/serialization_rule.dart » ('j') | pkg/serialization/lib/src/serialization_rule.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698