2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* An array which functions as a set. It guarantees unique elements
|
|
|
|
* and provides set functions for comparisons
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
export class ASet<T> extends Array {
|
2024-08-27 11:41:22 -04:00
|
|
|
/** Number of elements in set */
|
2024-04-16 14:05:41 -04:00
|
|
|
get size() {
|
|
|
|
return this.length;
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Array to create set from, duplicate values will be removed
|
|
|
|
* @param {T[]} elements Elements which will be added to set
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
constructor(elements: T[] = []) {
|
|
|
|
super();
|
|
|
|
if(!!elements?.['forEach'])
|
|
|
|
elements.forEach(el => this.add(el));
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
2024-10-06 19:14:36 -04:00
|
|
|
* Add elements to set if unique
|
|
|
|
* @param items
|
2024-08-27 11:41:22 -04:00
|
|
|
*/
|
2024-10-06 19:14:36 -04:00
|
|
|
add(...items: T[]) {
|
|
|
|
items.filter(el => !this.has(el)).forEach(el => this.push(el));
|
2024-04-16 14:05:41 -04:00
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
2024-10-06 19:14:36 -04:00
|
|
|
* Delete elements from set
|
|
|
|
* @param items Elements that will be deleted
|
2024-08-27 11:41:22 -04:00
|
|
|
*/
|
2024-10-06 19:14:36 -04:00
|
|
|
delete(...items: T[]) {
|
|
|
|
items.forEach(el => {
|
|
|
|
const index = this.indexOf(el);
|
|
|
|
if(index != -1) this.slice(index, 1);
|
|
|
|
})
|
2024-04-16 14:05:41 -04:00
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Create list of elements this set has which the comparison set does not
|
|
|
|
* @param {ASet<T>} set Set to compare against
|
|
|
|
* @return {ASet<T>} Different elements
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
difference(set: ASet<T>) {
|
2024-08-27 11:41:22 -04:00
|
|
|
return new ASet<T>(this.filter(el => !set.has(el)));
|
2024-04-16 14:05:41 -04:00
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Check if set includes element
|
|
|
|
* @param {T} el Element to look for
|
|
|
|
* @return {boolean} True if element was found, false otherwise
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
has(el: T) {
|
|
|
|
return this.indexOf(el) != -1;
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Create list of elements this set has in common with the comparison set
|
|
|
|
* @param {ASet<T>} set Set to compare against
|
|
|
|
* @return {boolean} Set of common elements
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
intersection(set: ASet<T>) {
|
2024-08-27 11:41:22 -04:00
|
|
|
return new ASet<T>(this.filter(el => set.has(el)));
|
2024-04-16 14:05:41 -04:00
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Check if this set has no elements in common with the comparison set
|
|
|
|
* @param {ASet<T>} set Set to compare against
|
|
|
|
* @return {boolean} True if nothing in common, false otherwise
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
isDisjointFrom(set: ASet<T>) {
|
|
|
|
return this.intersection(set).size == 0;
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Check if all elements in this set are included in the comparison set
|
|
|
|
* @param {ASet<T>} set Set to compare against
|
|
|
|
* @return {boolean} True if all elements are included, false otherwise
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
isSubsetOf(set: ASet<T>) {
|
|
|
|
return this.findIndex(el => !set.has(el)) == -1;
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Check if all elements from comparison set are included in this set
|
|
|
|
* @param {ASet<T>} set Set to compare against
|
|
|
|
* @return {boolean} True if all elements are included, false otherwise
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
isSuperset(set: ASet<T>) {
|
|
|
|
return set.findIndex(el => !this.has(el)) == -1;
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Create list of elements that are only in one set but not both (XOR)
|
|
|
|
* @param {ASet<T>} set Set to compare against
|
|
|
|
* @return {ASet<T>} New set of unique elements
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
symmetricDifference(set: ASet<T>) {
|
|
|
|
return new ASet([...this.difference(set), ...set.difference(this)]);
|
|
|
|
}
|
|
|
|
|
2024-08-27 11:41:22 -04:00
|
|
|
/**
|
|
|
|
* Create joined list of elements included in this & the comparison set
|
|
|
|
* @param {ASet<T>} set Set join
|
|
|
|
* @return {ASet<T>} New set of both previous sets combined
|
|
|
|
*/
|
2024-04-16 14:05:41 -04:00
|
|
|
union(set: ASet<T> | Array<T>) {
|
|
|
|
return new ASet([...this, ...set]);
|
|
|
|
}
|
|
|
|
}
|