Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FragCache.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 // :noTabs=false:tabSize=4:indentSize=4:
4 //
5 // (c) Copyright Rosetta Commons Member Institutions.
6 // (c) This file is part of the Rosetta software suite and is made available under license.
7 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 /// @file core/fragments/Frame.hh
12 /// @brief set of fragments for a certain alignment frame
13 /// @author Oliver Lange (olange@u.washington.edu)
14 /// @date Wed Oct 20 12:08:31 2007
15 ///
16 #ifndef INCLUDED_core_fragment_FragCache_HH
17 #define INCLUDED_core_fragment_FragCache_HH
18 
19 // Unit Headers
21 
22 // Package Headers
23 // AUTO-REMOVED #include <core/fragment/FragData.hh>
24 #include <core/fragment/Frame.hh>
27 
28 
29 // Project Headers
30 #include <core/pose/Pose.fwd.hh>
31 
32 // Utility headers
33 #include <utility/vector1.fwd.hh>
34 #include <utility/pointer/ReferenceCount.hh>
35 #include <utility/excn/Exceptions.hh>
36 
37 // C++ STL Headers
38 #include <map>
39 
40 #include <utility/vector1.hh>
41 
42 
43 
44 namespace core {
45 namespace fragment {
46 
47 template< class T>
48 //typedef core::Real T;
50  typedef std::map< core::Size, T > TMap;
51 public:
53  return new MapCacheUnit<T>;
54  };
55 
56  void remap_value( BaseCacheUnit const& source, Size source_id, Size new_id ) {
57  T value;
58  dynamic_cast< MapCacheUnit<T> const& > (source).retrieve( source_id, value );
59  store( new_id, value );
60  };
61 
62 
63  bool retrieve( core::Size frag_id, T& value ) const {
64  typename TMap::const_iterator iter( map_.find( frag_id ) );
65  if ( iter == map_.end() ) {
66  return false;
67  } else {
68  value = iter->second;
69  return true;
70  };
71  };
72 
73 
74  void store( Size frag_id, T const& value ) {
75  map_[ frag_id ] = value;
76  }
77 
78  void register_frag_id( Size ) { }; //do nothing -- cache has lazy evaluation
79 private:
80  TMap map_;
81 };
82 
83 template< class T>
84 //typedef core::Real T;
87 public:
89  return new VectorCacheUnit<T>;
90  }
91 
92  void remap_value( BaseCacheUnit const& source, Size source_id, Size new_id ) {
93  T value;
94  dynamic_cast< VectorCacheUnit<T> const& > (source).retrieve( source_id, value );
95  store( new_id, value );
96  };
97 
98 
99  bool retrieve( core::Size frag_id, T& value ) const {
100  value = list_[ frag_id ];
101  return true;
102  }
103 
104  T const& retrieve( core::Size frag_id ) const {
105  return list_[ frag_id ];
106  }
107 
108  void store( Size frag_id, T const& value ) {
109  if ( frag_id > list_.size() ) {
110  list_.resize( frag_id );
111  };
112  list_[ frag_id ] = value;
113  }
114 
115  void register_frag_id( Size frag_id ) {
116  if ( frag_id > list_.size() ) {
117  list_.resize( frag_id );
118  };
119  };
120 private:
121  TVector list_;
122 };
123 
124 template< class T, class XCacheUnit >
126 public:
129  typedef XCacheUnit TCacheUnit;
132  typedef std::pair< FragID, T > ScoredFrag;
134 
135 public:
137  tag_ ( tag ),
138  new_cache_ ( new TCacheUnit )
139  {};
140 
142 
143  bool retrieve( Frame const& frame, core::Size frag_num, T& score) const {
144  return cache( frame ).retrieve( frame.frag_id( frag_num ), score );
145  };
146 
147  bool retrieve( FragID const& frag_id, T& score ) const {
148  return cache( frag_id.frame() ).retrieve( frag_id.id(), score );
149  }
150 
151  T retrieve( core::Size frag_id ) const {
152  T val;
153  if ( retrieve( frag_id, val ) ) {
154  return val;
155  } else {
156  throw utility::excn::EXCN_RangeError( "no "+tag_+ "entry found for fragment: ");
157  }
158  }
159 
160  T retrieve( FragID const& frag_id ) const {
161  T val;
162  if ( retrieve( frag_id, val ) ) {
163  return val;
164  } else {
165  throw utility::excn::EXCN_RangeError( "no "+tag_+ "entry found for fragment: ");
166  }
167  }
168 
169 T retrieve( Frame const& frame, core::Size frag_num ) const {
170  T val;
171  if ( retrieve( frame, frag_num, val ) ) {
172  return val;
173  } else {
174  throw utility::excn::EXCN_RangeError( "no "+tag_+ "entry found for fragment");
175  }
176  }
177 
178 
179  void store( Frame const& frame, core::Size frag_num, T const& score ) {
180  cache( frame ).store( frame.frag_id( frag_num ), score );
181  };
182 
183  void store( FragID const& frag_id, T const& score ) {
184  cache( frag_id.frame() ).store( frag_id.id() , score );
185  };
186 
187  void scored_frag_ids( ScoredList &frag_ids, FragID_Iterator begin, FragID_Iterator end, T* empty = NULL ) const {
188  for ( FragID_Iterator it = begin; it!=end; ++it ) {
189  T score;
190  if ( retrieve( *it, score ) )
191  frag_ids.push_back( ScoredFrag( *it, score ) );
192  else if ( empty ) {
193  frag_ids.push_back( ScoredFrag( *it, *empty ) );
194  }
195  };
196  }
197 
198  void scored_frag_ids( ScoredList &frag_ids, FragID_Iterator begin, FragID_Iterator end, T empty ) const {
199  scored_frag_ids( frag_ids, begin, end, &empty );
200  }
201 
202  TCacheUnit const& cache( Frame const& frame ) const {
203  // TCacheUnitAP ptr=utility::pointer::dynamic_pointer_cast< TCacheUnit >( frame.cache( tag_, new_cache_ ) );
204  // assert(ptr);
205  // better with refernce, throws exception automatic if it goes wrong
206  return dynamic_cast< TCacheUnit const& >( frame.cache( tag_, new_cache_ ) );
207  }
208 
209  TCacheUnit& cache( Frame const& frame ) {
210  // TCacheUnitAP ptr=utility::pointer::dynamic_pointer_cast< TCacheUnit >( frame.cache( tag_, new_cache_ ) );
211  // assert(ptr);
212  // better with refernce, throws exception automatic if it goes wrong
213  return dynamic_cast< TCacheUnit& >( frame.cache( tag_, new_cache_ ) );
214  }
215 
216 
217  TCacheUnit& operator() ( Frame const& frame ) {
218  return cache( frame );
219  // return utility::pointer::dynamic_pointer_cast< TCacheUnit >( frame.cache( tag_, new_cache_ ) );
220  }
221 
224 };
225 
226 //FragCache uses MapCacheUnit, .i.e, some values might be not set --> retrieve returns false
227 //FragStore uses VectorCacheUnit, i.e., all values should be valid --> the user has to take care that every fragment in the frame
228 
229 template < class T >
230 class FragCache : public CacheWrapper< T, MapCacheUnit< T> > {
232  // typedef Parent::TCacheUnit TCacheUnit;
233  typedef T ValueType;
234 public:
235  FragCache( std::string tag ) : Parent( tag ) {};
236 };
237 
238 template < class T >
239 class FragStore : public CacheWrapper< T, VectorCacheUnit< T> > {
241  // typedef Parent::TCacheUnit TCacheUnit;
242  typedef T ValueType;
243 public:
244  FragStore( std::string tag ) : Parent( tag ) {};
245 };
246 
247 
248 } //fragment
249 } //core
250 
251 #endif