shushus

Спринт 15. Плотная упаковка.

May 31st, 2023
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.64 KB | None | 0 0
  1. -----------------------------------------------------------------------
  2. main.cpp
  3. -----------------------------------------------------------------------
  4. #include <cassert>
  5. #include <cstddef>
  6. #include <cstdint>
  7. #include <string>
  8. #include <array>
  9.  
  10. using namespace std;
  11.  
  12. struct Nucleotide {
  13.     char symbol;
  14.     size_t position;
  15.     int chromosome_num;
  16.     int gene_num;
  17.     bool is_marked;
  18.     char service_info;
  19. };
  20.  
  21. const static int N = 4;
  22. const std::array<char, N> Symbols = {'A','T','G','C'};
  23.  
  24. struct CompactNucleotide {
  25.     uint32_t position;
  26.     uint16_t gene_num:15;
  27.     uint8_t is_marked:1;
  28.     uint8_t chromosome_num:6;
  29.     uint8_t symbol:2;
  30.     char service_info;
  31. };
  32.  
  33. CompactNucleotide Compress(const Nucleotide& n) {
  34.    
  35.     CompactNucleotide cn;
  36.    
  37.     cn.position = uint32_t(n.position);
  38.     cn.gene_num = uint16_t(n.gene_num);                          
  39.     cn.is_marked = uint8_t(n.is_marked);
  40.     cn.chromosome_num = uint8_t(n.chromosome_num);
  41.                            
  42.     for (uint8_t i = 0; i < N; ++i) {
  43.        
  44.         if (Symbols[i] == n.symbol) {
  45.             cn.symbol = uint8_t(i);
  46.         }
  47.     }
  48.    
  49.     cn.service_info = n.service_info;
  50.    
  51.     return cn;
  52. }
  53.  
  54. Nucleotide Decompress(const CompactNucleotide& cn) {
  55.    
  56.     Nucleotide n;
  57.    
  58.     n.symbol = Symbols[cn.symbol];
  59.     n.position = size_t(cn.position);
  60.     n.chromosome_num = int(cn.chromosome_num);
  61.     n.gene_num = int(cn.gene_num);
  62.     n.is_marked = bool(cn.is_marked);
  63.     n.service_info = cn.service_info;
  64.    
  65.     return n;
  66. }
  67.  
  68. static_assert(sizeof(CompactNucleotide) <= 8, "Your CompactNucleotide is not compact enough"s);
  69. static_assert(alignof(CompactNucleotide) == 4, "Don't use '#pragma pack'!"s);
  70.  
  71. bool operator==(const Nucleotide& lhs, const Nucleotide& rhs) {
  72.     return (lhs.symbol == rhs.symbol)
  73.             && (lhs.position == rhs.position)
  74.             && (lhs.chromosome_num == rhs.chromosome_num)
  75.             && (lhs.gene_num == rhs.gene_num)
  76.             && (lhs.is_marked == rhs.is_marked)
  77.             && (lhs.service_info == rhs.service_info);
  78. }
  79.  
  80. void TestSize() {
  81.     assert(sizeof(CompactNucleotide) <= 8);
  82. }
  83.  
  84. void TestCompressDecompress() {
  85.     Nucleotide source;
  86.     source.symbol = 'T';
  87.     source.position = 1'000'000'000;
  88.    source.chromosome_num = 48;
  89.    source.gene_num = 1'000;
  90.     source.is_marked = true;
  91.     source.service_info = '!';
  92.  
  93.     CompactNucleotide compressedSource = Compress(source);
  94.     Nucleotide decompressedSource = Decompress(compressedSource);
  95.  
  96.     assert(source == decompressedSource);
  97. }
  98.  
  99. int main() {
  100.     TestSize();
  101.     TestCompressDecompress();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment