Sha256: 79ae4a9af453c35699d42618e8a70be34fdb6c8a9fe72eb365630264fafd5be7

Contents?: true

Size: 1.62 KB

Versions: 48

Compression:

Stored size: 1.62 KB

Contents

export default class CustomSet<T> {
    data: Set<T>

    constructor(data: T[] = []) {
        this.data = new Set<T>()
        data.forEach((el) => this.add(el))
    }

    add(el: T) {
        this.data.add(el)
        return this
    }

    delete(el: T) {
        this.data.delete(el)
        return this
    }

    size() {
        return this.data.size
    }

    empty() {
        return this.data.size === 0
    }

    contains(el: T) {
        return this.data.has(el)
    }

    eql(other: CustomSet<T>) {
        if (this.data.size !== other.data.size) {
            return false
        }
        for (const item of this.data) {
            if (!other.data.has(item)) {
                return false
            }
        }
        return true
    }

    difference(other: CustomSet<T>) {
        const result = new CustomSet<T>()
        for (const item of this.data) {
            if (!other.data.has(item)) {
                result.add(item)
            }
        }
        return result
    }

    disjoint(other: CustomSet<T>) {
        return this.size() === 0 || this.difference(other).size() === this.size()
    }

    intersection(other: CustomSet<T>) {
        return this.difference(this.difference(other))
    }

    union(other: CustomSet<T>) {
        const result = new CustomSet<T>()
        for (const item of this.data) {
            result.add(item)
        }
        for (const item of other.data) {
            result.add(item)
        }
        return result
    }

    subset(other: CustomSet<T>) {
        return this.eql(this.intersection(other))
    }

    toList() {
        return Object.values(this.data)
    }
}

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.179 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.178 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.177 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.176 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.175 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.174 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.173 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.172 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.171 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.170 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.169 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.167 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.166 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.165 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.164 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.163 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.162 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.161 tracks/typescript/exercises/custom-set/custom-set.example.ts
trackler-2.2.1.160 tracks/typescript/exercises/custom-set/custom-set.example.ts