AbstractSet.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 X-Way Rights BV
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00023 #ifndef _ABSTRACT_CLASS_BEE_UTIL_ABSTRACTSET_H
00024 #define _ABSTRACT_CLASS_BEE_UTIL_ABSTRACTSET_H
00025 
00026 #ifdef __cplusplus
00027 
00028 #include "beecrypt/c++/util/AbstractCollection.h"
00029 using beecrypt::util::AbstractCollection;
00030 #include "beecrypt/c++/util/Set.h"
00031 using beecrypt::util::Set;
00032 
00033 namespace beecrypt {
00034     namespace util {
00039         template<class E> class AbstractSet : public AbstractCollection<E>, public virtual Set<E>
00040         {
00041         protected:
00042             AbstractSet() {}
00043 
00044         public:
00045             virtual bool equals(const Object* obj) const throw ()
00046             {
00047                 if (this == obj)
00048                     return true;
00049 
00050                 if (obj)
00051                 {
00052                     if (!dynamic_cast<const Set<E>*>(obj))
00053                         return false;
00054 
00055                     const Collection<E>* c = dynamic_cast<const Collection<E>*>(obj);
00056                     if (c->size() != size())
00057                         return false;
00058 
00059                     return containsAll(*c);
00060                 }
00061                 return false;
00062             }
00063             virtual jint hashCode() const throw ()
00064             {
00065                 jint pos = size(), result = 0;
00066                 Iterator<E>* it = iterator();
00067                 assert(it != 0);
00068                 while (--pos >= 0)
00069                 {
00070                     E* e = it->next();
00071                     result += e->hashCode();
00072                 }
00073                 delete it;
00074                 return result;
00075             }
00076             virtual Iterator<E>* iterator() = 0;
00077             virtual Iterator<E>* iterator() const = 0;
00078             virtual bool removeAll(const Collection<E>& c)
00079             {
00080                 bool result = false;
00081                 jint pos = size(), cpos = c.size();
00082                 if (pos > cpos)
00083                 {
00084                     Iterator<E>* it = c.iterator();
00085                     assert(it != 0);
00086                     while (--cpos >= 0)
00087                         result |= AbstractCollection<E>::remove(it->next());
00088                     delete it;
00089                 }
00090                 else
00091                 {
00092                     Iterator<E>* it = iterator();
00093                     assert(it != 0);
00094                     while (--pos >= 0)
00095                         if (c.contains(it->next()))
00096                         {
00097                             it->remove();
00098                             result = true;
00099                         }
00100                     delete it;
00101                 }
00102                 return result;
00103             }
00104             virtual jint size() const throw () = 0;
00105         };
00106     }
00107 }
00108 
00109 #endif
00110 
00111 #endif

Generated on Fri Jun 19 13:39:40 2009 for BeeCrypt C++ by  doxygen 1.5.8