1 #ifndef ARRAY_AD_CACHE_H 2 #define ARRAY_AD_CACHE_H 21 virtual bool is_valid(
const K& key)
const = 0;
23 virtual void erase(
const K& key) = 0;
24 virtual void clear() = 0;
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;
48 return cache_map.find(key) != cache_map.end();
58 inline virtual void erase(
const K& key)
68 std::map<K, ArrayAd<T, D> > cache_map;
93 cached_data_.resize(prealloc_size);
97 inline typename std::vector<std::pair<K, ArrayAd<T, D> > >::const_iterator
find(
const K& key)
const 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();
109 inline typename std::vector<std::pair<K, ArrayAd<T, D> > >::iterator
find(
const K& key)
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();
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;
133 return find(key) != cached_data_.end();
142 if ((
int) cached_data_.size() > cache_index)
143 cached_data_[cache_index] = std::pair<
K,
ArrayAd<T, D> >(key, cached_copy);
145 cached_data_.push_back(std::pair<
K,
ArrayAd<T, D> >(key, cached_copy));
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);
169 inline virtual void clear(
bool clear_memory)
172 cached_data_.clear();
177 virtual std::vector<std::pair<K, ArrayAd<T, D> > >
cached_data()
const 179 return std::vector<std::pair<K, ArrayAd<T, D> > >(cached_data_.begin(), cached_data_.begin()+cache_index);
183 std::vector<std::pair<K, ArrayAd<T, D> > > cached_data_;
virtual ~ArrayAdMapCache()
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.
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...
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...
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.
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...