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 using std::string;
00013 #include <algorithm>
00014 #include <functional>
00015 
00016 #include "valuegen.h"
00017 #include "generic_stdlib.h"
00018 
00019 /*
00020    Note: the various (*iterator).item constructions were originally
00021    written in the more natural form iterator->item, but they were
00022    changed to the uglier notation because the Cray T3E compiler (PE
00023    3.1) couldn't handle them.
00024 */
00025 
00026 /******************************************************************************/
00027 /* ValueGeneratorMap class hierarchy                                          */
00028 /******************************************************************************/
00029 
00040 template<class T, class K=string>
00041 class ValueGeneratorMap : public ValueGen {
00042 public:
00044   typedef K key_type;
00045 
00047   typedef ValueGenerator<T>* data_type;
00048 
00050   typedef std::pair<const key_type, data_type> value_type;
00051 
00053   typedef std::map< key_type, data_type > map_type;
00054 
00056   typedef typename map_type::iterator iterator;
00057 
00059   typedef typename map_type::const_iterator const_iterator; 
00060   
00061   ValueGeneratorMap() { }
00062 
00063   ValueGeneratorMap(const ValueGeneratorMap& b)  /* Copy constructor */
00064     : ValueGen(b) {
00065     for (const_iterator i=b.begin(); i!=b.end(); i++)
00066       set((*i).first,(*i).second->clone());
00067   }
00068   
00069   ~ValueGeneratorMap() {  Generic::delete_contents(m);  }
00070 
00072   iterator       begin()       {  return m.begin();  } 
00073   const_iterator begin() const {  return m.begin();  } 
00074   iterator       end()         {  return m.end();    } 
00075   const_iterator end()   const {  return m.end();    } 
00077   std::pair<iterator,bool> insert(const value_type& x)  {  return m.insert(x);  }
00078 
00084   void set(const key_type& k, const data_type d);
00085   
00087   void set_default(const key_type& k, const T d);
00088 
00096   T get(const key_type& k) const;
00097     
00104   T* getptr(const key_type& k);
00105 
00107   const data_type getvalgenptr(const key_type& k) const;
00108 
00109   virtual bool next();
00110   virtual void reset();
00111 
00113   virtual void relink(const ValueGeneratorMap<T,K>* b);
00114 
00116   virtual void merge(const ValueGeneratorMap<T,K>& b);
00117 
00119   virtual ValueGeneratorMap<T,K>* clone() const { return new ValueGeneratorMap(*this); };
00120 
00121 private:
00122   map_type m;
00123 };
00124 
00125 
00126 
00127 template<class T, class K>
00128 void ValueGeneratorMap<T,K>::set(const key_type& k, const data_type d)
00129 {
00130   iterator existing = m.find(k);
00131   if (existing == end())
00132     insert(Generic::make_pair_leftconst(k,d));
00133   else {
00134     delete (*existing).second;
00135     (*existing).second=d;
00136   }
00137 }
00138 
00139 
00140 template<class T, class K>
00141 void ValueGeneratorMap<T,K>::set_default(const key_type& k, const T d)
00142 {
00143   iterator existing = m.find(k);
00144   if (existing == end()) {
00145     insert(Generic::make_pair_leftconst(k,new ValueGenerator<T>(d)));
00146   }
00147 }
00148 
00149 
00150 
00151 template<class T, class K>
00152 T ValueGeneratorMap<T,K>::get(const key_type& k) const
00153 {
00154   const_iterator existing = m.find(k);
00156   return (existing == end() ? 0 : (*existing).second->value());
00157 }
00158 
00159 
00160 
00161 template<class T, class K>
00162 T* ValueGeneratorMap<T,K>::getptr(const key_type& k)
00163 {
00164   const_iterator existing = m.find(k);
00165   data_type secptr = (existing == end() ?
00166                         (*((insert(Generic::make_pair_leftconst(k,new ValueGenerator<T>(T())))).first)).second
00167                         : (*existing).second);
00168 
00169   return secptr->valueptr();
00170 }
00171 
00172 
00173 
00174 template<class T, class K>
00175 const typename ValueGeneratorMap<T,K>::data_type ValueGeneratorMap<T,K>::getvalgenptr(const key_type& k) const
00176 {
00177   const_iterator existing = m.find(k);
00179   return (existing == end() ? 0 : (*existing).second);
00180 }
00181 
00182 
00183 
00184 template<class T, class K>
00185 bool ValueGeneratorMap<T,K>::next()
00186 {
00187   bool status=true;
00188   for (iterator i=m.begin(); i!=m.end(); i++) {
00189     const bool newstatus = (*i).second->next();
00190     if (!newstatus) status=newstatus;
00191   }
00192   return status;
00193 }
00194 
00195 
00196 
00197 template<class T, class K>
00198 void ValueGeneratorMap<T,K>::reset()
00199 {
00200   for (iterator i=m.begin(); i!=m.end(); i++)
00201     (*i).second->reset();
00202 }
00203 
00204 
00205 
00206 template<class T, class K>
00207 void ValueGeneratorMap<T,K>::relink(const ValueGeneratorMap<T,K>* b)
00208 {
00209   if (b)
00210     for (iterator i=m.begin(); i!=m.end(); i++) {
00211       const_iterator lookup = b->m.find((*i).first);
00212       if (lookup != b->end())
00213         (*i).second->relink((*lookup).second->valueptr());
00214     }
00215 }
00216 
00217 
00218 
00219 template<class T, class K>
00220 void ValueGeneratorMap<T,K>::merge(const ValueGeneratorMap<T,K>& b)
00221 {
00222   for (const_iterator i=b.begin(); i!=b.end(); i++)
00223     set((*i).first,(*i).second->clone());
00224 }
00225   
00226 
00227 
00229 typedef ValueGeneratorMap<double> DGenMap;
00230 
00231 
00232 
00233 #endif

Generated on Mon Jan 20 02:35:45 2003 for RF-LISSOM by doxygen1.3-rc2