From 0d5abd143f9413e9770fee067b4b888627c5e382 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 24 Jul 2023 02:39:03 -0400 Subject: [PATCH] readme update plus time functions and cleaner scoped_buffer WITH begin()/end() scoped_buffer update will break most uses --- CMakeLists.txt | 2 +- README.md | 21 +++++------- include/blt/std/memory.h | 73 ++++++++++++++++++++++++++-------------- include/blt/std/time.h | 8 +++++ src/tests/nbt_tests.h | 24 ++++++------- 5 files changed, 77 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fcbf4fd..9fcf8fb 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/README.md b/README.md index 55e0c1b..dc17ad6 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# **BLT v0.6.0a** +# **BLT v0.6.1a** A common utilities library I find useful ![Icon](icon_large.png) @@ -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::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 \ No newline at end of file + - Basic profiler with history and formatted output \ No newline at end of file diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index 9f8f974..055b495 100755 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -65,32 +65,53 @@ namespace blt { */ template 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(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) { + 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 begin(){ + return ptr_iterator{_buffer}; + } + + ptr_iterator end(){ + return ptr_iterator{&_buffer[_size]}; + } + + ~scoped_buffer() { + delete[] _buffer; + } }; template diff --git a/include/blt/std/time.h b/include/blt/std/time.h index edeafcb..292a0ca 100755 --- a/include/blt/std/time.h +++ b/include/blt/std/time.h @@ -29,6 +29,14 @@ namespace blt::system { return std::chrono::duration_cast(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::high_resolution_clock::now().time_since_epoch()).count(); + } + /** * Standard time string is formatted as follows: * Year-Month-Date Hour:Min:Second diff --git a/src/tests/nbt_tests.h b/src/tests/nbt_tests.h index bb2580f..3eb8e12 100755 --- a/src/tests/nbt_tests.h +++ b/src/tests/nbt_tests.h @@ -14,12 +14,12 @@ #include inline bool readLargeBlockUsingNBTBufferedReader(const std::string& file, const blt::scoped_buffer& bufferToCompare, size_t bufferSize) { - blt::scoped_buffer read_buffer{bufferToCompare.size}; + blt::scoped_buffer 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& 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 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;