Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AtomID_Map.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 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file core/id/AtomID_Map.hh
11 /// @brief Map from Atom identifiers to contained values class
12 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
13 ///
14 /// @note
15 /// @li Implemented as a vector< vector > for fast lookup but this has slower
16 /// insertion/deletion than a std::map or other associative containers
17 /// @li The outer vector is indexed by the residue number
18 /// @li The inner vector is indexed by the atom number within the residue
19 /// @li The map can be sized by first calling resize( n_res ) and then calling
20 /// resize( i_res, n_atom ) for each residue to set the number of atoms
21 /// @li When the Value type (T) is bool note that the vector< bool > specialization is
22 /// used so the values returned by the indexing lookups are not actually references
23 
24 
25 #ifndef INCLUDED_core_id_AtomID_Map_hh
26 #define INCLUDED_core_id_AtomID_Map_hh
27 
28 
29 // Unit headers
31 
32 // Package headers
33 #include <core/id/AtomID.hh>
34 
35 // Utility headers
36 // AUTO-REMOVED #include <utility/vector1.hh>
37 
38 #include <utility/vector1_bool.hh>
39 
40 
41 
42 namespace core {
43 namespace id {
44 
45 
46 /// @brief Map from Atom identifiers to contained values class
47 template< typename T >
48 class AtomID_Map
49 {
50 
51 
52 public: // Types
53 
54 
57 
58  // STL/boost style
59  typedef T value_type;
60  typedef typename AtomMap::reference reference;
61  typedef typename AtomMap::const_reference const_reference;
62  typedef typename AtomMap::size_type size_type;
63 
64  // Project style
65  typedef T Value;
66  typedef typename AtomMap::Reference Reference;
67  typedef typename AtomMap::ConstReference ConstReference;
69 
70 
71 public: // Creation
72 
73  /// @brief Default constructor with no arguments (PyRosetta workaround)
74  inline
75  explicit
78  {}
79 
80 
81  /// @brief Default constructor
82  inline
83  explicit
84  AtomID_Map( Value const & default_value_a ) : // AtomID_Map( Value const & default_value_a = Value() )
85  default_value_( default_value_a )
86  {}
87 
88 
89  /// @brief Number of residues constructor
90  inline
91  explicit
93  Size const n_res
94  ) :
95  default_value_( Value() ),
96  res_map_( n_res )
97  {}
98 
99  /// @brief Number of residues constructor
100  inline
101  explicit
103  Size const n_res,
104  Value const & default_value_a
105  ) :
106  default_value_( default_value_a ),
107  res_map_( n_res )
108  {}
109 
110 
111  /// @brief Destructor
112  inline
114  {}
115 
116 
117 public: // Methods
118 
119 
120  /// @brief Resize to a given number of residues
121  inline
122  void
123  resize( Size const n_res )
124  {
125  res_map_.resize( n_res );
126  }
127 
128 
129  /// @brief Resize the number of atoms of a residue and use the default fill value
130  inline
131  void
132  resize( Size const i_res, Size const n_atom )
133  {
134  res_map_[ i_res ].resize( n_atom, default_value_ );
135  }
136 
137 
138  /// @brief Resize the number of atoms of a residue and use a specified fill value
139  inline
140  void
141  resize( Size const i_res, Size const n_atom, Value const & value )
142  {
143  res_map_[ i_res ].resize( n_atom, value );
144  }
145 
146 
147  /// @brief Fill the map with the default fill value
148  inline
149  void
151  {
152  for ( Size i = 1, ie = res_map_.size(); i <= ie; ++i ) {
153  AtomMap & atom_map( res_map_[ i ] );
154  for ( Size j = 1, je = atom_map.size(); j <= je; ++j ) { // std::fill_n could do this too
155  atom_map[ j ] = default_value_;
156  }
157  }
158  }
159 
160 
161  /// @brief Fill the map with a specified fill value
162  inline
163  void
164  fill_with( Value const & value )
165  {
166  for ( Size i = 1, ie = res_map_.size(); i <= ie; ++i ) {
167  AtomMap & atom_map( res_map_[ i ] );
168  for ( Size j = 1, je = atom_map.size(); j <= je; ++j ) { // std::fill_n could do this too
169  atom_map[ j ] = value;
170  }
171  }
172  }
173 
174 
175  /// @brief Fill the map at position seqpos with a specified fill value
176  inline
177  void
178  fill_with( Size const seqpos, Value const & value )
179  {
180  AtomMap & atom_map( res_map_[ seqpos ] );
181  for ( Size j = 1, je = atom_map.size(); j <= je; ++j ) { // std::fill_n could do this too
182  atom_map[ j ] = value;
183  }
184  }
185 
186 
187  /// @brief Get the value for an AtomID: Extend the map if necessary, filling with the default value
188  /// Phil changing this to be a non-resizing function
189  inline
191  get( AtomID const & id ) const
192  {
193  if ( Size( id.rsd() ) > res_map_.size() ) return default_value_;
194  //res_map_.resize( id.rsd() );
195  AtomMap const & atom_map( res_map_[ id.rsd() ] );
196  if ( Size( id.atomno() ) > atom_map.size() ) return default_value_;
197  //atom_map.resize( id.atomno(), default_value_ );
198  return atom_map[ id.atomno() ];
199  }
200 
201 
202  /// @brief Set the value for an AtomID: Extend the map if necessary, filling with the default value
203  inline
204  void
205  set( AtomID const & id, Value const & value )
206  {
207  if ( Size( id.rsd() ) > res_map_.size() ) res_map_.resize( id.rsd() );
208  AtomMap & atom_map( res_map_[ id.rsd() ] );
209  if ( Size( id.atomno() ) > atom_map.size() ) atom_map.resize( id.atomno(), default_value_ );
210  atom_map[ id.atomno() ] = value;
211  }
212 
213 
214  /// @brief Finalize after sizing all the vectors
215  inline
216  void
218  {
219  shrink();
220  }
221 
222 
223  /// @brief Shrink the vectors to remove unused capacity
224  inline
225  void
227  {
228  for ( Size i = 1, e = res_map_.size(); i <= e; ++i ) {
229  res_map_[ i ].shrink();
230  }
231  res_map_.shrink();
232  }
233 
234 
235  /// @brief swap( AtomID_Map )
236  inline
237  void
239  {
240  res_map_.swap( s.res_map_ );
241  }
242 
243 
244 
245  /// @brief swap( AtomID_Map, AtomID_Map )
246  template< typename TF >
247  friend
248  void
250 
251 
252 
253  /// @brief Clear the map
254  inline
255  void
257  {
258  // default value isn't changed
259  res_map_.clear();
260  }
261 
262 
263  /// @brief Clear the map and set a new default value
264  inline
265  void
266  clear( Value const & default_value_a )
267  {
268  default_value_ = default_value_a;
269  res_map_.clear();
270  }
271 
272 
273  /// Should move to .cc?
274  /// if old2new[pos] == 0 , that position's mapping is lost
275  /// if old2new[1...old_size] doesnt cover all of [1...new_size], the missed positions will have res_map_[pos].empty()
276  inline
277  void
278  update_sequence_numbering( Size const new_size, utility::vector1< Size > const & old2new )
279  {
280  // swap is very slick
281  AtomID_Map replacement( new_size, default_value_ );
282  for ( Size i=1, i_end = size(); i<= i_end; ++i ) {
283  Size const new_pos( old2new[ i ] );
284  if ( new_pos ) {
285  replacement[ new_pos ].swap( res_map_[ i ] );
286  }
287  }
288  swap( replacement );
289  }
290 
291 public: // Properties
292 
293 
294  /// @brief Size
295  inline
296  Size
297  size() const
298  {
299  return res_map_.size();
300  }
301 
302 
303  /// @brief Number of residues (size)
304  inline
305  Size
306  n_residue() const
307  {
308  return res_map_.size();
309  }
310 
311 
312  /// @brief Number of atoms in a residue
313  inline
314  Size
315  n_atom( Size const i_res ) const
316  {
317  return res_map_[ i_res ].size();
318  }
319 
320 
321  /// @brief Empty?
322  inline
323  bool
324  empty() const
325  {
326  return res_map_.empty();
327  }
328 
329 
330  /// @brief Default value
331  inline
332  Value const &
334  {
335  return default_value_;
336  }
337 
338 
339  /// @brief Set default value
340  inline
341  void
342  default_value( Value const & default_value_a )
343  {
344  default_value_ = default_value_a;
345  }
346 
347 
348  /// @brief Is an element with this AtomID present?
349  inline
350  bool
351  has( AtomID const & id ) const
352  {
353  return ( ( id.rsd() >= 1 ) && ( id.atomno() >= 1 ) &&
354  ( Size( id.rsd() ) <= res_map_.size() ) && ( Size( id.atomno() ) <= res_map_[ id.rsd() ].size() ) );
355  }
356 
357 
358 public: // Indexers
359 
360 
361  // note These do not resize the map
362 
363 
364  /// @brief AtomID_Map[ atom_id ] const
365  inline
367  operator []( AtomID const & id ) const
368  {
369  return res_map_[ id.rsd() ][ id.atomno() ];
370  }
371 
372 
373  /// @brief AtomID_Map[ atom_id ]
374  inline
375  Reference
376  operator []( AtomID const & id )
377  {
378  return res_map_[ id.rsd() ][ id.atomno() ];
379  }
380 
381 
382  /// @brief AtomID_Map( atom_id ) const
383  inline
385  operator ()( AtomID const & id ) const
386  {
387  return res_map_[ id.rsd() ][ id.atomno() ];
388  }
389 
390 
391  /// @brief AtomID_Map( atom_id )
392  inline
393  Reference
394  operator ()( AtomID const & id )
395  {
396  return res_map_[ id.rsd() ][ id.atomno() ];
397  }
398 
399 
400  /// @brief AtomID_Map( i_res, i_atom ) const
401  inline
403  operator ()( Size const i_res, Size const i_atom ) const
404  {
405  return res_map_[ i_res ][ i_atom ];
406  }
407 
408 
409  /// @brief AtomID_Map( i_res, i_atom )
410  inline
411  Reference
412  operator ()( Size const i_res, Size const i_atom )
413  {
414  return res_map_[ i_res ][ i_atom ];
415  }
416 
417 
418  /// @brief AtomID_Map[ i_res ] const
419  inline
420  AtomMap const &
421  operator []( Size const i_res ) const
422  {
423  return res_map_[ i_res ];
424  }
425 
426 
427  /// @brief AtomID_Map[ i_res ]
428  inline
429  AtomMap &
430  operator []( Size const i_res )
431  {
432  return res_map_[ i_res ];
433  }
434 
435 
436  /// @brief AtomID_Map( i_res ) const
437  inline
438  AtomMap const &
439  operator ()( Size const i_res ) const
440  {
441  return res_map_[ i_res ];
442  }
443 
444 
445  /// @brief AtomID_Map( i_res )
446  inline
447  AtomMap &
448  operator ()( Size const i_res )
449  {
450  return res_map_[ i_res ];
451  }
452 
453 
454 public: // Comparison
455 
456 
457  /// @brief AtomID_Map == AtomID_Map
458  friend
459  inline
460  bool
461  operator ==( AtomID_Map const & a, AtomID_Map const & b )
462  {
463  return ( a.res_map_ == b.res_map_ );
464  }
465 
466 
467  /// @brief AtomID_Map != AtomID_Map
468  friend
469  inline
470  bool
471  operator !=( AtomID_Map const & a, AtomID_Map const & b )
472  {
473  return ( a.res_map_ != b.res_map_ );
474  }
475 
476 
477 private: // Fields
478 
479 #ifdef USEBOOSTSERIALIZE
480  friend class boost::serialization::access;
481 
482  template<class Archive>
483  void serialize(Archive & ar, const unsigned int version) {
484  ar & default_value_;
485  ar & res_map_;
486  }
487 #endif
488 
489  /// @brief Default value
491 
492  /// @brief Map from Atom identifiers to values
494 
495 
496 }; // AtomID_Map
497 
498 
499 /// @brief swap( AtomID_Map, AtomID_Map )
500 template< typename T >
501 inline
502 void
504 {
505  a.res_map_.swap( b.res_map_ );
506 }
507 
508 
509 } // namespace id
510 } // namespace core
511 
512 
513 #endif // INCLUDED_core_id_AtomID_Map_HH