Some encode/decode practice.

This commit is contained in:
James Grogan 2022-11-21 17:45:12 +00:00
parent 8a41337e2d
commit ff962a6b16
29 changed files with 727 additions and 305 deletions

View file

@ -25,6 +25,46 @@ public:
return word & ByteUtils::WORD_LAST_BYTE;
}
static unsigned char getHigherNBits(unsigned char input, unsigned num)
{
return input >> 8 - num;
}
static unsigned char getLowerNBits(unsigned char input, unsigned num)
{
switch (num)
{
case 1:
return input & 0x01;
case 2:
return input & 0x03;
case 3:
return input & 0x07;
case 4:
return input & 0x0F;
case 5:
return input & 0x1F;
case 6:
return input & 0x3F;
case 7:
return input & 0x7F;
case 8:
return input;
default:
return 0;
}
}
static unsigned char getTwoBitsAtN(unsigned char input, unsigned n)
{
return (input & (0x03 << n)) >> n;
}
static unsigned char getBitN(unsigned char input, unsigned n)
{
return input & (1 << n);
}
static void ReverseBuffer(char* buffer, char* reverse, unsigned size, unsigned targetSize)
{
for(unsigned idx=0; idx<targetSize; idx++)

View file

@ -18,6 +18,7 @@ list(APPEND core_LIB_INCLUDES
Dictionary.cpp
Color.cpp
CommandLineArgs.cpp
data_structures/RawTree.cpp
data_structures/Tree.cpp
loggers/FileLogger.cpp
file_utilities/Directory.cpp

View file

@ -20,6 +20,11 @@ bool StringUtils::IsSpace(char c)
return std::isspace(c, loc);
}
bool StringUtils::IsAlphabetical(char c)
{
return std::isalpha(c);
}
std::vector<std::string> StringUtils::toLines(const std::string& input)
{
auto result = std::vector<std::string>{};

View file

@ -17,6 +17,9 @@ public:
static constexpr char COLON = ':';
static bool IsAlphaNumeric(char c);
static bool IsAlphabetical(char c);
static bool IsSpace(char c);
static std::string ToLower(const std::string& s);
static std::string convert(const std::wstring& input);

View file

View file

@ -0,0 +1,97 @@
#pragma once
#include <memory>
template<typename T>
class RawNode
{
public:
RawNode(T data)
: mData(data)
{
}
~RawNode()
{
if(mLeftChild)
{
delete mLeftChild;
}
if(mRightChild)
{
delete mRightChild;
}
}
void addChild(RawNode* child)
{
if(!mLeftChild)
{
mLeftChild = child;
}
else
{
mRightChild = child;
}
}
bool isLeaf() const
{
return !mLeftChild && !mRightChild;
}
T getData() const
{
return mData;
}
RawNode* getLeftChild() const
{
return mLeftChild;
}
RawNode* getRightChild() const
{
return mRightChild;
}
private:
T mData;
unsigned char mTag{0};
RawNode* mLeftChild{nullptr};
RawNode* mRightChild{nullptr};
};
template<typename T>
class RawTree
{
public:
RawTree()
{
}
~RawTree()
{
if (mRootNode)
{
delete mRootNode;
}
}
void addRootNode(RawNode<T>* root)
{
mRootNode = root;
}
RawNode<T>* getRootNode() const
{
return mRootNode;
}
private:
RawNode<T>* mRootNode{nullptr};
};

View file

@ -6,37 +6,47 @@ template<typename T>
class Node
{
public:
Node(T data)
: mData(data)
{}
Node(T data)
: mData(data)
{}
void addChild(std::unique_ptr<Node> child)
{
if(!mLeftChild)
{
mLeftChild = std::move(child);
}
else
{
mRightChild = std::move(child);
}
}
void addChild(std::unique_ptr<Node> child)
{
if(!mLeftChild)
{
mLeftChild = std::move(child);
}
else
{
mRightChild = std::move(child);
}
}
bool isLeaf() const
{
return !mLeftChild && !mRightChild;
}
bool isLeaf() const
{
return !mLeftChild && !mRightChild;
}
T getData() const
{
return mData;
}
T getData() const
{
return mData;
}
Node* getLeftChild() const
{
return mLeftChild.get();
}
Node* getRightChild() const
{
return mRightChild.get();
}
private:
T mData;
unsigned char mTag{0};
std::unique_ptr<Node> mLeftChild;
std::unique_ptr<Node> mRightChild;
T mData;
unsigned char mTag{0};
std::unique_ptr<Node> mLeftChild;
std::unique_ptr<Node> mRightChild;
};
template<typename T>
@ -46,21 +56,21 @@ template<typename T>
class Tree
{
public:
Tree()
{
Tree()
{
}
}
void addRootNode(NodePtr<T> root)
{
mRootNode = std::move(root);
}
void addRootNode(NodePtr<T> root)
{
mRootNode = std::move(root);
}
Node<T>* getRootNode() const
{
return mRootNode.get();
}
Node<T>* getRootNode() const
{
return mRootNode.get();
}
private:
NodePtr<T> mRootNode;
NodePtr<T> mRootNode;
};

View file

@ -32,6 +32,11 @@ std::ofstream* File::GetOutHandle() const
return mOutHandle.get();
}
unsigned char File::readNextByte()
{
return mInHandle->get();
}
void File::Open(bool asBinary)
{
if(mAccessMode == AccessMode::Read)

View file

@ -48,6 +48,8 @@ public:
void Close();
unsigned char readNextByte();
private: