Format source files

This commit is contained in:
2023-02-18 10:37:38 +01:00
parent 76d93bb1cf
commit d09d4d3d92
12 changed files with 793 additions and 850 deletions

View File

@@ -4,7 +4,7 @@ CFLAGS = -std=c++17 -Wall -g -Wextra
CPPS = $(wildcard test/*.cpp) CPPS = $(wildcard test/*.cpp)
EXES = $(patsubst %.cpp, %.out, $(CPPS)) EXES = $(patsubst %.cpp, %.out, $(CPPS))
.PHONY: all clean rebuild check .PHONY: all clean rebuild check format
all: $(EXES) all: $(EXES)
@@ -19,5 +19,11 @@ EXESQUOTED = $(patsubst %, "%", $(EXES))
check: all check: all
./execute-all.sh ./execute-all.sh
format:
find src/ -name *.cpp -exec clang-format -i {} \;
find src/ -name *.hpp -exec clang-format -i {} \;
find test/ -name *.cpp -exec clang-format -i {} \;
find test/ -name *.hpp -exec clang-format -i {} \;
%.out: %.cpp %.out: %.cpp
$(CC) $(CFLAGS) $< -o $@ $(CC) $(CFLAGS) $< -o $@

View File

@@ -5,27 +5,21 @@
namespace List namespace List
{ {
template<class T> template<class T> struct Node
struct Node {
{
T value; T value;
std::unique_ptr<Node<T>> next; std::unique_ptr<Node<T>> next;
Node(T const & _value) Node(T const & _value) : value(_value), next(nullptr) { }
: value(_value), };
next(nullptr)
{}
};
template<class T> template<class T> class List {
class List private:
{
private:
std::unique_ptr<Node<T>> root; std::unique_ptr<Node<T>> root;
Node<T> * tailPtr; Node<T> * tailPtr;
std::size_t size; std::size_t size;
public: public:
void Append(T const & value) void Append(T const & value)
{ {
if(size == 0) if(size == 0)
@@ -177,15 +171,8 @@ public:
return currentNode->value; return currentNode->value;
} }
std::size_t GetSize() const std::size_t GetSize() const { return size; }
{
return size;
}
List() List() : root(nullptr), tailPtr(nullptr), size(0) { }
: root(nullptr), };
tailPtr(nullptr),
size(0)
{}
};
} }

View File

@@ -1,9 +1,7 @@
#pragma once #pragma once
#include "vector.hpp" #include "vector.hpp"
template<class T> template<class T> class RingBuffer {
class RingBuffer
{
private: private:
Vector<T> data; Vector<T> data;
std::size_t head, tail; std::size_t head, tail;
@@ -61,16 +59,7 @@ public:
} }
} }
bool IsEmpty() const bool IsEmpty() const { return head == tail; }
{
return head == tail;
}
RingBuffer(std::size_t const size) RingBuffer(std::size_t const size) : data(), head(0), tail(0) { data.Resize(size + 1ul); }
: data(),
head(0),
tail(0)
{
data.Resize(size + 1ul);
}
}; };

View File

@@ -1,9 +1,7 @@
#pragma once #pragma once
#include "vector.hpp" #include "vector.hpp"
template<class T> template<class T> class Stack {
class Stack
{
private: private:
Vector<T> data; Vector<T> data;
std::size_t actualSize; std::size_t actualSize;
@@ -34,13 +32,7 @@ public:
return retval; return retval;
} }
std::size_t GetSize() const std::size_t GetSize() const { return actualSize; }
{
return actualSize;
}
Stack() Stack() : actualSize(0) { }
: actualSize(0)
{
}
}; };

View File

@@ -2,9 +2,7 @@
#include <cstdlib> #include <cstdlib>
#include <stdexcept> #include <stdexcept>
template<class T> template<class T> class Vector {
class Vector
{
private: private:
T * data; T * data;
std::size_t reserveSize; std::size_t reserveSize;
@@ -38,7 +36,7 @@ private:
public: public:
void Resize(std::size_t const newSize) void Resize(std::size_t const newSize)
{ {
if (newSize > reserveSize) if(newSize > reserveSize)
{ {
Allocate(newSize); Allocate(newSize);
size = newSize; size = newSize;
@@ -68,19 +66,9 @@ public:
return data[index]; return data[index];
} }
std::size_t GetSize() const std::size_t GetSize() const { return size; }
{
return size;
}
std::size_t GetReserveSize() const std::size_t GetReserveSize() const { return reserveSize; }
{
return reserveSize;
}
Vector() Vector() : data(nullptr), reserveSize(0ul), size(0ul) { }
: data(nullptr),
reserveSize(0ul),
size(0ul)
{}
}; };

View File

@@ -3,42 +3,28 @@
namespace BinaryTree namespace BinaryTree
{ {
template <class T> template<class T> struct Node
struct Node {
{
T value; T value;
std::unique_ptr<Node<T>> left; std::unique_ptr<Node<T>> left;
std::unique_ptr<Node<T>> right; std::unique_ptr<Node<T>> right;
bool HasChildren() const bool HasChildren() const { return static_cast<bool>(left) || static_cast<bool>(right); }
{
return static_cast<bool>(left) || static_cast<bool>(right);
}
bool IsLeaf() const bool IsLeaf() const { return !HasChildren(); }
{
return !HasChildren();
}
Node(T const &_value) Node(T const & _value) : value(_value), left(nullptr), right(nullptr) { }
: value(_value), };
left(nullptr),
right(nullptr)
{
}
};
template <class T> template<class T> class Tree {
class Tree private:
{
private:
std::unique_ptr<Node<T>> root; std::unique_ptr<Node<T>> root;
T ExtractSmallestLeaf(std::unique_ptr<Node<T>> &node) T ExtractSmallestLeaf(std::unique_ptr<Node<T>> & node)
{ {
std::unique_ptr<Node<T>> *nodePtr = &(node); std::unique_ptr<Node<T>> * nodePtr = &(node);
while ((*nodePtr)->HasChildren()) while((*nodePtr)->HasChildren())
{ {
if((*nodePtr)->left) if((*nodePtr)->left)
{ {
@@ -85,10 +71,10 @@ private:
std::unique_ptr<Node<T>> & FindNode(T const & value) std::unique_ptr<Node<T>> & FindNode(T const & value)
{ {
std::unique_ptr<Node<T>> *currentPtr = &root; std::unique_ptr<Node<T>> * currentPtr = &root;
while (*currentPtr && (*currentPtr)->value != value) while(*currentPtr && (*currentPtr)->value != value)
{ {
if ((*currentPtr)->value > value) if((*currentPtr)->value > value)
{ {
currentPtr = &((*currentPtr)->left); currentPtr = &((*currentPtr)->left);
} }
@@ -100,13 +86,13 @@ private:
return *currentPtr; return *currentPtr;
} }
public: public:
void Insert(T const & value) void Insert(T const & value)
{ {
std::unique_ptr<Node<T>> *currentPtr = &root; std::unique_ptr<Node<T>> * currentPtr = &root;
while (*currentPtr) while(*currentPtr)
{ {
if ((*currentPtr)->value > value) if((*currentPtr)->value > value)
{ {
currentPtr = &((*currentPtr)->left); currentPtr = &((*currentPtr)->left);
} }
@@ -129,10 +115,7 @@ public:
toInsertIn = std::make_unique<Node<T>>(value); toInsertIn = std::make_unique<Node<T>>(value);
} }
bool Contains(T const & value) bool Contains(T const & value) { return static_cast<bool>(FindNode(value)); }
{
return static_cast<bool>(FindNode(value));
}
void Delete(T const & value) void Delete(T const & value)
{ {
@@ -140,8 +123,6 @@ public:
DeleteNode(node); DeleteNode(node);
} }
Tree() Tree() : root(nullptr) { }
: root(nullptr) };
{}
};
} // namespace BinaryTree } // namespace BinaryTree

View File

@@ -1,9 +1,10 @@
#include "../src/tree/binarytree.hpp" #include "../src/tree/binarytree.hpp"
#include "testutil.hpp" #include "testutil.hpp"
#include <vector>
#include <unordered_set> #include <unordered_set>
#include <vector>
void FillWithUniqueRandomNumbers(BinaryTree::Tree<unsigned> & tree, void FillWithUniqueRandomNumbers(
BinaryTree::Tree<unsigned> & tree,
std::unordered_set<unsigned> & control, std::unordered_set<unsigned> & control,
unsigned const count = 10000u) unsigned const count = 10000u)
{ {
@@ -29,7 +30,7 @@ bool TestInsert()
tree.Insert(value); tree.Insert(value);
} }
for(auto const & num : control) for(auto const & num: control)
{ {
if(!tree.Contains(num)) if(!tree.Contains(num))
{ {
@@ -48,7 +49,7 @@ bool TestInsertNoDuplicates()
FillWithUniqueRandomNumbers(tree, control); FillWithUniqueRandomNumbers(tree, control);
for(auto const & num : control) for(auto const & num: control)
{ {
if(!tree.Contains(num)) if(!tree.Contains(num))
{ {
@@ -82,7 +83,7 @@ bool TestDeletion()
} }
} }
for(auto const & num : control) for(auto const & num: control)
{ {
if(!tree.Contains(num)) if(!tree.Contains(num))
{ {
@@ -92,7 +93,7 @@ bool TestDeletion()
} }
} }
for(auto const & num : deletedValues) for(auto const & num: deletedValues)
{ {
if(tree.Contains(num)) if(tree.Contains(num))
{ {
@@ -110,18 +111,18 @@ bool TestDeletionCase(
std::vector<unsigned> const result) std::vector<unsigned> const result)
{ {
BinaryTree::Tree<unsigned> tree; BinaryTree::Tree<unsigned> tree;
for(auto n : toInsert) for(auto n: toInsert)
{ {
tree.Insert(n); tree.Insert(n);
} }
for(auto n : toDelete) for(auto n: toDelete)
{ {
tree.Delete(n); tree.Delete(n);
} }
bool ok = true; bool ok = true;
for(auto n : result) for(auto n: result)
{ {
if(!tree.Contains(n)) if(!tree.Contains(n))
{ {
@@ -130,7 +131,7 @@ bool TestDeletionCase(
} }
} }
for(auto n : toDelete) for(auto n: toDelete)
{ {
if(tree.Contains(n)) if(tree.Contains(n))
{ {
@@ -146,27 +147,27 @@ bool TestDeletionCases()
{ {
std::puts("[INFO] Testing deleting root with single child right."); std::puts("[INFO] Testing deleting root with single child right.");
TestDeletionCase( TestDeletionCase(
std::vector<unsigned> { 10, 12, 11, 13}, std::vector<unsigned> {10, 12, 11, 13},
std::vector<unsigned> { 10 }, std::vector<unsigned> {10},
std::vector<unsigned> { 12, 11, 13 }); std::vector<unsigned> {12, 11, 13});
std::puts("[INFO] Testing deleting root with single child left."); std::puts("[INFO] Testing deleting root with single child left.");
TestDeletionCase( TestDeletionCase(
std::vector<unsigned> { 15, 12, 11, 13}, std::vector<unsigned> {15, 12, 11, 13},
std::vector<unsigned> { 15 }, std::vector<unsigned> {15},
std::vector<unsigned> { 12, 11, 13 }); std::vector<unsigned> {12, 11, 13});
std::puts("[INFO] Testing deleting root with 2 children but no child right left."); std::puts("[INFO] Testing deleting root with 2 children but no child right left.");
TestDeletionCase( TestDeletionCase(
std::vector<unsigned> { 15, 10, 20, 22, 25, 18, 5}, std::vector<unsigned> {15, 10, 20, 22, 25, 18, 5},
std::vector<unsigned> { 20 }, std::vector<unsigned> {20},
std::vector<unsigned> { 15, 10, 22, 25, 18, 5 }); std::vector<unsigned> {15, 10, 22, 25, 18, 5});
std::puts("[INFO] Testing deleting root with 2 children."); std::puts("[INFO] Testing deleting root with 2 children.");
TestDeletionCase( TestDeletionCase(
std::vector<unsigned> { 50, 40, 60, 75, 55, 45, 42, 58 }, std::vector<unsigned> {50, 40, 60, 75, 55, 45, 42, 58},
std::vector<unsigned> { 50 }, std::vector<unsigned> {50},
std::vector<unsigned> { 40, 60, 75, 55, 45, 42, 58 }); std::vector<unsigned> {40, 60, 75, 55, 45, 42, 58});
return true; return true;
} }

View File

@@ -55,7 +55,7 @@ bool TestPrepending()
bool TestPrependAppendMix() bool TestPrependAppendMix()
{ {
std::vector<unsigned> numbers = { 5, 6, 4, 7, 3, 8, 2, 9, 1, 10 }; std::vector<unsigned> numbers = {5, 6, 4, 7, 3, 8, 2, 9, 1, 10};
List::List<unsigned> list; List::List<unsigned> list;
for(unsigned i = 0; i < numbers.size(); i += 2) for(unsigned i = 0; i < numbers.size(); i += 2)

View File

@@ -7,19 +7,19 @@
namespace Util namespace Util
{ {
unsigned static GetRandomNumber() unsigned static GetRandomNumber()
{ {
static std::default_random_engine eng; static std::default_random_engine eng;
static std::uniform_int_distribution<unsigned> valueDist(0u, 8096u); static std::uniform_int_distribution<unsigned> valueDist(0u, 8096u);
return valueDist(eng); return valueDist(eng);
} }
}; };
namespace Test namespace Test
{ {
void Execute(bool (*testFunction)(void), char const * const message) void Execute(bool (*testFunction)(void), char const * const message)
{ {
try try
{ {
if(testFunction()) if(testFunction())
@@ -35,10 +35,10 @@ void Execute(bool (*testFunction)(void), char const * const message)
{ {
std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what()); std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what());
} }
} }
void Execute(bool (*testFunction)(std::vector<std::string> &), char const * const message) void Execute(bool (*testFunction)(std::vector<std::string> &), char const * const message)
{ {
std::vector<std::string> issues; std::vector<std::string> issues;
try try
{ {
@@ -49,7 +49,7 @@ void Execute(bool (*testFunction)(std::vector<std::string> &), char const * cons
else else
{ {
std::printf("[FAIL] %s\n", message); std::printf("[FAIL] %s\n", message);
for(auto & issue : issues) for(auto & issue: issues)
{ {
std::printf(" Issue: %s\n", issue.c_str()); std::printf(" Issue: %s\n", issue.c_str());
} }
@@ -59,5 +59,5 @@ void Execute(bool (*testFunction)(std::vector<std::string> &), char const * cons
{ {
std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what()); std::printf("[FAIL] Exception thrown during execution of <%s>, error: %s\n", message, e.what());
} }
} }
}; };

View File

@@ -1,8 +1,7 @@
#include "../src/sequential/vector.hpp" #include "../src/sequential/vector.hpp"
#include "testutil.hpp" #include "testutil.hpp"
void FillWithSequentialNumbers(Vector<unsigned> & vector, void FillWithSequentialNumbers(Vector<unsigned> & vector, unsigned const count = 1024u)
unsigned const count = 1024u)
{ {
for(unsigned i = 0; i < count; ++i) for(unsigned i = 0; i < count; ++i)
{ {