Commit 076d8ab2 authored by Nathan/Eilisha Shiraini's avatar Nathan/Eilisha Shiraini
Browse files

Garbage correction has been fully corrected

parent 27428cd7
......@@ -9,6 +9,7 @@
#include <cstdint>
#include <string>
#include <map>
#include <algorithm>
static inline std::list<FixLenInteger> buildFromBinary(std::string data) {
std::list<FixLenInteger> result;
......@@ -254,4 +255,11 @@ void EncodingPath::buildLengthTable(std::map<const EncodingPath*, uint64_t>& tab
for (auto const& child : this->children) {
child->buildLengthTable(table, currentLength);
}
}
void EncodingPath::forgetChild(const EncodingPath* child) {
auto it = std::find(this->children.begin(), this->children.end(), child);
if (it != this->children.end()) {
this->children.erase(it);
}
}
\ No newline at end of file
......@@ -13,7 +13,7 @@ class EncodingPath {
public:
const Mode currentMode;
const EncodingPath * const parent;
EncodingPath * const parent;
EncodingPath();
EncodingPath(Mode mode);
......@@ -48,6 +48,7 @@ class EncodingPath {
void buildLengthTable(std::map<const EncodingPath*, uint64_t>& table) const;
private:
void forgetChild(const EncodingPath*);
void buildLengthTable(std::map<const EncodingPath*, uint64_t>& table, uint64_t currentLength) const;
std::list<FixLenInteger> currentElements;
......
......@@ -52,12 +52,10 @@ cdef class Encoder:
return result
cdef replaceLeaf(self, EncodingPath* oldLeaf, EncodingPath* newLeaf):
it = find(self.leaves.begin(), self.leaves.end(), oldLeaf)
if it != self.leaves.end():
self.leaves.erase(it)
self.leaves.remove(oldLeaf)
self.leaves.push_back(newLeaf)
cdef removeLeaf(self, EncodingPath* leaf):
cdef removeLeaf(self, const EncodingPath* leaf):
it = find(self.leaves.begin(), self.leaves.end(), leaf)
if it != self.leaves.end():
self.leaves.erase(it)
......@@ -65,13 +63,10 @@ cdef class Encoder:
cdef void addData(self, string data):
cdef uint64_t datasize = data.size()
cdef char nextchar
cdef uint16_t skip = 0
cdef uint8_t paircode
cdef uint16_t i, bincount
for i in range(datasize):
if skip > 0:
skip -= 1
continue
i = 0
while i < datasize:
nextchar = data.at(i + 1) if i + 1 < datasize else 0
paircode = {
b"\r\n" : 2,
......@@ -82,14 +77,15 @@ cdef class Encoder:
if paircode > 0:
self.addPair(paircode)
skip += 1
i += 1
elif <uint8_t>data.at(i) < 128:
self.addChar(data[i])
else:
bincount = self.countNextBinary(data.substr(i))
self.addBinary(data.substr(i, bincount))
skip += bincount - 1
i += bincount - 1
self.cleanup()
i += 1
cdef string getOutputString(self):
cdef list[FixLenInteger] words
......@@ -108,27 +104,26 @@ cdef class Encoder:
return bits
cdef const EncodingPath* shortestPath(self):
cdef map[const EncodingPath*, uint64_t] lengthtable
cdef uint64_t length
cdef const EncodingPath* shortest = NULL
cdef uint64_t shortestlength = numeric_limits[uint64_t].max()
dereference(self.root).buildLengthTable(lengthtable)
for it in lengthtable:
if it.second < shortestlength:
shortest = it.first
shortestlength = it.second
for leaf in self.leaves:
length = dereference(leaf).getChainLength()
if length < shortestlength:
shortest = leaf
shortestlength = length
return shortest
cdef void cleanup(self):
cdef map[const EncodingPath*, uint64_t] lengthtable
cdef uint64_t length
cdef uint64_t shortestlength = numeric_limits[uint64_t].max()
dereference(self.root).buildLengthTable(lengthtable)
for it in lengthtable:
if it.second < shortestlength:
shortestlength = it.second
for it in lengthtable:
if it.second > shortestlength:
self.removeLeaf(it.first)
del it.first
for leaf in self.leaves:
length = dereference(leaf).getChainLength()
if length < shortestlength:
shortestlength = length
for leaf in self.leaves:
if dereference(leaf).getChainLength() > shortestlength:
self.removeLeaf(leaf)
cdef uint16_t countNextBinary(self, string data):
cdef uint16_t i = 0
......@@ -140,7 +135,8 @@ cdef class Encoder:
cdef list[EncodingPath*] oldleaves = self.leaves
for ptr in oldleaves:
if dereference(ptr).getMode() == Mode.PUNCT:
dereference(ptr).appendPair(paircode)
nptr = dereference(ptr).appendPair(paircode)
self.replaceLeaf(ptr, nptr)
else:
lptr = dereference(ptr).latchAndAppendPair(paircode)
self.replaceLeaf(ptr, lptr)
......@@ -153,7 +149,8 @@ cdef class Encoder:
cdef list[EncodingPath*] oldleaves = self.leaves
for ptr in oldleaves:
if (charInMode(dereference(ptr).getMode(), character)):
dereference(ptr).append(character)
nptr = dereference(ptr).append(character)
self.replaceLeaf(ptr, nptr)
else:
for value in charvalues:
lptr = dereference(ptr).latchAndAppend(value.first, value.second)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment