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