#define S16_NUMSIZE        16
#define S16_BITSSIZE 28
#define Simple16_Mask 0x7FFFFFFF extern int S16_NUM[];
extern int S16_NUMOFF[];
extern int S16_BITS[16][28]; class Simple16
{
public:
static int* Compress(int * pnIn, int nLen, int * pnLenOut); static void Decompress(int * pnIn, int nLen, sgi::vector<unsigned int>& outVec); static int* Decompress(int * pnIn, int nLen, int * pnLenOut); private:
static int S16CompresShort(int out[], int outOffset, short in[], int inOffset, int n); static int S16Compress(int out[], int outOffset, int in[], int inOffset, int n); static int ReadBitsForS16(int in[], int inIntOffset, int inWithIntOffset, int bits); static int S16Decompress(int out[], int outOffset, int in[], int inOffset, int n); static int S16Decompress(sgi::vector<unsigned int>& outVec, int outOffset, int in[], int inOffset, int n); static int S16DecompressShort(short out[], int outOffset, int in[], int inOffset, int n); static int* CompressShort(short * pnIn, int nLen, int * pnLenOut); static short* DecompressShort(int * pnIn, int nLen, int * pnLenOut);
};
int S16_NUM[] = { 28, 21, 21, 21, 14, 9, 8, 7, 6, 6, 5, 5, 4, 3, 2, 1 };
int S16_NUMOFF[] = { 15, 14, 13, 12, 10, 8, 7, 6, 5, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }; int S16_BITS[16][28] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 2, 2, 2, 2, 2, 2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0, 0, 0, 0 } }; int Simple16::S16CompresShort(int out[], int outOffset, short in[], int inOffset, int n)
{
int numIdx = 0, j = 0, num = 0, bits = 0; for (numIdx = 0; (numIdx < S16_NUMSIZE); numIdx++)
{
out[outOffset] = numIdx << S16_BITSSIZE;
num = (S16_NUM[numIdx] < n) ? S16_NUM[numIdx] : n; for (j = 0, bits = 0; (j < num) && in[inOffset + j] < (1 << S16_BITS[numIdx][j]);)
{
out[outOffset] |= (in[inOffset + j] << bits);
bits += S16_BITS[numIdx][j];
j++;
} if (j == num)
{
return num;
}
} return -1;
} /**
* Compress an integer array using Simple16
*
* @param out the compressed output
* @param outOffset the offset of the output in the number of integers
* @param in the integer input array
* @param inOffset the offset of the input in the number of integers
* @param n the number of elements to be compressed
* @return the number of compressed integers
*/
int Simple16::S16Compress(int out[], int outOffset, int in[], int inOffset, int n )
{
int numIdx = 0, j = 0, num = 0, bits = 0;
int mi = 1;
unsigned int id = 0u;
for (j = 0, bits = 0; j < n; j++)
{
id = in[inOffset + j];
mi = 1;
if (id == 0)
{
bits++;
}
else
{
if ((id >> 16) == 0) { mi = mi + 16; id = id << 16; }
if ((id >> 24) == 0) { mi = mi + 8; id = id << 8; }
if ((id >> 28) == 0) { mi = mi + 4; id = id << 4; }
if ((id >> 30) == 0) { mi = mi + 2; id = id << 2; }
bits += (32 - mi + (id >> 31));
} if (bits > S16_BITSSIZE)
{
break;
}
}
numIdx = S16_NUMOFF[j - 1]; for (; (numIdx < S16_NUMSIZE); numIdx++)
{
out[outOffset] = numIdx << S16_BITSSIZE;
num = (S16_NUM[numIdx] < n) ? S16_NUM[numIdx] : n; for (j = 0, bits = 0; (j < num) && in[inOffset + j] < (1 << S16_BITS[numIdx][j]);)
{
out[outOffset] |= (in[inOffset + j] << bits);
bits += S16_BITS[numIdx][j];
j++;
} if (j == num)
{
return num;
}
} return -1;
}
/**
* Read a certain number of bits of a integer on the input array
* @param in the input array
* @param inIntOffset the start offset in ints in the input array
* @param inWithIntOffset the start offset within a int in the input array
* @param bits the number of bits to be read
* @return the bits bits of the input
*/
int Simple16::ReadBitsForS16(int in[], int inIntOffset, int inWithIntOffset, int bits) {
// 正整数不需要考虑 补0 的问题
int nVal = (in[inIntOffset] >> inWithIntOffset); if (nVal < 0)
nVal &= 0x7FFFFFFF; return nVal & ((0xffffffff >> (32 - bits)) & 0x7FFFFFFF);
} /**
* Decompress an integer array using Simple16
*
* @param out the decompressed output
* @param outOffset the offset of the output in the number of integers
* @param in the compressed input array
* @param inOffset the offset of the input in the number of integers
* @param n the number of elements to be compressed
* @return the number of processed integers
*/
int Simple16::S16Decompress(int out[], int outOffset, int in[], int inOffset, int n)
{
int numIdx, j = 0, bits = 0;
int num;
numIdx = (in[inOffset] >> S16_BITSSIZE) & (Simple16_Mask >> (S16_BITSSIZE - 1));
num = S16_NUM[numIdx] < n ? S16_NUM[numIdx] : n;
for (j = 0, bits = 0; j < num; j++)
{
out[outOffset + j] = ReadBitsForS16(in, inOffset, bits, S16_BITS[numIdx][j]);
bits += S16_BITS[numIdx][j];
}
return num;
} int Simple16::S16Decompress(sgi::vector<unsigned int>& outVec, int outOffset, int in[], int inOffset, int n)
{
int numIdx, j = 0, bits = 0;
int num;
numIdx = (in[inOffset] >> S16_BITSSIZE) & (Simple16_Mask >> (S16_BITSSIZE - 1));
num = S16_NUM[numIdx] < n ? S16_NUM[numIdx] : n;
for (j = 0, bits = 0; j < num; j++)
{
outVec[outOffset + j] = ReadBitsForS16(in, inOffset, bits, S16_BITS[numIdx][j]);
bits += S16_BITS[numIdx][j];
}
return num;
} int Simple16::S16DecompressShort(short out[], int outOffset, int in[], int inOffset, int n)
{
int numIdx, j = 0, bits = 0;
int num;
numIdx = (in[inOffset] >> S16_BITSSIZE) & (Simple16_Mask >> (S16_BITSSIZE - 1));
num = S16_NUM[numIdx] < n ? S16_NUM[numIdx] : n;
for (j = 0, bits = 0; j < num; j++)
{
out[outOffset + j] = ReadBitsForS16(in, inOffset, bits, S16_BITS[numIdx][j]);
bits += S16_BITS[numIdx][j];
}
return num;
} int* Simple16::Decompress(int * pnIn, int nLen, int * pnLenOut)
{
int * pnRes = NULL;
*pnLenOut = 0;
if (NULL == pnIn || nLen <= 0)
return pnRes; {
int nNum = 0;
int nNumLeft = nLen;
int nOutOffset = 0;
int nInOffset = 0;
int nLenTemp = sizeof(int) * nLen;
//int * pnResTemp = NULL; pnRes = (int*)malloc(nLenTemp);
//memset(pnRes, 0, nLenTemp); for (nNumLeft = nLen; nNumLeft > 0; nNumLeft -= nNum)
{
nNum = S16Decompress(pnRes, nOutOffset, pnIn, nInOffset, nNumLeft);
nOutOffset += nNum;
nInOffset++;
} *pnLenOut = nInOffset;
} return pnRes;
} void Simple16::Decompress(int * pnIn, int nLen, sgi::vector<unsigned int>& outVec)
{
if (NULL == pnIn || nLen <= 0)
return; {
int nNum = 0;
int nNumLeft = nLen;
int nInOffset = 0;
int nOutOffset = 0; //outVec.clear();
outVec.resize(nLen); for (nNumLeft = nLen; nNumLeft > 0; nNumLeft -= nNum)
{
nNum = S16Decompress(outVec, nOutOffset, pnIn, nInOffset, nNumLeft);
nOutOffset += nNum;
nInOffset++;
}
}
} int* Simple16::Compress(int * pnIn, int nLen, int * pnLenOut)
{
int * pnRes = NULL;
*pnLenOut = 0; if (NULL == pnIn || nLen <= 0)
return pnRes; {
int nNum = 0;
int nNumLeft = nLen;
int nOutOffset = 0;
int nInOffset = 0;
int nLenTemp = sizeof(int)* nLen;
// int * pnResTemp = NULL; pnRes = (int*)malloc(nLenTemp);
//memset(pnRes, 0, nLenTemp); for (nNumLeft = nLen; nNumLeft > 0; nNumLeft -= nNum)
{
nNum = S16Compress(pnRes, nOutOffset, pnIn, nInOffset, nNumLeft);
nOutOffset++;
nInOffset += nNum;
} *pnLenOut = nOutOffset;
// nLenTemp = sizeof(int)* nOutOffset; // pnResTemp = (int*)malloc(nLenTemp);
// memset(pnResTemp, 0, nLenTemp);
//
// memcpy(pnResTemp, pnRes, nLenTemp);
// free(pnRes);
// pnRes = pnResTemp;
return pnRes;
} return pnRes;
} int* Simple16::CompressShort(short * pnIn, int nLen, int * pnLenOut)
{
int * pnRes = NULL;
*pnLenOut = 0; if (NULL == pnIn || nLen <= 0)
return pnRes; {
int nNum = 0;
int nNumLeft = nLen;
int nOutOffset = 0;
int nInOffset = 0;
int nLenTemp = sizeof(int)* nLen;
int * pnResTemp = NULL; pnRes = (int*)malloc(nLenTemp);
memset(pnRes, 0, nLenTemp); for (nNumLeft = nLen; nNumLeft > 0; nNumLeft -= nNum)
{
nNum = S16CompresShort(pnRes, nOutOffset, pnIn, nInOffset, nNumLeft);
nOutOffset++;
nInOffset += nNum;
} *pnLenOut = nOutOffset;
nLenTemp = sizeof(int)* nOutOffset; pnResTemp = (int*)malloc(nLenTemp);
memset(pnResTemp, 0, nLenTemp); memcpy(pnResTemp, pnRes, nLenTemp);
free(pnRes);
pnRes = pnResTemp;
} return pnRes;
} short* Simple16::DecompressShort(int * pnIn, int nLen, int * pnLenOut)
{
short * pnRes = NULL;
*pnLenOut = 0;
if (NULL == pnIn || nLen <= 0)
return pnRes; {
int nNum = 0;
int nNumLeft = nLen;
int nOutOffset = 0;
int nInOffset = 0;
int nLenTemp = sizeof(short)* nLen;
//int * pnResTemp = NULL; pnRes = (short*)malloc(nLenTemp);
memset(pnRes, 0, nLenTemp); for (nNumLeft = nLen; nNumLeft > 0; nNumLeft -= nNum)
//while ( nInOffset < nLen)
{
nNum = S16DecompressShort(pnRes, nOutOffset, pnIn, nInOffset, nNumLeft);
nOutOffset += nNum;
nInOffset++;
} *pnLenOut = nInOffset; //nLenTemp = sizeof(short)* nOutOffset;
//pnResTemp = MM_MALLOC(nLenTemp);
//MEMSET(pnResTemp, 0, nLenTemp);
//MEMCPY(pnResTemp, pnRes, nLenTemp);
//MM_FREEIF(pnRes);
//pnRes = pnResTemp;
} return pnRes;
}

