MLtonIntInf.rep

View: New views
2 Messages — Rating Filter:   Alert me  

MLtonIntInf.rep

by Wesley W. Terpstra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

There is already this in MLtonIntInf:
      structure BigWord : WORD
structure SmallInt : INTEGER
datatype rep =
Big of BigWord.word vector
| Small of SmallInt.int
val rep: t -> rep
This is pretty useful since it makes it possible to convert an IntInf into a byte-stream or hexadecimal in linear-time. Without this API the fastest algorithm I know requires nlog(n) time usingĀ  >> recursively. However, I also need to be able to convert the other direction too; from rep to t. I can see this is easy enough to do with Prim.fromVector. Does anyone have an objection to the addition of 'fromRep' to MLtonIntInf? Otherwise loading an IntInf from the network will have to be slow.


_______________________________________________
MLton mailing list
MLton@...
http://mlton.org/mailman/listinfo/mlton

Re: MLtonIntInf.rep

by Wesley W. Terpstra :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Wed, Sep 2, 2009 at 4:09 PM, Wesley W. Terpstra <wesley@...> wrote:
Does anyone have an objection to the addition of 'fromRep' to MLtonIntInf?

Something along the lines of the patch I've attached.



[fromRep.patch]

Index: mlton/int-inf.sig
===================================================================
--- mlton/int-inf.sig (revision 7219)
+++ mlton/int-inf.sig (working copy)
@@ -19,4 +19,5 @@
          Big of BigWord.word vector
        | Small of SmallInt.int
       val rep: t -> rep
+      val fromRep: rep -> t option
    end
Index: integer/int-inf.sig
===================================================================
--- integer/int-inf.sig (revision 7219)
+++ integer/int-inf.sig (working copy)
@@ -29,6 +29,7 @@
          Big of BigWord.word Vector.vector
        | Small of SmallInt.int
       val rep: int -> rep
+      val fromRep: rep -> int option
 
       val zero: int
       val one: int
Index: integer/int-inf0.sml
===================================================================
--- integer/int-inf0.sml (revision 7219)
+++ integer/int-inf0.sml (working copy)
@@ -24,6 +24,7 @@
          Big of C_MPLimb.word vector
        | Small of ObjptrInt.int
       val rep: int -> rep
+      val fromRep: rep -> int option
 
       structure Prim :
          sig
@@ -423,6 +424,24 @@
          if isSmall i
             then Small (dropTagCoerceInt i)
             else Big (Prim.toVector i)
+      
+      fun fromRep r =
+         case r of
+            Big v =>
+               let
+                  val ok =
+                     SeqIndex.> (Vector.length v, 1) andalso
+                     W.<= (V.subUnsafe (v, 0), 0w1)
+               in
+                  if ok then SOME (Prim.fromVector v) else NONE
+               end
+          | Small i =>
+                let
+                   val out = addTagCoerceInt i
+                   val undo = dropTagCoerceInt out
+                in
+                   if i = undo then SOME out else NONE
+                end
 
       local
          fun 'a make {zextdToMPLimb: 'a -> MPLimb.word,


_______________________________________________
MLton mailing list
MLton@...
http://mlton.org/mailman/listinfo/mlton