Merge remote repo native into ext/native/.

This commit is contained in:
Unknown W. Brackets committed 2015-09-06 12:24:17 -07:00
commit f279c2a3c2
378 files changed
+190761

No files matched your search

+105
View File
@@ -0,0 +1,105 @@
// Little utility functions for data compression.
// Taken from http://panthema.net/2007/0328-ZLibString.html
#include <string>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <zlib.h>
#include "base/logging.h"
/** Compress a STL string using zlib with given compression level and return
* the binary data. */
bool compress_string(const std::string& str, std::string *dest, int compressionlevel) {
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (deflateInit(&zs, compressionlevel) != Z_OK) {
ELOG("deflateInit failed while compressing.");
return false;
}
zs.next_in = (Bytef*)str.data();
zs.avail_in = (uInt)str.size(); // set the z_stream's input
int ret;
char outbuffer[32768];
std::string outstring;
// retrieve the compressed bytes blockwise
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out) {
// append the block to the output string
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
deflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib compression: (" << ret << ") " << zs.msg;
return false;
}
*dest = outstring;
return true;
}
/** Decompress an STL string using zlib and return the original data. */
bool decompress_string(const std::string& str, std::string *dest) {
if (!str.size())
return false;
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
// modification by hrydgard: inflateInit2, 16+MAXWBITS makes it read gzip data too
if (inflateInit2(&zs, 32+MAX_WBITS) != Z_OK) {
ELOG("inflateInit failed while decompressing.");
return false;
}
zs.next_in = (Bytef*)str.data();
zs.avail_in = (uInt)str.size();
int ret;
char outbuffer[32768];
std::string outstring;
// get the decompressed bytes blockwise using repeated calls to inflate
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
ELOG("Exception during zlib decompression: (%i) %s", ret, zs.msg);
return false;
}
*dest = outstring;
return true;
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <string>
bool compress_string(const std::string& str, std::string *dest, int compressionlevel = 9);
bool decompress_string(const std::string& str, std::string *dest);
// Delta encoding/decoding - many formats benefit from a pass of this before zlibbing.
// WARNING : Do not use these with floating point data, especially not float16...
template <class T>
inline void delta(T *data, int length) {
T prev = data[0];
for (int i = 1; i < length; i++) {
T temp = data[i] - prev;
prev = data[i];
data[i] = temp;
}
}
template <class T>
inline void dedelta(T *data, int length) {
for (int i = 1; i < length; i++) {
data[i] += data[i - 1];
}
}
+49
View File
@@ -0,0 +1,49 @@
#pragma once
#include <vector>
#include <string>
class Listable
{
public:
virtual ~Listable() {}
virtual const char *getItem(size_t i) const = 0;
virtual size_t numItems() const = 0;
// Returns -1 for not found.
// Child classes are meant to specialize this if they have a faster way
// than brute force search.
virtual int getIndex(const char *text) {
for (size_t i = 0; i < numItems(); i++) {
if (!strcmp(getItem(i), text))
return i;
}
return -1;
}
};
class ArrayListable : public Listable
{
public:
ArrayListable(const char * const*arr, size_t count) : arr_(arr), count_(count) {}
virtual ~ArrayListable() {}
virtual const char *getItem(size_t i) const { return arr_[i]; }
virtual size_t numItems() const { return count_; }
private:
const char *const*arr_;
size_t count_;
};
class VectorListable : public Listable
{
VectorListable(const std::vector<std::string> &vec) : vec_(vec) {}
virtual ~VectorListable() {}
virtual const char *getItem(size_t i) const { return vec_[i].c_str(); }
virtual size_t numItems() const { return vec_.size(); }
private:
const std::vector<std::string> &vec_;
};