import { Collection , ReadonlyCollection } from "@esfx/collection-core" ; import { Equaler } from "@esfx/equatable" ; import { isIterable } from '@esfx/internal-guards' ; import { HashData , createHashData , findEntryIndex , insertEntry , deleteEntry , clearEntries , ensureCapacity , trimExcessEntries , iterateEntries , selectEntryKey , selectEntryValue , selectEntryEntry , forEachEntry } from '@esfx/internal-collections-hash/dist/hashData' ; export class HashSet < T > implements Collection < T > { private _hashData : HashData < T , T > ; constructor ( equaler ? ) ; constructor ( iterable ? , equaler ? ) ; constructor ( capacity , equaler ? ) ; constructor ( ... args ) { let capacity ; let iterable ; let equaler ; if ( args . length > 0 ) { const arg0 = args [ 0 ] ; if ( typeof arg0 === "number" ) { capacity = arg0 ; if ( args . length > 1 ) equaler = args [ 1 ] ; } else if ( isIterable ( arg0 ) ) { iterable = arg0 ; if ( args . length > 1 ) equaler = args [ 1 ] ; } else { equaler = arg0 ; } } if ( capacity === undefined ) capacity = 0 ; if ( equaler === undefined ) equaler = Equaler . defaultEqualer ; if ( capacity < 0 ) throw new RangeError ( ) ; this . _hashData = createHashData ( equaler , capacity ) ; if ( iterable ) { for ( const value of iterable ) { this . add ( value ) ; } } } get equaler ( ) { return this . _hashData . equaler ; } get size ( ) { return this . _hashData . size - this . _hashData . freeSize ; } has ( value ) { return findEntryIndex ( this . _hashData , value ) >= 0 ; } add ( value ) { insertEntry ( this . _hashData , value , value ) ; return this ; } tryAdd ( value ) { const oldSize = this . size ; insertEntry ( this . _hashData , value , value ) ; return this . size > oldSize ; } delete ( value ) { return deleteEntry ( this . _hashData , value ) ; } clear ( ) { clearEntries ( this . _hashData ) ; } ensureCapacity ( capacity ) { return ensureCapacity ( this . _hashData , capacity ) ; } trimExcess ( capacity ? ) { trimExcessEntries ( this . _hashData , capacity ) ; } keys ( ) { return iterateEntries ( this . _hashData . head , selectEntryKey ) ; } values ( ) { return iterateEntries ( this . _hashData . head , selectEntryValue ) ; } entries ( ) { return iterateEntries ( this . _hashData . head , selectEntryEntry ) ; } [ Symbol . iterator ] ( ) { return this . values ( ) ; } forEach ( callback , thisArg ? ) { forEachEntry ( this , this . _hashData . head , callback , thisArg ) ; } [ Symbol . toStringTag ] : string ; get [ Collection . size ] ( ) { return this . size ; } [ Collection . has ] ( value ) { return this . has ( value ) ; } [ Collection . add ] ( value ) { this . add ( value ) ; } [ Collection . delete ] ( value ) { return this . delete ( value ) ; } [ Collection . clear ] ( ) { this . clear ( ) ; } } Object . defineProperty ( HashSet , Symbol . toStringTag , { enumerable : false , configurable : true , writable : true , value : "HashSet" } ) ; export interface ReadonlyHashSet < T > extends ReadonlySet < T > , ReadonlyCollection < T > { readonly equaler : Equaler < T > ; [ Symbol . iterator ] ( ) : IterableIterator < T > ; }