internal export default class OrderedSet < T > implements Iterable < T > { readonly [ Symbol . toStringTag ] = Symbol ( 'OrderedSet' ) ; private readonly set = new Set < T > ( ) ; private readonly list : T [ ] = [ ] ; constructor ( values = [ ] ) { this . addAll ( Array . isArray ( values ) ? values : Array . from ( values ) ) ; } get size ( ) { return this . set . size ; } clear ( ) { this . set . clear ( ) ; this . list . splice ( 0 , this . list . length ) ; } addAll ( values ) { values . forEach ( ( v ) => this . add ( v ) ) ; return this ; } add ( value ) { if ( this . set . has ( value ) ) { return this ; } this . set . add ( value ) ; this . list . push ( value ) ; return this ; } has ( value ) { return this . set . has ( value ) ; } delete ( value ) { const r = this . set . delete ( value ) ; if ( ! r ) { return false ; } const index = this . list . indexOf ( value ) ; console . assert ( index >= 0 ) ; this . list . splice ( index , 1 ) ; return true ; } deleteAll ( values ) { return values . reduce ( ( acc , act ) => this . delete ( act ) && acc , true ) ; } forEach ( callbackfn , thisArg ? ) { this . list . forEach ( function ( this , v ) { callbackfn . call ( this , v , v , this ) ; } , thisArg ) ; } [ Symbol . iterator ] ( ) { return this . list [ Symbol . iterator ] ( ) ; } }