@@ -9,17 +9,23 @@ namespace AlgorithmsLibrary
99{
1010 public static class LZ77Algm
1111 {
12- static bool Zavod = false ;
12+ static bool ExtendedAlgm = false ;
13+ public static IAlgmEncoded < List < LZ77CodeBlock > > Encode ( string source , bool extended )
14+ {
15+ ExtendedAlgm = extended ;
16+ return Encode ( source ) ;
17+ }
1318 /// <summary>
1419 /// String compression using the LZ77 algorithm
1520 /// </summary>
1621 /// <param name="inputString">Source string</param>
1722 /// <returns>Compressed (encoded) string</returns>
18- public static IAlgmEncoded < List < CodeBlock > > Encode ( string inputString )
23+ public static IAlgmEncoded < List < LZ77CodeBlock > > Encode ( string inputString )
1924 {
20- List < CodeBlock > result = new List < CodeBlock > ( ) ;
25+ List < LZ77CodeBlock > result = new List < LZ77CodeBlock > ( ) ;
2126 StringBuilder searchBuffer = new StringBuilder ( ) ;
2227 StringBuilder establishingBuffer = new StringBuilder ( inputString ) ;
28+ StringBuilder extended = new StringBuilder ( string . Empty ) ;
2329
2430 int currentLengthSubString = 0 ;
2531 //Кодирование идет до тех пор пока учреждающий буфер (establishingBuffer) не окажется пуст
@@ -57,8 +63,11 @@ public static IAlgmEncoded<List<CodeBlock>> Encode(string inputString)
5763 searchBuffer . Append ( nextChar ) ;
5864 establishingBuffer . Remove ( 0 , 1 ) ;
5965
60- var codeblock = new CodeBlock ( 0 , 0 , nextChar ) ;
61- if ( Zavod ) Console . WriteLine ( searchBuffer . ToString ( ) + "\t \t " + establishingBuffer . ToString ( ) + "\t \t " + codeblock ) ;
66+ var codeblock = new LZ77CodeBlock ( 0 , 0 , nextChar ) ;
67+ if ( ExtendedAlgm )
68+ {
69+ extended . Append ( searchBuffer . ToString ( ) + "\t \t " + establishingBuffer . ToString ( ) + "\t \t " + codeblock + "\n " ) ;
70+ }
6271 result . Add ( codeblock ) ; //указываем на него метку
6372 }
6473 else
@@ -76,15 +85,18 @@ public static IAlgmEncoded<List<CodeBlock>> Encode(string inputString)
7685 establishingBuffer . Remove ( 0 , length + 1 ) ;
7786 searchBuffer . Append ( subInEstablish ) ;
7887
79- var codeblock = new CodeBlock ( offset , length , nextChar ) ;
80- if ( Zavod ) Console . WriteLine ( searchBuffer . ToString ( ) + "\t \t " + establishingBuffer . ToString ( ) + "\t \t " + codeblock ) ;
88+ var codeblock = new LZ77CodeBlock ( offset , length , nextChar ) ;
89+ if ( ExtendedAlgm )
90+ {
91+ extended . Append ( searchBuffer . ToString ( ) + "\t \t " + establishingBuffer . ToString ( ) + "\t \t " + codeblock + "\n " ) ;
92+ }
8193 result . Add ( codeblock ) ;
8294 }
8395
8496 currentLengthSubString = 0 ;
8597 }
8698
87- return new EncodedMessage < List < CodeBlock > > ( result , CalculateCompressionRatio ( inputString , result ) ) ;
99+ return new EncodedMessage < List < LZ77CodeBlock > > ( result , CalculateCompressionRatio ( inputString , result ) , extended . ToString ( ) ) ;
88100 }
89101
90102 /// <summary>
@@ -93,13 +105,13 @@ public static IAlgmEncoded<List<CodeBlock>> Encode(string inputString)
93105 /// <param name="sourceString">Source string</param>
94106 /// <param name="compressionString">Compressed (encoded) string</param>
95107 /// <returns>Compression ratio</returns>
96- private static double CalculateCompressionRatio ( string sourceString , List < CodeBlock > compressionString )
108+ private static double CalculateCompressionRatio ( string sourceString , List < LZ77CodeBlock > compressionString )
97109 {
98110 //Считаем что в стандартной кодировке один символ = 8бит
99111 double countBitsSourceString = 8 * sourceString . Length ;
100112
101113 double countBitsCompressionString = 0 ;
102- foreach ( CodeBlock compression in compressionString )
114+ foreach ( LZ77CodeBlock compression in compressionString )
103115 {
104116 int countBitsOffset = Convert . ToString ( compression . Offset , 2 ) . Length ;
105117 int countBitsLength = Convert . ToString ( compression . Length , 2 ) . Length ;
@@ -111,9 +123,9 @@ private static double CalculateCompressionRatio(string sourceString, List<CodeBl
111123 return Math . Round ( countBitsSourceString / countBitsCompressionString , 3 ) ;
112124 }
113125
114- private static List < CodeBlock > ParseEncodedString ( string encodedString )
126+ private static List < LZ77CodeBlock > ParseEncodedString ( string encodedString )
115127 {
116- List < CodeBlock > encodedStringParsed = new List < CodeBlock > ( ) ;
128+ List < LZ77CodeBlock > encodedStringParsed = new List < LZ77CodeBlock > ( ) ;
117129 // вид кодового блока:
118130 //({0},{1},{2})...({0},{1},{2})
119131 //парсит всю строку на блоки
@@ -131,7 +143,7 @@ private static List<CodeBlock> ParseEncodedString(string encodedString)
131143 {
132144 string codeBlock = match . Value ;
133145 MatchCollection matchesBlock = intRegex . Matches ( codeBlock ) ;
134- encodedStringParsed . Add ( new CodeBlock ( int . Parse ( matchesBlock [ 0 ] . Value ) , int . Parse ( matchesBlock [ 1 ] . Value ) , codeBlock [ codeBlock . Length - 2 ] ) ) ;
146+ encodedStringParsed . Add ( new LZ77CodeBlock ( int . Parse ( matchesBlock [ 0 ] . Value ) , int . Parse ( matchesBlock [ 1 ] . Value ) , codeBlock [ codeBlock . Length - 2 ] ) ) ;
135147 }
136148
137149 return encodedStringParsed ;
@@ -148,7 +160,7 @@ public static IAlgmEncoded<string> Decode(string encodedString)
148160
149161 StringBuilder resultDecoding = new StringBuilder ( string . Empty ) ;
150162
151- foreach ( CodeBlock codeBlock in encodedStringParsed )
163+ foreach ( LZ77CodeBlock codeBlock in encodedStringParsed )
152164 {
153165 if ( codeBlock . Length > 0 )
154166 {
0 commit comments