Skip to content

Commit

Permalink
added cxbits.h header
Browse files Browse the repository at this point in the history
  • Loading branch information
gk646 committed Apr 5, 2024
1 parent 708303f commit b417ee9
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/cxml/word2vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Word2Vec {
auto target = mat::unit_matrix(1, vocab_len, 0, i);
net.train(in[i], target, epochs);
}
in.print();
in.bits_print();
}
vec<float> predict_next(int vocab_index) {
mat in(1, vocab_len);
Expand Down
15 changes: 4 additions & 11 deletions src/cxstructs/PriorityQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
#define CXSTRUCTS_SRC_CXSTRUCTS_PRIORITYQUEUE_H_

#include "../cxconfig.h"
#include <memory> // For std::allocator<T>
#ifdef CX_INCLUDE_TESTS
#include <queue>
#endif

namespace cxstructs {

/**
Expand Down Expand Up @@ -103,7 +105,7 @@ class PriorityQueue {

public:
/**
* Per default is a min heap. Pass std::greater<> as comparator to get a max-heap
* 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)
Expand Down Expand Up @@ -241,16 +243,7 @@ class PriorityQueue {
CX_WARNING(len_ > size_ * 1.5, "calling shrink_to_fit for no reason");
shrink();
}
friend std::ostream& operator<<(std::ostream& os, const PriorityQueue& q) {
if (q.size() == 0) {
return os << "[]";
}
os << "[" << q.arr_[0];
for (uint_32_cx i = 1; i < q.size_; i++) {
os << "," << q.arr_[i];
}
return os << "]";
}

class Iterator {
T* ptr;

Expand Down
6 changes: 3 additions & 3 deletions src/cxstructs/QuadTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
#ifndef CXSTRUCTS_SRC_DATASTRUCTURES_QUADTREE_H_
#define CXSTRUCTS_SRC_DATASTRUCTURES_QUADTREE_H_

#include <utility>

#include "../cxconfig.h"
#include "Geometry.h"
#include "vec.h"
#include <utility>

//used in kNN 2D

Expand Down Expand Up @@ -279,7 +279,7 @@ class QuadTree {
};
/**
* Actively iterates down the tree summing all subtree sizes<p>
* <b>This can get slow on large trees </b>
* <b>This can bits_get slow on large trees </b>
* @return the max depth of the tree
*/
[[nodiscard]] inline uint_32_cx size() const {
Expand All @@ -289,7 +289,7 @@ class QuadTree {
}
/**
* Actively iterates down the tree for its max depth<p>
* <b>This can get slow on large trees </b>
* <b>This can bits_get slow on large trees </b>
* @return the max depth of the tree
*/
[[nodiscard]] inline uint_16_cx depth() const {
Expand Down
13 changes: 2 additions & 11 deletions src/cxstructs/Queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define CXSTRUCTS_QUEUE_H

#include "../cxconfig.h"
#include <memory> //For std::allocator<T>
#ifdef CX_INCLUDE_TESTS
#include <queue>
#endif
Expand Down Expand Up @@ -279,7 +280,7 @@ class Queue {
* Clears the queue of all elements
*/
inline void clear() {
if (!std::is_trivial_v<T>) {
if constexpr (!std::is_trivial_v<T>) {
for (uint_32_cx i = 0; i < size_; i++) {
std::allocator_traits<Allocator>::destroy(alloc, &arr_[i]);
}
Expand All @@ -301,16 +302,6 @@ class Queue {
* @return
*/
[[nodiscard]] inline uint_32_cx capacity() const { return capacity_; }
friend std::ostream& operator<<(std::ostream& os, const Queue& q) {
if (q.size() == 0) {
return os << "[]";
}
os << "[" << q.arr_[0];
for (uint_32_cx i = q.front_ + 1; i < q.size_; i++) {
os << "," << q.arr_[i];
}
return os << "]";
}
class Iterator {
T* ptr;
uint_32_cx current;
Expand Down
1 change: 1 addition & 0 deletions src/cxstructs/Stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Stack {
using Allocator =
typename std::conditional<UseCXPoolAllocator, CXPoolAllocator<T, sizeof(T) * 33, 1>,
std::allocator<T>>::type;

Allocator alloc;
T* arr_;
uint_32_cx size_;
Expand Down
80 changes: 69 additions & 11 deletions src/cxstructs/StackArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,81 @@
#include "../cxconfig.h"
#include <iterator> // For std::forward_iterator_tag

template <typename T, size_t N>
namespace cxstructs {
template <typename T, size_t N, typename size_type = uint32_t>
class StackArray {
static_assert(std::is_trivial_v<T>, "StackArray only supports trivial types.");
using size_type = uint32_t;

T data_[N]; // Stack-allocated array
size_type size_; // Current number of elements in the array

public:
StackArray() : size_(0) {}
StackArray(size_type elems) : size_(elems) {}
//Call can fail
StackArray(const StackArray&) = delete;
StackArray& operator=(const StackArray&) = delete;
StackArray(StackArray&& other) noexcept(std::is_nothrow_move_constructible_v<T>)
: size_(other.size_) {
if constexpr (std::is_trivially_copyable_v<T>) {
std::memcpy(data_, other.data_, other.size_ * sizeof(T));
} else {
for (size_type i = 0; i < other.size_; ++i) {
new (data_ + i) T(std::move(other.data_[i]));
other.data_[i].~T();
}
}
other.size_ = 0;
}
StackArray& operator=(StackArray&& other) noexcept(std::is_nothrow_move_assignable_v<T>) {
if (this != &other) {
clear();
size_ = other.size_;
if constexpr (std::is_trivially_copyable_v<T>) {
std::memcpy(data_, other.data_, other.size_ * sizeof(T));
} else {
for (size_type i = 0; i < other.size_; ++i) {
new (data_ + i) T(std::move(other.data_[i]));
other.data_[i].~T();
}
}
other.size_ = 0;
}
return *this;
}
~StackArray() { clear(); }
// Call can fail
void push_back(const T& value) {
CX_ASSERT(size_ < N, "Attempt to add to a full StackArray");
data_[size_++] = value;
if constexpr (std::is_trivially_copyable_v<T>) {
data_[size_++] = value;
} else {
new (data_ + size_) T(value);
++size_;
}
}
//Call cannot fail | Loops around and overwrites first elements and sets size to length from 0
void push_back(T&& value) {
CX_ASSERT(size_ < N, "Attempt to add to a full StackArray");
if constexpr (std::is_trivially_copyable_v<T>) {
data_[size_++] = std::move(value);
} else {
new (data_ + size_) T(std::move(value));
++size_;
}
}
// Call cannot fail | Overwrites first elements and resets size on loop
void push_back_loop(const T& value) {
data_[size_ % N] = value;
size_type index = size_ % N;
if constexpr (!std::is_trivially_copyable_v<T>) {
if (size_ >= N) {
data_[index].~T();
}
}
new (data_ + index) T(value);
size_ = (size_ + 1) % N;
}
template <typename... Args>
void emplace_back(Args&&... args) {
CX_ASSERT(size_ < N, "Attempt to add to a full StackArray");
new (data_ + size_) T(std::forward<Args>(args)...);
new (data_ + size_) T(std::forward<Args>(args)...); // Direct construction with arguments
++size_;
}
T& operator[](size_type index) {
Expand All @@ -59,7 +109,14 @@ class StackArray {
CX_ASSERT(index < size_, "Index out of range");
return data_[index];
}
inline void clear() { size_ = 0; }
inline void clear() {
if constexpr (!std::is_trivially_destructible_v<T>) {
for (size_type i = 0; i < size_; ++i) {
data_[i].~T(); // Call destructor for each constructed element
}
}
size_ = 0;
}
[[nodiscard]] inline size_type size() const { return size_; }
[[nodiscard]] bool empty() const { return size_ == 0; }
[[nodiscard]] inline bool full() const { return size_ == N; }
Expand Down Expand Up @@ -122,7 +179,8 @@ class StackArray {
// Move elements after pos one position to the left
std::move(posPtr + 1, &(*end()), posPtr);

// Destroy the last element (now a duplicate) if it's not trivially destructible
// Destroy the last element (now a duplicate) if it's not trivially
// destructible
if constexpr (!std::is_trivially_destructible_v<T>) {
data_[size_ - 1].~T();
}
Expand Down Expand Up @@ -150,5 +208,5 @@ class StackArray {
}
#endif
};

} // namespace cxstructs
#endif //CXSTRUCTS_SRC_CXSTRUCTS_STACKARRAY_H_
11 changes: 6 additions & 5 deletions src/cxstructs/StackHashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
#define CXSTRUCTS_SRC_CXSTRUCTS_STACKHASHMAP_H_

#include "../cxconfig.h"
#include <bitset> //For std::bitset<N> and std::hash<K>
#include <iterator> // For std::forward_iterator_tag
#include <bitset> //For std::bitset<N> and std::hash<K>

//Memory footprint is still quite big
//Using std::bitset<> saves memory but is a bit slower
//Use set_rand(1) if you have your own perfect hash function -> will be overridden on collision
namespace cxstructs {

/**
* StackHashMap is a hash map implemented entirely on the stack with an STL-like interface.<br>
Expand Down Expand Up @@ -259,6 +260,7 @@ class StackHashMap {
return register_[hash] && data_[hash].key == key;
}

//The multiplier for the hash | Set to 1 if you supply a perfect hash function | Resets on collision
inline void set_rand(int rand) noexcept { rand_ = rand; }

inline void clear() noexcept {
Expand Down Expand Up @@ -317,7 +319,6 @@ class StackHashMap {
size_t size_;
size_t index_;
};

class ValueIterator {
public:
using iterator_category = std::forward_iterator_tag;
Expand Down Expand Up @@ -362,7 +363,6 @@ class StackHashMap {
size_t size_;
size_t index_;
};

class PairIterator {
public:
using iterator_category = std::forward_iterator_tag;
Expand Down Expand Up @@ -470,8 +470,9 @@ class StackHashMap {
myMap2.insert("hey", 100);
myMap2.insert("blabla", 100);

printf("%d", myMap2["hey"]);
CX_ASSERT(myMap2["hey"] == 100, "");
}
#endif
};
} // namespace cxstructs
#endif //CXSTRUCTS_SRC_CXSTRUCTS_STACKHASHMAP_H_
2 changes: 1 addition & 1 deletion src/cxstructs/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ class vec {
size_ += endIndex - startIndex;
}
/**
* Attempts to print the complete list to std::cout
* Attempts to bits_print the complete list to std::cout
* @param prefix optional prefix
*/
inline void print(const std::string& prefix = "") {
Expand Down
Loading

0 comments on commit b417ee9

Please sign in to comment.