Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

valuegenmap.h

Go to the documentation of this file.
00001 
00007 #ifndef __VALUEGENMAP_H__
00008 #define __VALUEGENMAP_H__
00009 
00010 #include <map>
00011 #include <string>
00012 #include <algorithm>
00013 #include <functional>
00014 
00015 #include "valuegen.h"
00016 #include "generic_stdlib.h"
00017 
00018 /*
00019    Note: the various (*iterator).item constructions were originally
00020    written in the more natural form iterator->item, but they were
00021    changed to the uglier notation because the Cray T3E compiler (PE
00022    3.1) couldn't handle them.
00023 */
00024 
00025 /******************************************************************************/
00026 /* ValueGeneratorMap class hierarchy                                          */
00027 /******************************************************************************/
00028 
00039 template<class T, class K=string>
00040 class ValueGeneratorMap : public ValueGen {
00041 public:
00043   typedef K key_type;
00044 
00046   typedef ValueGenerator<T>* data_type;
00047 
00049   typedef pair<const key_type, data_type> value_type;
00050 
00052   typedef std::map< key_type, data_type > map_type;
00053 
00055   typedef typename map_type::iterator iterator;
00056 
00058   typedef typename map_type::const_iterator const_iterator; 
00059   
00060   ValueGeneratorMap() { }
00061 
00062   ValueGeneratorMap(const ValueGeneratorMap& b)  /* Copy constructor */
00063     : ValueGen(b) {
00064     for (const_iterator i=b.begin(); i!=b.end(); i++)
00065       set((*i).first,(*i).second->clone());
00066   }
00067   
00068   ~ValueGeneratorMap() {  Generic::delete_contents(m);  }
00069 
00071   iterator       begin()       {  return m.begin();  } 
00072   const_iterator begin() const {  return m.begin();  } 
00073   iterator       end()         {  return m.end();    } 
00074   const_iterator end()   const {  return m.end();    } 
00076   pair<iterator,bool> insert(const value_type& x)  {  return m.insert(x);  }
00077 
00083   void set(const key_type& k, const data_type d);
00084   
00086   void set_default(const key_type& k, const T d);
00087 
00095   T get(const key_type& k) const;
00096     
00103   T* getptr(const key_type& k);
00104   virtual bool next();
00105   virtual void reset();
00106 
00108   virtual void relink(const ValueGeneratorMap<T,K>* b);
00109 
00111   virtual void merge(const ValueGeneratorMap<T,K>& b);
00112 
00114   virtual ValueGeneratorMap<T,K>* clone() const { return new ValueGeneratorMap(*this); };
00115 
00116 private:
00117   map_type m;
00118 };
00119 
00120 
00121 
00122 template<class T, class K>
00123 void ValueGeneratorMap<T,K>::set(const key_type& k, const data_type d)
00124 {
00125   iterator existing = m.find(k);
00126   if (existing == end())
00127     insert(Generic::make_pair_leftconst(k,d));
00128   else {
00129     delete (*existing).second;
00130     (*existing).second=d;
00131   }
00132 }
00133 
00134 
00135 template<class T, class K>
00136 void ValueGeneratorMap<T,K>::set_default(const key_type& k, const T d)
00137 {
00138   iterator existing = m.find(k);
00139   if (existing == end()) {
00140     insert(Generic::make_pair_leftconst(k,new ValueGenerator<T>(d)));
00141   }
00142 }
00143 
00144 
00145 
00146 template<class T, class K>
00147 T ValueGeneratorMap<T,K>::get(const key_type& k) const
00148 {
00149   const_iterator existing = m.find(k);
00151   return (existing == end() ? 0 : (*existing).second->value());
00152 }
00153 
00154 
00155 
00156 template<class T, class K>
00157 T* ValueGeneratorMap<T,K>::getptr(const key_type& k)
00158 {
00159   const_iterator existing = m.find(k);
00160   data_type secptr = (existing == end() ?
00161                         (*((insert(Generic::make_pair_leftconst(k,new ValueGenerator<T>(T())))).first)).second
00162                         : (*existing).second);
00163 
00164   return secptr->valueptr();
00165 }
00166 
00167 
00168 
00169 template<class T, class K>
00170 bool ValueGeneratorMap<T,K>::next()
00171 {
00172   bool status=true;
00173   for (iterator i=m.begin(); i!=m.end(); i++) {
00174     const bool newstatus = (*i).second->next();
00175     if (!newstatus) status=newstatus;
00176   }
00177   return status;
00178 }
00179 
00180 
00181 
00182 template<class T, class K>
00183 void ValueGeneratorMap<T,K>::reset()
00184 {
00185   for (iterator i=m.begin(); i!=m.end(); i++)
00186     (*i).second->reset();
00187 }
00188 
00189 
00190 
00191 template<class T, class K>
00192 void ValueGeneratorMap<T,K>::relink(const ValueGeneratorMap<T,K>* b)
00193 {
00194   if (b)
00195     for (iterator i=m.begin(); i!=m.end(); i++) {
00196       const_iterator lookup = b->m.find((*i).first);
00197       if (lookup != b->end())
00198         (*i).second->relink((*lookup).second->valueptr());
00199     }
00200 }
00201 
00202 
00203 
00204 template<class T, class K>
00205 void ValueGeneratorMap<T,K>::merge(const ValueGeneratorMap<T,K>& b)
00206 {
00207   for (const_iterator i=b.begin(); i!=b.end(); i++)
00208     set((*i).first,(*i).second->clone());
00209 }
00210   
00211 
00212 
00214 typedef ValueGeneratorMap<double> DGenMap;
00215 
00216 
00217 
00218 #endif

Generated at Mon Aug 21 00:30:55 2000 for RF-LISSOM by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000