packageDocumentation module import { assert } from "./Assert" ; import { CompressedId64Set } from "./CompressedId64Set" ; import { Id64Array , Id64String } from "./Id" ; public export type OrderedId64Iterable = Iterable < Id64String > ; see see see public export namespace OrderedId64Iterable { see export function compare ( lhs , rhs ) { if ( lhs . length !== rhs . length ) return lhs . length < rhs . length ? - 1 : 1 ; if ( lhs !== rhs ) return lhs < rhs ? - 1 : 1 ; return 0 ; } see returns note export function sortArray ( ids ) { ids . sort ( ( x , y ) => compare ( x , y ) ) ; return ids ; } note export function areEqualSets ( ids1 , ids2 ) { const leftIter = uniqueIterator ( ids1 ) ; const rightIter = uniqueIterator ( ids2 ) ; let leftState = leftIter . next ( ) ; let rightState = rightIter . next ( ) ; while ( ! leftState . done && ! rightState . done ) { const left = leftState . value ; const right = rightState . value ; if ( 0 !== compare ( left , right ) ) return false ; leftState = leftIter . next ( ) ; rightState = rightIter . next ( ) ; } if ( leftState . done && rightState . done ) return true ; return false ; } param ids returns export function isEmptySet ( ids ) { if ( typeof ids === "string" ) return "" === ids ; return true === ids [ Symbol . iterator ] ( ) . next ( ) . done ; } note export function unique ( ids ) { return { [ Symbol . iterator ] : ( ) => uniqueIterator ( ids ) } ; } note export function * uniqueIterator ( ids ) { const iter = ids [ Symbol . iterator ] ( ) ; let state = iter . next ( ) ; let prev ; while ( ! state . done ) { const id = state . value ; state = iter . next ( ) ; if ( id !== prev ) { prev = id ; yield id ; } } } note export function union ( ids1 , ids2 ) { return { [ Symbol . iterator ] : ( ) => unionIterator ( ids1 , ids2 ) } ; } note export function intersection ( ids1 , ids2 ) { return { [ Symbol . iterator ] : ( ) => intersectionIterator ( ids1 , ids2 ) } ; } note export function difference ( ids1 , ids2 ) { return { [ Symbol . iterator ] : ( ) => differenceIterator ( ids1 , ids2 ) } ; } note export function * unionIterator ( ids1 , ids2 ) { const leftIter = ids1 [ Symbol . iterator ] ( ) ; const rightIter = ids2 [ Symbol . iterator ] ( ) ; let leftState = leftIter . next ( ) ; let rightState = rightIter . next ( ) ; let prev ; while ( ! leftState . done || ! rightState . done ) { const left = leftState . done ? undefined : leftState . value ; const right = rightState . done ? undefined : rightState . value ; assert ( undefined !== left || undefined !== right ) ; if ( undefined === left && undefined === right ) break ; let next ; if ( undefined === left ) { assert ( undefined !== right ) ; next = right ; rightState = rightIter . next ( ) ; } else if ( undefined === right ) { next = left ; leftState = leftIter . next ( ) ; } else { const cmp = compare ( left , right ) ; if ( cmp <= 0 ) { next = left ; leftState = leftIter . next ( ) ; if ( 0 === cmp ) rightState = rightIter . next ( ) ; } else { next = right ; rightState = rightIter . next ( ) ; } } if ( prev === next ) continue ; prev = next ; yield next ; } } note export function * intersectionIterator ( ids1 , ids2 ) { const leftIter = ids1 [ Symbol . iterator ] ( ) ; const rightIter = ids2 [ Symbol . iterator ] ( ) ; let leftState = leftIter . next ( ) ; let rightState = rightIter . next ( ) ; let prev ; while ( ! leftState . done && ! rightState . done ) { const left = leftState . value ; leftState = leftIter . next ( ) ; if ( left === prev ) continue ; prev = left ; let right = rightState . value ; let cmp = compare ( left , right ) ; while ( cmp > 0 ) { rightState = rightIter . next ( ) ; if ( rightState . done ) return ; right = rightState . value ; cmp = compare ( left , right ) ; } if ( 0 === cmp ) yield left ; } } note export function * differenceIterator ( ids1 , ids2 ) { const leftIter = ids1 [ Symbol . iterator ] ( ) ; const rightIter = ids2 [ Symbol . iterator ] ( ) ; let leftState = leftIter . next ( ) ; let rightState = rightIter . next ( ) ; let prev ; while ( ! leftState . done ) { const left = leftState . value ; leftState = leftIter . next ( ) ; if ( left === prev ) continue ; else if ( rightState . done ) { yield prev = left ; continue ; } let right = rightState . value ; let cmp = compare ( left , right ) ; while ( cmp > 0 && ! rightState . done ) { rightState = rightIter . next ( ) ; if ( rightState . done ) { yield prev = left ; continue ; } right = rightState . value ; cmp = compare ( left , right ) ; } if ( cmp < 0 ) yield prev = left ; } } }