multimap.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.MultiMap = void 0;
  6. let _Symbol$iterator;
  7. _Symbol$iterator = Symbol.iterator;
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class MultiMap {
  24. constructor() {
  25. this._map = void 0;
  26. this._map = new Map();
  27. }
  28. set(key, value) {
  29. let values = this._map.get(key);
  30. if (!values) {
  31. values = [];
  32. this._map.set(key, values);
  33. }
  34. values.push(value);
  35. }
  36. get(key) {
  37. return this._map.get(key) || [];
  38. }
  39. has(key) {
  40. return this._map.has(key);
  41. }
  42. delete(key, value) {
  43. const values = this._map.get(key);
  44. if (!values) return;
  45. if (values.includes(value)) this._map.set(key, values.filter(v => value !== v));
  46. }
  47. deleteAll(key) {
  48. this._map.delete(key);
  49. }
  50. hasValue(key, value) {
  51. const values = this._map.get(key);
  52. if (!values) return false;
  53. return values.includes(value);
  54. }
  55. get size() {
  56. return this._map.size;
  57. }
  58. [_Symbol$iterator]() {
  59. return this._map[Symbol.iterator]();
  60. }
  61. keys() {
  62. return this._map.keys();
  63. }
  64. values() {
  65. const result = [];
  66. for (const key of this.keys()) result.push(...this.get(key));
  67. return result;
  68. }
  69. clear() {
  70. this._map.clear();
  71. }
  72. }
  73. exports.MultiMap = MultiMap;