Simple16 字符压缩的更多相关文章

  1. 利用ICSharpCode.SharpZipLib.Zip进行文件压缩

    官网http://www.icsharpcode.net/ 支持文件和字符压缩. 创建全新的压缩包 第一步,创建压缩包 using ICSharpCode.SharpZipLib.Zip; ZipOu ...

  2. RGB图像数据字符叠加,图像压缩(ijl库),YUV转RGB

    jackyhwei 发布于 2010-01-01 12:02 点击:3218次  来自:CSDN.NET 一些非常有用的图像格式转换及使用的源代码,包括RGB图像数据字符叠加,图像压缩(ijl库),Y ...

  3. 重温CLR(十) 字符、字符串和文本处理

    本章将介绍.net中处理字符和字符串的机制 字符 在.NET Framewole中,字符总是表示成16位Unicode代码值,这简化了国际化应用程序的开发. 每个字符都表示成System.Char结构 ...

  4. zip压缩详细分析

    该文章转自:http://www.cnblogs.com/esingchan/p/3958962.html (文章写得很详细,让我对zip压缩有了了解,感谢博主,贴在这是为了防止忘了有这么好的文章,侵 ...

  5. C#之字符编码

    在 Windows Vista 及之后的版本中,每个Unicode字符都使用UTF-16编码,UTF的全称是 Unicode Transformation Format(Unicode 转换格式).U ...

  6. 3.20 tr:替换或删除字符

    tr命令 从标准输入中替换.缩减或删除字符,并将结果写到标准输出. tr [option] [SET1]  [SET2] tr [选项]   [字符1]  [字符2]   -d    删除字符 -s  ...

  7. ZIP压缩算法详细分析及解压实例解释

    最近自己实现了一个ZIP压缩数据的解压程序,觉得有必要把ZIP压缩格式进行一下详细总结,数据压缩是一门通信原理和计算机科学都会涉及到的学科,在通信原理中,一般称为信源编码,在计算机科学里,一般称为数据 ...

  8. 2015 ACM Syrian Collegiate Programming Contest

    A. My Friend of Misery 计算出答案的上下界即可. 时间复杂度$O(n)$. #include<bits/stdc++.h> using namespace std; ...

  9. 【数据压缩】LZ77算法原理及实现

    1. 引言 LZ77算法是采用字典做数据压缩的算法,由以色列的两位大神Jacob Ziv与Abraham Lempel在1977年发表的论文<A Universal Algorithm for ...

