Commit 1b447c36 authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz
Browse files

More interfaces to the int array compression routines.

* src/misc/intvcomp.cc, src/misc/intvcomp.cc: Add interfaces to
compress vector<int> to vector<unsigned>.
* src/tgbatest/intvcomp.cc: Test them.
* src/sanity/style.test: Refine check to avoid a spurious report.
parent 3aa9c3ba
2011-04-12 Alexandre Duret-Lutz <adl@lrde.epita.fr>
More interfaces to the int array compression routines.
* src/misc/intvcomp.cc, src/misc/intvcomp.cc: Add interfaces to
compress vector<int> to vector<unsigned>.
* src/tgbatest/intvcomp.cc: Test them.
* src/sanity/style.test: Refine check to avoid a spurious report.
2011-04-11 Alexandre Duret-Lutz <adl@lrde.epita.fr> 2011-04-11 Alexandre Duret-Lutz <adl@lrde.epita.fr>
* iface/dve2/dve2.cc: Typo when handling dead==true. * iface/dve2/dve2.cc: Typo when handling dead==true.
......
...@@ -232,6 +232,50 @@ namespace spot ...@@ -232,6 +232,50 @@ namespace spot
std::vector<unsigned int>* result_; std::vector<unsigned int>* result_;
}; };
class int_vector_vector_compression:
public stream_compression_base<int_vector_vector_compression>
{
public:
int_vector_vector_compression(const std::vector<int>& input,
std::vector<unsigned int>& output)
: input_(input), pos_(input.begin()), end_(input.end()), output_(output)
{
}
void push_data(unsigned int i)
{
output_.push_back(i);
}
bool have_data() const
{
return pos_ < end_;
}
unsigned int next_data()
{
return static_cast<unsigned int>(*pos_++);
}
bool skip_if(unsigned int val)
{
if (!have_data())
return false;
if (static_cast<unsigned int>(*pos_) != val)
return false;
++pos_;
return true;
}
protected:
const std::vector<int>& input_;
std::vector<int>::const_iterator pos_;
std::vector<int>::const_iterator end_;
std::vector<unsigned int>& output_;
};
class int_array_array_compression: class int_array_array_compression:
public stream_compression_base<int_array_array_compression> public stream_compression_base<int_array_array_compression>
{ {
...@@ -283,6 +327,14 @@ namespace spot ...@@ -283,6 +327,14 @@ namespace spot
}; };
} }
void
int_vector_vector_compress(const std::vector<int>& input,
std::vector<unsigned>& output)
{
int_vector_vector_compression c(input, output);
c.run();
}
const std::vector<unsigned int>* const std::vector<unsigned int>*
int_array_vector_compress(const int* array, size_t n) int_array_vector_compress(const int* array, size_t n)
{ {
...@@ -456,6 +508,57 @@ namespace spot ...@@ -456,6 +508,57 @@ namespace spot
unsigned int buffer_mask_; unsigned int buffer_mask_;
}; };
class int_vector_vector_decompression:
public stream_decompression_base<int_vector_vector_decompression>
{
public:
int_vector_vector_decompression(const std::vector<unsigned int>& array,
std::vector<int>& res, size_t size)
: prev_(0), array_(array),
pos_(array.begin()), end_(array.end()),
result_(res), size_(size)
{
result_.reserve(size);
}
bool complete() const
{
return size_ == 0;
}
void push_data(int i)
{
prev_ = i;
result_.push_back(i);
--size_;
}
void repeat(unsigned int i)
{
size_ -= i;
while (i--)
result_.push_back(prev_);
}
bool have_comp_data() const
{
return pos_ != end_;
}
unsigned int next_comp_data()
{
return *pos_++;
}
protected:
int prev_;
const std::vector<unsigned int>& array_;
std::vector<unsigned int>::const_iterator pos_;
std::vector<unsigned int>::const_iterator end_;
std::vector<int>& result_;
size_t size_;
};
class int_vector_array_decompression: class int_vector_array_decompression:
public stream_decompression_base<int_vector_array_decompression> public stream_decompression_base<int_vector_array_decompression>
{ {
...@@ -559,6 +662,14 @@ namespace spot ...@@ -559,6 +662,14 @@ namespace spot
} }
void
int_vector_vector_decompress(const std::vector<unsigned int>& input,
std::vector<int>& output, size_t size)
{
int_vector_vector_decompression c(input, output, size);
c.run();
}
void void
int_vector_array_decompress(const std::vector<unsigned int>* array, int* res, int_vector_array_decompress(const std::vector<unsigned int>* array, int* res,
size_t size) size_t size)
......
...@@ -28,11 +28,24 @@ namespace spot ...@@ -28,11 +28,24 @@ namespace spot
/// \addtogroup misc_tools /// \addtogroup misc_tools
/// @{ /// @{
/// Compress an int vector into a vector of unsigned int.
void
int_vector_vector_compress(const std::vector<int>& input,
std::vector<unsigned int>& output);
/// \brief Uncompress a vector of unsigned int into a vector of
/// size \a size.
///
/// \a size must be the exact expected size of uncompressed array.
void
int_vector_vector_decompress(const std::vector<unsigned int>& array,
std::vector<int>& output, size_t size);
/// Compress an int array if size \a n into a vector of unsigned int. /// Compress an int array if size \a n into a vector of unsigned int.
const std::vector<unsigned int>* const std::vector<unsigned int>*
int_array_vector_compress(const int* array, size_t n); int_array_vector_compress(const int* array, size_t n);
/// \brief Uncompress a vector of unsigned int into an int array if /// \brief Uncompress a vector of unsigned int into an int array of
/// size \a size. /// size \a size.
/// ///
/// \a size must be the exact expected size of uncompressed array. /// \a size must be the exact expected size of uncompressed array.
......
...@@ -165,7 +165,7 @@ for dir in "${INCDIR-..}" "${INCDIR-..}"/../iface; do ...@@ -165,7 +165,7 @@ for dir in "${INCDIR-..}" "${INCDIR-..}"/../iface; do
egrep '(->|[.])size\(\) [=!]= 0|![a-zA-Z0-9_]*(->|[.])size\(\)|(if |while |assert)\([a-zA-Z0-9_]*(->|[.])size\(\)\)' $tmp && egrep '(->|[.])size\(\) [=!]= 0|![a-zA-Z0-9_]*(->|[.])size\(\)|(if |while |assert)\([a-zA-Z0-9_]*(->|[.])size\(\)\)' $tmp &&
diag 'Prefer empty() to check emptiness.' diag 'Prefer empty() to check emptiness.'
egrep '^[^=]*([+][+]|--);' $tmp && egrep '^[^=*]*([+][+]|--);' $tmp &&
diag 'Take good habits: use ++i instead of i++ when you have the choice.' diag 'Take good habits: use ++i instead of i++ when you have the choice.'
grep '[^a-zA-Z0-9_](\*[a-zA-Z0-9_]*)\.' $tmp && grep '[^a-zA-Z0-9_](\*[a-zA-Z0-9_]*)\.' $tmp &&
......
...@@ -22,6 +22,54 @@ ...@@ -22,6 +22,54 @@
#include "misc/intvcomp.hh" #include "misc/intvcomp.hh"
#include <cstring> #include <cstring>
int check_vv(int* data, int size, unsigned expected = 0)
{
std::vector<int> input;
input.reserve(size);
for (int i = 0; i < size; ++i)
input.push_back(data[i]);
std::vector<unsigned int> output;
spot::int_vector_vector_compress(input, output);
std::cout << "WC[" << output.size() << "] ";
for (size_t i = 0; i < output.size(); ++i)
std::cout << output[i] << " ";
std::cout << std::endl;
std::vector<int> decomp;
spot::int_vector_vector_decompress(output, decomp, size);
std::cout << "WD[" << decomp.size() << "] ";
for (size_t i = 0; i < decomp.size(); ++i)
std::cout << decomp[i] << " ";
std::cout << std::endl;
int res = (decomp != input);
if (res)
{
std::cout << "*** cmp error *** " << std::endl;
std::cout << "WE[" << size << "] ";
for (int i = 0; i < size; ++i)
std::cout << data[i] << " ";
std::cout << std::endl;
}
if (expected && (output.size() * sizeof(int) != expected))
{
std::cout << "*** size error *** (expected "
<< expected << " bytes, got " << output.size() * sizeof(int)
<< " bytes)" << std::endl;
res = 1;
}
std::cout << std::endl;
return !!res;
}
int check_av(int* data, int size, unsigned expected = 0) int check_av(int* data, int size, unsigned expected = 0)
{ {
const std::vector<unsigned int>* v = const std::vector<unsigned int>* v =
...@@ -113,7 +161,10 @@ int check_aa(int* data, int size, unsigned expected = 0) ...@@ -113,7 +161,10 @@ int check_aa(int* data, int size, unsigned expected = 0)
int check(int* comp, int size, unsigned expected = 0) int check(int* comp, int size, unsigned expected = 0)
{ {
return check_av(comp, size, expected) + check_aa(comp, size, expected); return
check_vv(comp, size, expected) +
check_av(comp, size, expected) +
check_aa(comp, size, expected);
} }
int main() int main()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment