Skip to content

Commit

Permalink
added small vector and hashgrid
Browse files Browse the repository at this point in the history
  • Loading branch information
gk646 committed Jun 7, 2024
1 parent 0bc892e commit f88c90a
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 285 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ EmptyLineBeforeAccessModifier: LogicalBlock
FixNamespaceComments: true
IncludeBlocks: Preserve
IndentCaseLabels: true
IndentPPDirectives: AfterHash
IndentPPDirectives: None
IndentWidth: 2
KeepEmptyLinesAtTheStartOfBlocks: true
MaxEmptyLinesToKeep: 1
Expand Down
52 changes: 29 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ 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*
*Note: Some datastructures are outdated*

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

### Speed Comparison

*Relative to the fastest / with CXPoolAllocator (if applicable)*
Note: *These are old benchmarks*

| | vector | Stack | HashMap | StackHashMap | HashSet | LinkedList | Queue | DeQueue |
|:----------------|:--------:|:--------:|:--------:|:------------:|:-------:|:----------:|:--------:|:--------:|
Expand Down Expand Up @@ -57,26 +56,30 @@ The general namespace is `cxstructs`.

#### Data Structures

- **Vector**(*vec*):
- **Matrix**(*mat*): *flattened float array, lots of methods*
- **Row**(*row*): *compile-time sized, non-mutable container*
- **Pair**: *static container for two types*
- **Trie**: *limited to ASCII (128)*
- **Stack**:
- **HashMap**: *using separate chaining with LinkedLists with static buffer*
- **HashSet**: *using separate chaining with LinkedLists with static buffer*
- **HashGrid**: *uses STL containers / 2D spatial lookups*
- **Linked List**:
- **HashGrid**: *Very fast cache friendly implementation*
- **SmallVector**: *Vector with configurable stack based storage buffer*
- **BitMask**: *various bit mask container*
- **StackVector**: *stack container with std::vector interface*
- **StackHashMap**: *(yes :) stack container with std::unordered map interface*
- **Double Linked List**:
- **Queue**: *using circular array*
- **DeQueue**: *using circular array*
- **PriorityQueue**: *using binary heap*
- **Binary Tree**:
- **QuadTree**: *allows custom Types with x() and y() getters*
- **BitMask**: *various bit mask container*
- **Geometry**(*Rect,Circle,Point*): *standard efficient 2D shapes*


- **Outdated**
- **Vector**(*vec*):
- **Matrix**(*mat*): *flattened float array, lots of methods*
- **Row**(*row*): *compile-time sized, non-mutable container*
- **Pair**: *static container for two types*
- **Trie**: *limited to ASCII (128)*
- **Stack**:
- **HashMap**: *using separate chaining with LinkedLists with static buffer*
- **HashSet**: *using separate chaining with LinkedLists with static buffer*
- **Linked List**:
- **Double Linked List**:
- **Queue**: *using circular array*
- **DeQueue**: *using circular array*
- **Binary Tree**:
- **QuadTree**: *allows custom Types with x() and y() getters*
- **Geometry**(*Rect,Circle,Point*): *standard efficient 2D shapes*

#### Machine Learning

Expand All @@ -97,13 +100,17 @@ The general namespace is `cxstructs`.

- **cxassert**: *custom assertions with optional text*
- **cxbits**: *bit operations on numbers for embedding and retrieving information*
- **cxgraphics**: *simple native windowing and graphics output header*
- **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*


- **Outdated**:
- **cxgraphics**: *simple native windowing and graphics output header*


---

### Usage Guide
Expand Down Expand Up @@ -219,5 +226,4 @@ for PriorityQueue)

