Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
integer_mapping.cc
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 utility/integer_mapping.cc
11 /// @brief A set of useful classes to map between two enumerations. So far, only a subset mapping is implemented.
12 /// @author Andrew Leaver-Fay (aleaverfay@gmail.com)
13 
14 // Unit headers
16 
17 // Package headers
18 #include <utility/string_util.hh>
20 
21 // Platform headers
22 #include <platform/types.hh>
23 
24 namespace utility {
25 
27 
29 subset_mapping::subset_mapping( platform::Size source_enumeration_size ) : src_2_dst_( source_enumeration_size, UNMAPPED ) {}
30 subset_mapping::subset_mapping( subset_mapping const & src ) : ReferenceCount(), src_2_dst_( src.src_2_dst_ ), dst_2_src_( src.dst_2_src_ ) {}
32 {
33  if ( this != &rhs ) {
34  src_2_dst_ = rhs.src_2_dst_;
35  dst_2_src_ = rhs.dst_2_src_;
36  }
37  return *this;
38 }
39 
41 
43 {
44  src_2_dst_.resize( src_size );
45  std::fill( src_2_dst_.begin(), src_2_dst_.end(), UNMAPPED );
46 }
47 
49 {
50  dst_2_src_.reserve( dst_size );
51 }
52 
53 /// @details Prevent out-of-bounds assignment and the overwriting of previously
54 /// mapped ids. Throws excn::EXCN_Msg_Exceptions
56 {
57  if ( source_id > src_2_dst_.size() || source_id == 0 ) {
58  throw excn::EXCN_Msg_Exception( "subset_mapping::set_next_correspondence "
59  "recieved an out-of-bounds source id (" + to_string( source_id ) + ") "
60  "with a source-enumeration size of " + to_string( src_2_dst_.size() ) );
61  }
62  if ( src_2_dst_[ source_id ] != UNMAPPED ) {
63  throw excn::EXCN_Msg_Exception( "subset_mapping::set_next_correspondence "
64  "recieved an already-mapped source id (" + to_string( source_id ) + ") "
65  "which had been previously assigned to destination id " + to_string( src_2_dst_[ source_id ] ) );
66  }
67  dst_2_src_.push_back( source_id );
68  src_2_dst_[ source_id ] = dst_2_src_.size();
69 }
70 
72 
74 
75 platform::Size subset_mapping::s2d( platform::Size source_id ) const { return src_2_dst_[ source_id ]; }
76 
77 platform::Size subset_mapping::d2s( platform::Size destination_id ) const { return dst_2_src_[ destination_id ]; }
78 
79 bool subset_mapping::source_id_is_mapped( platform::Size source_id ) const { return src_2_dst_[ source_id ] != UNMAPPED; }
80 
81 }
82 
void set_source_size(platform::Size)
Required before the first call to set_next_correspondence may be called. The size of the source enume...
platform::Size s2d(platform::Size source_id) const
Map from the id of an element in source enumeration to an id in the the destination enumeration...
subset_mapping & operator=(subset_mapping const &rhs)
platform::Size d2s(platform::Size destination_id) const
Map from the id of an element in the destination enumeration to an id in the source enumeration...
void reserve_destination_size(platform::Size)
If you know the size of the destination enumeration, then you can save some under-the-hood vector res...
bool source_id_is_mapped(platform::Size source_id) const
platform::Size source_size() const
The number of elements in the source enumeration.
void set_next_correspondence(platform::Size source_id)
Inform the mapping of the next source-enumeration id that should be mapped to a destination-enumerati...
static platform::Size const UNMAPPED
common derived classes for thrown exceptions
utility::vector1< platform::Size > dst_2_src_
platform::Size destination_size() const
The number of elements in the destination enumeration – this represents the number of calls that hav...
utility::vector1< platform::Size > src_2_dst_
A set of useful classes to map between two enumerations. So far, only a subset mapping is implemented...
std::string to_string(const T &t)
Definition: string_util.hh:205
Some std::string helper functions.
std::size_t Size
Definition: types.hh:37
This class handles the bookeeping to map between a set of integer ids in the "source" enumeration to ...