import "Prelude" as Prelude import "JavaBuiltin" as Java import "MSet" as MSet import "MList" as MList import "Iterator" as Iterator importJava "java.util.Map$Entry" where data Entry a b @JavaName getKey keyOf :: Entry a b -> a @JavaName getValue valueOf :: Entry a b -> b importJava "java.util.Map" where data T a b containsKey :: T a b -> a -> Boolean containsValue :: T a b -> b -> Boolean size :: T a b -> Integer isEmpty :: T a b -> Boolean entrySet :: T a b -> MSet.T (Entry a b) get :: T a b -> a -> Maybe b @JavaName get unsafeGet :: T a b -> a -> b // needs no , because this is a view keySet :: T a b -> MSet.T a @private @JavaName values values_ :: T a b -> Iterator.Iterable b put :: T a b -> a -> b -> (Maybe b) remove :: T a b -> a -> (Maybe b) clear :: T a b -> () @inline getOrCreate :: T a b -> (a -> b) -> a -> b getOrCreate m f k = match get m k with Just v -> v Nothing -> do v = f k put m k v v @inline iter :: (a -> b -> ()) -> T a b -> () iter f m = MSet.iter (\entry -> f (keyOf entry) (valueOf entry)) (entrySet m) @inline iterB :: (a -> b -> Boolean) -> T a b -> Boolean iterB f m = MSet.iterB (\entry -> f (keyOf entry) (valueOf entry)) (entrySet m) @inline fold :: (a -> b -> c -> a) -> a -> T b c -> a fold f init m = MSet.fold (\cur entry -> f cur (keyOf entry) (valueOf entry)) init (entrySet m) importJava "gnu.trove.map.hash.THashMap" where @JavaName "" create :: () -> T a b @JavaName "" createC :: Integer -> T a b @JavaName "" clone :: T a b -> T a b fromEntryList :: [(a,b)] -> T a b fromEntryList l = do result = createC (Prelude.length l) Prelude.iter (\(k,v) -> Prelude.ignore (put result k v)) l result putM :: (b -> b -> b) -> T a b -> a -> b -> () putM merge m k v = match get m k with Just v2 -> Prelude.ignore (put m k (merge v v2)) Nothing -> Prelude.ignore (put m k v) @inline map :: (b -> c) -> T a b -> T a c map f m = do result = createC (size m) iter (\k v -> Prelude.ignore (put result k (f v))) m result keys :: T a b -> [a] keys m = MSet.toList (keySet m) values :: T a b -> [b] values m = do result = MList.createC (size m) Iterator.iter (MList.add result) (Iterator.iterator (values_ m)) MList.freeze result entries :: T a b -> [(a,b)] entries m = do result = MList.createC (size m) iter (Prelude.curry (MList.add result)) m MList.freeze result