Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gk646/cxstructs
Browse files Browse the repository at this point in the history
  • Loading branch information
gk646 committed Apr 20, 2024
2 parents bba0436 + 62ea0c1 commit f57929f
Show file tree
Hide file tree
Showing 41 changed files with 2,857 additions and 485 deletions.
37 changes: 26 additions & 11 deletions include/cxalgos/GraphTraversal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_DFS_H
#define CXSTRUCTS_DFS_H
# define CXSTRUCTS_DFS_H

#include <iostream>
#include <unordered_map>
#include <utility>
#include <vector>
#include "../cxconfig.h"
#include "../cxstructs/HashSet.h"
#include "../cxstructs/Pair.h"
#include "../cxstructs/vec.h"
# include <iostream>
# include <unordered_map>
# include <utility>
# include <vector>
# include "../cxconfig.h"
# include "../cxstructs/HashSet.h"
# include "../cxstructs/Pair.h"
# include "../cxstructs/vec.h"

namespace cxstructs {

Expand All @@ -55,8 +55,9 @@ int depth_first_search(std::vector<std::vector<T>>& mat, int nodeIndex,
return count;
}
template <typename State, typename Action>
vec<vec<std::pair<Action, State>>> shortest_path_search(
State start, std::unordered_map<State, Action> (*successors)(State), bool (*is_goal)(State)) {
vec<vec<std::pair<Action, State>>>
shortest_path_search(State start, std::unordered_map<State, Action> (*successors)(State),
bool (*is_goal)(State)) {
if (is_goal(start)) {
return {{Action(), start}};
}
Expand All @@ -68,5 +69,19 @@ vec<vec<std::pair<Action, State>>> shortest_path_search(
}
}
} // namespace cxstructs
# ifdef CX_INCLUDE_TESTS
namespace cxtests { // namespace cxtests
static void TEST_DFS() {
std::cout << "TESTING DEPTH FIRST SEARCH" << std::endl;
std::vector<std::vector<int>> mat = {
{0, 1, 0, 1, 0}, {1, 0, 1, 0, 0}, {0, 1, 0, 0, 0}, {1, 0, 0, 0, 1}, {0, 0, 0, 1, 0}};

// Perform depth_first_search from node 0
int reachableNodes = depth_first_search(mat, 0);

CX_ASSERT(reachableNodes == 5, "");
}
} // namespace cxtests
# endif

#endif // CXSTRUCTS_DFS_H
33 changes: 27 additions & 6 deletions include/cxalgos/MathFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_SRC_ALGORITHMS_MATHFUNCTIONS_H_
#define CXSTRUCTS_SRC_ALGORITHMS_MATHFUNCTIONS_H_
# define CXSTRUCTS_SRC_ALGORITHMS_MATHFUNCTIONS_H_

#include <cmath>
#include <cstdint>
#include <type_traits>
#include "../cxconfig.h"
#include "../cxutil/cxmath.h"
# include <cmath>
# include <cstdint>
# include <type_traits>
# include "../cxconfig.h"
# include "../cxutil/cxmath.h"

namespace cxstructs {

Expand Down Expand Up @@ -95,4 +95,25 @@ double integral_arc_length(Function fx, double a, double b, uint_32_cx steps = 1
return arc_length;
}
} // namespace cxstructs
# ifdef CX_INCLUDE_TESTS
namespace cxtests {
using namespace cxstructs;
static void TEST_MATH() {
std::cout << "TESTING MATH FUNCTIONS" << std::endl;
auto integral = integral_aprox([](double x) { return x * x; }, 0, 5, 100000);
CX_ASSERT(integral - std::pow(5, 3) / 3 < 0.0001, "");
auto volume = integral_volume_solids_of_revolution([](double x) { return std::sqrt(x); }, 0, 4);

CX_ASSERT(volume - CX_PI * 8 < 0.001, "");
auto length =
integral_arc_length([](double x) { return (1.0 / 3.0) * std::pow((x * x + 2), 3.0 / 2.0); },
0, std::sqrt(8), 100000);

CX_ASSERT(cxstructs::next_power_of_2(3) == 4, "");
CX_ASSERT(cxstructs::next_power_of_2(3) != 3, "");
CX_ASSERT(cxstructs::next_power_of_2(10) == 16, "");
CX_ASSERT(cxstructs::next_power_of_2(53) == 64, "");
}
} // namespace cxtests
# endif
#endif //CXSTRUCTS_SRC_ALGORITHMS_MATHFUNCTIONS_H_
8 changes: 4 additions & 4 deletions include/cxalgos/Misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_SRC_CXALGOS_MISC_H_
#define CXSTRUCTS_SRC_CXALGOS_MISC_H_
# define CXSTRUCTS_SRC_CXALGOS_MISC_H_

#include <ctime>
#include <vector>
#include "../cxstructs/Geometry.h"
# include <ctime>
# include <vector>
# include "../cxstructs/Geometry.h"

namespace cxstructs {
template <typename S, typename B>
Expand Down
58 changes: 39 additions & 19 deletions include/cxalgos/PathFinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_ASTAR_PATHFINDING_H
#define CXSTRUCTS_ASTAR_PATHFINDING_H
# define CXSTRUCTS_ASTAR_PATHFINDING_H

#include <cstdint>
#include <queue>
#include <type_traits>
#include "../cxconfig.h"
#include "../cxstructs/Geometry.h"
#include "../cxstructs/HashSet.h"
#include "../cxstructs/Pair.h"
#include "../cxstructs/PriorityQueue.h"
# include <cstdint>
# include <queue>
# include <type_traits>
# include "../cxconfig.h"
# include "../cxstructs/Geometry.h"
# include "../cxstructs/HashSet.h"
# include "../cxstructs/Pair.h"
# include "../cxstructs/PriorityQueue.h"

namespace cxhelper {
using namespace cxstructs;
Expand All @@ -40,16 +40,10 @@ struct Node {
Node* parent;
Node() {}
Node(const Node& n)
: f_cost(n.f_cost),
position(n.position),
parent(n.parent),
h_cost(n.h_cost),
: f_cost(n.f_cost), position(n.position), parent(n.parent), h_cost(n.h_cost),
g_cost(n.g_cost) {}
Node(Point position, uint16_t g_cost, uint16_t h_cost, Node* parent)
: position(position),
g_cost(g_cost),
h_cost(h_cost),
f_cost(g_cost + h_cost),
: position(position), g_cost(g_cost), h_cost(h_cost), f_cost(g_cost + h_cost),
parent(parent) {}

inline bool operator<(const Node& o) const { return f_cost < o.f_cost; }
Expand Down Expand Up @@ -109,8 +103,8 @@ std::vector<Point> astar_pathfinding(const std::vector<std::vector<S>>& field, c
int newX = static_cast<int>(current->position.x()) + dir[0];
int newY = static_cast<int>(current->position.y()) + dir[1];
Point new_pos(newX, newY);
if (newX >= 0 && newY < field.size() && newY >= 0 && newX < field[0].size() &&
field[newY][newX] != blocked_val && !closedSet.contains(new_pos)) {
if (newX >= 0 && newY < field.size() && newY >= 0 && newX < field[0].size()
&& field[newY][newX] != blocked_val && !closedSet.contains(new_pos)) {

uint16_t tentative_g_cost = current->g_cost + 1;
uint16_t h_cost =
Expand All @@ -124,5 +118,31 @@ std::vector<Point> astar_pathfinding(const std::vector<std::vector<S>>& field, c
}

} // namespace cxstructs
# ifdef CX_INCLUDE_TESTS
namespace cxtests { // namespace cxtests
using namespace cxstructs;
static void TEST_PATH_FINDING() {
std::cout << "TESTING PATHFINDING" << std::endl;
std::vector<std::vector<int>> maze = {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1},
{1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

};
Point start(1, 1);
Point target(11, 8);

auto path = astar_pathfinding(maze, 1, start, target);

CX_ASSERT(path[path.size() - 3] == Point(11, 6), "");
}
} // namespace cxtests
# endif

#endif //CXSTRUCTS_ASTAR_PATHFINDING_H
12 changes: 6 additions & 6 deletions include/cxalgos/PatternMatching.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_SRC_ALGORITHMS_PATTERNMATCHING_H_
#define CXSTRUCTS_SRC_ALGORITHMS_PATTERNMATCHING_H_
# define CXSTRUCTS_SRC_ALGORITHMS_PATTERNMATCHING_H_

#include <algorithm>
#include <string>
#include "../cxconfig.h"
# include <algorithm>
# include <string>
# include "../cxconfig.h"

namespace cxhelper {

Expand Down Expand Up @@ -148,8 +148,8 @@ inline int findString_Boyer_Moore(const std::string& text, const std::string& pa
//produce bad character table
for (uint_32_cx i = 0; i < len; i++) {
bad_char[static_cast<uint8_t>(pattern[i]) & 0x7F] =
len - 1 -
i; //cast number to 0-256 and bitwise and with 01111111 to zero the first bit / from my Trie implementation
len - 1
- i; //cast number to 0-256 and bitwise and with 01111111 to zero the first bit / from my Trie implementation
}

auto* good_suffix = new int[len]; //dynamic array
Expand Down
25 changes: 22 additions & 3 deletions include/cxalgos/Search.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_BINARYSEARCH_H
#define CXSTRUCTS_BINARYSEARCH_H
# define CXSTRUCTS_BINARYSEARCH_H

#include <cstdint>
#include "../cxconfig.h"
# include <cstdint>
# include "../cxconfig.h"

namespace cxhelper { // helper methods to provide clean calling interface
template <typename T>
Expand Down Expand Up @@ -121,4 +121,23 @@ int binary_search_index(T* arr, T target, int_32_cx len, bool ascending) {
}

} // namespace cxstructs
# ifdef CX_INCLUDE_TESTS
namespace cxtests {
using namespace cxstructs;
static void TEST_SEARCH() {
std::cout << "TESTING BINARY SEARCH" << std::endl;
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
CX_ASSERT(binary_search(arr, 5, 9) == true, "");
CX_ASSERT(binary_search(arr, -1, 9) == false, "");

std::cout << "TESTING BINARY SEARCH RECURSIVE" << std::endl;
CX_ASSERT(binary_search_recursive(arr, 5, 9) == true, "");
CX_ASSERT(binary_search_recursive(arr, -1, 9) == false, "");

std::cout << "TESTING BINARY SEARCH INDEX" << std::endl;
CX_ASSERT(binary_search_index(arr, 7, 9, true) == 6, "");
CX_ASSERT(binary_search_index(arr, 2, 9, true) == 1, "");
}
} // namespace cxtests
# endif
#endif // CXSTRUCTS_BINARYSEARCH_H
54 changes: 51 additions & 3 deletions include/cxalgos/Sorting.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_SORTING_H
#define CXSTRUCTS_SORTING_H
# define CXSTRUCTS_SORTING_H

#include <algorithm>
#include "../cxconfig.h"
# include <algorithm>
# include "../cxconfig.h"

namespace cxhelper { // helper methods to provide clean calling interface
template <typename T>
Expand Down Expand Up @@ -276,4 +276,52 @@ template <typename T>
void heapSort(T* arr, uint_32_cx len, bool ascending) {}

} // namespace cxstructs
# ifdef CX_INCLUDE_TESTS
namespace cxtests {
std::vector<int> generate_shuffled_vector(int size) {
std::vector<int> vec(size);
std::iota(vec.begin(), vec.end(), 1); // fill with increasing numbers
std::shuffle(vec.begin(), vec.end(), std::mt19937{std::random_device{}()});
return vec;
}
void CX_ASSERT_sorted(std::vector<int>& vec, bool ascending = true) {
for (size_t i = 1; i < vec.size(); i++) {
if (ascending) {
CX_ASSERT(vec[i - 1] <= vec[i], "");
} else {
CX_ASSERT(vec[i - 1] >= vec[i], "");
}
}
}
using namespace cxstructs;
static void TEST_SORTING() {
const int SIZE = 10000;

std::cout << "TESTING BUBBLESORT" << std::endl;
std::vector<int> bubble_vec = generate_shuffled_vector(SIZE);
bubble_sort(bubble_vec.data(), SIZE);
CX_ASSERT_sorted(bubble_vec);

std::cout << "TESTING QUICKSORT" << std::endl;
std::vector<int> quick_vec = generate_shuffled_vector(SIZE);
quick_sort(quick_vec.data(), SIZE);
CX_ASSERT_sorted(quick_vec);

std::cout << "TESTING SELECTIONSORT" << std::endl;
std::vector<int> selection_vec = generate_shuffled_vector(SIZE);
selection_sort(selection_vec.data(), SIZE);
CX_ASSERT_sorted(selection_vec);

std::cout << "TESTING MERGE SORT" << std::endl;
std::vector<int> merge_vec = generate_shuffled_vector(SIZE);
merge_sort(merge_vec.data(), SIZE);
CX_ASSERT_sorted(merge_vec);

std::cout << "TESTING BOGO SORT" << std::endl;
std::vector<int> bogo_vec = generate_shuffled_vector(10);
bogo_sort(bogo_vec.data(), 10);
CX_ASSERT_sorted(bogo_vec);
}
} // namespace cxtests
# endif
#endif // CXSTRUCTS_SORTING_H
10 changes: 5 additions & 5 deletions include/cxallocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_SRC_CXALLOCATOR_H_
#define CXSTRUCTS_SRC_CXALLOCATOR_H_
# define CXSTRUCTS_SRC_CXALLOCATOR_H_

/*
Use the last template option to specify whether to use the allocator or not cxstruct<Type,false>.
Expand All @@ -28,9 +28,9 @@ In turn, generally do not use it if it's a temporary or fixed size structure
In case of slower performance just switch to the other.
*/

#include <deque>
#include <vector>
#include "cxconfig.h"
# include <deque>
# include <vector>
# include "cxconfig.h"

namespace cxstructs {
template <uint_32_cx BlockSize, uint_16_cx ReservedBlocks>
Expand Down Expand Up @@ -60,7 +60,7 @@ class Pool {
}
}

void* allocate() {
auto allocate() -> void* {
if (addrs_.empty()) {
allocateBlock();
}
Expand Down
Loading

0 comments on commit f57929f

Please sign in to comment.