| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 library engine.utilities.collection; | 5 library analyzer.src.generated.utilities_collection; |
| 6 | 6 |
| 7 import 'dart:collection'; |
| 7 import "dart:math" as math; | 8 import "dart:math" as math; |
| 8 import 'dart:collection'; | |
| 9 | 9 |
| 10 import 'java_core.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| 11 import 'scanner.dart' show Token; | 11 import 'package:analyzer/src/generated/java_core.dart'; |
| 12 | 12 |
| 13 /** | 13 /** |
| 14 * The class `BooleanArray` defines methods for operating on integers as if they
were arrays | 14 * Returns `true` if a and b contain equal elements in the same order. |
| 15 * of booleans. These arrays can be indexed by either integers or by enumeration
constants. | 15 */ |
| 16 bool listsEqual(List a, List b) { |
| 17 // TODO(rnystrom): package:collection also implements this, and analyzer |
| 18 // already transitively depends on that package. Consider using it instead. |
| 19 if (identical(a, b)) { |
| 20 return true; |
| 21 } |
| 22 |
| 23 if (a.length != b.length) { |
| 24 return false; |
| 25 } |
| 26 |
| 27 for (int i = 0; i < a.length; i++) { |
| 28 if (a[i] != b[i]) { |
| 29 return false; |
| 30 } |
| 31 } |
| 32 |
| 33 return true; |
| 34 } |
| 35 |
| 36 /** |
| 37 * Methods for operating on integers as if they were arrays of booleans. These |
| 38 * arrays can be indexed by either integers or by enumeration constants. |
| 16 */ | 39 */ |
| 17 class BooleanArray { | 40 class BooleanArray { |
| 18 /** | 41 /** |
| 19 * Return the value of the element at the given index. | 42 * Return the value of the element of the given [array] at the given [index]. |
| 20 * | |
| 21 * @param array the array being accessed | |
| 22 * @param index the index of the element being accessed | |
| 23 * @return the value of the element at the given index | |
| 24 * @throws IndexOutOfBoundsException if the index is not between zero (0) and
31, inclusive | |
| 25 */ | 43 */ |
| 26 static bool get(int array, int index) { | 44 static bool get(int array, int index) { |
| 27 _checkIndex(index); | 45 _checkIndex(index); |
| 28 return (array & (1 << index)) > 0; | 46 return (array & (1 << index)) > 0; |
| 29 } | 47 } |
| 30 | 48 |
| 31 /** | 49 /** |
| 32 * Return the value of the element at the given index. | 50 * Return the value of the element at the given index. |
| 33 * | |
| 34 * @param array the array being accessed | |
| 35 * @param index the index of the element being accessed | |
| 36 * @return the value of the element at the given index | |
| 37 * @throws IndexOutOfBoundsException if the index is not between zero (0) and
31, inclusive | |
| 38 */ | 51 */ |
| 52 @deprecated |
| 39 static bool getEnum(int array, Enum index) => get(array, index.ordinal); | 53 static bool getEnum(int array, Enum index) => get(array, index.ordinal); |
| 40 | 54 |
| 41 /** | 55 /** |
| 42 * Set the value of the element at the given index to the given value. | 56 * Set the value of the element of the given [array] at the given [index] to |
| 43 * | 57 * the given [value]. |
| 44 * @param array the array being modified | |
| 45 * @param index the index of the element being set | |
| 46 * @param value the value to be assigned to the element | |
| 47 * @return the updated value of the array | |
| 48 * @throws IndexOutOfBoundsException if the index is not between zero (0) and
31, inclusive | |
| 49 */ | 58 */ |
| 50 static int set(int array, int index, bool value) { | 59 static int set(int array, int index, bool value) { |
| 51 _checkIndex(index); | 60 _checkIndex(index); |
| 52 if (value) { | 61 if (value) { |
| 53 return array | (1 << index); | 62 return array | (1 << index); |
| 54 } else { | 63 } else { |
| 55 return array & ~(1 << index); | 64 return array & ~(1 << index); |
| 56 } | 65 } |
| 57 } | 66 } |
| 58 | 67 |
| 59 /** | 68 /** |
| 60 * Set the value of the element at the given index to the given value. | 69 * Set the value of the element at the given index to the given value. |
| 61 * | |
| 62 * @param array the array being modified | |
| 63 * @param index the index of the element being set | |
| 64 * @param value the value to be assigned to the element | |
| 65 * @return the updated value of the array | |
| 66 * @throws IndexOutOfBoundsException if the index is not between zero (0) and
31, inclusive | |
| 67 */ | 70 */ |
| 71 @deprecated |
| 68 static int setEnum(int array, Enum index, bool value) => | 72 static int setEnum(int array, Enum index, bool value) => |
| 69 set(array, index.ordinal, value); | 73 set(array, index.ordinal, value); |
| 70 | 74 |
| 71 /** | 75 /** |
| 72 * Throw an exception if the index is not within the bounds allowed for an int
eger-encoded array | 76 * Throw an exception if the index is not within the bounds allowed for an |
| 73 * of boolean values. | 77 * integer-encoded array of boolean values. |
| 74 * | |
| 75 * @throws IndexOutOfBoundsException if the index is not between zero (0) and
31, inclusive | |
| 76 */ | 78 */ |
| 77 static void _checkIndex(int index) { | 79 static void _checkIndex(int index) { |
| 78 if (index < 0 || index > 30) { | 80 if (index < 0 || index > 30) { |
| 79 throw new RangeError("Index not between 0 and 30: $index"); | 81 throw new RangeError("Index not between 0 and 30: $index"); |
| 80 } | 82 } |
| 81 } | 83 } |
| 82 } | 84 } |
| 83 | 85 |
| 84 /** | 86 /** |
| 85 * Instances of the class `DirectedGraph` implement a directed graph in which th
e nodes are | 87 * Instances of the class `DirectedGraph` implement a directed graph in which th
e nodes are |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 } | 175 } |
| 174 | 176 |
| 175 /** | 177 /** |
| 176 * Return a list of nodes that form a cycle containing the given node. If the
node is not part of | 178 * Return a list of nodes that form a cycle containing the given node. If the
node is not part of |
| 177 * this graph, then a list containing only the node itself will be returned. | 179 * this graph, then a list containing only the node itself will be returned. |
| 178 * | 180 * |
| 179 * @return a list of nodes that form a cycle containing the given node | 181 * @return a list of nodes that form a cycle containing the given node |
| 180 */ | 182 */ |
| 181 List<N> findCycleContaining(N node) { | 183 List<N> findCycleContaining(N node) { |
| 182 if (node == null) { | 184 if (node == null) { |
| 183 throw new IllegalArgumentException(); | 185 throw new ArgumentError(); |
| 184 } | 186 } |
| 185 DirectedGraph_SccFinder<N> finder = new DirectedGraph_SccFinder<N>(this); | 187 DirectedGraph_SccFinder<N> finder = new DirectedGraph_SccFinder<N>(this); |
| 186 return finder.componentContaining(node); | 188 return finder.componentContaining(node); |
| 187 } | 189 } |
| 188 | 190 |
| 189 /** | 191 /** |
| 190 * Return a set containing the tails of edges that have the given node as thei
r head. The set will | 192 * Return a set containing the tails of edges that have the given node as thei
r head. The set will |
| 191 * be empty if there are no such edges or if the node is not part of the graph
. Clients must not | 193 * be empty if there are no such edges or if the node is not part of the graph
. Clients must not |
| 192 * modify the returned set. | 194 * modify the returned set. |
| 193 * | 195 * |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 component.add(w); | 461 component.add(w); |
| 460 _nodeMap[w].component = component; | 462 _nodeMap[w].component = component; |
| 461 } while (!identical(w, v)); | 463 } while (!identical(w, v)); |
| 462 _allComponents.add(component); | 464 _allComponents.add(component); |
| 463 } | 465 } |
| 464 return vInfo; | 466 return vInfo; |
| 465 } | 467 } |
| 466 } | 468 } |
| 467 | 469 |
| 468 /** | 470 /** |
| 469 * The class `ListUtilities` defines utility methods useful for working with [Li
st | |
| 470 ]. | |
| 471 */ | |
| 472 class ListUtilities { | |
| 473 /** | |
| 474 * Add all of the elements in the given array to the given list. | |
| 475 * | |
| 476 * @param list the list to which the elements are to be added | |
| 477 * @param elements the elements to be added to the list | |
| 478 */ | |
| 479 static void addAll(List list, List<Object> elements) { | |
| 480 int count = elements.length; | |
| 481 for (int i = 0; i < count; i++) { | |
| 482 list.add(elements[i]); | |
| 483 } | |
| 484 } | |
| 485 } | |
| 486 | |
| 487 /** | |
| 488 * The interface `MapIterator` defines the behavior of objects that iterate over
the entries | 471 * The interface `MapIterator` defines the behavior of objects that iterate over
the entries |
| 489 * in a map. | 472 * in a map. |
| 490 * | 473 * |
| 491 * This interface defines the concept of a current entry and provides methods to
access the key and | 474 * This interface defines the concept of a current entry and provides methods to
access the key and |
| 492 * value in the current entry. When an iterator is first created it will be posi
tioned before the | 475 * value in the current entry. When an iterator is first created it will be posi
tioned before the |
| 493 * first entry and there is no current entry until [moveNext] is invoked. When a
ll of the | 476 * first entry and there is no current entry until [moveNext] is invoked. When a
ll of the |
| 494 * entries have been accessed there will also be no current entry. | 477 * entries have been accessed there will also be no current entry. |
| 495 * | 478 * |
| 496 * There is no guarantee made about the order in which the entries are accessibl
e. | 479 * There is no guarantee made about the order in which the entries are accessibl
e. |
| 497 */ | 480 */ |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 559 int count = maps.length; | 542 int count = maps.length; |
| 560 _iterators = new List<MapIterator<K, V>>(count); | 543 _iterators = new List<MapIterator<K, V>>(count); |
| 561 for (int i = 0; i < count; i++) { | 544 for (int i = 0; i < count; i++) { |
| 562 _iterators[i] = new SingleMapIterator<K, V>(maps[i]); | 545 _iterators[i] = new SingleMapIterator<K, V>(maps[i]); |
| 563 } | 546 } |
| 564 } | 547 } |
| 565 | 548 |
| 566 @override | 549 @override |
| 567 K get key { | 550 K get key { |
| 568 if (_currentIterator == null) { | 551 if (_currentIterator == null) { |
| 569 throw new NoSuchElementException(); | 552 throw new StateError('No element'); |
| 570 } | 553 } |
| 571 return _currentIterator.key; | 554 return _currentIterator.key; |
| 572 } | 555 } |
| 573 | 556 |
| 574 @override | 557 @override |
| 575 V get value { | 558 V get value { |
| 576 if (_currentIterator == null) { | 559 if (_currentIterator == null) { |
| 577 throw new NoSuchElementException(); | 560 throw new StateError('No element'); |
| 578 } | 561 } |
| 579 return _currentIterator.value; | 562 return _currentIterator.value; |
| 580 } | 563 } |
| 581 | 564 |
| 582 @override | 565 @override |
| 583 void set value(V newValue) { | 566 void set value(V newValue) { |
| 584 if (_currentIterator == null) { | 567 if (_currentIterator == null) { |
| 585 throw new NoSuchElementException(); | 568 throw new StateError('No element'); |
| 586 } | 569 } |
| 587 _currentIterator.value = newValue; | 570 _currentIterator.value = newValue; |
| 588 } | 571 } |
| 589 | 572 |
| 590 @override | 573 @override |
| 591 bool moveNext() { | 574 bool moveNext() { |
| 592 if (_iteratorIndex < 0) { | 575 if (_iteratorIndex < 0) { |
| 593 if (_iterators.length == 0) { | 576 if (_iterators.length == 0) { |
| 594 _currentIterator = null; | 577 _currentIterator = null; |
| 595 return false; | 578 return false; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 661 * | 644 * |
| 662 * @param map the map containing the entries to be iterated over | 645 * @param map the map containing the entries to be iterated over |
| 663 */ | 646 */ |
| 664 SingleMapIterator(this._map) { | 647 SingleMapIterator(this._map) { |
| 665 this._keyIterator = _map.keys.iterator; | 648 this._keyIterator = _map.keys.iterator; |
| 666 } | 649 } |
| 667 | 650 |
| 668 @override | 651 @override |
| 669 K get key { | 652 K get key { |
| 670 if (_currentKey == null) { | 653 if (_currentKey == null) { |
| 671 throw new NoSuchElementException(); | 654 throw new StateError('No element'); |
| 672 } | 655 } |
| 673 return _currentKey; | 656 return _currentKey; |
| 674 } | 657 } |
| 675 | 658 |
| 676 @override | 659 @override |
| 677 V get value { | 660 V get value { |
| 678 if (_currentKey == null) { | 661 if (_currentKey == null) { |
| 679 throw new NoSuchElementException(); | 662 throw new StateError('No element'); |
| 680 } | 663 } |
| 681 if (_currentValue == null) { | 664 if (_currentValue == null) { |
| 682 _currentValue = _map[_currentKey]; | 665 _currentValue = _map[_currentKey]; |
| 683 } | 666 } |
| 684 return _currentValue; | 667 return _currentValue; |
| 685 } | 668 } |
| 686 | 669 |
| 687 @override | 670 @override |
| 688 void set value(V newValue) { | 671 void set value(V newValue) { |
| 689 if (_currentKey == null) { | 672 if (_currentKey == null) { |
| 690 throw new NoSuchElementException(); | 673 throw new StateError('No element'); |
| 691 } | 674 } |
| 692 _currentValue = newValue; | 675 _currentValue = newValue; |
| 693 _map[_currentKey] = newValue; | 676 _map[_currentKey] = newValue; |
| 694 } | 677 } |
| 695 | 678 |
| 696 @override | 679 @override |
| 697 bool moveNext() { | 680 bool moveNext() { |
| 698 if (_keyIterator.moveNext()) { | 681 if (_keyIterator.moveNext()) { |
| 699 _currentKey = _keyIterator.current; | 682 _currentKey = _keyIterator.current; |
| 700 _currentValue = null; | 683 _currentValue = null; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 734 /** | 717 /** |
| 735 * Map the key to the value. | 718 * Map the key to the value. |
| 736 * | 719 * |
| 737 * @param key the token being mapped to the value | 720 * @param key the token being mapped to the value |
| 738 * @param value the token to which the key will be mapped | 721 * @param value the token to which the key will be mapped |
| 739 */ | 722 */ |
| 740 void put(Token key, Token value) { | 723 void put(Token key, Token value) { |
| 741 _map[key] = value; | 724 _map[key] = value; |
| 742 } | 725 } |
| 743 } | 726 } |
| OLD | NEW |