- The CX_ASSERT macro is inspired by the
video ["How I use C++;a line-by-line code review"](https://www.youtube.com/watch?v=W8-G_PL6p-0&pp=ygUYbXkgYysrIGlzIGluc2FuZSBzdHJhZ2Vy)
by Strager

by Strager
29 changes: 14 additions & 15 deletions src/cxconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_SRC_CONFIG_H_
# define CXSTRUCTS_SRC_CONFIG_H_
#define CXSTRUCTS_SRC_CONFIG_H_

# include <cstdint> //Used for almost all size_types
# include "cxutil/cxassert.h" //includes only <cstdio>
#include <cstdint> //Used for almost all size_types
#include "cxutil/cxassert.h" //includes only <cstdio>

//-----------DEFINES-----------//

Expand All @@ -31,28 +31,27 @@
//
//

# define CX_INL inline
# define CX_NDISC [[nodiscard]]
#define CX_INL inline
#define CX_NDISC [[nodiscard]]

// namespace for exposed structs and functions
namespace cxstructs {}

#ifdef CX_STACK_ABORT
#define CX_STACK_ABORT_IMPL() \
if (size_ >= N) std::abort()
#else
#define CX_STACK_ABORT_IMPL() (void(0))
#endif

# ifdef CX_STACK_ABORT
# define CX_STACK_ABORT_IMPL() \
if (size_ >= N) std::abort()
# else
# define CX_STACK_ABORT_IMPL() (void(0))
# endif

# ifndef CX_USE_INT
#ifndef CX_USE_INT
using uint_32_cx = uint_fast32_t;
using uint_16_cx = uint_fast16_t;
using int_32_cx = int_fast32_t;
# else
#else
typedef int uint_32_cx;
typedef int uint_16_cx;
typedef int int_32_cx;
# endif
#endif

#endif //CXSTRUCTS_SRC_CONFIG_H_
47 changes: 20 additions & 27 deletions src/cxstructs/BinaryTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,11 @@
// SOFTWARE.
#define CX_FINISHED
#ifndef CXSTRUCTS_BINARYTREE_H
# define CXSTRUCTS_BINARYTREE_H
#define CXSTRUCTS_BINARYTREE_H

# include <algorithm>
# include <deque>
# include <iostream>
# include <stdexcept>
# include <vector>
# include "../cxconfig.h"
#include "../cxconfig.h"

namespace cxhelper { // namespace to hide helper structs
namespace cxhelper {
/**
* Helper struct for the BinaryTree
* @tparam T type
Expand All @@ -38,10 +33,9 @@ struct TreeNode {
TreeNode* left_;
TreeNode* right_;
T data_;
inline explicit TreeNode(const T& val) : data_(val), left_(nullptr), right_(nullptr) {}
explicit TreeNode(const T& val) : data_(val), left_(nullptr), right_(nullptr) {}

inline TreeNode(const T& val, TreeNode* left, TreeNode* right)
: data_(val), left_(left), right_(right) {}
TreeNode(const T& val, TreeNode* left, TreeNode* right) : data_(val), left_(left), right_(right) {}
};
} // namespace cxhelper
namespace cxstructs {
Expand All @@ -65,7 +59,7 @@ class BinaryTree {
TNode* root_;
uint_32_cx size_;

inline int subTreeDepth(TNode* node) {
int subTreeDepth(TNode* node) {
if (!node) {
return 0;
} else {
Expand All @@ -74,7 +68,7 @@ class BinaryTree {
return std::max(left, right) + 1;
}
}
inline void insert(const T& val, TNode* node) {
void insert(const T& val, TNode* node) {
if (val < node->data_) {
if (!node->left_) {
node->left_ = new TNode(val);
Expand All @@ -85,7 +79,7 @@ class BinaryTree {
} else insert(val, node->right_);
}
}
inline bool contains(const T& val, TNode* node) const {
bool contains(const T& val, TNode* node) const {
if (node) {
if (val < node->data_) {
if (node->left_) {
Expand All @@ -103,7 +97,7 @@ class BinaryTree {
}
return false;
}
inline TNode* erase(const T& val, TNode* node) {
TNode* erase(const T& val, TNode* node) {
if (!node) {
return node;
}
Expand Down Expand Up @@ -131,7 +125,7 @@ class BinaryTree {
}
return node;
}
inline TNode* minValueNode(TNode* node) {
TNode* minValueNode(TNode* node) {
TNode* current = node;

while (current && current->left_ != nullptr) {
Expand All @@ -142,7 +136,6 @@ class BinaryTree {

public:
BinaryTree() : root_(nullptr), size_(0){};
//no moving around yet
BinaryTree(const BinaryTree&) = delete;
BinaryTree& operator=(const BinaryTree&) = delete;
BinaryTree(BinaryTree&&) = delete;
Expand Down Expand Up @@ -174,7 +167,7 @@ class BinaryTree {
/**
* Inverts this BinaryTree starting from the given node
*/
inline void invert(TNode* node) {
void invert(TNode* node) {
if (node == nullptr) {
return;
}
Expand All @@ -186,13 +179,13 @@ class BinaryTree {
/**
* Inverts this BinaryTree starting from the root
*/
inline void invert() { invert(root_); }
void invert() { invert(root_); }

/**
* Inserts the given element into the tree at the right position
* @param val - the inserted value
*/
inline void insert(const T& val) {
void insert(const T& val) {
if (root_) {
insert(val, root_);
size_++;
Expand All @@ -207,14 +200,14 @@ class BinaryTree {
* @param val - the value to search for
* @return - true if the tree contained the given value, false otherwise
*/
[[nodiscard]] inline bool contains(const T& val) const { return contains(val, root_); }
[[nodiscard]] bool contains(const T& val) const { return contains(val, root_); }

/**
* Erases the first occurrence of a node with this value
* @param val - the node-value to search for
* @return true if a deletion happened
*/
inline bool erase(const T& val) {
bool erase(const T& val) {
auto temp = size_;
root_ = erase(val, root_);
return (temp - 1 == size_);
Expand All @@ -223,7 +216,7 @@ class BinaryTree {
@brief Removes all nodes from the binary tree<p>
After the operation, the tree becomes empty and its size is 0
**/
inline void clear() {
void clear() {
std::deque<TNode*> nodesToDelete;
nodesToDelete.push_back(root_);

Expand Down Expand Up @@ -252,7 +245,7 @@ class BinaryTree {
*
* @return true if the BinaryTree is empty, false otherwise.
*/
[[nodiscard]] inline bool empty() const { return size_ == 0; }
[[nodiscard]] bool empty() const { return size_ == 0; }
/**
*
* @return the maximum depth of the tree
Expand Down Expand Up @@ -295,7 +288,7 @@ class BinaryTree {
*/
InOrderIterator end() { return InOrderIterator(nullptr); }

# ifdef CX_INCLUDE_TESTS
#ifdef CX_INCLUDE_TESTS
static void TEST() {
std::cout << "BINARY SEARCH TREE TESTS" << std::endl;

Expand Down Expand Up @@ -376,7 +369,7 @@ class BinaryTree {
prev_num = num;
}
}
# endif
#endif
};
} // namespace cxstructs
#endif // CXSTRUCTS_BINARYTREE_H
#endif // CXSTRUCTS_BINARYTREE_H
Loading

0 comments on commit f88c90a

Please sign in to comment.