Multimap.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright 2017 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @template T
  18. * @template V
  19. */
  20. class Multimap {
  21. constructor() {
  22. this._map = new Map();
  23. }
  24. /**
  25. * @param {T} key
  26. * @param {V} value
  27. */
  28. set(key, value) {
  29. let set = this._map.get(key);
  30. if (!set) {
  31. set = new Set();
  32. this._map.set(key, set);
  33. }
  34. set.add(value);
  35. }
  36. /**
  37. * @param {T} key
  38. * @return {!Set<V>}
  39. */
  40. get(key) {
  41. let result = this._map.get(key);
  42. if (!result)
  43. result = new Set();
  44. return result;
  45. }
  46. /**
  47. * @param {T} key
  48. * @return {boolean}
  49. */
  50. has(key) {
  51. return this._map.has(key);
  52. }
  53. /**
  54. * @param {T} key
  55. * @param {V} value
  56. * @return {boolean}
  57. */
  58. hasValue(key, value) {
  59. const set = this._map.get(key);
  60. if (!set)
  61. return false;
  62. return set.has(value);
  63. }
  64. /**
  65. * @return {number}
  66. */
  67. get size() {
  68. return this._map.size;
  69. }
  70. /**
  71. * @param {T} key
  72. * @param {V} value
  73. * @return {boolean}
  74. */
  75. delete(key, value) {
  76. const values = this.get(key);
  77. const result = values.delete(value);
  78. if (!values.size)
  79. this._map.delete(key);
  80. return result;
  81. }
  82. /**
  83. * @param {T} key
  84. */
  85. deleteAll(key) {
  86. this._map.delete(key);
  87. }
  88. /**
  89. * @param {T} key
  90. * @return {V}
  91. */
  92. firstValue(key) {
  93. const set = this._map.get(key);
  94. if (!set)
  95. return null;
  96. return set.values().next().value;
  97. }
  98. /**
  99. * @return {T}
  100. */
  101. firstKey() {
  102. return this._map.keys().next().value;
  103. }
  104. /**
  105. * @return {!Array<V>}
  106. */
  107. valuesArray() {
  108. const result = [];
  109. for (const key of this._map.keys())
  110. result.push(...Array.from(this._map.get(key).values()));
  111. return result;
  112. }
  113. /**
  114. * @return {!Array<T>}
  115. */
  116. keysArray() {
  117. return Array.from(this._map.keys());
  118. }
  119. clear() {
  120. this._map.clear();
  121. }
  122. }
  123. module.exports = Multimap;