Enhace constants pool handling.
[hs-java.git] / JVM / Common.hs
index 1b0e5eb022e012076f6605712d9e2538aefe0b57..422cc8b88b3a7562861c9ca63c64401c730b1fa9 100644 (file)
@@ -5,6 +5,7 @@ module JVM.Common
   poolSize,
   (!),
   showListIx,
+  mapFindIndex,
   byteString
   ) where
 
@@ -13,6 +14,7 @@ import Data.Binary.Put
 import qualified Data.ByteString.Lazy as B
 import qualified Data.Map as M
 import Data.Default
+import Data.List
 
 import JVM.ClassFile
 
@@ -31,10 +33,16 @@ poolSize = M.size
 (!) :: (Ord k) => M.Map k a -> k -> a
 (!) = (M.!)
 
-showListIx :: (Show a) => [a] -> String
-showListIx list = unlines $ zipWith s [1..] list
-  where s i x = show i ++ ":\t" ++ show x
+showListIx :: (Show i, Show a) => [(i,a)] -> String
+showListIx list = unlines $ map s list
+  where s (i, x) = show i ++ ":\t" ++ show x
 
 byteString ::  (Binary t) => t -> B.ByteString
 byteString x = runPut (put x)
 
+mapFindIndex :: (Num k) => (v -> Bool) -> M.Map k v -> Maybe k
+mapFindIndex check m =
+  case find (check . snd) (M.assocs m) of
+    Nothing -> Nothing
+    Just (k,_) -> Just k
+