ReFRACtor
array_ad_cache.h
Go to the documentation of this file.
1 #ifndef ARRAY_AD_CACHE_H
2 #define ARRAY_AD_CACHE_H
3 
4 #include <map>
5 #include "array_ad.h"
6 
7 namespace FullPhysics {
8 
9  /****************************************************************/
16  template<class K, class T, int D> class ArrayAdCache
17  {
18  public:
19  virtual ~ArrayAdCache() {}
20  virtual ArrayAd<T, D> operator[](const K& key) const = 0;
21  virtual bool is_valid(const K& key) const = 0;
22  virtual void insert(const K& key, const ArrayAd<T, D>& value) = 0;
23  virtual void erase(const K& key) = 0;
24  virtual void clear() = 0;
25  };
26 
27  /****************************************************************/
32  template<class K, class T, int D> class ArrayAdMapCache : public ArrayAdCache<K, T, D>
33  {
34  public:
36  virtual ~ArrayAdMapCache() {}
37  inline virtual ArrayAd<T, D> operator[](const K& key) const
38  {
39  typename std::map<K, ArrayAd<T, D> >::const_iterator value_it;
40  value_it = cache_map.find(key);
41  if (value_it == cache_map.end())
42  throw Exception("Could not find value_it in cache");
43  return value_it->second;
44  }
45 
46  inline virtual bool is_valid(const K& key) const
47  {
48  return cache_map.find(key) != cache_map.end();
49  }
50 
51  inline virtual void insert(const K& key, const ArrayAd<T, D>& value)
52  {
53  ArrayAd<T, D> cached_copy;
54  cached_copy = value;
55  cache_map.insert(std::pair<K, ArrayAd<T, D> >(key, cached_copy));
56  }
57 
58  inline virtual void erase(const K& key)
59  {
60  cache_map.erase(key);
61  }
62 
63  inline virtual void clear()
64  {
65  cache_map.clear();
66  }
67  private:
68  std::map<K, ArrayAd<T, D> > cache_map;
69  };
70 
71  /****************************************************************/
86  template<class K, class T, int D> class ArrayAdVectorCache : public ArrayAdCache<K, T, D>
87  {
88  public:
89 
91  ArrayAdVectorCache(int prealloc_size = 0) : cache_index(0)
92  {
93  cached_data_.resize(prealloc_size);
94  }
95 
97  inline typename std::vector<std::pair<K, ArrayAd<T, D> > >::const_iterator find(const K& key) const
98  {
99  typename std::vector<std::pair<K, ArrayAd<T, D> > >::const_iterator curr_it;
100  curr_it = cached_data_.begin();
101  for ( ; curr_it != cached_data_.end() && curr_it != cached_data_.begin()+cache_index; curr_it++) if ( curr_it->first == key ) break;
102  if(curr_it == cached_data_.begin()+cache_index)
103  return cached_data_.end();
104  else
105  return curr_it;
106  }
107 
109  inline typename std::vector<std::pair<K, ArrayAd<T, D> > >::iterator find(const K& key)
110  {
111  typename std::vector<std::pair<K, ArrayAd<T, D> > >::iterator curr_it;
112  curr_it = cached_data_.begin();
113  for ( ; curr_it != cached_data_.end() && curr_it != cached_data_.begin()+cache_index; curr_it++) if ( curr_it->first == key ) break;
114  if(curr_it == cached_data_.begin()+cache_index)
115  return cached_data_.end();
116  else
117  return curr_it;
118  }
119 
122  inline virtual ArrayAd<T, D> operator[](const K& key) const
123  {
124  typename std::vector<std::pair<K, ArrayAd<T, D> > >::const_iterator value_it;
125  value_it = find(key);
126  if (value_it == cached_data_.end())
127  throw Exception("Could not find key in vector cache to retrieve");
128  return value_it->second;
129  }
130 
131  inline virtual bool is_valid(const K& key) const
132  {
133  return find(key) != cached_data_.end();
134  }
135 
138  inline virtual void insert(const K& key, const ArrayAd<T, D>& value)
139  {
140  ArrayAd<T, D> cached_copy;
141  cached_copy = value;
142  if ((int) cached_data_.size() > cache_index)
143  cached_data_[cache_index] = std::pair<K, ArrayAd<T, D> >(key, cached_copy);
144  else
145  cached_data_.push_back(std::pair<K, ArrayAd<T, D> >(key, cached_copy));
146  cache_index++;
147  }
148 
151  inline virtual void erase(const K& key)
152  {
153  typename std::vector<std::pair<K, ArrayAd<T, D> > >::iterator value_it;
154  value_it = find(key);
155  if (value_it == cached_data_.end())
156  throw Exception("Could not find key in vector cache to erase");
157  cached_data_.erase(value_it);
158  cache_index--;
159  }
160 
163  inline virtual void clear()
164  {
165  cache_index = 0;
166  }
167 
169  inline virtual void clear(bool clear_memory)
170  {
171  if (clear_memory)
172  cached_data_.clear();
173  clear();
174  }
175 
177  virtual std::vector<std::pair<K, ArrayAd<T, D> > > cached_data() const
178  {
179  return std::vector<std::pair<K, ArrayAd<T, D> > >(cached_data_.begin(), cached_data_.begin()+cache_index);
180  }
181  private:
182  int cache_index;
183  std::vector<std::pair<K, ArrayAd<T, D> > > cached_data_;
184  };
185 
186 }
187 
188 #endif
virtual void erase(const K &key)=0
Caches ArrayAd objects using a std::vector.
virtual bool is_valid(const K &key) const
virtual void clear()
clear simply resets the location of of the cache index by default without clearing any memory...
virtual void clear(bool clear_memory)
Clear the vector as well as reset insertion location.
virtual void erase(const K &key)
Removes a specific item from the vector This call can be expensive since it will need to shift items ...
virtual ArrayAd< T, D > operator[](const K &key) const =0
This is the base of the exception hierarchy for Full Physics code.
Definition: fp_exception.h:16
virtual void insert(const K &key, const ArrayAd< T, D > &value)
virtual bool is_valid(const K &key) const
ArrayAdVectorCache(int prealloc_size=0)
Optionally pass a preallocation size.
virtual void insert(const K &key, const ArrayAd< T, D > &value)
Insert the cached item into the currently available slot in the vector or creates a new slot...
The AutoDerivative<T> works well, and it works with blitz if you create a blitz::Array<AutoDerivative...
Definition: array_ad.h:40
const Unit K("K", 1.0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0)
virtual void insert(const K &key, const ArrayAd< T, D > &value)=0
virtual ArrayAd< T, D > operator[](const K &key) const
Retrieve a specific key and error if it is no keys are available However, will return the closest one...
virtual void clear()=0
std::vector< std::pair< K, ArrayAd< T, D > > >::iterator find(const K &key)
Finds the requested item using a STL lower bound call.
virtual ArrayAd< T, D > operator[](const K &key) const
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
std::vector< std::pair< K, ArrayAd< T, D > > >::const_iterator find(const K &key) const
Finds the requested item using a STL lower bound call.
Caches ArrayAd objects using a std::map.
virtual std::vector< std::pair< K, ArrayAd< T, D > > > cached_data() const
Obtain cached data up to last point inserted.
virtual bool is_valid(const K &key) const =0
virtual void erase(const K &key)
double value(const FullPhysics::AutoDerivative< double > &Ad)
Defines an interface for caching ArrayAd objects such that the underlying caching mechanism is not ne...

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11