(* Operations for pairs of CPCDs (see cpcd.sml) analogous to operations provided by ListPair. For latest version see: http://moonflare.com/code/cpcd/cpcd.php Copyright (c) 2003, Derrick Coetzee All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - The name of Derrick Coetzee may not be used to endorse or promote products derived from this software without specific prior written permission. This software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright owner or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. *) structure CPCDPair (* : sig end *) = struct structure Q = CPCD; (* Q as in deQUE *) (* Gets a deque of pairs (i,j), so that the nth pair has i=nth element of first deque and j=nth element of second deque. If deques are of unequal length, the tail of the longer one is ignored. (Time: O(min(m,n)), where m,n=length of given deques) *) fun zip(q,r) = let fun zipHelper(Q.EmptyDeque, _, result) = result | zipHelper(_, Q.EmptyDeque, result) = result | zipHelper(q, r, result) = zipHelper(Q.tl q, Q.tl r, Q.inject( (Q.hd q, Q.hd r), result )) in zipHelper(q,r,Q.EmptyDeque) end (* Given a deque of pairs, extracts two deques, one from the first elements of the pairs, and from the second elements. (Time: O(n), n=deque length) *) fun unzip(q) = let fun unzipHelper(Q.EmptyDeque, result) = result | unzipHelper(q, (resultLeft, resultRight)) = unzipHelper(Q.tl q, case (Q.hd q) of (left, right) => (Q.inject(left, resultLeft), Q.inject(right, resultRight))) in unzipHelper(q, (Q.EmptyDeque, Q.EmptyDeque)) end (* Equivalent to CPCD.map f (zip (q, r)), without the extra space usage. (Time: O(min(m,n)), m,n=input deque lengths) *) fun map f (q, r) = let fun mapHelper(Q.EmptyDeque, _, result) = result | mapHelper(_, Q.EmptyDeque, result) = result | mapHelper(q, r, result) = mapHelper(Q.tl q, Q.tl r, Q.inject( f(Q.hd q, Q.hd r), result )) in mapHelper(q,r,Q.EmptyDeque) end (* Equivalent to CPCD.app f (zip (q, r)), without the extra space usage. (Time: O(min(m,n)), m,n=input deque lengths) *) fun app f (Q.EmptyDeque, _) = () | app f (_, Q.EmptyDeque) = () | app f (q, r) = (f(Q.hd q, Q.hd r); app f (Q.tl q, Q.tl r)) (* Folds f over two deques, passing f (x,y,soFar), where x is from the left deque, y from the right deque, and soFar the result that has been built up so far. (Time: O(min(m,n)), where m,n=length of given deques) *) fun foldl f init (q,r) = let fun foldlHelper(Q.EmptyDeque, _, result) = result | foldlHelper(_, Q.EmptyDeque, result) = result | foldlHelper(q, r, result) = foldlHelper(Q.tl q, Q.tl r, f(Q.hd q, Q.hd r, result)) in foldlHelper(q,r,init) end (* Folds f over two deques, passing f (x,y,soFar), where x is from the left deque, y from the right deque, and soFar the result that has been built up so far. (Time: O(min(m,n)), where m,n=length of given deques) *) fun foldr f init (q,r) = let fun foldrHelper(Q.EmptyDeque, _, result) = result | foldrHelper(_, Q.EmptyDeque, result) = result | foldrHelper(q, r, result) = foldrHelper(Q.dropLast q, Q.dropLast r, f(Q.last q, Q.last r, result)) in foldrHelper(q,r,init) end (* Returns true iff f(x,y) returns true for some corresponding pair of elements from the two queues. (Time: O(min(m,n)), where m,n=length of given deques) *) fun exists f (Q.EmptyDeque,_) = false | exists f (_,Q.EmptyDeque) = false | exists f (q,r) = f(Q.hd q,Q.hd r) orelse exists f (Q.tl q, Q.tl r) (* Returns true iff f(x,y) returns true for every corresponding pair of elements from the two queues. (Time: O(min(m,n)), where m,n=length of given deques) *) fun all f qpair = not(exists (not o f) qpair) end