| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {List, Seq} from 'immutable'; |
| b69ab31 | | | 9 | import type {Hash} from '../types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {OrderedSet as ImSet} from 'immutable'; |
| b69ab31 | | | 12 | import {SelfUpdate} from 'shared/immutableExt'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | /** |
| b69ab31 | | | 15 | * Set of commit hashes, with extra methods. |
| b69ab31 | | | 16 | * Internally maintains the order of the hashes information. |
| b69ab31 | | | 17 | */ |
| b69ab31 | | | 18 | export class HashSet extends SelfUpdate<ImSet<Hash>> { |
| b69ab31 | | | 19 | constructor(set?: ImSet<Hash>) { |
| b69ab31 | | | 20 | super(set ?? ImSet()); |
| b69ab31 | | | 21 | } |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | static fromHashes(hashes: SetLike): HashSet { |
| b69ab31 | | | 24 | if (hashes == null) { |
| b69ab31 | | | 25 | return new HashSet(ImSet()); |
| b69ab31 | | | 26 | } else if (hashes instanceof HashSet) { |
| b69ab31 | | | 27 | return hashes; |
| b69ab31 | | | 28 | } else if (typeof hashes === 'string') { |
| b69ab31 | | | 29 | return new HashSet(ImSet([hashes])); |
| b69ab31 | | | 30 | } else if (ImSet.isOrderedSet(hashes)) { |
| b69ab31 | | | 31 | return new HashSet(hashes as ImSet<Hash>); |
| b69ab31 | | | 32 | } else { |
| b69ab31 | | | 33 | return new HashSet(ImSet(hashes)); |
| b69ab31 | | | 34 | } |
| b69ab31 | | | 35 | } |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | toHashes(): ImSet<Hash> { |
| b69ab31 | | | 38 | return this.set; |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | toSeq(): Seq.Set<Hash> { |
| b69ab31 | | | 42 | return this.set.toSeq(); |
| b69ab31 | | | 43 | } |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | toList(): List<Hash> { |
| b69ab31 | | | 46 | return this.set.toList(); |
| b69ab31 | | | 47 | } |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | toArray(): Array<Hash> { |
| b69ab31 | | | 50 | return this.set.toArray(); |
| b69ab31 | | | 51 | } |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | /** Union with another set. Preserves the order: this, then other without the overlapping subset. */ |
| b69ab31 | | | 54 | union(other: SetLike): HashSet { |
| b69ab31 | | | 55 | const set = this.set.union(HashSet.fromHashes(other).set); |
| b69ab31 | | | 56 | return new HashSet(set); |
| b69ab31 | | | 57 | } |
| b69ab31 | | | 58 | |
| b69ab31 | | | 59 | /** Interset with another set. Preserves the order of `this` set. */ |
| b69ab31 | | | 60 | intersect(other: SetLike): HashSet { |
| b69ab31 | | | 61 | const set = this.set.intersect(HashSet.fromHashes(other).set); |
| b69ab31 | | | 62 | return new HashSet(set); |
| b69ab31 | | | 63 | } |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | /** Remove items that exist in another set. Preserves the order of the `this` set. */ |
| b69ab31 | | | 66 | subtract(other: SetLike): HashSet { |
| b69ab31 | | | 67 | const set = this.set.subtract(HashSet.fromHashes(other).set); |
| b69ab31 | | | 68 | return new HashSet(set); |
| b69ab31 | | | 69 | } |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | /** Test if this set contains the given hash. */ |
| b69ab31 | | | 72 | contains(hash: Hash): boolean { |
| b69ab31 | | | 73 | return this.set.has(hash); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | /** Reverse the order of the set. */ |
| b69ab31 | | | 77 | reverse(): HashSet { |
| b69ab31 | | | 78 | return new HashSet(this.inner.reverse()); |
| b69ab31 | | | 79 | } |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | /** Convert to sorted array. Mainly for testing. */ |
| b69ab31 | | | 82 | toSortedArray(): Array<Hash> { |
| b69ab31 | | | 83 | return this.set.toArray().sort(); |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | [Symbol.iterator](): IterableIterator<Hash> { |
| b69ab31 | | | 87 | return this.set[Symbol.iterator](); |
| b69ab31 | | | 88 | } |
| b69ab31 | | | 89 | |
| b69ab31 | | | 90 | get size(): number { |
| b69ab31 | | | 91 | return this.set.size; |
| b69ab31 | | | 92 | } |
| b69ab31 | | | 93 | |
| b69ab31 | | | 94 | private get set(): ImSet<Hash> { |
| b69ab31 | | | 95 | return this.inner; |
| b69ab31 | | | 96 | } |
| b69ab31 | | | 97 | } |
| b69ab31 | | | 98 | |
| b69ab31 | | | 99 | export function arrayFromHashes(hashes: SetLike): Array<Hash> { |
| b69ab31 | | | 100 | if (hashes == null) { |
| b69ab31 | | | 101 | return []; |
| b69ab31 | | | 102 | } else if (hashes instanceof HashSet) { |
| b69ab31 | | | 103 | return hashes.toArray(); |
| b69ab31 | | | 104 | } else if (typeof hashes === 'string') { |
| b69ab31 | | | 105 | return [hashes]; |
| b69ab31 | | | 106 | } else { |
| b69ab31 | | | 107 | return [...hashes]; |
| b69ab31 | | | 108 | } |
| b69ab31 | | | 109 | } |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | /** A convenient type that converts to HashSet. `null` converts to an empty set. */ |
| b69ab31 | | | 112 | export type SetLike = HashSet | ImSet<Hash> | Iterable<Hash> | Hash | null | undefined; |