-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompressor.cpp
178 lines (141 loc) · 4.67 KB
/
Compressor.cpp
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
#include "HashTable.h"
#include "provided.h"
#include <iostream>
#include <string>
using namespace std;
unsigned int computeHash(string key)
{
unsigned int h = 2166136261U; //code provided by Professor Smallberg for efficient
for (int k = 0; k < key.size(); k++) //usigned ints from strings
{
h += key[k];
h *= 16777619;
}
return h;
}
unsigned int computeHash(unsigned short key)
{
unsigned int h = key;
return h;
}
void Compressor::compress(const std::string& s, std::vector<unsigned short>& numbers)
{
numbers.clear(); //numbers vector emptied out
unsigned int a = s.size()/2 + 512;
unsigned int b = 16384;
unsigned int cap;
if (a <= b)
cap = a;
else
cap = b;
HashTable<string, unsigned short> H(2 * cap, cap); //buckets size is twice that of the capacity
//this sets the load factor at 0.5
for (int j = 0; j < 256; j++)
{
string temp(1, static_cast<char>(j));
H.set(temp, j, true);
}
int nextFreeID = 256;
string runSoFar;
vector<unsigned short> V;
unsigned short x;
for (int k = 0; k < s.size(); k++)
{
string expandedRun = runSoFar + s[k];
if (H.get(expandedRun, x)) //checks to see if expandedRun is already contained in HashTable
{
runSoFar = expandedRun;
continue; //if so next character of the string is processed
}
H.get(runSoFar, x);
V.push_back(x);
H.touch(runSoFar); //runSoFar string is moved to the top of the recently-updated list
runSoFar = "";
string currentC (1, s[k]);
unsigned short cv;
H.get(currentC, cv);
V.push_back(cv);
if (!H.isFull()) //if there is an open space in the HashTable, then expandedRun is inserted
{ //with the next available ID number
H.set(expandedRun, nextFreeID);
nextFreeID++;
}
else //otherwise the least recently updated spot is discard, and new data is inserted
{ //with that same ID number
string dummy;
unsigned short discardSpot;
H.discard(dummy, discardSpot);
H.set(expandedRun, discardSpot);
}
}
if (runSoFar != "")
{
unsigned short x;
H.get(runSoFar, x);
V.push_back(x);
}
V.push_back(cap); //the capacity is pushed onto the end of the numbers vector
numbers = V;
}
bool Compressor::decompress(const vector<unsigned short>& numbers, string& s)
{
if (numbers.empty()) //ensures there are numbers to actually decompress
{
return false;
}
unsigned int cap = numbers.back();
HashTable<unsigned short, string> H(2 * cap, cap);
for (int j = 0; j < 256; j++)
{
string temp(1, static_cast<char>(j));
H.set(j, temp, true);
}
int nextFreeID = 256;
string runSoFar;
string output;
for (int n = 0 ; n < numbers.size() - 1; n++)
{
unsigned short us = numbers[n];
if (us <= 255) //if us <= 255, the string is a 1 digit character
{
string temp;
H.get(us, temp); //since all one digit chars exist in H, that char is found and appended
output += temp;
if (runSoFar == "")
{
runSoFar = temp;
continue;
}
string expandedRun = runSoFar + temp;
if (!H.isFull()) //if the Hashtable is not full, the nextFreeID key can be associated
{ //without any discarding
string x;
H.set(nextFreeID, expandedRun);
nextFreeID++;
}
else //otherwise a discard must be made to create space for new associations
{
string y;
unsigned short discardSpot;
H.discard(discardSpot, y);
H.set(discardSpot, expandedRun);
}
runSoFar = "";
continue;
}
else
{
string s;
if (!H.touch(us))
{
return false;
}
H.touch(us);
H.get(us, s);
output += s;
runSoFar = s;
}
}
s = output; //the string s is finally set to the output string
return true;
}