(* vim: syntax=sml A structure implementing SET containers using ML lists. $Id: ListSet.sml,v 1.1 2004/09/09 21:15:11 cstork Exp $ Copyright (c) 2003-2004 by Peter H. Froehlich . All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. *) functor ListSet (Element: ORDERED): SET = struct type element = Element.t; type set = element list; (* A new set is just the empty list. *) val new = []; (* An empty set is an empty list. *) fun empty s = null s; (* We insert by consing to the list representing the set. While this can lead to duplicate elements in the list, it also yields O(1) insertion. *) fun insert s e = e::s; (* We remove by filtering out all list elements equal to the one to be removed. Note that an element might be present *any* number of times in the list although it is logically in the set only once, so remove is O(n). *) fun remove s e = List.filter (fn x => (Element.compare x e) <> EQUAL) s; (* We check for the presence of an element by running down the list until we find it, so has is O(n). *) fun has s e = List.exists (fn x => (Element.compare x e) = EQUAL) s; end; (* ListSet *)