MCPcopy Create free account
hub / github.com/Effect-TS/effect / compareHashed

Function compareHashed

packages/effect/src/Equal.ts:351–381  ·  view source on GitHub ↗
(
  self: Iterable<A>,
  that: Iterable<A>,
  hashOf: (item: A) => number,
  equivalent: (self: A, that: A) => boolean
)

Source from the content-addressed store, hash-verified

349
350// Match items one-to-one within equal-hash groups.
351function compareHashed<A>(
352 self: Iterable<A>,
353 that: Iterable<A>,
354 hashOf: (item: A) => number,
355 equivalent: (self: A, that: A) => boolean
356): boolean {
357 const groups = new Map<number, Array<A>>()
358 let remaining = 0
359 for (const item of that) {
360 const h = hashOf(item)
361 const group = groups.get(h)
362 if (group) group.push(item)
363 else groups.set(h, [item])
364 remaining++
365 }
366 outer: for (const item of self) {
367 const group = groups.get(hashOf(item))
368 if (group) {
369 for (let i = 0; i < group.length; i++) {
370 if (equivalent(item, group[i])) {
371 group[i] = group[group.length - 1]
372 group.pop()
373 remaining--
374 continue outer
375 }
376 }
377 }
378 return false
379 }
380 return remaining === 0
381}
382
383const entryHash = (entry: readonly [unknown, unknown]): number => Hash.hash(entry[0])
384

Callers 2

compareStructureFunction · 0.85
makeCompareSetFunction · 0.85

Calls 4

getMethod · 0.65
pushMethod · 0.65
setMethod · 0.65
hashOfFunction · 0.50

Tested by

no test coverage detected