CrystalSpace

Public API Reference

csutil/set.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 2003 by Mat Sutcliffe <[email protected]>
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 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     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public
00015     License along with this library; if not, write to the Free
00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00017 */
00018 
00019 #ifndef __CS_UTIL_SET_H__
00020 #define __CS_UTIL_SET_H__
00021 
00022 #include "csutil/hash.h"
00023 
00036 template <class T, class Allocator = CS::Memory::AllocatorMalloc> 
00037 class csSet
00038 {
00039 public:
00040   typedef csHash<bool, T, Allocator> HashType;
00041 
00042 private:
00043   typedef typename HashType::ConstGlobalIterator ParentIter;
00044   HashType map;
00045 
00046 public:
00047   /* Unfortunately, MSVC6 barfs if we derive this from ParentIter. */
00049   class GlobalIterator
00050   {
00051   protected:
00052     ParentIter iter;
00053     GlobalIterator () {}
00054     GlobalIterator (const csSet<T>* s) : iter(s->map.GetIterator()) {}
00055 
00056   public:
00057     friend class csSet<T>;
00058 
00059     GlobalIterator (const GlobalIterator& o) : iter(o.iter) {}
00060     GlobalIterator& operator=(const GlobalIterator& o)
00061     { iter = o.iter; return *this; }
00062 
00064     bool HasNext () const
00065     { return iter.HasNext(); }
00066 
00068     T Next()
00069     {
00070       T key;
00071       iter.Next(key);
00072       return key;
00073     }
00074   };
00075   friend class GlobalIterator;
00076 
00082   csSet (int size = 23, int grow_rate = 5, int max_size = 20000)
00083         : map (size, grow_rate, max_size)
00084   {
00085   }
00086 
00091   void Add (const T& object)
00092   {
00093     if (!Contains (object))
00094       AddNoTest (object);
00095   }
00096 
00103   void AddNoTest (const T& object)
00104   {
00105     map.Put (object, true);
00106   }
00107 
00111   bool Contains (const T& object) const
00112   {
00113     return map.Contains (object);
00114   }
00115 
00121   bool In (const T& object) const
00122   { return Contains(object); }
00123 
00127   void DeleteAll ()
00128   {
00129     map.DeleteAll ();
00130   }
00131 
00133   void Empty() { DeleteAll(); }
00134 
00140   bool Delete (const T& object)
00141   {
00142     return map.Delete (object, true);
00143   }
00144 
00146   size_t GetSize () const
00147   {
00148     return map.GetSize ();
00149   }
00150 
00156   bool IsEmpty() const
00157   {
00158     return GetSize() == 0;
00159   }
00160 
00166   GlobalIterator GetIterator () const
00167   {
00168     return GlobalIterator(this);
00169   }
00170 };
00171 
00174 #endif // __CS_UTIL_SET_H__

Generated for Crystal Space by doxygen 1.4.7