Debug swcsa
[SXSI/TextCollection.git] / incbwt / lcpsamples.cpp
1 #include <algorithm>
2 #include <fstream>
3 #include <iostream>
4
5 #include "lcpsamples.h"
6 #include "misc/utils.h"
7
8
9 namespace CSA
10 {
11
12
13 LCPSamples::LCPSamples(std::ifstream& sample_file)
14 {
15   this->indexes = new DeltaVector(sample_file);
16   this->values = new Array(sample_file);
17
18   this->size = indexes->getSize();
19   this->items = indexes->getNumberOfItems();
20 }
21
22 LCPSamples::LCPSamples(pair_type* input, usint _size, usint _items, bool report, bool free_input) :
23   size(_size),
24   items(_items)
25 {
26   DeltaEncoder index_encoder(LCPSamples::INDEX_BLOCK_SIZE);
27   ArrayEncoder value_encoder(LCPSamples::VALUE_BLOCK_SIZE);
28
29   usint max_sample = 0;
30   for(usint i = 0; i < this->items; i++)
31   {
32     index_encoder.setBit(input[i].first);
33     value_encoder.addItem(input[i].second + 1);
34     max_sample = std::max(max_sample, input[i].second);
35   }
36
37   this->indexes = new DeltaVector(index_encoder, this->size);
38   this->values = new Array(value_encoder);
39
40   if(free_input) { delete[] input; }
41
42   if(report)
43   {
44     usint max_bits = length(max_sample);
45     double total_size = max_bits * (double)(this->items) / (CHAR_BIT * (double)MEGABYTE);
46     std::cout << "Maximum sample value: " << max_sample << " (" << max_bits << " bits)" << std::endl;
47     std::cout << "Raw samples: " << total_size << " MB" << std::endl;
48     std::cout << "Encoded samples: " << (this->values->reportSize() / (double)MEGABYTE) << " MB" << std::endl;
49     std::cout << std::endl;
50   }
51 }
52
53 LCPSamples::LCPSamples(DeltaEncoder& index_encoder, ArrayEncoder& value_encoder, usint _size) :
54   size(_size)
55 {
56   this->indexes = new DeltaVector(index_encoder, this->size);
57   this->values = new Array(value_encoder);
58   this->items = this->indexes->getNumberOfItems();
59 }
60
61 LCPSamples::~LCPSamples()
62 {
63   delete this->indexes;
64   delete this->values;
65 }
66
67 void
68 LCPSamples::writeTo(std::ofstream& sample_file) const
69 {
70   this->indexes->writeTo(sample_file);
71   this->values->writeTo(sample_file);
72 }
73
74 //--------------------------------------------------------------------------
75
76 usint
77 LCPSamples::reportSize() const
78 {
79   usint bytes = sizeof(*this);
80   bytes += this->indexes->reportSize();
81   bytes += this->values->reportSize();
82   return bytes;
83 }
84
85 //--------------------------------------------------------------------------
86
87
88 } // namespace CSA