{"version":3,"file":"HashMap.cjs","names":["hashAttributes"],"sources":["../../src/state/HashMap.ts"],"sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport type { Attributes } from '@opentelemetry/api';\nimport { hashAttributes } from '../utils';\n\nexport interface Hash<ValueType, HashCodeType> {\n  (value: ValueType): HashCodeType;\n}\n\nexport class HashMap<KeyType, ValueType, HashCodeType> {\n  private _valueMap = new Map<HashCodeType, ValueType>();\n  private _keyMap = new Map<HashCodeType, KeyType>();\n  private _hash;\n\n  constructor(hash: Hash<KeyType, HashCodeType>) {\n    this._hash = hash;\n  }\n\n  get(key: KeyType, hashCode?: HashCodeType) {\n    hashCode ??= this._hash(key);\n    return this._valueMap.get(hashCode);\n  }\n\n  getOrDefault(key: KeyType, defaultFactory: () => ValueType) {\n    const hash = this._hash(key);\n    if (this._valueMap.has(hash)) {\n      return this._valueMap.get(hash);\n    }\n    const val = defaultFactory();\n    if (!this._keyMap.has(hash)) {\n      this._keyMap.set(hash, key);\n    }\n    this._valueMap.set(hash, val);\n    return val;\n  }\n\n  set(key: KeyType, value: ValueType, hashCode?: HashCodeType) {\n    hashCode ??= this._hash(key);\n    if (!this._keyMap.has(hashCode)) {\n      this._keyMap.set(hashCode, key);\n    }\n    this._valueMap.set(hashCode, value);\n  }\n\n  has(key: KeyType, hashCode?: HashCodeType) {\n    hashCode ??= this._hash(key);\n    return this._valueMap.has(hashCode);\n  }\n\n  *keys(): IterableIterator<[KeyType, HashCodeType]> {\n    const keyIterator = this._keyMap.entries();\n    let next = keyIterator.next();\n    while (next.done !== true) {\n      yield [next.value[1], next.value[0]];\n      next = keyIterator.next();\n    }\n  }\n\n  *entries(): IterableIterator<[KeyType, ValueType, HashCodeType]> {\n    const valueIterator = this._valueMap.entries();\n    let next = valueIterator.next();\n    while (next.done !== true) {\n      // next.value[0] here can not be undefined\n      yield [this._keyMap.get(next.value[0])!, next.value[1], next.value[0]];\n      next = valueIterator.next();\n    }\n  }\n\n  get size() {\n    return this._valueMap.size;\n  }\n}\n\nexport class AttributeHashMap<ValueType> extends HashMap<\n  Attributes,\n  ValueType,\n  string\n> {\n  constructor() {\n    super(hashAttributes);\n  }\n}\n"],"mappings":";;AAYA,IAAa,UAAb,MAAuD;CACrD,4BAAoB,IAAI,IAA6B;CACrD,0BAAkB,IAAI,IAA2B;CACjD;CAEA,YAAY,MAAmC;EAC7C,KAAK,QAAQ;CACf;CAEA,IAAI,KAAc,UAAyB;EACzC,aAAa,KAAK,MAAM,GAAG;EAC3B,OAAO,KAAK,UAAU,IAAI,QAAQ;CACpC;CAEA,aAAa,KAAc,gBAAiC;EAC1D,MAAM,OAAO,KAAK,MAAM,GAAG;EAC3B,IAAI,KAAK,UAAU,IAAI,IAAI,GACzB,OAAO,KAAK,UAAU,IAAI,IAAI;EAEhC,MAAM,MAAM,eAAe;EAC3B,IAAI,CAAC,KAAK,QAAQ,IAAI,IAAI,GACxB,KAAK,QAAQ,IAAI,MAAM,GAAG;EAE5B,KAAK,UAAU,IAAI,MAAM,GAAG;EAC5B,OAAO;CACT;CAEA,IAAI,KAAc,OAAkB,UAAyB;EAC3D,aAAa,KAAK,MAAM,GAAG;EAC3B,IAAI,CAAC,KAAK,QAAQ,IAAI,QAAQ,GAC5B,KAAK,QAAQ,IAAI,UAAU,GAAG;EAEhC,KAAK,UAAU,IAAI,UAAU,KAAK;CACpC;CAEA,IAAI,KAAc,UAAyB;EACzC,aAAa,KAAK,MAAM,GAAG;EAC3B,OAAO,KAAK,UAAU,IAAI,QAAQ;CACpC;CAEA,CAAC,OAAkD;EACjD,MAAM,cAAc,KAAK,QAAQ,QAAQ;EACzC,IAAI,OAAO,YAAY,KAAK;EAC5B,OAAO,KAAK,SAAS,MAAM;GACzB,MAAM,CAAC,KAAK,MAAM,IAAI,KAAK,MAAM,EAAE;GACnC,OAAO,YAAY,KAAK;EAC1B;CACF;CAEA,CAAC,UAAgE;EAC/D,MAAM,gBAAgB,KAAK,UAA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