export abstract class AbstractSet{ public abstract add(item: T): this; public abstract entries(): Array; public abstract remove(item: T): boolean; public abstract has(item: T): boolean; public abstract get Size(): number; /** * 获取左差集 * @param set * @returns Array */ public diff(set: AbstractSet){ if (!set){ return this.entries(); } const items = this.entries(); const result: Array = []; for (const element of items) { if (!set.has(element)){ result.push(element); } } return result; } /** * 获取并集 * @param set * @returns Array */ public union(set: AbstractSet){ if (!set){ return this.entries(); } const items = set.entries(); const thisItems = this.entries(); for (const element of items) { if (!this.has(element)){ thisItems.push(element); } } return thisItems; } /** * 获取交集 * @param set * @returns Array */ public intersect(set: AbstractSet){ if (!set){ return []; } const result: Array = []; let largeSet: AbstractSet, smallItems: Array; if (this.Size > set.Size){ largeSet = this; smallItems = set.entries(); }else{ largeSet = set; smallItems = this.entries(); } // 遍历大的集合取两个集合的交集 for (const element of smallItems) { if (largeSet.has(element)){ result.push(element); } } return result; } /** * 判断是否为空集合 * @returns boolean */ public isEmpty(){ return this.Size === 0; } }