set.h

Go to the documentation of this file.
00001 #ifndef SET_CLASS
00002 #define SET_CLASS
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : set                                                               *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "array.h"
00019 #include "packable.h"
00020 #include "string_array.h"
00021 //hmmm: seems like we need a string_set file to get rid of these
00022 //      extra includes that sets themselves don't need.
00023 
00024 namespace basis {
00025 
00027 
00028 template <class contents>
00029 class set : public array<contents>
00030 {
00031 public:
00033 
00035   inline set(int num = 0, const contents *init = NIL,
00036       u_short flags = array<contents>::EXPONE)
00037   : array<contents>(num, init, flags) {}
00038 
00039   inline ~set() {}  
00040 
00041   inline int elements() const { return this->length(); }
00043 
00044   inline bool empty() const { return this->elements() == 0; }
00046   inline bool non_empty() const { return this->elements() != 0; }
00048 
00049   inline void clear() { this->reset(); }  
00050 
00051   bool member(const contents &to_test) const;
00053   
00054   bool add(const contents &to_add);
00056 
00059   inline set &operator += (const contents &to_add)
00060       { this->add(to_add); return *this; }
00062   inline set &operator += (const set &to_add)
00063       { this->unionize(to_add); return *this; }
00065 
00066   bool remove(const contents &to_remove);
00068 
00070   inline set &operator -= (const contents &to_zap)
00071       { this->remove(to_zap); return *this; }
00073   inline set &operator -= (const set &to_zap)
00074       { this->differentiate(to_zap); return *this; }
00076 
00077   set set_union(const set &union_with) const;
00079 
00083   void unionize(const set &union_with);
00085 
00086   inline set operator + (const set &uw) const { return this->set_union(uw); }
00088 
00089   set intersection(const set &intersect_with) const;
00091 
00092   inline set operator * (const set &iw) const { return this->intersection(iw); }
00094 
00095   set difference(const set &differ_with) const;
00097 
00100   void differentiate(const set &differ_with);
00102 
00105   inline set operator - (const set &dw) const { return this->difference(dw); }
00107 
00108   int find(const contents &to_find) const;
00110 
00114 
00115 
00117   inline bool remove_index(int index)
00118       { return this->zap(index, index) == common::OKAY; }
00119 };
00120 
00122 
00124 template <class contents>
00125 void pack(byte_array &packed_form, const basis::set<contents> &to_pack)
00126 {
00127   basis::obscure_attach(packed_form, to_pack.elements());
00128   for (int i = 0; i < to_pack.elements(); i++) to_pack[i].pack(packed_form);
00129 }
00130 
00132 template <class contents>
00133 bool unpack(byte_array &packed_form, basis::set<contents> &to_unpack)
00134 {
00135   to_unpack.clear();
00136   int len;
00137   if (!basis::obscure_detach(packed_form, len)) return false;
00138   contents to_fill;
00139   for (int i = 0; i < len; i++) {
00140     if (!to_fill.unpack(packed_form)) return false;
00141     to_unpack.add(to_fill);
00142   }
00143   return true;
00144 }
00145 
00146 } // namespace.
00147 
00149 
00150 #include "object_base.h"
00151 
00153 class int_set : public basis::set<int>, public virtual object_base
00154 {
00155 public:
00156   inline int_set() {}
00158   inline int_set(const basis::set<int> &to_copy) : basis::set<int>(to_copy) {}
00160 
00161   IMPLEMENT_CLASS_NAME("int_set");
00162 };
00163 
00165 class string_set : public basis::set<istring>, public packable,
00166     public virtual object_base
00167 {
00168 public:
00169   inline string_set() {}
00171   inline string_set(const basis::set<istring> &to_copy)
00172       : basis::set<istring>(to_copy) {}
00174   inline string_set(const string_array &to_copy) {
00175     for (int i = 0; i < to_copy.length(); i++)
00176       add(to_copy[i]);
00177   }
00178 
00179   IMPLEMENT_CLASS_NAME("string_set");
00180 
00181   inline bool operator == (const string_set &compare) const {
00182     for (int i = 0; i < elements(); i++)
00183       if (get(i) != compare.get(i)) return false;
00184     return true;
00185   }
00186 
00187   inline operator string_array() const {
00188     string_array to_return;
00189     for (int i = 0; i < length(); i++)
00190       to_return += get(i);
00191     return to_return;
00192   }
00193 
00194   inline virtual void pack(byte_array &packed_form) const
00195       { basis::pack(packed_form, *this); }
00196   inline virtual bool unpack(byte_array &packed_form)
00197       { return basis::unpack(packed_form, *this); }
00198   inline virtual int packed_size() const {
00199     int to_return = sizeof(int) * 2;  // length packed in, using obscure.
00200     for (int i = 0; i < length(); i++)
00201       to_return += get(i).length() + 1;
00202     return to_return;
00203   }
00204 };
00205 
00207 class pointer_set : public basis::set<void *>
00208 {
00209 public:
00210   inline pointer_set() {}
00212   inline pointer_set(const basis::set<void *> &to_copy) : basis::set<void *>(to_copy)
00213       {}
00215 };
00216 
00217 #endif
00218 

Generated on Sat Oct 11 04:28:40 2008 for HOOPLE Libraries by  doxygen 1.5.1