* Creates a Set with custom equality and hash code functionality. This is useful when you * want to use something looser than object identity - e.g. "has the same span". * * If `equals(a, b)`, it must be the case that `getHashCode(a) === getHashCode(b)`. * The converse is not re
(getHashCode, equals)
| 1673 | * assumed not to be an array type. |
| 1674 | */ |
| 1675 | function createSet(getHashCode, equals) { |
| 1676 | var multiMap = new ts.Map(); |
| 1677 | var size = 0; |
| 1678 | function getElementIterator() { |
| 1679 | var valueIt = multiMap.values(); |
| 1680 | var arrayIt; |
| 1681 | return { |
| 1682 | next: function () { |
| 1683 | while (true) { |
| 1684 | if (arrayIt) { |
| 1685 | var n = arrayIt.next(); |
| 1686 | if (!n.done) { |
| 1687 | return { value: n.value }; |
| 1688 | } |
| 1689 | arrayIt = undefined; |
| 1690 | } |
| 1691 | else { |
| 1692 | var n = valueIt.next(); |
| 1693 | if (n.done) { |
| 1694 | return { value: undefined, done: true }; |
| 1695 | } |
| 1696 | if (!isArray(n.value)) { |
| 1697 | return { value: n.value }; |
| 1698 | } |
| 1699 | arrayIt = arrayIterator(n.value); |
| 1700 | } |
| 1701 | } |
| 1702 | } |
| 1703 | }; |
| 1704 | } |
| 1705 | var set = { |
| 1706 | has: function (element) { |
| 1707 | var hash = getHashCode(element); |
| 1708 | if (!multiMap.has(hash)) |
| 1709 | return false; |
| 1710 | var candidates = multiMap.get(hash); |
| 1711 | if (!isArray(candidates)) |
| 1712 | return equals(candidates, element); |
| 1713 | for (var _i = 0, candidates_1 = candidates; _i < candidates_1.length; _i++) { |
| 1714 | var candidate = candidates_1[_i]; |
| 1715 | if (equals(candidate, element)) { |
| 1716 | return true; |
| 1717 | } |
| 1718 | } |
| 1719 | return false; |
| 1720 | }, |
| 1721 | add: function (element) { |
| 1722 | var hash = getHashCode(element); |
| 1723 | if (multiMap.has(hash)) { |
| 1724 | var values = multiMap.get(hash); |
| 1725 | if (isArray(values)) { |
| 1726 | if (!contains(values, element, equals)) { |
| 1727 | values.push(element); |
| 1728 | size++; |
| 1729 | } |
| 1730 | } |
| 1731 | else { |
| 1732 | var value = values; |
nothing calls this directly
no test coverage detected
searching dependent graphs…