随机推荐

  1. CF1578I Interactive Rays:ICPC WF Moscow Invitational Contest I 题解

    题意简述:在平面上有一个坐标 \((x_c,y_c)\) 和半径 \(r\) 都是整数的圆 \((1\leq r_c\leq \sqrt{x_c^2+y_c^2}-1)\),你可以询问不超过 \(60 ...

  2. Oracle——创建多个实例(数据库)、切换实例、登录数据库实例

    oracle中怎么创建多个实例? 其实很简单,怎么创建第一个实例,其他实例应该也怎么创建. 我的理解其实在linux中的oracle数据库中创建一个实例,实际上就是创建一个新的数据库,只是实例名字不同 ...

  3. 在WEB网页上模拟人的操作(批量操作)

    思路:selenium IDE网页测试工具+firefox浏览器=>录制网页操作脚本->导出为Perl/python/Ruby/C/R等语言 参考: (1)selenium IDE网页测试 ...

  4. QQ空间技术架构之深刻揭秘

    QQ空间技术架构之深刻揭秘 来源: 腾讯大讲堂  发布时间: 2012-05-17 17:24  阅读: 7822 次  推荐: 4   [收藏]   QQ 空间作为腾讯海量互联网服务产品,经过近七年 ...

  5. ubuntu 常用指令

    1.进入到root权限的指令 sudo su,效果同su,只是不需要root的密码,而需要当前用户的密码.(亲测有效) 2.从root权限里面退出到 普通用户模式 exit---指令亲测有效 3.下载 ...

  6. Beautiful Soup解析库的安装和使用

    Beautiful Soup是Python的一个HTML或XML的解析库,我们可以用它来方便地从网页中提取数据.它拥有强大的API和多样的解析方式.官方文档:https://www.crummy.co ...

  7. Scala【需求二:求各省市的各个指标】

    需求处理步骤 原始数据->json->过滤->列裁剪 需求二:求各省市的各个指标 原始数据 文本pmt.json,每一行都是一个json字符串.里面包含ip等信息 {"se ...

  8. k8s-hpa自动横向扩容

    目录 hpa自动扩容 官方文档 HPA是什么 Horizontal Pod Autoscaler 演练 参数 案例:监控cpu,内存,每秒数据包自动扩容 度量指标 pod清单案例-pod定义cup内存 ...

  9. 容器之分类与各种测试(四)——unordered_set和unordered_map

    关于set和map的区别前面已经说过,这里仅是用hashtable将其实现,所以不做过多说明,直接看程序 unordered_set #include<stdexcept> #includ ...

  10. Mybatis 批量插入

    一.首先对于批量数据的插入有两种解决方案(下面内容只讨论和Mysql交互的情况) 1)for循环调用Dao中的单条插入方法 2)传一个List<Object>参数,使用Mybatis的批量 ...