Skip to content

Commit

Permalink
updated and repurposed cxio header
Browse files Browse the repository at this point in the history
updated readme
  • Loading branch information
gk646 committed Apr 10, 2024
1 parent a37611c commit c86e205
Show file tree
Hide file tree
Showing 11 changed files with 321 additions and 96 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
## Datastructures, algorithms, machine-learning and utilities library in C++

A medium-sized modern header only library of selected datastructures, algorithms, machinelearning topics and utilities.
This
collection is built for educational purposes and for use in non-essential projects. All implementations are easy to read
and well documented.
This collection is built for educational purposes and for use in non-essential projects.

**Documentation is provided in-header.**

While I am not an expert in datastructures nor C++, I am still aiming for reference type implementations in terms of
efficiency and interface.

The general namespace is `cxstructs`.

*Note: Currently the old datastructures are modernized with focus on better includes for compile times and correctness.*
*Non-Stack datastructure might not be usable for now*

**1.** [Contents](#contents)
**2.** [Usage Guide](#usage-guide)
**3.** [Installation](#installation)
Expand All @@ -19,12 +22,12 @@ The general namespace is `cxstructs`.

### Speed Comparison

*Relative to the fastest / with CXPoolAllocator*
*Relative to the fastest / with CXPoolAllocator (if applicable)*

| | vector | Stack | HashMap | HashSet | LinkedList | Queue | DeQueue |
|:----------------|:--------:|:--------:|:--------:|:-------:|:----------:|:--------:|:--------:|
| **std::** | *0.81* | *0.52* | *0.51* | *0.52* | *0.71* | *0.46* | *0.57* |
| **cxstructs::** | **1.00** | **1.00** | **1.00** | **1.0** | **1.00** | **1.00** | **1.00** |
| | vector | Stack | HashMap | StackHashMap | HashSet | LinkedList | Queue | DeQueue |
|:----------------|:--------:|:--------:|:--------:|:------------:|:-------:|:----------:|:--------:|:--------:|
| **std::** | *0.81* | *0.52* | *0.51* | *0.5* | *0.52* | *0.71* | *0.46* | *0.57* |
| **cxstructs::** | **1.00** | **1.00** | **1.00** | **1.00** | **1.0** | **1.00** | **1.00** | **1.00** |

### Features

Expand Down Expand Up @@ -92,12 +95,14 @@ The general namespace is `cxstructs`.

#### Utilities

- **cxtime**: *easily measure the time from `now()` to `printTime()`*
- **cxio**: *load_text,*
- **cxassert**: *custom assertions with optional text*
- **cxmath**: *activation functions,distance function, next_power_of_2*
- **cxbits**: *bit operations on numbers for embedding and retrieving information*
- **cxgraphics**: *simple native windowing and graphics output header*
- **cxbits**: *bit operations on numbers*
- **cxio**: *simple, readable and symmetric file io format*
- **cxmath**: *activation functions,distance functions, next_power_of_2, square root*
- **cxstring**: *operations on strings*
- **cxtime**: *simple time measurements with multiple time points and formats*
- **cxtips**: *collection of helpful resources and personal guidelines with examples*

---

Expand Down
1 change: 1 addition & 0 deletions src/cxconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace cxtests {}
#define CX_STACK_ABORT_IMPL() (void(0))
#endif


#ifndef CX_USE_INT
typedef uint_fast32_t uint_32_cx;
typedef uint_fast16_t uint_16_cx;
Expand Down
16 changes: 8 additions & 8 deletions src/cxstructs/PriorityQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class PriorityQueue {
uint_32_cx size_;
Compare comp;

inline void resize() noexcept {
void resize() noexcept {
len_ *= 2;

T* n_arr = alloc.allocate(len_);
Expand All @@ -60,7 +60,7 @@ class PriorityQueue {
alloc.deallocate(arr_, size_);
arr_ = n_arr;
}
inline void shrink() noexcept {
void shrink() noexcept {
auto old_len = len_;
len_ = size_ * 1.5;

Expand All @@ -77,15 +77,15 @@ class PriorityQueue {
alloc.deallocate(arr_, old_len);
arr_ = n_arr;
}
inline void sift_up(uint_32_cx index) noexcept {
void sift_up(uint_32_cx index) noexcept {
auto parent = (index - 1) / 2;
while (index != 0 && !comp(arr_[index], arr_[parent])) {
std::swap(arr_[index], arr_[parent]);
index = parent;
parent = (index - 1) / 2;
}
}
inline void sift_down(uint_32_cx index) noexcept {
void sift_down(uint_32_cx index) noexcept {
auto left = 2 * index + 1;
auto right = 2 * index + 2;
while ((left < size_ && comp(arr_[index], arr_[left])) ||
Expand All @@ -97,7 +97,7 @@ class PriorityQueue {
right = 2 * index + 2;
}
}
inline void heapify() noexcept {
void heapify() noexcept {
for (uint_fast32_t i = len_ - 1; i > -1; i--) {
sift_down(i);
}
Expand All @@ -108,15 +108,15 @@ class PriorityQueue {
* Per default is a min heap. Pass std::greater<> as comparator to bits_get a max-heap
* @param len initial length and expansion factor
*/
inline explicit PriorityQueue(uint_32_cx len = 32)
explicit PriorityQueue(uint_32_cx len = 32)
: arr_(alloc.allocate(len)), len_(len), size_(0) {}
/**
* @brief Constructor that initializes the priority queue with an existing array.
* <b>Takes ownership of the array</b>
* @param arr Pointer to the array to copy elements from.
* @param len The number of elements in the array.
*/
inline explicit PriorityQueue(T*&& arr, uint_32_cx len) : arr_(arr), len_(len), size_(len) {
explicit PriorityQueue(T*&& arr, uint_32_cx len) : arr_(arr), len_(len), size_(len) {
heapify();
arr = nullptr; //avoid double deletion
}
Expand All @@ -126,7 +126,7 @@ class PriorityQueue {
* @param arr Pointer to the array to copy elements from.
* @param len The number of elements in the array.
*/
inline explicit PriorityQueue(const T* arr, uint_32_cx len)
explicit PriorityQueue(const T* arr, uint_32_cx len)
: arr_(alloc.allocate(len)), len_(len), size_(len) {
if (std::is_trivial_v<T>) {
std::copy(arr, arr + len, arr_);
Expand Down
2 changes: 1 addition & 1 deletion src/cxstructs/StackVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class StackVector {

#ifdef CX_INCLUDE_TESTS
static void TEST() {
StackArray<int, 100> arr;
StackVector<int, 100> arr;
for (int i = 0; i < 10; i++) {
arr.push_back(100);
}
Expand Down
4 changes: 2 additions & 2 deletions src/cxstructs/mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
#ifndef CXSTRUCTS_SRC_CXSTRUCTS_MAT_H_
#define CXSTRUCTS_SRC_CXSTRUCTS_MAT_H_

#include <cmath>
#include "../cxconfig.h"
#include <cmath>
#include "vec.h"

namespace cxstructs {
Expand Down Expand Up @@ -449,7 +449,7 @@ class mat {
/**Prints out the matrix
* @param header optional header
*/
void print(const std::string& header = "") const {
void print(const char* name = "") const {
if (!header.empty()) {
std::cout << header << std::endl;
for (uint_32_cx i = 0; i < n_rows_; i++) {
Expand Down
3 changes: 2 additions & 1 deletion src/cxutil/cxbits.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ static void TEST_BITS() {
"Concatenation of uint16_t failed.");
CX_ASSERT(bits_concat(static_cast<uint32_t>(0x1), static_cast<uint32_t>(0x2)) == 0x200000001,
"Concatenation of uint32_t failed.");
uint16_t num1 = 1, num2 = 2;
uint16_t num1 = 1;
uint16_t num2 = 2;
CX_ASSERT(bits_concat(num1, num2) == 0x20001,
"Concatenation of uint16_t with different values failed.");

Expand Down
Loading

0 comments on commit c86e205

Please sign in to comment.