/* set.q: ordered set data structure implemented by AVL trees $Id: set.q,v 1.3 2004/01/28 17:04:27 agraef Exp $ */ /* This file is part of the Q programming system. The Q programming system 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, or (at your option) any later version. The Q programming system is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ include stdlib, stddecl; public type Set = private const nil, bin H X M1 M2; /* Construction and type checking: */ public emptyset; // return the empty set public set Xs; // create a set from list Xs public isset X; // check whether X is a set /* Overloaded and public operations: */ /* The comparison operators are overloaded to implement the sub-/superset predicates (e.g., M1<=M2: M1 is a subset of M2, M1Y; = member M2 Y if XY; = rebal (mkbin X M1 (insert M2 Y)) if XY; = rebal (mkbin X M1 (delete M2 Y)) if X M2:Set = members M1 <> members M2; M1:Set <= M2:Set = all (member M2) (members M1); M1:Set >= M2:Set = all (member M1) (members M2); M1:Set < M2:Set = (M1<=M2) and then (M1<>M2); M1:Set > M2:Set = (M1>=M2) and then (M1<>M2); M1:Set + M2:Set = foldl insert M1 (members M2); M1:Set - M2:Set = foldl delete M1 (members M2); M1:Set * M2:Set = M1-(M1-M2);