import { Dictionary } from "typescript-collections" ; declare global { interface Array < T > { /** Returns the last element from an array */ last ( ) : T ; /** Deletes all elements from an array */ clear ( ) : void ; /** Returns true if the element is found in the array */ contains ( elem ) : boolean ; } } if ( ! Array . prototype . last ) { Object . defineProperty ( Array . prototype , "last" , { enumerable : false , value : function < T > ( ) : T { return this [ this . length - 1 ] ; } } ) ; } if ( ! Array . prototype . clear ) { Object . defineProperty ( Array . prototype , "clear" , { enumerable : false , value : function < T > ( ) : void { this . length = 0 ; } } ) ; } if ( ! Array . prototype . contains ) { Object . defineProperty ( Array . prototype , "contains" , { enumerable : false , value : function < T > ( elem ) : boolean { return this . indexOf ( elem ) !== - 1 ; } } ) ; } /**
 * This class implements static methods to perform useful operations on lists, dictionaries, ...
 */ export class CollectionUtil { public static contains2 ( array , object ) { for ( let i = 0 ; i < array . length ; i ++ ) { if ( array [ i ] === object ) { return true ; } } return false ; } public static last ( array ) { return array [ array . length - 1 ] ; } param dict param iterationFunction public static removeDictElementIfTrue < S , T , V > ( thisPointer , dict , iterationFunction ) { const toDeleteEntries = [ ] ; dict . forEach ( function ( key , value ) : void { const shallDelete = iterationFunction ( thisPointer , key , value ) ; if ( shallDelete ) { toDeleteEntries . push ( key ) ; } } ) ; for ( let i = 0 ; i < toDeleteEntries . length ; i ++ ) { dict . remove ( toDeleteEntries [ i ] ) ; } } public static getLastElement < T > ( array ) { return array [ array . length - 1 ] ; } public static binarySearch < T > ( array , element , cmp , startIndex = 0 , endIndex = array . length - 1 ) { let mid = 1 ; while ( startIndex < endIndex ) { mid = Math . floor ( ( startIndex + endIndex ) / 2 ) ; const c = cmp ( array [ mid ] , element ) ; if ( c === 0 ) { return mid ; } if ( c < 0 ) { startIndex = mid + 1 ; } if ( 0 < c ) { endIndex = mid ; } } return - mid ; } }