class Node < T > { v : T ; k : string ; next : Node < T > ; } class Map < T > { head : Node < T > ; getElt ( k ) { return mapGet ( this , k ) ; } setElt ( k , v ) { mapSet ( this , k , v ) ; } } function mapSet < T > ( m , k , v ) { for ( let p = m . head ; p != null ; p = p . next ) { if ( p . k == k ) { p . v = v ; return ; } } let n = new Node < T > ( ) ; n . next = m . head ; n . k = k ; n . v = v ; m . head = n ; } function mapGet < T > ( m , k ) { for ( let p = m . head ; p != null ; p = p . next ) { if ( p . k == k ) { return p . v ; } } return null ; } function search_array < T > ( a , item ) { for ( let i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == item ) { return i ; } } return - 1 ; } class MyMap < K , V > { keys : K [ ] ; values : V [ ] ; constructor ( ) { this . keys = [ ] ; this . values = [ ] ; } push ( key , value ) { this . keys . push ( key ) ; this . values . push ( value ) ; } value_for ( key ) { let i = search_array ( this . keys , key ) ; if ( i == - 1 ) { return null ; } return this . values [ i ] ; } key_for ( value ) { let i = search_array ( this . values , value ) ; if ( i == - 1 ) { return null ; } return this . keys [ i ] ; } set ( key , value ) { let i = search_array ( this . keys , key ) ; if ( i == - 1 ) { this . keys . push ( key ) ; this . values . push ( value ) ; } else { this . values [ i ] = value ; } } has_key ( key ) { return search_array ( this . keys , key ) != - 1 ; } has_value ( value ) { return search_array ( this . values , value ) != - 1 ; } } function testMaps ( ) { let m = new Map < number > ( ) ; let q = new Map < string > ( ) ; let r = new MyMap < number , string > ( ) ; mapSet ( q , "one" , "foo" + "bar" ) ; assert ( mapGet ( q , "one" ) . length == 6 , "m0" ) ; mapSet ( q , "one" , "foo2" + "bar" ) ; assert ( mapGet ( q , "one" ) . length == 7 , "m1" ) ; q . setElt ( "two" , "x" + "y" ) ; assert ( q . getElt ( "two" ) . length == 2 , "m2" ) ; q . setElt ( "two" , "x" + "yz" ) ; assert ( q . getElt ( "two" ) . length == 3 , "thr" ) ; mapSet ( m , "one" , 1 ) ; assert ( mapGet ( m , "one" ) == 1 , "1" ) ; mapSet ( m , "two" , 2 ) ; assert ( m . getElt ( "two" ) == 2 , "2" ) ; } testMaps ( ) ;