Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] View of /smlnj-lib/trunk/Util/plist.sml
ViewVC logotype

View of /smlnj-lib/trunk/Util/plist.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 651 - (download) (annotate)
Thu Jun 1 18:34:03 2000 UTC (19 years, 2 months ago) by monnier
Original Path: sml/trunk/src/smlnj-lib/Util/plist.sml
File size: 1678 byte(s)
bring revisions from the vendor branch to the trunk
(* plist.sml
 *
 * COPYRIGHT (c) 1999 Bell Labs, Lucent Technologies.
 *
 * Property lists using Stephen Weeks's implementation.
 *)

structure PropList :> PROP_LIST =
  struct 

    type holder = exn list ref 

    fun newHolder() : holder = ref []

    fun clearHolder r = (r := [])

    fun mkProp () = let
	  exception E of 'a 
	  fun cons(a, l) = E a :: l 
	  fun peek [] = NONE
	    | peek (E a :: _) = SOME a
	    | peek (_ :: l) = peek l
	  fun delete [] = []
	    | delete (E a :: r) = r
	    | delete (x :: r) = x :: delete r
	  in
	    { cons = cons, peek = peek, delete = delete }
	  end

    fun mkFlag () = let
	  exception E
	  fun peek [] = false
	    | peek (E :: _) = true
	    | peek (_ :: l) = peek l
	  fun set (l, flg) = let
		fun set ([], _) = if flg then E::l else l
		  | set (E::r, xs) = if flg then l else List.revAppend(xs, r)
		  | set (x::r, xs) = set (r, x::xs)
		in
		  set (l, [])
		end
	  in
	    { set = set, peek = peek }
	  end

    fun newProp (selHolder : 'a -> holder, init : 'a -> 'b) = let
	  val {peek, cons, delete} = mkProp() 
	  fun peekFn a = peek(!(selHolder a))
	  fun getF a = let
		val h = selHolder a
		in
		  case peek(!h)
		   of NONE => let val b = init a in h := cons(b, !h); b end
		    | (SOME b) => b
		  (* end case *)
		end
	  fun clrF a = let
		val h = selHolder a
		in
		  h := delete(!h)
		end
	  in
	    {peekFn = peekFn, getFn = getF, clrFn = clrF}
	  end

    fun newFlag (selHolder : 'a -> holder) = let
	  val {peek, set} = mkFlag() 
	  fun getF a = peek(!(selHolder a))
	  fun setF (a, flg) = let
		val h = selHolder a
		in
		  h := set(!h, flg)
		end
	  in
	    {getFn = getF, setFn = setF}
	  end

  end 


root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0