~grubng-dev/grubng/tools-urlsdb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// RangeCoderBit.cs
//  
//  Copyright (C) 2009 Bartek Jasicki based on LZMA SDK
// 
//  This file is part of Grubng
// 
//  Grubng is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 

using System;

namespace Grubng
{
	struct BitEncoder : IEquatable<BitEncoder>
	{
		public const int kNumBitModelTotalBits = 11;
		public const uint kBitModelTotal = (1 << kNumBitModelTotalBits);
		const int kNumMoveBits = 5;
		const int kNumMoveReducingBits = 2;
		public const int kNumBitPriceShiftBits = 6;

		uint Prob;

		public void Init() { Prob = kBitModelTotal >> 1; }

		public void Encode(RangeEncoder encoder, uint symbol)
		{
			uint newBound = (encoder.Range >> kNumBitModelTotalBits) * Prob;
			if (symbol == 0)
			{
				encoder.Range = newBound;
				Prob += (kBitModelTotal - Prob) >> kNumMoveBits;
			}
			else
			{
				encoder.Low += newBound;
				encoder.Range -= newBound;
				Prob -= (Prob) >> kNumMoveBits;
			}
			if (encoder.Range < RangeEncoder.kTopValue)
			{
				encoder.Range <<= 8;
				encoder.ShiftLow();
			}
		}

		private static uint[] ProbPrices = new uint[kBitModelTotal >> kNumMoveReducingBits];

		static BitEncoder()
		{
			int kNumBits = (kNumBitModelTotalBits - kNumMoveReducingBits);
			uint start;
			uint end;
			for (int i = kNumBits - 1; i >= 0; i--)
			{
				start = (uint)1 << (kNumBits - i - 1);
				end = (uint)1 << (kNumBits - i);
				for (uint j = start; j < end; j++)
					ProbPrices[j] = ((uint)i << kNumBitPriceShiftBits) +
						(((end - j) << kNumBitPriceShiftBits) >> (kNumBits - i - 1));
			}
		}

		public uint GetPrice(uint symbol)
		{
			return ProbPrices[(((Prob - symbol) ^ ((-(int)symbol))) & (kBitModelTotal - 1)) >> kNumMoveReducingBits];
		}
		
		public uint GetPrice0
		{ 
			get {return ProbPrices[Prob >> kNumMoveReducingBits];}
		}
		
		public uint GetPrice1 
		{ 
			get {return ProbPrices[(kBitModelTotal - Prob) >> kNumMoveReducingBits]; }
		}
		
		/// <summary>
		/// Function override standard Object.Equals(object) function
		/// </summary>
		/// <param name="obj">
		/// A <see cref="System.Object"/> Object to compare
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/> True if both objects are that same, otherwise false
		/// </returns>
		public override bool Equals(object obj)
		{
			if (obj == null)
			{
				return false;
			}
			if (GetType() != obj.GetType()) 
			{
				return false;
			}    
			BitEncoder btencoder = (BitEncoder)obj;                    
			return this == btencoder;
		}
        
		/// <summary>
		/// Function overload function BitEncoder.Equals(object)
		/// </summary>
		/// <param name="right">
		/// A <see cref="BitEncoder"/> value to compare
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/> True if both objects are that same, otherwise false
		/// </returns>
		public bool Equals(BitEncoder right)
		{                    
			return this == right;
		}
		
		/// <summary>
		/// Function override operator ==
		/// </summary>
		/// <param name="left">
		/// A <see cref="BitEncoder"/> left value to compare
		/// </param>
		/// <param name="right">
		/// A <see cref="BitEncoder"/> right value to compare
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/> True if both object are that same, otherwise false
		/// </returns>
		public static bool operator == (BitEncoder left, BitEncoder right)
		{
			return left.Equals(right);
		}
    
		/// <summary>
		/// Function override operator !=
		/// </summary>
		/// <param name="left">
		/// A <see cref="BitEncoder"/> left value to compare
		/// </param>
		/// <param name="right">
		/// A <see cref="BitEncoder"/> right value to compare
		/// </param>
		/// <returns>
		/// A <see cref="System.Boolean"/> True if both object different, otherwise false
		/// </returns>
		public static bool operator != (BitEncoder left, BitEncoder right)
		{
			return !left.Equals(right);
		}
    
		/// <summary>
		/// Function override function Object.GetHashCode();
		/// </summary>
		/// <returns>
		/// A <see cref="System.Int32"/> hash code for current instance
		/// </returns>
		public override int GetHashCode()
		{
			int hash; 
			unchecked
			{
				hash = Prob.GetHashCode();
			}       
			return hash;
		}
	}
}