import { LinkedList } from "./array.js";
/**
 * An implementation of Set that support first() and last() method.
 */
export class OrderedSet {
    set;
    array;
    constructor() {
        this.set = new Set();
        this.array = new LinkedList();
    }
    add(item) {
        if (!this.set.has(item)) {
            this.set.add(item);
            this.array.push(item);
        }
    }
    delete(item, searchFromHead) {
        if (this.set.has(item)) {
            this.set.delete(item);
            if (searchFromHead) {
                this.array.deleteFirst(item);
            }
            else {
                this.array.deleteLast(item);
            }
        }
    }
    first() {
        if (this.array.length === 0) {
            return null;
        }
        return this.array.first();
    }
    last() {
        if (this.array.length === 0) {
            return null;
        }
        return this.array.last();
    }
    toArray() {
        return this.array.toArray();
    }
    values() {
        return this.array;
    }
    has(item) {
        return this.set.has(item);
    }
    get size() {
        return this.set.size;
    }
}
//# sourceMappingURL=set.js.map