/ utils / set.ts
set.ts
 1  /**
 2   * Note: this code is hot, so is optimized for speed.
 3   */
 4  export function difference<A>(a: Set<A>, b: Set<A>): Set<A> {
 5    const result = new Set<A>()
 6    for (const item of a) {
 7      if (!b.has(item)) {
 8        result.add(item)
 9      }
10    }
11    return result
12  }
13  
14  /**
15   * Note: this code is hot, so is optimized for speed.
16   */
17  export function intersects<A>(a: Set<A>, b: Set<A>): boolean {
18    if (a.size === 0 || b.size === 0) {
19      return false
20    }
21    for (const item of a) {
22      if (b.has(item)) {
23        return true
24      }
25    }
26    return false
27  }
28  
29  /**
30   * Note: this code is hot, so is optimized for speed.
31   */
32  export function every<A>(a: ReadonlySet<A>, b: ReadonlySet<A>): boolean {
33    for (const item of a) {
34      if (!b.has(item)) {
35        return false
36      }
37    }
38    return true
39  }
40  
41  /**
42   * Note: this code is hot, so is optimized for speed.
43   */
44  export function union<A>(a: Set<A>, b: Set<A>): Set<A> {
45    const result = new Set<A>()
46    for (const item of a) {
47      result.add(item)
48    }
49    for (const item of b) {
50      result.add(item)
51    }
52    return result
53  }