PLearn 0.1
Set.h
Go to the documentation of this file.
00001 #ifndef Set_H
00002 #define Set_H
00003 
00004 #include <plearn/math/TMat.h>
00005 
00006 namespace PLearn {
00007 using namespace std;
00008 
00009 class PPointableSet : public set<int>, public PPointable
00010 {
00011 public:
00012     PPointableSet() {}
00013     virtual ~PPointableSet() {}
00014 };
00015 
00016 inline PStream &operator<<(PStream &out, const PPointableSet &pp_set)
00017 { out << static_cast<const set<int> &>(pp_set); return out; }
00018 
00019 inline PStream &operator>>(PStream &in, PPointableSet &pp_set)
00020 { in >> static_cast<set<int> &>(pp_set); return in; }
00021 
00022 typedef PPointableSet::iterator SetIterator;
00023 
00024 class Set : public PP<PPointableSet>
00025 {
00026 
00027 public:
00028 
00029     Set() : PP<PPointableSet>(new PPointableSet) {}
00030     Set(PPointableSet* p) : PP<PPointableSet>(p) {}
00031 
00032     bool contains(int elem) { return ptr->find(elem) != ptr->end(); }
00033     SetIterator find(int elem) { return ptr->find(elem); }
00034     void insert(int elem) { ptr->insert(elem); }
00035     int size() { return ptr->size(); }
00036     bool isEmpty() { return (ptr->size() == 0); }
00037     void remove(int elem) { ptr->erase(elem); }
00038     void clear() { ptr->clear(); }
00039   
00040     void replace(int old_elem, int new_elem) 
00041     {
00042         remove(old_elem);
00043         insert(new_elem);
00044     }
00045   
00046     void merge(Set s) 
00047     {
00048 //     Set res;
00049 //     set_union(begin(), end(),
00050 //               s.begin(), s.end(),
00051 //               insert_iterator<PPointableSet>(*res, res.begin()));
00052 //     *ptr = *res;
00053         for (SetIterator it = s.begin(); it != s.end(); ++it)
00054             insert(*it);
00055     }
00056 
00057     void difference(Set s)
00058     {
00059         Set res;
00060         set_difference(begin(), end(),
00061                        s.begin(), s.end(),
00062                        insert_iterator<PPointableSet>(*res, res.begin()));
00063         *ptr = *res;
00064     }
00065 
00066     void intersection(Set s)
00067     {
00068         Set res;
00069         set_intersection(begin(), end(),
00070                          s.begin(), s.end(),
00071                          insert_iterator<PPointableSet>(*res, res.begin()));
00072         *ptr = *res;
00073     }
00074 
00075     SetIterator begin() { return ptr->begin(); }
00076     SetIterator end() { return ptr->end(); }
00077   
00078     bool operator==(Set& s) { return *ptr == *s.ptr; }
00079     bool operator!=(Set& s) { return *ptr != *s.ptr; }
00080 
00081 };
00082 
00083 inline PPointableSet * newSet()
00084 { return new PPointableSet; };
00085 
00086 inline void merge(Set a, Set b, Set res)
00087 {
00088     set_union(a.begin(), a.end(),
00089               b.begin(), b.end(),
00090               insert_iterator<PPointableSet>(*res, res.begin()));
00091 }
00092 
00093 inline void difference(Set a, Set b, Set res)
00094 {
00095     set_difference(a.begin(), a.end(),
00096                    b.begin(), b.end(),
00097                    insert_iterator<PPointableSet>(*res, res.begin()));
00098 }
00099 
00100 inline void intersection(Set a, Set b, Set res)
00101 {
00102     set_intersection(a.begin(), a.end(),
00103                      b.begin(), b.end(),
00104                      insert_iterator<PPointableSet>(*res, res.begin()));
00105 }
00106 
00107 inline ostream& operator<<(ostream& out, Set s)
00108 {
00109     for(SetIterator it = s.begin(); it != s.end(); ++it)
00110     {
00111         out << *it << " ";
00112     }
00113     return out;
00114 }
00115 
00116 //include "pl_io.h"
00117 // inline pl_ostream& operator<<(pl_ostream& out, Set& s)
00118 // {
00119 //   int l = s.size(); 
00120 //   out << l;
00121 //   if (out.flags.test(plf_binary)) {
00122 //     for(SetIterator it = s.begin(); it != s.end(); ++it)
00123 //       PLearn::binwrite(out,*it);
00124 //   }
00125 //   else {
00126 //     out << raw << '\n';
00127 //     for(SetIterator it = s.begin(); it != s.end(); ++it)
00128 //       out << *it << " ";
00129 //     out << raw << '\n';
00130 //   }
00131 //   return out;
00132 // }
00133 
00134 // inline pl_istream& operator>>(pl_istream& in, Set& s)
00135 // {
00136 //   int l;
00137 //   s.clear();
00138 //   in >> l;
00139 //   int v=0;
00140 //   if (in.flags.test(plf_binary)) {
00141 //     for (int i=0;i<l;i++)
00142 //     {
00143 //       PLearn::binread(in,v);
00144 //       s.insert(v);
00145 //     }
00146 //   }
00147 //   else {
00148 //     for (int i=0;i<l;i++)
00149 //     {
00150 //       in >> v;
00151 //       s.insert(v);
00152 //     }
00153 //   }
00154 // }
00155 
00156 }
00157 
00158 #endif
00159 
00160 
00161 /*
00162   Local Variables:
00163   mode:c++
00164   c-basic-offset:4
00165   c-file-style:"stroustrup"
00166   c-file-offsets:((innamespace . 0)(inline-open . 0))
00167   indent-tabs-mode:nil
00168   fill-column:79
00169   End:
00170 */
00171 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines