@technobuddha/library
Version:
A large library of useful functions
142 lines (141 loc) • 5.02 kB
TypeScript
import { type Cartesian } from './geometry.ts';
/**
* Represents a set of 2D cartesian, optimized for efficient storage and lookup.
*
* `CartesianSet` provides set-like operations (union, intersection, difference, etc.)
* for objects with `{ x, y }` properties, where `x` and `y` are numbers.
*
* Internally, cartesian are stored in a nested `Map<number, Set<number>>` structure,
* allowing for fast addition, deletion, and membership checks.
*
* Example usage:
* ```typescript
* const set = new CartesianSet([{ x: 1, y: 2 }, { x: 3, y: 4 }]);
* set.add({ x: 5, y: 6 });
* set.has({ x: 1, y: 2 }); // true
* ```
*
* Supports all standard set operations, as well as iteration and forEach.
*
* @group Geometry
* @category Coordinates
*/
export declare class CartesianSet implements ReadonlySetLike<Cartesian> {
private readonly xaxis;
/**
* Creates a new `CartesianSet` optionally initialized with an array of cartesian.
*
* @param cartesian - Optional array of cartesian to initialize the set.
*/
constructor(cartesian?: Cartesian[] | null);
/**
* Gets the number of unique cartesian in the set.
*/
get size(): number;
readonly [Symbol.toStringTag] = "CartesianSet";
/**
* Adds one or more cartesian to the set.
*
* @param value - A single coordinate or an array of cartesian to add.
* @returns The set itself, for chaining.
*/
add(value: Cartesian | Cartesian[]): this;
/**
* Removes all cartesian from the set.
*/
clear(): void;
/**
* Removes a coordinate from the set.
*
* @param value - The coordinate to remove.
* @returns `true` if the coordinate was present and removed, `false` otherwise.
*/
delete(value: Cartesian): boolean;
/**
* Returns a new set containing the cartesian present in this set but not in the other set.
*
* @param other - The set to compare against.
* @returns A new `CartesianSet` with the difference.
*/
difference(other: CartesianSet): CartesianSet;
/**
* Returns an iterator over `[coordinate, coordinate]` pairs for each coordinate in the set.
*
* @returns An iterator of `[Cartesian, Cartesian]` pairs.
*/
entries(): SetIterator<[Cartesian, Cartesian]>;
/**
* Executes a provided function once for each coordinate in the set.
*
* @param callback - Function to execute for each coordinate.
* @param thisArg - Value to use as `this` when executing `callback`.
*/
forEach(callback: (value: Cartesian, key: Cartesian, set: CartesianSet) => void, thisArg?: this): void;
/**
* Checks if a coordinate is present in the set.
*
* @param coordinate - The coordinate to check.
* @returns `true` if the coordinate exists in the set, `false` otherwise.
*/
has({ x, y }: Cartesian): boolean;
/**
* Returns a new set containing only the cartesian present in both this set and the other set.
*
* @param other - The set to intersect with.
* @returns A new `CartesianSet` with the intersection.
*/
intersection(other: CartesianSet): CartesianSet;
/**
* Checks if this set and the other set have no cartesian in common.
*
* @param other - The set to compare against.
* @returns `true` if the sets are disjoint, `false` otherwise.
*/
isDisjointFrom(other: CartesianSet): boolean;
/**
* Checks if this set is a subset of another set.
*
* @param other - The set to compare against.
* @returns `true` if every coordinate in this set is also in the other set.
*/
isSubsetOf(other: CartesianSet): boolean;
/**
* Checks if this set is a superset of another set.
*
* @param other - The set to compare against.
* @returns `true` if every coordinate in the other set is also in this set.
*/
isSupersetOf(other: CartesianSet): boolean;
/**
* Returns an iterator over the cartesian in the set.
*
* @returns An iterator of `Cartesian`.
*/
keys(): SetIterator<Cartesian>;
/**
* Returns a new set containing cartesian that are in either this set or the other set, but not both.
*
* @param other - The set to compare against.
* @returns A new `CartesianSet` with the symmetric difference.
*/
symmetricDifference(other: CartesianSet): CartesianSet;
/**
* Returns a new set containing all cartesian from both this set and the other set.
*
* @param other - The set to unite with.
* @returns A new `CartesianSet` with the union.
*/
union(other: CartesianSet): CartesianSet;
/**
* Returns an iterator over the cartesian in the set.
*
* @returns An iterator of `Cartesian`.
*/
values(): SetIterator<Cartesian>;
/**
* Returns an iterator over the cartesian in the set.
*
* @returns An iterator of `Cartesian`.
*/
[Symbol.iterator](): SetIterator<Cartesian>;
}