Skip to content

Commit af75d18

Browse files
committed
Add ExtendedAlgm bool into answer & encoding method
1 parent 9d6580d commit af75d18

File tree

13 files changed

+129
-63
lines changed

13 files changed

+129
-63
lines changed

AlgorithmsLibrary/AlgorithmsLibrary.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<Compile Include="Results\IAlgmEncoded.cs" />
6565
<Compile Include="LinearCodesType52\LinearCodesType52.cs" />
6666
<Compile Include="LZ77Algm\LZ77Algm.cs" />
67-
<Compile Include="LZ77Algm\CodeBlock.cs" />
67+
<Compile Include="LZ77Algm\LZ77CodeBlock.cs" />
6868
<Compile Include="Properties\AssemblyInfo.cs" />
6969
<Compile Include="RLEAlgmBWT\BurrowsWheelerTransform.cs" />
7070
<Compile Include="RLEAlgmBWT\RLEAlgmBWT.cs" />

AlgorithmsLibrary/ArithmeticCodingAlgm/ArithmeticCodingAlgm.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace AlgorithmsLibrary
77
{
88
public static class ArithmeticCodingAlgm
99
{
10+
static bool ExtendedAlgm = false;
1011
private static Dictionary<char, int> GetFrequencies(string source)
1112
{
1213
Dictionary<char, int> frequencies = new Dictionary<char, int>();
@@ -45,6 +46,12 @@ private static List<Symbol> GetSymbolsRanges(Dictionary<char, int> frequencies,
4546
return nodes;
4647
}
4748

49+
public static IAlgmEncoded<string, IAlgmEncoded<int, Dictionary<char, int>>> Encode(string source, bool extended)
50+
{
51+
ExtendedAlgm = extended;
52+
return Encode(source);
53+
}
54+
4855
public static IAlgmEncoded<string, IAlgmEncoded<int, Dictionary<char, int>>> Encode(string source)
4956
{
5057
List<Symbol> codes = GetSymbolsRanges(source);

AlgorithmsLibrary/HammingAlgm/HammingAlgm.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace AlgorithmsLibrary
77
{
88
public static class HammingAlgm
99
{
10+
static bool ExtendedAlgm = false;
1011
/// <summary>
1112
/// Вычисление количества контрольных бит, необходимых для кодирования сообщения.
1213
/// </summary>
@@ -96,6 +97,12 @@ public static IAlgmEncoded<string> DecodeASCII(string source)
9697
return new EncodedMessage<string>(Encoding.ASCII.GetString(GetByteArray(restored)), 0.0);
9798
}
9899

100+
public static IAlgmEncoded<string> Encode(string source, bool extended)
101+
{
102+
ExtendedAlgm = extended;
103+
return Encode(source);
104+
}
105+
99106
/// <summary>
100107
/// Encoding of the string. Arranging of information bits and calculation of control bits.
101108
/// </summary>

AlgorithmsLibrary/HuffmanAlgm/HuffmanAlgm.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace AlgorithmsLibrary
88
{
99
public static class HuffmanAlgm
1010
{
11+
static bool ExtendedAlgm = false;
1112
private static Dictionary<char, int> GetFrequencies(string source)
1213
{
1314
Dictionary<char, int> frequencies = new Dictionary<char, int>();
@@ -53,6 +54,12 @@ private static Dictionary<char, string> GetHuffmanCodes(Dictionary<char, int> fr
5354
return nodes.FirstOrDefault().InOrderTraversal();
5455
}
5556

57+
public static IAlgmEncoded<string, Dictionary<char, string>> Encode(string source, bool extended)
58+
{
59+
ExtendedAlgm = extended;
60+
return Encode(source);
61+
}
62+
5663
/// <summary>
5764
/// Encoding. The algorithm compile a dictionary of frequencies from the source string,
5865
/// then builds a tree on them and gets a dictionary with Huffman codes.

AlgorithmsLibrary/LZ77Algm/LZ77Algm.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

AlgorithmsLibrary/LZ77Algm/CodeBlock.cs renamed to AlgorithmsLibrary/LZ77Algm/LZ77CodeBlock.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace AlgorithmsLibrary
55
/// <summary>
66
/// Метка. Кодовый блок, состоящий из компонетов: смещение, длина, следующий символ.
77
/// </summary>
8-
public class CodeBlock : IEquatable<CodeBlock>
8+
public class LZ77CodeBlock : IEquatable<LZ77CodeBlock>
99
{
1010
public int Offset { get; private set; }
1111
public int Length { get; private set; }
1212
public char NextChar { get; private set; }
1313

14-
public CodeBlock(int offset, int length, char nextChar)
14+
public LZ77CodeBlock(int offset, int length, char nextChar)
1515
{
1616
this.Offset = offset;
1717
this.Length = length;
@@ -23,7 +23,7 @@ public override string ToString()
2323
return string.Format("({0},{1},{2})", Offset, Length, NextChar);
2424
}
2525

26-
public bool Equals(CodeBlock other)
26+
public bool Equals(LZ77CodeBlock other)
2727
{
2828
return Offset == other.Offset && Length == other.Length && NextChar == other.NextChar;
2929
}

AlgorithmsLibrary/LZ78Algm/LZ78Algm.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,21 @@ namespace AlgorithmsLibrary
1010
/// <summary>
1111
/// Здесь идея этого алгоритма.
1212
/// </summary>
13-
public static class LZ78Algm
13+
public static class LZ78Algm
1414
{
15-
static bool Zavod = false;
15+
static bool ExtendedAlgm = false;
16+
public static IAlgmEncoded<List<LZ78CodeBlock>> Encode(string source, bool extended)
17+
{
18+
ExtendedAlgm = extended;
19+
return Encode(source);
20+
}
1621
public static IAlgmEncoded<List<LZ78CodeBlock>> Encode(string source)
1722
{
1823
string Buffer = ""; //строка для формирования ключа для словаря
1924
Dictionary<string, int> Dictionary = new Dictionary<string, int> { { "", 0 } };
2025
List<LZ78CodeBlock> EncodedString = new List<LZ78CodeBlock>(); // ответ
26+
StringBuilder extended = new StringBuilder(string.Empty);
27+
2128
for (int i = 0; i < source.Length; i++)
2229
{
2330
if (Dictionary.ContainsKey(Buffer + source[i]))
@@ -27,10 +34,10 @@ public static IAlgmEncoded<List<LZ78CodeBlock>> Encode(string source)
2734
else
2835
{
2936
var codeblock = new LZ78CodeBlock(Dictionary[Buffer], source[i]);
30-
if (Zavod)
37+
if (ExtendedAlgm)
3138
{
3239
char c = Buffer.Length == 0 ? source[i] : Buffer[0];
33-
Console.WriteLine(c + " " + codeblock);
40+
extended.Append(c + " " + codeblock + "\n");
3441
}
3542
EncodedString.Add(codeblock); // добавляем пару в ответ
3643
Dictionary.Add(Buffer + source[i], Dictionary.Count); // добавляем слово в словарь
@@ -43,7 +50,10 @@ public static IAlgmEncoded<List<LZ78CodeBlock>> Encode(string source)
4350
if (Dictionary.ContainsKey(Buffer))
4451
{
4552
var codeblock = new LZ78CodeBlock(Dictionary[Buffer], '$');
46-
if (Zavod) Console.WriteLine('$' + " " + codeblock);
53+
if (ExtendedAlgm)
54+
{
55+
extended.Append('$' + " " + codeblock + "\n");
56+
}
4757
EncodedString.Add(codeblock);
4858
}
4959
else
@@ -52,18 +62,23 @@ public static IAlgmEncoded<List<LZ78CodeBlock>> Encode(string source)
5262
Buffer = Buffer.Remove(Buffer.Length - 1); // удаляем последний символ из буфера
5363

5464
var codeblock = new LZ78CodeBlock(Dictionary[Buffer], last_ch);
55-
if (Zavod) Console.WriteLine(Buffer[0] + " " + codeblock);
65+
if (ExtendedAlgm)
66+
{
67+
extended.Append(Buffer[0] + " " + codeblock + "\n");
68+
}
5669
EncodedString.Add(codeblock); // добавляем пару в ответ
5770
}
5871
}
5972

60-
if (Zavod)
73+
if (ExtendedAlgm)
74+
{
6175
foreach (var item in Dictionary)
6276
{
63-
Console.WriteLine(item.Key + " " + item.Value);
77+
extended.Append(item.Key + " " + item.Value + "\n");
6478
}
79+
}
6580

66-
return new EncodedMessage<List<LZ78CodeBlock>>(EncodedString, CalculateCompressionRatio(source, EncodedString));
81+
return new EncodedMessage<List<LZ78CodeBlock>>(EncodedString, CalculateCompressionRatio(source, EncodedString), extended.ToString());
6782
}
6883

6984
private static List<LZ78CodeBlock> ParseEncodedString(string encodedString)

AlgorithmsLibrary/RLEAlgmBWT/RLEAlgmBWT.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ namespace AlgorithmsLibrary
88
{
99
public static class RLEAlgm
1010
{
11-
public static IAlgmEncoded<List<RLECodeBlock>> Encode(string inputString)
11+
static bool ExtendedAlgm = false;
12+
public static IAlgmEncoded<List<RLECodeBlock>> Encode(string source, bool extended)
1213
{
13-
if (string.IsNullOrEmpty(inputString))
14+
ExtendedAlgm = extended;
15+
return Encode(source);
16+
}
17+
public static IAlgmEncoded<List<RLECodeBlock>> Encode(string source)
18+
{
19+
if (string.IsNullOrEmpty(source))
1420
{
1521
throw new ArgumentNullException("string for encoding is null or empty");
1622
}
1723

1824
List<RLECodeBlock> result = new List<RLECodeBlock>();
19-
var encoded = BurrowsWheelerTransform.Encode(inputString);
25+
var encoded = BurrowsWheelerTransform.Encode(source);
2026

2127
result.Add(new RLECodeBlock(default, encoded.index));
2228
int countRepeatsSymbols = 0;
@@ -36,7 +42,7 @@ public static IAlgmEncoded<List<RLECodeBlock>> Encode(string inputString)
3642
currentSymbol = symbol;
3743
}
3844

39-
return new EncodedMessage<List<RLECodeBlock>>(result, CalculateCompressionRatio(inputString, result));
45+
return new EncodedMessage<List<RLECodeBlock>>(result, CalculateCompressionRatio(source, result));
4046
}
4147

4248
private static List<RLECodeBlock> ParseEncodedString(string encodedString)

AlgorithmsLibrary/Results/EncodedMessage.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ namespace AlgorithmsLibrary
55
{
66
internal class EncodedMessage<T> : IAlgmEncoded<T>
77
{
8+
private string extended;
89
private T answer;
910
private double compressionRatio;
1011

@@ -17,7 +18,8 @@ public override string ToString()
1718
{
1819
// В некоторых алгоритмах выхлопной ответ выглядит где то как строка
1920
// где то как массив "своих" объектов
20-
StringBuilder stringBuilder = new StringBuilder();
21+
StringBuilder stringBuilder = new StringBuilder(extended);
22+
2123
if (typeof(T).Name == typeof(List<>).Name)
2224
{
2325
var m = (IEnumerable<object>)answer;
@@ -41,10 +43,15 @@ public double GetCompressionRatio()
4143
return compressionRatio;
4244
}
4345

44-
public EncodedMessage(T answer, double compressionRatio)
46+
public EncodedMessage(T answer, double compressionRatio) : this(answer, compressionRatio, string.Empty)
47+
{
48+
}
49+
50+
public EncodedMessage(T answer, double compressionRatio, string extended)
4551
{
4652
this.answer = answer;
4753
this.compressionRatio = compressionRatio;
54+
this.extended = extended;
4855
}
4956
}
5057
}

AlgorithmsLibrary/Results/EncodedMessageCollection.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
internal class EncodedMessage<T1, T2> : IAlgmEncoded<T1, T2>
44
{
5+
private string extended;
56
private T1 answer;
67
private T2 data;
78
private double compressionRatio;
@@ -30,11 +31,15 @@ public double GetCompressionRatio()
3031
return compressionRatio;
3132
}
3233

33-
public EncodedMessage(T1 answer, T2 frequencies, double compressionRatio)
34+
public EncodedMessage(T1 answer, T2 frequencies, double compressionRatio) : this(answer, frequencies, compressionRatio, string.Empty)
35+
{
36+
}
37+
public EncodedMessage(T1 answer, T2 frequencies, double compressionRatio, string extended)
3438
{
3539
this.answer = answer;
3640
this.data = frequencies;
3741
this.compressionRatio = compressionRatio;
42+
this.extended = extended;
3843
}
3944
}
4045
}

0 commit comments

Comments
 (0)