9
9
10
10
namespace HuffmanCompression
11
11
{
12
-
13
12
public class Compressor
14
13
{
15
14
public static readonly string CompressedFileExtension = ".comp" ;
@@ -74,26 +73,7 @@ public static string GetCompressionSummary(long originalFileSize, long compresse
74
73
return $ "\n original size: { originalFileSize } bytes.\n Compressed size: { compressedFileSize } bytes.\n Reduced by { percentage } %";
75
74
}
76
75
77
- // Only for debugging purposes.
78
- static void WriteCompressionInfo ( string beforeWritingToBytes , byte [ ] afterWritingToBytes , Dictionary < char , string > dict )
79
- {
80
- Console . WriteLine ( "Dictionary:" ) ;
81
- int j = 0 ;
82
- foreach ( var pair in dict )
83
- Console . WriteLine ( ++ j + " { '" + ( pair . Key == '\n ' ? "[new line]" : "" + pair . Key ) + "', \" " + pair . Value + "\" }," ) ;
84
-
85
- Console . Write ( "Bits before writing to bytes: " ) ;
86
- IOUtils . PrintFirst48Chars ( beforeWritingToBytes ) ;
87
-
88
- Console . Write ( "\n Bits after writing to bytes: " ) ;
89
- IOUtils . PrintFirst48Bits ( afterWritingToBytes ) ;
90
-
91
- Console . Write ( $ "\n Amount of redundant zeros: { afterWritingToBytes . Length * 8 - beforeWritingToBytes . Length } ") ;
92
-
93
- Console . Write ( "\n \n \n " ) ;
94
- }
95
-
96
- static bool CodesAreTooLong ( Dictionary < char , string > codes )
76
+ private static bool CodesAreTooLong ( Dictionary < char , string > codes )
97
77
{
98
78
foreach ( var pair in codes )
99
79
if ( pair . Value . Length > MaxCodeLength )
@@ -102,7 +82,7 @@ static bool CodesAreTooLong(Dictionary<char, string> codes)
102
82
}
103
83
104
84
// [1][length][as encoded bits][code] OR [0][code]
105
- static string AddTreeInfoAndReplaceCharsWithCode ( string text , Dictionary < char , string > dict )
85
+ private static string AddTreeInfoAndReplaceCharsWithCode ( string text , Dictionary < char , string > dict )
106
86
{
107
87
var textCopy = new StringBuilder ( text ) ;
108
88
var usedChars = new List < char > ( ) ;
@@ -140,7 +120,7 @@ static string AddTreeInfoAndReplaceCharsWithCode(string text, Dictionary<char, s
140
120
return textCopy . ToString ( ) ;
141
121
}
142
122
143
- static string CorrectCodeLength ( string codeLength )
123
+ private static string CorrectCodeLength ( string codeLength )
144
124
{
145
125
int difference = AmountOfBitsToRepresentCodeLength - codeLength . Length ;
146
126
@@ -151,7 +131,7 @@ static string CorrectCodeLength(string codeLength)
151
131
return codeLength ;
152
132
}
153
133
154
- static byte EncodeCharacter ( char character )
134
+ private static byte EncodeCharacter ( char character )
155
135
{
156
136
var bytes = TextEncoding . GetBytes ( new char [ ] { character } ) ;
157
137
@@ -162,15 +142,15 @@ static byte EncodeCharacter(char character)
162
142
}
163
143
164
144
// Character | the character's representation code.
165
- static Dictionary < char , string > CreateCodesDictionary ( ( int occurence , char character ) [ ] occurences )
145
+ private static Dictionary < char , string > CreateCodesDictionary ( ( int occurence , char character ) [ ] occurences )
166
146
{
167
147
var codes = new Dictionary < char , string > ( ) ;
168
148
BoxCharacters ( occurences ) . ForEach ( x => codes . Add ( x . Character , FindCharacterCode ( x ) ) ) ;
169
149
170
150
return codes ;
171
151
}
172
152
173
- static string FindCharacterCode ( CharacterBox box )
153
+ private static string FindCharacterCode ( CharacterBox box )
174
154
{
175
155
string code = "" ;
176
156
@@ -182,8 +162,8 @@ static string FindCharacterCode(CharacterBox box)
182
162
return code . Reverse ( ) ;
183
163
}
184
164
185
- // Correct.
186
- static List < CharacterBox > BoxCharacters ( ( int occurence , char character ) [ ] occurences )
165
+ // Correct.
166
+ private static List < CharacterBox > BoxCharacters ( ( int occurence , char character ) [ ] occurences )
187
167
{
188
168
var CharacterList = new List < CharacterBox > ( ) ;
189
169
var tree = new List < Box > ( ) ;
@@ -212,8 +192,8 @@ static List<CharacterBox> BoxCharacters((int occurence, char character)[] occure
212
192
return CharacterList ;
213
193
}
214
194
215
- // Correct.
216
- static void InsertAtAppropriatePlace ( List < Box > tree , Box boxToInsert )
195
+ // Correct.
196
+ private static void InsertAtAppropriatePlace ( List < Box > tree , Box boxToInsert )
217
197
{
218
198
if ( tree . Count == 0 || boxToInsert . Sum > tree . Last ( ) . Sum || ( boxToInsert . Sum == tree . Last ( ) . Sum && tree . Last ( ) . Depth < boxToInsert . Depth ) )
219
199
{
@@ -235,9 +215,8 @@ static void InsertAtAppropriatePlace(List<Box> tree, Box boxToInsert)
235
215
throw new Exception ( $ "Could not insert box with sum { boxToInsert . Sum } to a tree with count { tree . Count } and highest sum { tree . Last ( ) . Sum } .") ;
236
216
}
237
217
238
-
239
- // Correct.
240
- static ( int occurence , char character ) [ ] GetCharactersOccurence ( string text )
218
+ // Correct.
219
+ private static ( int occurence , char character ) [ ] GetCharactersOccurence ( string text )
241
220
{
242
221
var set = new HashSet < ( int occurence , char character ) > ( ) ;
243
222
foreach ( char ch in text )
0 commit comments