fdabfb38085a — Chris Cannam 2 years ago
Add peeks to accompany pops
4 files changed, 21 insertions(+), 3 deletions(-)

M persistent-array.sig
M persistent-array.sml
M persistent-queue.sig
M persistent-queue.sml
M persistent-array.sig +1 -0
@@ 41,5 41,6 @@ signature PERSISTENT_ARRAY = sig
     val isEmpty : 'a array -> bool
     val append : 'a array * 'a -> 'a array
     val popEnd : 'a array -> 'a array * 'a
+    val peekEnd : 'a array -> 'a
                                 
 end

          
M persistent-array.sml +6 -1
@@ 43,13 43,18 @@ structure PersistentArrayImpl = struct
 
     fun popEnd (A { size, trie }) =
         case T.find (trie, size - 0w1) of
-            NONE => raise Size
+            NONE => raise Empty
           | SOME x =>
             let val t = T.remove (trie, size - 0w1)
             in
                 (A { size = size - 0w1, trie = t }, x)
             end
 
+    fun peekEnd (A { size, trie }) =
+        case T.find (trie, size - 0w1) of
+            NONE => raise Empty
+          | SOME x => x
+
     fun sub (v as A { size, trie }, i) =
         if i < 0 orelse i >= length v
         then raise Subscript

          
M persistent-queue.sig +2 -0
@@ 25,11 25,13 @@ signature PERSISTENT_QUEUE = sig
     val isEmpty : 'a queue -> bool
     val append : 'a queue * 'a -> 'a queue
     val popEnd : 'a queue -> 'a queue * 'a
+    val peekEnd : 'a queue -> 'a
             
     (* II. Functions specific to PERSISTENT_QUEUE *)
             
     val queue : int * 'a -> 'a queue
     val prepend : 'a queue * 'a -> 'a queue
     val popStart : 'a queue -> 'a queue * 'a
+    val peekStart : 'a queue -> 'a
 
 end

          
M persistent-queue.sml +12 -2
@@ 33,12 33,17 @@ structure PersistentQueue :> PERSISTENT_
 
     fun popStart ({ start, size, trie } : 'a queue) : ('a queue * 'a) =
         case T.find (trie, start) of
-            NONE => raise Size
+            NONE => raise Empty
           | SOME x =>
             let val t = T.remove (trie, start)
             in
                 ({ start = start + 0w1, size = size - 0w1, trie = t }, x)
             end
+
+    fun peekStart ({ start, size, trie } : 'a queue) : 'a =
+        case T.find (trie, start) of
+            NONE => raise Empty
+          | SOME x => x
             
     fun append ({ start, size, trie }, x) =
         let val _ = if size = maxLenW then raise Size else ()

          
@@ 49,13 54,18 @@ structure PersistentQueue :> PERSISTENT_
 
     fun popEnd ({ start, size, trie } : 'a queue) : ('a queue * 'a) =
         case T.find (trie, start + size - 0w1) of
-            NONE => raise Size
+            NONE => raise Empty
           | SOME x =>
             let val t = T.remove (trie, start + size - 0w1)
             in
                 ({ start = start, size = size - 0w1, trie = t }, x)
             end
 
+    fun peekEnd ({ start, size, trie } : 'a queue) : 'a =
+        case T.find (trie, start + size - 0w1) of
+            NONE => raise Empty
+          | SOME x => x
+
     fun sub (v as { start, size, trie }, i) =
         if i < 0 orelse i >= length v
         then raise Subscript