All files / collections/src setMultiMap.ts

33.82% Statements 23/68
13.04% Branches 3/23
21.42% Functions 6/28
35.48% Lines 22/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1921x 1x 1x   1x                 18x 1x               18x 18x 18x   18x 13x         24x 24x       4x 4x 2x   2x   4x       2x                     2x       1x                                                                                           1x                                                                                                                                                                      
import { identity } from "@benneq/function";
import { count, flatMap, map, some } from "@benneq/iterable";
import { computeIfAbsent } from "@benneq/map";
import { Entry } from "@benneq/object";
import { AbstractMultiMap } from "./abstractMultiMap";
import { MultiMap } from "./_types";
 
/**
 * A {@link SetMultiMap} can contain each value only once per key.
 *
 * @typeParam K - the key type
 * @typeParam V - the value type
 */
export class SetMultiMap<K, V> extends AbstractMultiMap<K, V> {
  #value;
 
  /**
   * Create a new {@link SetMultiMap} from the given entries.
   *
   * @param iterable - the optional {@link Iterable} to consume the entries from
   */
  constructor(iterable?: Iterable<Entry<K, V>>) {
    const value = new Map<K, Set<V>>();
    super(value);
    this.#value = value;
 
    if (iterable) {
      this.addAll(iterable);
    }
  }
 
  add(key: K, value: V): this {
    (computeIfAbsent(this.#value)(key, () => new Set()) as Set<V>).add(value);
    return this;
  }
 
  set(key: K, values: Iterable<V>): this {
    const value = new Set(values);
    if (value.size) {
      this.#value.set(key, value);
    } else {
      this.#value.delete(key);
    }
    return this;
  }
 
  delete(key: K, value?: V): boolean {
    Iif (value) {
      const values = this.#value.get(key);
      Iif (!values || !values.delete(value)) {
        return false;
      }
 
      Iif (values.size) {
        return true;
      }
    }
 
    return this.#value.delete(key);
  }
}
 
export const absmm = <K, V>(data: Map<K, Iterable<V>>) => {
  return {
    clear() {
      data.clear();
    },
    addAll(entries: any) {
      for (const [key, value] of entries) {
        // this.add(key, value);
      }
      return this;
    },
    forEach(callbackfn: any) {
      for (const [key, value] of this) {
        callbackfn(value, key, this);
      }
    },
    has(key: any, value?: V) {
      return (
        data.has(key) &&
        (!value || some((e) => e === value)(data.get(key) as Iterable<V>))
      );
    },
    get(key: any) {
      return data.get(key) || [];
    },
    values() {
      return flatMap<Iterable<V>, V>(identity)(data.values());
    },
    entries() {
      return flatMap<[K, Iterable<V>], [K, V]>(([key, values]) =>
        map<V, [K, V]>((value) => [key, value])(values)
      )(data);
    },
    get size() {
      return count(this) || 0;
    },
    keys() {
      return data.keys();
    },
    [Symbol.toStringTag]: "MultiMap",
    [Symbol.iterator]() {
      return this.entries();
    },
  };
};
 
export const smm = <K, V>(iterable?: Iterable<Entry<K, V>>): MultiMap<K, V> => {
  const data = new Map<K, Set<V>>();
 
  const a = (x: any) => (key: K, value: V) => {
    (computeIfAbsent(data)(key, () => new Set()) as Set<V>).add(value);
    return x;
  };
 
  Iif (iterable) {
    for (const [key, value] of iterable) {
      a("")(key, value);
    }
  }
 
  return {
    ...absmm(data),
 
    delete(key, value?: V) {
      Iif (value) {
        const values = data.get(key);
        Iif (!values || !values.delete(value)) {
          return false;
        }
 
        Iif (values.size) {
          return true;
        }
      }
 
      return data.delete(key);
    },
 
    set(key, values) {
      const value = new Set(values);
      if (value.size) {
        data.set(key, value);
      } else {
        data.delete(key);
      }
      return this;
    },
 
    add: a(this),
    // addAll(entries) {
    //   for(const [key, value] of entries) {
    //     this.add(key, value);
    //   }
    //   return this;
    // },
    // forEach(callbackfn) {
    //   for (const [key, value] of this) {
    //     callbackfn(value, key, this);
    //   }
    // },
    // has(key, value?: V) {
    //   return (
    //     data.has(key) &&
    //     (!value || some((e) => e === value)(data.get(key) as Iterable<V>))
    //   );
    // },
    // get(key) {
    //   return data.get(key) || [];
    // },
    // values() {
    //   return flatMap<Iterable<V>, V>(identity)(data.values());
    // },
    // entries() {
    //   return flatMap<[K, Iterable<V>], [K, V]>(([key, values]) =>
    //     map<V, [K, V]>((value) => [key, value])(values)
    //   )(data);
    // },
    // get size() {
    //   return count(this) || 0;
    // },
    // keys() {
    //   return data.keys();
    // },
    // [Symbol.toStringTag]: "MultiMap",
    // [Symbol.iterator]() {
    //   return this.entries();
    // }
  };
};