~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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// LzmaBase.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
{
	internal static class Base
	{
		public const uint kNumRepDistances = 4;
		public const uint kNumStates = 12;

		public struct State : IEquatable<State>
		{
			public uint Index;
			public void Init() { Index = 0; }
			public void UpdateChar()
			{
				if (Index < 4) Index = 0;
				else if (Index < 10) Index -= 3;
				else Index -= 6;
			}
				
			/// <summary>
			/// Merge UpdateMatch(), UpdateRep(), UpdateShortRep()
			/// </summary>
			/// <param name="type">
			/// A <see cref="System.Int16"/>
			/// 1 - Match, 2 - Rep, 3 - ShortRep
			/// </param>
			public void Update(short type)
			{
				switch (type)
				{
				case 1:
					Index = (uint)(Index < 7 ? 7 : 10);
					break;
				case 2:
					Index = (uint)(Index < 7 ? 8 : 11);
					break;
				case 3:
					Index = (uint)(Index < 7 ? 9 : 11);
					break;
				default:
					break;
				}
			}
			
			public bool IsCharState 
			{ 
				get {return Index < 7; }
			}
			
			/// <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;
				}    
				State state = (State)obj;                    
				return this == state;
			}
        
			/// <summary>
			/// Function overload function State.Equals(object)
			/// </summary>
			/// <param name="right">
			/// A <see cref="State"/> value to compare
			/// </param>
			/// <returns>
			/// A <see cref="System.Boolean"/> True if both objects are that same, otherwise false
			/// </returns>
			public bool Equals(State right)
			{                    
				return this == right;
			}
 
			/// <summary>
			/// Function override operator ==
			/// </summary>
			/// <param name="left">
			/// A <see cref="State"/> left value to compare
			/// </param>
			/// <param name="right">
			/// A <see cref="State"/> 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 == (State left, State right)
			{
				return left.Equals(right);
			}
    
			/// <summary>
			/// Function override operator !=
			/// </summary>
			/// <param name="left">
			/// A <see cref="State"/> left value to compare
			/// </param>
			/// <param name="right">
			/// A <see cref="State"/> right value to compare
			/// </param>
			/// <returns>
			/// A <see cref="System.Boolean"/> True if both object different, otherwise false
			/// </returns>
			public static bool operator != (State left, State 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 = Index.GetHashCode();
				}       
				return hash;
			}
		}

		public const int kNumPosSlotBits = 6;
		public const int kDicLogSizeMin = 0;

		public const int kNumLenToPosStatesBits = 2; // it's for speed optimization
		public const uint kNumLenToPosStates = 1 << kNumLenToPosStatesBits;

		public const uint kMatchMinLen = 2;

		public static uint GetLenToPosState(uint len)
		{
			len -= kMatchMinLen;
			if (len < kNumLenToPosStates)
				return len;
			return (uint)(kNumLenToPosStates - 1);
		}

		public const int kNumAlignBits = 4;
		public const uint kAlignTableSize = 1 << kNumAlignBits;
		public const uint kAlignMask = (kAlignTableSize - 1);

		public const uint kStartPosModelIndex = 4;
		public const uint kEndPosModelIndex = 14;
		public const uint kNumPosModels = kEndPosModelIndex - kStartPosModelIndex;

		public const uint kNumFullDistances = 1 << ((int)kEndPosModelIndex / 2);

		public const uint kNumLitPosStatesBitsEncodingMax = 4;
		public const uint kNumLitContextBitsMax = 8;

		public const int kNumPosStatesBitsMax = 4;
		public const uint kNumPosStatesMax = (1 << kNumPosStatesBitsMax);
		public const int kNumPosStatesBitsEncodingMax = 4;
		public const uint kNumPosStatesEncodingMax = (1 << kNumPosStatesBitsEncodingMax);

		public const int kNumLowLenBits = 3;
		public const int kNumMidLenBits = 3;
		public const int kNumHighLenBits = 8;
		public const uint kNumLowLenSymbols = 1 << kNumLowLenBits;
		public const uint kNumMidLenSymbols = 1 << kNumMidLenBits;
		public const uint kNumLenSymbols = kNumLowLenSymbols + kNumMidLenSymbols +
				(1 << kNumHighLenBits);
		public const uint kMatchMaxLen = kMatchMinLen + kNumLenSymbols - 1;
	}
}