| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library services.src.index.store.collection; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 import 'dart:typed_data' show Uint32List; | |
| 9 | |
| 10 import 'package:analyzer/src/generated/utilities_general.dart'; | |
| 11 | |
| 12 /** | |
| 13 * A hash map with `List<int>` keys and [int] values. | |
| 14 */ | |
| 15 class IntArrayToIntMap { | |
| 16 final Map<Uint32List, int> map = new HashMap<Uint32List, int>( | |
| 17 equals: _intArrayEquals, hashCode: _intArrayHashCode); | |
| 18 | |
| 19 /** | |
| 20 * Returns the value for the given [key] or null if [key] is not in the map. | |
| 21 */ | |
| 22 int operator [](List<int> key) { | |
| 23 Uint32List typedKey = _getTypedKey(key); | |
| 24 return map[typedKey]; | |
| 25 } | |
| 26 | |
| 27 /** | |
| 28 * Associates the [key] with the given [value]. | |
| 29 * | |
| 30 * If the key was already in the map, its associated value is changed. | |
| 31 * Otherwise the key-value pair is added to the map. | |
| 32 */ | |
| 33 void operator []=(List<int> key, int value) { | |
| 34 Uint32List typedKey = _getTypedKey(key); | |
| 35 map[typedKey] = value; | |
| 36 } | |
| 37 | |
| 38 /** | |
| 39 * Returns an [Uint32List] version of the given `List<int>` key. | |
| 40 */ | |
| 41 static Uint32List _getTypedKey(List<int> key) { | |
| 42 if (key is Uint32List) { | |
| 43 return key; | |
| 44 } | |
| 45 return new Uint32List.fromList(key); | |
| 46 } | |
| 47 | |
| 48 static bool _intArrayEquals(List<int> a, List<int> b) { | |
| 49 int length = a.length; | |
| 50 if (length != b.length) { | |
| 51 return false; | |
| 52 } | |
| 53 for (int i = 0; i < length; i++) { | |
| 54 if (a[i] != b[i]) { | |
| 55 return false; | |
| 56 } | |
| 57 } | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 static int _intArrayHashCode(List<int> key) { | |
| 62 return key.fold(0, JenkinsSmiHash.combine); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 /** | |
| 67 * A table mapping [int] keys to sets of [int]s. | |
| 68 */ | |
| 69 class IntToIntSetMap { | |
| 70 final Map<int, Uint32List> _map = new HashMap<int, Uint32List>(); | |
| 71 | |
| 72 /** | |
| 73 * The number of key-value pairs in the map. | |
| 74 */ | |
| 75 int get length => _map.length; | |
| 76 | |
| 77 /** | |
| 78 * Adds the [value] to the set associated with the given [value]. | |
| 79 */ | |
| 80 void add(int key, int value) { | |
| 81 Uint32List values = _map[key]; | |
| 82 if (values == null) { | |
| 83 values = new Uint32List(1); | |
| 84 values[0] = value; | |
| 85 _map[key] = values; | |
| 86 } | |
| 87 if (values.indexOf(value) == -1) { | |
| 88 int length = values.length; | |
| 89 Uint32List newSet = new Uint32List(length + 1); | |
| 90 newSet.setRange(0, length, values); | |
| 91 newSet[length] = value; | |
| 92 _map[key] = newSet; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * Removes all pairs from the map. | |
| 98 */ | |
| 99 void clear() { | |
| 100 _map.clear(); | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Returns the set of [int]s for the given [key] or an empty list if [key] is | |
| 105 * not in the map. | |
| 106 */ | |
| 107 List<int> get(int key) { | |
| 108 List<int> values = _map[key]; | |
| 109 if (values == null) { | |
| 110 values = <int>[]; | |
| 111 } | |
| 112 return values; | |
| 113 } | |
| 114 } | |
| OLD | NEW |