Compare commits
7 Commits
7fd3fbadb3
...
c3aab51030
Author | SHA1 | Date |
---|---|---|
|
c3aab51030 | |
|
983d7de820 | |
|
f5d6ef19a4 | |
|
85fdc4fa65 | |
|
31855dd0a4 | |
|
0d5abd143f | |
|
20f8238f70 |
|
@ -1,5 +1,5 @@
|
|||
cmake_minimum_required(VERSION 3.0)
|
||||
project(BLT VERSION 0.6.0)
|
||||
project(BLT VERSION 0.6.1)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
|
|
21
README.md
21
README.md
|
@ -1,4 +1,4 @@
|
|||
# **BLT v0.6.0a**
|
||||
# **BLT v0.6.1a**
|
||||
A common utilities library I find useful
|
||||
|
||||

|
||||
|
@ -26,11 +26,10 @@ If you are using BLT as a CMake library (as you should!) this is done for you.
|
|||
- ## Data Structures
|
||||
- Queue / Stack
|
||||
- faster than std::queue / std::stack
|
||||
- Binary Tree
|
||||
- Hashmap (TODO)
|
||||
- backed by a contiguous array
|
||||
- ## Utility
|
||||
- Simple Random Interface
|
||||
- No more worrying about min/max bounds!
|
||||
- Simple Random Wrapper Interface
|
||||
- Simple random functions based on the PCG Hash
|
||||
- ### String Functions
|
||||
- starts_with
|
||||
- ends_with
|
||||
|
@ -40,14 +39,12 @@ If you are using BLT as a CMake library (as you should!) this is done for you.
|
|||
- split
|
||||
- trim
|
||||
- Logging
|
||||
- Trace / Debug / Info / Warn / Error / Fatal
|
||||
- Log to file
|
||||
- Log to console with color!
|
||||
- Easy to disable for release
|
||||
- define BLT_DISABLE_LOGGING before including the logging.h file.
|
||||
- See blt::logging section above
|
||||
- Time
|
||||
- Current time in nanoseconds (without all the c++ gobbledygook)
|
||||
- Java's currentTimeMilliseconds
|
||||
- nanoTime as well
|
||||
- `std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()` becomes `blt::system::nanoTime()`
|
||||
- Formatted time string with year/month/date + current time
|
||||
- ## Profiling
|
||||
- Basic profiler
|
||||
- WIP
|
||||
- Basic profiler with history and formatted output
|
|
@ -36,6 +36,26 @@ namespace blt {
|
|||
return y;
|
||||
}
|
||||
|
||||
|
||||
static inline constexpr double pow(int b, int p) {
|
||||
int collection = 1;
|
||||
for (int i = 0; i < p; i++)
|
||||
collection *= b;
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a fast rounding function and is not guaranteed to be 100% correct
|
||||
* @tparam decimal_places
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
template<int decimal_places>
|
||||
static inline double round_up(double value) {
|
||||
constexpr double multiplier = pow(10, decimal_places);
|
||||
return ((int)(value * multiplier) + 1) / multiplier;
|
||||
}
|
||||
|
||||
/*inline std::ostream& operator<<(std::ostream& out, const mat4x4& v) {
|
||||
return out << "\rMatrix4x4{" << v.m00() << ", " << v.m01() << ", " << v.m02() << ", " << v.m03() << "} \n"\
|
||||
<< " {" << v.m10() << ", " << v.m11() << ", " << v.m12() << ", " << v.m13() << "} \n"\
|
||||
|
|
|
@ -7,6 +7,8 @@
|
|||
#ifndef BLT_TESTS_NBT_H
|
||||
#define BLT_TESTS_NBT_H
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "blt/std/format.h"
|
||||
#include "blt/std/filesystem.h"
|
||||
|
||||
|
@ -15,26 +17,43 @@ namespace blt::nbt {
|
|||
|
||||
std::string readUTF8String(std::fstream& stream);
|
||||
|
||||
enum nbt_type {
|
||||
tag_end = 0,
|
||||
tag_byte = 1,
|
||||
tag_short = 2,
|
||||
tag_int = 3,
|
||||
tag_long = 4,
|
||||
tag_float = 5,
|
||||
tag_double = 6,
|
||||
tag_byte_array = 7,
|
||||
tag_string = 8,
|
||||
tag_list = 9,
|
||||
tag_compound = 10,
|
||||
tag_int_array = 11,
|
||||
tag_long_array = 12
|
||||
enum class nbt_tag : char {
|
||||
END = 0,
|
||||
BYTE = 1,
|
||||
SHORT = 2,
|
||||
INT = 3,
|
||||
LONG = 4,
|
||||
FLOAT = 5,
|
||||
DOUBLE = 6,
|
||||
BYTE_ARRAY = 7,
|
||||
STRING = 8,
|
||||
LIST = 9,
|
||||
COMPOUND = 10,
|
||||
INT_ARRAY = 11,
|
||||
LONG_ARRAY = 12
|
||||
};
|
||||
|
||||
class nbt_tag {
|
||||
class tag {
|
||||
public:
|
||||
virtual void readTag() = 0;
|
||||
virtual void writeTag() = 0;
|
||||
virtual void writePayload(std::fstream& out) = 0;
|
||||
virtual void readPayload(std::fstream& in) = 0;
|
||||
};
|
||||
|
||||
class named_tag : public tag {
|
||||
private:
|
||||
std::string name;
|
||||
public:
|
||||
explicit named_tag(std::string name): name(std::move(name)) {}
|
||||
named_tag() = default;
|
||||
void writeName(std::fstream& out);
|
||||
void readName(std::fstream& in);
|
||||
};
|
||||
|
||||
class tag_end : public tag {
|
||||
public:
|
||||
void writePayload(std::fstream& out) final;
|
||||
// nothing to read
|
||||
void readPayload(std::fstream& in) final {}
|
||||
};
|
||||
|
||||
class NBTDecoder {
|
||||
|
@ -51,7 +70,6 @@ namespace blt::nbt {
|
|||
class NBTReader {
|
||||
private:
|
||||
std::string m_file;
|
||||
|
||||
public:
|
||||
explicit NBTReader(std::string file): m_file(std::move(file)) {}
|
||||
};
|
||||
|
|
|
@ -10,27 +10,9 @@
|
|||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <blt/math/math.h>
|
||||
|
||||
namespace blt::string {
|
||||
static inline constexpr double _static_pow(int p) {
|
||||
int collection = 1;
|
||||
for (int i = 0; i < p; i++)
|
||||
collection *= 10;
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a fast rounding function and is not guaranteed to be 100% correct
|
||||
* @tparam decimal_places
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
template<int decimal_places>
|
||||
static inline double round_up(double value) {
|
||||
constexpr double multiplier = _static_pow(decimal_places);
|
||||
return ((int)(value * multiplier) + 1) / multiplier;
|
||||
}
|
||||
|
||||
|
||||
static inline std::string fromBytes(unsigned long bytes){
|
||||
if (bytes > 1073741824) {
|
||||
|
@ -87,8 +69,7 @@ namespace blt::string {
|
|||
|
||||
// taken from java, adapted for c++.
|
||||
static inline utf8_string createUTFString(const std::string& str) {
|
||||
|
||||
const unsigned int strlen = (unsigned int) str.size();
|
||||
const auto strlen = (unsigned int) str.size();
|
||||
unsigned int utflen = strlen;
|
||||
|
||||
for (unsigned int i = 0; i < strlen; i++) {
|
||||
|
|
|
@ -58,39 +58,68 @@ namespace blt {
|
|||
* Creates an encapsulation of a T array which will be automatically deleted when this object goes out of scope.
|
||||
* This is a simple buffer meant to be used only inside of a function and not moved around, with a few minor exceptions.
|
||||
* The internal buffer is allocated on the heap.
|
||||
* The operator * has been overloaded to return the internal buffer. (or just use scoped_buffer.buffer if you wish to be explicit)
|
||||
* The operator & was not used because I think it is stupid to do so.
|
||||
* "*" on a reference is fine however since it isn't used to dereference in the case.
|
||||
* The operator * has been overloaded to return the internal buffer.
|
||||
* @tparam T type that is stored in buffer eg char
|
||||
*/
|
||||
template<typename T>
|
||||
struct scoped_buffer {
|
||||
T* buffer;
|
||||
unsigned long size;
|
||||
|
||||
explicit scoped_buffer(unsigned long size): size(size) {
|
||||
buffer = new T[size];
|
||||
}
|
||||
|
||||
scoped_buffer(scoped_buffer& copy) = delete;
|
||||
|
||||
scoped_buffer(scoped_buffer&& move) = delete;
|
||||
|
||||
scoped_buffer operator=(scoped_buffer& copyAssignment) = delete;
|
||||
|
||||
scoped_buffer operator=(scoped_buffer&& moveAssignment) = delete;
|
||||
|
||||
inline T& operator[](unsigned long index) const {
|
||||
return buffer[index];
|
||||
}
|
||||
|
||||
inline T* operator*(){
|
||||
return buffer;
|
||||
}
|
||||
|
||||
~scoped_buffer() {
|
||||
delete[] buffer;
|
||||
}
|
||||
private:
|
||||
T* _buffer;
|
||||
size_t _size;
|
||||
public:
|
||||
explicit scoped_buffer(size_t size): _size(size) {
|
||||
_buffer = new T[size];
|
||||
}
|
||||
|
||||
scoped_buffer(const scoped_buffer& copy) = delete;
|
||||
|
||||
scoped_buffer(scoped_buffer&& move) noexcept {
|
||||
_buffer = move._buffer;
|
||||
_size = move.size();
|
||||
move._buffer = nullptr;
|
||||
}
|
||||
|
||||
scoped_buffer operator=(scoped_buffer& copyAssignment) = delete;
|
||||
|
||||
scoped_buffer& operator=(scoped_buffer&& moveAssignment) noexcept {
|
||||
_buffer = moveAssignment._buffer;
|
||||
_size = moveAssignment.size();
|
||||
moveAssignment._buffer = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline T& operator[](unsigned long index) {
|
||||
return _buffer[index];
|
||||
}
|
||||
|
||||
inline const T& operator[](unsigned long index) const {
|
||||
return _buffer[index];
|
||||
}
|
||||
|
||||
inline T* operator*(){
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
[[nodiscard]] inline size_t size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
inline T* ptr(){
|
||||
return _buffer;
|
||||
}
|
||||
|
||||
ptr_iterator<T> begin(){
|
||||
return ptr_iterator{_buffer};
|
||||
}
|
||||
|
||||
ptr_iterator<T> end(){
|
||||
return ptr_iterator{&_buffer[_size]};
|
||||
}
|
||||
|
||||
~scoped_buffer() {
|
||||
delete[] _buffer;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
@ -168,6 +197,7 @@ namespace blt {
|
|||
delete[] _values;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif //BLT_TESTS_MEMORY_H
|
||||
|
|
|
@ -29,6 +29,14 @@ namespace blt::system {
|
|||
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
static inline auto nanoTime() {
|
||||
return getCurrentTimeNanoseconds();
|
||||
}
|
||||
|
||||
static inline auto getCurrentTimeMilliseconds(){
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard time string is formatted as follows:
|
||||
* Year-Month-Date Hour:Min:Second
|
||||
|
|
|
@ -23,8 +23,20 @@ namespace blt::nbt {
|
|||
|
||||
stream.read(str.characters, str.size);
|
||||
|
||||
auto strOut = std::move(blt::string::getStringFromUTF8(str));
|
||||
auto strOut = blt::string::getStringFromUTF8(str);
|
||||
delete[] str.characters;
|
||||
return strOut;
|
||||
}
|
||||
|
||||
void named_tag::writeName(std::fstream& out) {
|
||||
writeUTF8String(out, name);
|
||||
}
|
||||
|
||||
void named_tag::readName(std::fstream& in) {
|
||||
name = readUTF8String(in);
|
||||
}
|
||||
|
||||
void tag_end::writePayload(std::fstream& out) {
|
||||
out.put('\0');
|
||||
}
|
||||
}
|
|
@ -9,8 +9,88 @@
|
|||
#include "blt/math/matrix.h"
|
||||
#include <bitset>
|
||||
#include "hashmap_tests.h"
|
||||
#include <functional>
|
||||
|
||||
int main() {
|
||||
std::function<int(int i)> test{
|
||||
[](int i) -> int {
|
||||
int acc = 1;
|
||||
for (int j = 0; j < i; j++){
|
||||
acc += j * i;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
};
|
||||
|
||||
int test_as_func(int i){
|
||||
int acc = 1;
|
||||
for (int j = 0; j < i; j++){
|
||||
acc += j * i;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
|
||||
class super_func {
|
||||
public:
|
||||
virtual int test(int i) = 0;
|
||||
virtual ~super_func() = default;
|
||||
};
|
||||
|
||||
class class_func : public super_func {
|
||||
public:
|
||||
int test(int i) override {
|
||||
int acc = 1;
|
||||
for (int j = 0; j < i; j++){
|
||||
acc += j * i;
|
||||
}
|
||||
return acc;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
if (argc > 1 && std::string(argv[1]) == "--no_color") {
|
||||
for (int i = (int)blt::logging::log_level::NONE; i < (int)blt::logging::log_level::FATAL; i++) {
|
||||
blt::logging::setLogColor((blt::logging::log_level)i, "");
|
||||
}
|
||||
blt::logging::setLogOutputFormat("[${{TIME}}] [${{LOG_LEVEL}}] (${{FILE}}:${{LINE}}) ${{STR}}\n");
|
||||
}
|
||||
|
||||
class_func* funy = new class_func;
|
||||
super_func* virtual_funy = new class_func;
|
||||
|
||||
int num_of_tests = 100000;
|
||||
int acc = 1;
|
||||
BLT_START_INTERVAL("Functions Test", "std::function");
|
||||
acc = 1;
|
||||
for (int i = 0; i < num_of_tests; i++){
|
||||
acc = test(acc);
|
||||
}
|
||||
BLT_END_INTERVAL("Functions Test", "std::function");
|
||||
|
||||
BLT_START_INTERVAL("Functions Test", "normal function");
|
||||
acc = 1;
|
||||
for (int i = 0; i < num_of_tests; i++){
|
||||
acc = test_as_func(acc);
|
||||
}
|
||||
BLT_END_INTERVAL("Functions Test", "normal function");
|
||||
|
||||
BLT_START_INTERVAL("Functions Test", "virtual class direct");
|
||||
acc = 1;
|
||||
for (int i = 0; i < num_of_tests; i++){
|
||||
acc = funy->test(acc);
|
||||
}
|
||||
BLT_END_INTERVAL("Functions Test", "virtual class direct");
|
||||
|
||||
BLT_START_INTERVAL("Functions Test", "virtual class");
|
||||
acc = 1;
|
||||
for (int i = 0; i < num_of_tests; i++){
|
||||
acc = virtual_funy->test(acc);
|
||||
}
|
||||
BLT_END_INTERVAL("Functions Test", "virtual class");
|
||||
|
||||
BLT_PRINT_PROFILE("Functions Test");
|
||||
delete virtual_funy;
|
||||
|
||||
binaryTreeTest();
|
||||
|
||||
run_logging();
|
||||
|
|
|
@ -14,12 +14,12 @@
|
|||
#include <blt/std/filesystem.h>
|
||||
|
||||
inline bool readLargeBlockUsingNBTBufferedReader(const std::string& file, const blt::scoped_buffer<char>& bufferToCompare, size_t bufferSize) {
|
||||
blt::scoped_buffer<char> read_buffer{bufferToCompare.size};
|
||||
blt::scoped_buffer<char> read_buffer{bufferToCompare.size()};
|
||||
std::fstream largeBlockInputLarge(file, std::ios::in | std::ios::binary);
|
||||
blt::fs::fstream_block_reader byteLargeBlockInputLarge(largeBlockInputLarge, bufferSize);
|
||||
byteLargeBlockInputLarge.read(read_buffer.buffer, bufferToCompare.size);
|
||||
for (unsigned int i = 0; i < bufferToCompare.size; i++) {
|
||||
if (read_buffer[i] != bufferToCompare.buffer[i])
|
||||
byteLargeBlockInputLarge.read(*read_buffer, bufferToCompare.size());
|
||||
for (unsigned int i = 0; i < bufferToCompare.size(); i++) {
|
||||
if (read_buffer[i] != bufferToCompare[i])
|
||||
return false;
|
||||
}
|
||||
largeBlockInputLarge.close();
|
||||
|
@ -29,7 +29,7 @@ inline bool readLargeBlockUsingNBTBufferedReader(const std::string& file, const
|
|||
inline bool readIndividualUsingNBTBufferedReader(const std::string& file, const blt::scoped_buffer<char>& bufferToCompare, size_t bufferSize) {
|
||||
std::fstream largeBlockInput(file, std::ios::in | std::ios::binary);
|
||||
blt::fs::fstream_block_reader byteLargeBlockInput(largeBlockInput, bufferSize);
|
||||
for (unsigned int i = 0; i < bufferToCompare.size; i++) {
|
||||
for (unsigned int i = 0; i < bufferToCompare.size(); i++) {
|
||||
char byte;
|
||||
byteLargeBlockInput.read(&byte, 1);
|
||||
if (byte != bufferToCompare[i]) {
|
||||
|
@ -52,11 +52,11 @@ inline void nbt_read_tests(){
|
|||
bool fstream_large_correct = true;
|
||||
|
||||
for (int i = 0; i < bufferSize; i++)
|
||||
buffer.buffer[i] = i+1;
|
||||
buffer[i] = i + 1;
|
||||
|
||||
BLT_START_INTERVAL("nbt read", "Raw Write");
|
||||
std::fstream largeOutput("HeyThere.txt", std::ios::out | std::ios::binary);
|
||||
largeOutput.write(buffer.buffer, bufferSize);
|
||||
largeOutput.write(*buffer, bufferSize);
|
||||
largeOutput.flush();
|
||||
largeOutput.close();
|
||||
BLT_END_INTERVAL("nbt read", "Raw Write");
|
||||
|
@ -129,7 +129,7 @@ inline void nbt_write_tests(){
|
|||
blt::scoped_buffer<char> read_buffer{bufferSize};
|
||||
|
||||
for (int i = 0; i < bufferSize; i++)
|
||||
buffer.buffer[i] = i+1;
|
||||
buffer[i] = i + 1;
|
||||
|
||||
std::fstream fileOutput("IAmAFile.txt", std::ios::binary | std::ios::out);
|
||||
for (int i = 0; i < 8; i++) {
|
||||
|
@ -139,11 +139,11 @@ inline void nbt_write_tests(){
|
|||
blt::fs::fstream_block_writer writer(fileOutput, size);
|
||||
|
||||
BLT_START_INTERVAL("nbt write block", profiler_string);
|
||||
writer.write(buffer.buffer, buffer.size);
|
||||
writer.write(*buffer, buffer.size());
|
||||
BLT_END_INTERVAL("nbt write block", profiler_string);
|
||||
BLT_START_INTERVAL("nbt write individual", profiler_string);
|
||||
for (int j = 0; j < bufferSize; j++) {
|
||||
writer.write(&buffer.buffer[j], 1);
|
||||
writer.write(&buffer[j], 1);
|
||||
}
|
||||
BLT_END_INTERVAL("nbt write individual", profiler_string);
|
||||
}
|
||||
|
@ -155,7 +155,7 @@ inline void nbt_write_tests(){
|
|||
auto size = (size_t) std::pow(2, 11 + i);
|
||||
auto size_str = std::to_string(size);
|
||||
bool results = true;
|
||||
fileInput.read(read_buffer.buffer, bufferSize);
|
||||
fileInput.read(*read_buffer, bufferSize);
|
||||
for (int j = 0; j < bufferSize; j++) {
|
||||
if (buffer[j] != read_buffer[j]) {
|
||||
results = false;
|
||||
|
@ -166,7 +166,7 @@ inline void nbt_write_tests(){
|
|||
BLT_INFO("NBT %s Block Write Correctly? %s;\n", size_str.c_str(), results ? "True" : "False");
|
||||
|
||||
results = true;
|
||||
fileInput.read(read_buffer.buffer, bufferSize);
|
||||
fileInput.read(*read_buffer, bufferSize);
|
||||
for (int j = 0; j < bufferSize; j++) {
|
||||
if (buffer[j] != read_buffer[j]) {
|
||||
results = false;
|
||||
|
|
Loading…
Reference in New Issue