-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
158 lines (137 loc) · 3.15 KB
/
util.cpp
File metadata and controls
158 lines (137 loc) · 3.15 KB
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
#include "util.h"
#include <filesystem>
void error(std::string message)
{
std::cerr << "ERROR: " << message << std::endl;
exit(1);
}
IStreamWrapper::IStreamWrapper(std::string filename) : filename(filename),
readfile(std::ifstream(filename, std::ios::binary)), buf(0), buf_index(8) {}
bool IStreamWrapper::good() const
{
return readfile.good();
}
long unsigned int IStreamWrapper::size() const
{
return std::filesystem::file_size(filename);
}
void IStreamWrapper::reset()
{
readfile.clear(); //clear EOF
readfile.seekg(0); //reset file reading
buf = 0; //clear buffer
buf_index = 8; //reset bitwise buffer index
}
unsigned long IStreamWrapper::readLong()
{
if (buf_index != 8)
error("Active bit buffer");
unsigned long num;
readfile.read((char*)&num, sizeof(num));
if (readfile.eof())
error("Not enough bytes to read long from file");
return num;
}
unsigned int IStreamWrapper::readInt()
{
if (buf_index != 8)
error("Active bit buffer");
unsigned int num;
readfile.read((char*)&num, sizeof(num));
if (readfile.eof())
error("Not enough bytes to read int from file");
return num;
}
int IStreamWrapper::readByte()
{
return readfile.get();
}
unsigned char IStreamWrapper::readBit()
{
if (buf_index == 8)
{
unsigned char temp = readByte();
//if there are no more bytes to read,
//return char(-1)
if (temp == -1)
return -1;
buf = temp;
buf_index = 0;
}
//return current bit
return (buf >> (7 - buf_index++)) & 1;
}
OStreamWrapper::OStreamWrapper(std::string filename) :
writefile(std::ofstream(filename, std::ios::binary)), buf(0), buf_index(0) {}
bool OStreamWrapper::good() const
{
return writefile.good();
}
void OStreamWrapper::writeLong(unsigned long l)
{
if (buf_index)
error("Active bit buffer");
writefile.write((char*)&l, sizeof(l));
}
void OStreamWrapper::writeInt(unsigned int n)
{
if (buf_index)
error("Active bit buffer");
writefile.write((char*)&n, sizeof(n));
}
void OStreamWrapper::writeByte(unsigned char b)
{
if (buf_index)
error("Active bit buffer");
else
writefile.put(b);
}
void OStreamWrapper::writeBit(unsigned char b)
{
if (!(b == 0 || b == 1))
error("Invalid bit");
buf |= (b << (7-buf_index++));
//write 8 bits (1 byte) at a time
if (buf_index == 8)
flush_bitwise();
}
void OStreamWrapper::flush_bitwise()
{
if (buf_index)
{
writefile.put(buf);
buf = 0;
buf_index = 0;
}
}
void OStreamWrapper::flush()
{
flush_bitwise();
writefile.flush();
}
OStreamWrapper::~OStreamWrapper()
{
flush();
}
Node::Node(unsigned long int count, unsigned char symbol)
{
this->count = count;
this->symbol = symbol;
this->l = nullptr;
this->r = nullptr;
this->p = nullptr;
}
bool Node::operator<(const Node& other)
{
//compare counts
if (count != other.count)
{
return count > other.count;
}
//compare symbols
return symbol > other.symbol;
}
bool NodeComp::operator()(Node*& lhs, Node*& rhs)
{
return *lhs < *rhs;
}