惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Cisco Talos Blog
Cisco Talos Blog
C
Check Point Blog
博客园_首页
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Martin Fowler
Martin Fowler
Recorded Future
Recorded Future
S
Security @ Cisco Blogs
L
LINUX DO - 最新话题
博客园 - 司徒正美
P
Privacy International News Feed
G
Google Developers Blog
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 聂微东
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Scott Helme
Scott Helme
K
Kaspersky official blog
I
InfoQ
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
D
Docker
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Microsoft Azure Blog
Microsoft Azure Blog
Spread Privacy
Spread Privacy
量子位
H
Hacker News: Front Page
Simon Willison's Weblog
Simon Willison's Weblog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
SecWiki News
SecWiki News
S
Security Affairs
Latest news
Latest news
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
S
Security Archives - TechRepublic
V
Visual Studio Blog
T
Troy Hunt's Blog
S
Secure Thoughts
F
Fortinet All Blogs
V
V2EX
The Register - Security
The Register - Security
J
Java Code Geeks
MongoDB | Blog
MongoDB | Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

博客园 - 海风吹

Chrome浏览器界面截图 linux文件锁flock【转】 go的临时对象池--sync.Pool golang 小知识-持续更新中 【转】Go Channels Golang文件名命名规则 Parquet存储格式 - 论文翻译【转】 预装的Office2016,文件图标表显示以及新建失败问题解决 方法 时序数据库技术体系 – InfluxDB 多维查询之倒排索引 时序数据库技术体系 – InfluxDB TSM存储引擎之TSMFile 时间序列数据的存储和计算 - 开源时序数据库解析 时序数据库技术体系-时序数据存储模型设计 时序数据库-为万物互联插上一双翅膀 JMS和ActiveMQ的关系 JMS(Java消息服务)入门教程 Influxdb时序数据库阅读笔记 代码只是事业的 5%,程序员创业注意事项 - 海风吹 golang 之 channel golang中context包学习
Gamma编码及Delta编码概述
海风吹 · 2018-03-19 · via 博客园 - 海风吹

一、Elias Gamma Coding

即Gamma编码,是一种对正整数进行编码的统一编码,由Peter Elias发明。适用于预先无法获知最大编码整数的情况,而且小整数出现频率高,大整数出现频率低的情况。

编码原理:

对任何正整数NUM,对INT(Log2(NUM))+1进行一元编码,后缀上NUM二进制串除去最高位的子串。如5的编码为001,01。

编码思路:

对于任意的自然数xN={1,2,3,...},它的二进制需要floor(log(x))+1 bits来表示。在其二进制表示的前面加上floor(log(x))0,即Elias Gamma Code

例如:13d = 1011b        所以,EGC(13d) = 000 1011b

解码:

首先计算出Elias Gamma Code的开始的0的个数n

if(n == 0)

解码结果为1;

else

{

读入剩下的n+1bits;

解码结果为这些bits10进制表示;

}

编码示例:

NUM

EliasGamma Code

Implied probability

1 = 20 + 0

1

1/2

2 = 21 + 0

010

1/8

3 = 21 + 1

011

1/8

4 = 22 + 0

00100

1/32

5 = 22 + 1

00101

1/32

6 = 22 + 2

00110

1/32

7 = 22 + 3

00111

1/32

8 = 23 + 0

0001000

1/128

9 = 23 + 1

0001001

1/128

编码、解码算法:

 1 /**************************************************** 
 2 Encode_EliasGamma: 
 3 Encoding algorithm of EliasGamma Coding. 
 4 *****************************************************/  
 5 int Encode_EliasGamma(int *pSourceData,char *pEncodedData,int nSourceDataLen,int &nEncodedDataLen)  
 6 {  
 7     //Encoding EliasGamma Coding.         
 8     int k=-1;   
 9     for(int i=0;i<nSourceDataLen;i++)   
10     {  
11         int num =  pSourceData[i];       
12         int numPow = int(log10(num + 0.0)/log10(2+ 0.0));  
13         int j = 0;  
14         for ( j=0; j < numPow; j++)  pEncodedData[++k]=0;          
15         pEncodedData[++k]=1;  
16         for (j=numPow-1; j >= 0; j--)        
17         {  
18             if (num & 1 << j)  pEncodedData[++k]=1;    
19             else               pEncodedData[++k]=0;    
20         }     
21         nEncodedDataLen=k+1;  
22           
23     }  
24     //End of Encoding.  
25     return 1;  
26 }  
27   
28   
29 /**************************************************** 
30 Decode_EliasGamma: 
31 Decoding algorithm of EliasGamma Coding. 
32 *****************************************************/  
33 int Decode_EliasGamma(int *pDecodedData,char *pEncodedData,int &nDecodedDataLen,int nEncodedDataLen)  
34 {  
35     int i=0,j=0;  
36     while (1)  
37     {  
38         int numPow = 0;  
39         while (!pEncodedData[i++])   numPow++;            
40         if(numPow >=48)      break;              
41         int num = 0;  
42         for (int h=numPow-1; h >= 0; h--)      
43             if (pEncodedData[i++])  num |= 1 << h;        
44         num |= 1 << numPow;   
45         pDecodedData[j++]=num;        
46     }  
47     nDecodedDataLen=j;  
48       
49     return 1;  
50 }  

View Code

二、Elisa Delta Code

编码:

对于任意的xN = {1,2,3,...},分步介绍它的编码方式:

1)用Elisa Gamma Code的方式编码x的长度:

Cr (floor(log(x)) + 1);

2)求出x的二进制表示,并且用Cr做前缀

3)去掉x二进制表示中的第一个1

for example:

x = 13d = 1101b;

log(x) = log(13) = 3;

Cr(log(x)+1) = Cr(4) = 00100b;

EDC(x) = 00100 101b;

解码:

1)首先计算EDC中前缀0的个数n

2)读出n+1bits,即m = log(x) + 1的二进制表示

3)读出剩余的(m-1)bits,并且在前面加1,即最终的解码结果

for example:

EDC(x) = 00100 101b

n = 2;

m = n+1bits:100b = 4d

(m-1)bits:101b

1101b = 13d

效率:对特别大的整型范围NEDC的长度接近熵,是近似最优的,但是在小N的时候,EGC要好一些。