Skip to content

Commit 3a895bb

Browse files
committed
It is customary to store characters in char : the size of records is limited, it has become better to compress
1 parent d6c00b8 commit 3a895bb

1 file changed

Lines changed: 16 additions & 28 deletions

File tree

Homework3/LZW/LZW/LZW.cs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,17 @@ public void CompressFile(string pathToFile, string path)
3838
}
3939
else
4040
{
41-
string number = bor.FindIndex(currentString).ToString();
4241
using (StreamWriter writer = new StreamWriter(pathToFile, true))
4342
{
44-
for (int k = 0; k < number.Length; k++)
45-
{
46-
writer.Write(number[k]);
47-
}
48-
writer.Write(' ');
43+
writer.Write((char)bor.FindIndex(currentString));
4944
}
5045
return;
5146
}
5247
}
5348
i = newCounter - 1;
54-
string num = bor.Add(currentString).ToString();
5549
using (StreamWriter writer = new StreamWriter(pathToFile, true))
5650
{
57-
for (int k = 0; k < num.Length; k++)
58-
{
59-
writer.Write(num[k]);
60-
}
61-
writer.Write(' ');
51+
writer.Write((char)bor.Add(currentString));
6252
}
6353
}
6454
}
@@ -68,45 +58,43 @@ public void UncompressFile(string pathToFile, string path)
6858
FileStream fs = new FileStream(pathToFile, FileMode.Create);
6959
fs.Close();
7060
string stringToConvert = File.ReadAllText(path);
71-
int[] nums = stringToConvert.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
72-
var dictionary = new Dictionary<int, string>();
61+
var dictionary = new Dictionary<char, string>();
7362

7463
for (int i = 0; i < 256; i++)
7564
{
76-
dictionary.Add(i, "" + (char)i);
65+
dictionary.Add((char)i, "" + (char)i);
7766
}
7867

79-
for (int i = 0, j = 256; i < nums.Length; i++, j++)
68+
for (int i = 0, j = 256; i < stringToConvert.Length; i++, j++)
8069
{
81-
string? stringForAdd;
82-
dictionary.TryGetValue(nums[i], out stringForAdd);
83-
if (stringForAdd == null)
84-
{
85-
return;
86-
}
70+
string stringForAdd = dictionary[stringToConvert[i]];
8771
while (dictionary.ContainsValue(stringForAdd))
8872
{
89-
if (i == nums.Length - 1)
73+
if (i == stringToConvert.Length - 1)
9074
{
9175
break;
9276
}
9377
string? newString;
94-
if (!dictionary.TryGetValue(nums[i + 1], out newString))
78+
if (!dictionary.TryGetValue(stringToConvert[i + 1], out newString))
9579
{
9680
stringForAdd += stringForAdd[0];
9781
break;
9882
}
9983
stringForAdd += newString[0];
10084
}
101-
dictionary.Add(j, stringForAdd);
85+
dictionary.Add((char)j, stringForAdd);
86+
if (j == 65536)
87+
{
88+
return;
89+
}
10290
}
10391

10492
using (StreamWriter writer = new StreamWriter(pathToFile, true))
10593
{
106-
for (int i = 0; i < nums.Length; i++)
94+
for (int i = 0; i < stringToConvert.Length; i++)
10795
{
10896
string? newString = "";
109-
dictionary.TryGetValue(nums[i], out newString);
97+
dictionary.TryGetValue(stringToConvert[i], out newString);
11098
writer.Write(newString);
11199
}
112100
}
@@ -139,4 +127,4 @@ static void Main(string[] args)
139127
ob.UncompressFile(fileName, pathToFile);
140128
}
141129
}
142-
}
130+
}

0 commit comments

Comments
 (0)