#ifndef AG_SET_H #define AG_SET_H /* * The AGCLIB1 Class Library, Version 1.0 * Copyright (c) Parsifal Software, 2001. All Rights Reserved. * http://www.parsifalsoft.com */ #include "agbset.h" template class AgSet : public AgCopyControl > { typedef AgSetBase SetBase; typedef AgCopyControl Base; typedef AgSet This; public: typedef AgContents Contents; AgSet() : Base(new SetBase()) {} AgSet(const AgContainerContents &x) : Base(new AgSetBase(x)) {} AgSet(const This &s) : Base(s) {} ~AgSet() {} const Object *add(const Object &object) {return kernel()->add(object);} int remove(const Object &object) {return kernel()->remove(object);} Object *contains(const Object &e) {return kernel()->contains(e);} const Object *contains(const Object &e) const {return kernel()->contains(e);} int size() const {return kernel()->size();} AgSet &reset() {kernel()->reset(); return *this;} const Contents contents() const {return Contents(*this);} This &operator = (const This &x) {Base::operator = (x); return *this;} This &operator = (const AgContainerContents &x) {*kernel() = x; return *this;} Object &operator [] (int x) {return kernel()->operator [] (x);} const Object &operator [] (int x) const {return kernel()->operator [] (x);} int operator <= (const AgSet &x) const {return *kernel() <= *x.kernel();} int operator >= (const AgSet &x) const {return *kernel() >= *x.kernel();} int operator < (const AgSet &x) const {return *kernel() < *x.kernel();} int operator > (const AgSet &x) const {return *kernel() > *x.kernel();} // int operator == (const AgSet &x) const {return *kernel() == *x.kernel();} // int operator != (const AgSet &x) const {return *kernel() != *x.kernel();} #ifdef TESTING_AG_SET int maxDepth() const {return kernel()->maxDepth();} double avgDepth() const {return kernel()->avgDepth();} #endif }; // Definition of set union template AgSet &operator += (AgSet &dest, const Container &source) { int n = source.size(); for (int i = 0; i < n; i++) dest.add((Object &) source[i]); return dest; } template AgSet operator + (const AgSet &x, const Container &y) { AgSet result = x; return result += y; } // Definition of set difference template AgSet &operator -= (AgSet &dest, const Container &source) { int i, n = source.size(); for (i = 0; i < n; i++) dest.remove((Object &) source[i]); return dest; } template AgSet operator -(const AgSet &x, const Container &y) { AgSet result = x; return result -= y; } // Definition of set intersection template AgSet operator *(const AgSet &x, const Container &y) { AgSet result; int n = y.size(); while (n--) { const Object &object = y[n]; if (x.contains(object)) result.add(object); } return result; } template AgSet &operator *= (AgSet &dest, const Container &source) { return dest = dest * source; } // Subset operators // x >= y is true if and only if x contains all the elements of y template int operator >= (const AgSet &x, const Container &y) { int n = y.size(); while (n--) if (!x.contains(y[n])) return 0; return 1; } template int agABTHash(const AgSet &c, int startValue) { return agABTHash(c.kernel()->hash(), startValue); } template int agABTHash(const AgSet &c) { return agABTHash(c, 0); } #endif