1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
struct code_serial : std::tuple< std::string, unsigned int, double > // code, serial, weight
{
using base = std::tuple< std::string, unsigned int, double > ;
code_serial() = default ;
code_serial( std::string cd, unsigned int ser, double w ) : base( std::move(cd), ser, w ) {}
const std::string& code() const { return std::get<std::string>(*this) ; } // C++11: std::get<0>(*this)
std::string& code() { return std::get<std::string>(*this) ; }
unsigned int serial() const { return std::get<unsigned int>(*this) ; } // C++11: std::get<1>(*this)
unsigned int& serial() { return std::get<unsigned int>(*this) ; }
double weight() const { return std::get<double>(*this) ; } // C++11: std::get<2>(*this)
double& weight() { return std::get<double>(*this) ; }
};
using lookup_table= std::map< unsigned int, std::vector<code_serial> >;
void add_entry( lookup_table& table, unsigned int refer, std::string code, unsigned int serial, double weight )
{
table[refer].emplace_back( std::move(code), serial, weight );
}
std::vector<code_serial> look_up( const lookup_table& table, unsigned int refer )
{
auto iter = table.find(refer) ;
return iter != table.end() ? iter->second : std::vector<code_serial>{} ;
}
void print_all_entries( const lookup_table& table, std::ostream& stm = std::cout )
{
for( const auto& entry : table )
{
stm << "ref: " << entry.first << " code_serial: [ " ;
for( const auto& cs : entry.second ) stm << '{' << cs.code() << ',' << cs.serial() << ',' << cs.weight() << "} " ;
stm << "]\n" ;
}
}
void sort_all_entries_on_code( lookup_table& table )
{ for( auto& entry : table ) std::sort( entry.second.begin(), entry.second.end() ) ; }
void sort_all_entries_on_serial( lookup_table& table )
{
for( auto& entry : table )
{
// http://www.stroustrup.com/C++11FAQ.html#lambda
std::sort( entry.second.begin(), entry.second.end(),
[]( const code_serial& a, const code_serial& b ) { return a.serial() < b.serial() ; } ) ;
}
}
|