我试图插入boost::dynamic_bitset<>
成boost::bimap
。 但是,它是相对于插入整数或字符串作为非常缓慢。 最小实施例中给出,其中代码如下所示
// Example program
#include <iostream>
#include <string>
#include <boost/bimap.hpp>
#include <boost/dynamic_bitset.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/unordered_multiset_of.hpp>
namespace std {
template <typename Block, typename Alloc> struct hash<boost::dynamic_bitset<Block, Alloc> > {
size_t operator()(boost::dynamic_bitset<Block, Alloc> const& bs) const {
size_t seed = boost::hash_value(bs.size());
std::vector<Block> blocks(bs.num_blocks());
boost::hash_range(seed, blocks.begin(), blocks.end());
return seed;
}
};
}
namespace bimaps = boost::bimaps;
typedef boost::dynamic_bitset<> Bitset;
typedef boost::bimap<
bimaps::unordered_set_of<Bitset, std::hash<Bitset>>,
bimaps::unordered_multiset_of<Bitset, std::hash<Bitset> > > bimap_reference;
typedef bimap_reference::value_type position;
bimap_reference reference_index_vector;
class bitsets{
public:
void encode_string(
std::string &sequence_content,
std::string &binary_sequence);
void encode_positions(
std::string &pos,
std::string &pos_binary_sequence);
};
int main()
{
bitsets b;
std::string binary_sequence, decoded_string ;
std::string pos_binary_sequence, decoded_positions;
std::string sequence_content = "ADGFGGFFAAACGFGCAAFGCAAFGCAFGCAAFGCAFGCAAGGGCAGDDDCGGAFFGCA";
for(size_t i = 0; i < sequence_content.size(); i++){
std::string substr = sequence_content.substr(i,15);
b.encode_string(substr, binary_sequence);
boost::dynamic_bitset<> bits = boost::dynamic_bitset<> (binary_sequence);
std::string pos = std::to_string(i);
b.encode_positions(pos, pos_binary_sequence);
boost::dynamic_bitset<> pos_bits = boost::dynamic_bitset<> (pos_binary_sequence);
reference_index_vector.insert(position(pos_bits, bits));
binary_sequence.clear();
pos_binary_sequence.clear();
i += 14;
}
for( bimap_reference::const_iterator iter = reference_index_vector.begin(), iend = reference_index_vector.end();
iter != iend; ++iter ) {
std::cout << iter->left << " <--> "<< iter->right <<std::endl;
}
}
void bitsets::encode_string(std::string &substr, std::string &binary_sequence){
for (size_t i = 0; i < substr.size(); ++i){
switch (substr[i]){
case 'A':
case 'a':
binary_sequence += "00";
break;
case 'C':
case 'c':
binary_sequence += "01";
break;
case 'D':
case 'd':
binary_sequence += "10";
break;
case 'G':
case 'g':
binary_sequence += "110";
break;
case 'F':
case 'f':
binary_sequence += "110";
break;
}
}
}
void bitsets::encode_positions(std::string &pos, std::string &pos_binary_sequence){
for(size_t i = 0; i < pos.size(); ++i){
switch (pos[i]){
case '0':
pos_binary_sequence += "1101";
break;
case '1':
pos_binary_sequence += "100";
break;
case '2':
pos_binary_sequence += "1110";
break;
case '3':
pos_binary_sequence += "1100";
break;
case '4':
pos_binary_sequence += "101";
break;
case '5':
pos_binary_sequence += "000";
break;
case '6':
pos_binary_sequence += "001";
break;
case '7':
pos_binary_sequence += "011";
break;
case '8':
pos_binary_sequence += "1111";
break;
case '9':
pos_binary_sequence += "010";
break;
}
}
}
对于500万个字符的字符串长时间需要花费大量的时间(约5分钟)为整数/字符串几秒这一翻译插入到bimap
。 为什么boost::dynamic_bitset<>
是缓慢的,我怎么能改善它。