readme update plus time functions and cleaner scoped_buffer WITH begin()/end()

scoped_buffer update will break most uses
v1
Brett 2023-07-24 02:39:03 -04:00
parent 20f8238f70
commit 0d5abd143f
5 changed files with 77 additions and 51 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(BLT VERSION 0.6.0) project(BLT VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)

View File

@ -1,4 +1,4 @@
# **BLT v0.6.0a** # **BLT v0.6.1a**
A common utilities library I find useful A common utilities library I find useful
![Icon](icon_large.png) ![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 - ## Data Structures
- Queue / Stack - Queue / Stack
- faster than std::queue / std::stack - faster than std::queue / std::stack
- Binary Tree - backed by a contiguous array
- Hashmap (TODO)
- ## Utility - ## Utility
- Simple Random Interface - Simple Random Wrapper Interface
- No more worrying about min/max bounds! - Simple random functions based on the PCG Hash
- ### String Functions - ### String Functions
- starts_with - starts_with
- ends_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 - split
- trim - trim
- Logging - Logging
- Trace / Debug / Info / Warn / Error / Fatal - See blt::logging section above
- Log to file
- Log to console with color!
- Easy to disable for release
- define BLT_DISABLE_LOGGING before including the logging.h file.
- Time - Time
- Current time in nanoseconds (without all the c++ gobbledygook) - 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 - Formatted time string with year/month/date + current time
- ## Profiling - ## Profiling
- Basic profiler - Basic profiler with history and formatted output
- WIP

View File

@ -65,11 +65,12 @@ namespace blt {
*/ */
template<typename T> template<typename T>
struct scoped_buffer { struct scoped_buffer {
T* buffer; private:
unsigned long size; T* _buffer;
size_t _size;
explicit scoped_buffer(unsigned long size): size(size) { public:
buffer = new T[size]; explicit scoped_buffer(size_t size): _size(size) {
_buffer = new T[size];
} }
scoped_buffer(scoped_buffer& copy) = delete; scoped_buffer(scoped_buffer& copy) = delete;
@ -80,16 +81,36 @@ namespace blt {
scoped_buffer operator=(scoped_buffer&& moveAssignment) = delete; scoped_buffer operator=(scoped_buffer&& moveAssignment) = delete;
inline T& operator[](unsigned long index) const { inline T& operator[](unsigned long index) {
return buffer[index]; return _buffer[index];
}
inline const T& operator[](unsigned long index) const {
return _buffer[index];
} }
inline T* operator*(){ inline T* operator*(){
return buffer; 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() { ~scoped_buffer() {
delete[] buffer; delete[] _buffer;
} }
}; };

View File

@ -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(); 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: * Standard time string is formatted as follows:
* Year-Month-Date Hour:Min:Second * Year-Month-Date Hour:Min:Second

View File

@ -14,12 +14,12 @@
#include <blt/std/filesystem.h> #include <blt/std/filesystem.h>
inline bool readLargeBlockUsingNBTBufferedReader(const std::string& file, const blt::scoped_buffer<char>& bufferToCompare, size_t bufferSize) { 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); std::fstream largeBlockInputLarge(file, std::ios::in | std::ios::binary);
blt::fs::fstream_block_reader byteLargeBlockInputLarge(largeBlockInputLarge, bufferSize); blt::fs::fstream_block_reader byteLargeBlockInputLarge(largeBlockInputLarge, bufferSize);
byteLargeBlockInputLarge.read(read_buffer.buffer, bufferToCompare.size); byteLargeBlockInputLarge.read(*read_buffer, bufferToCompare.size());
for (unsigned int i = 0; i < bufferToCompare.size; i++) { for (unsigned int i = 0; i < bufferToCompare.size(); i++) {
if (read_buffer[i] != bufferToCompare.buffer[i]) if (read_buffer[i] != bufferToCompare[i])
return false; return false;
} }
largeBlockInputLarge.close(); 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) { 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); std::fstream largeBlockInput(file, std::ios::in | std::ios::binary);
blt::fs::fstream_block_reader byteLargeBlockInput(largeBlockInput, bufferSize); 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; char byte;
byteLargeBlockInput.read(&byte, 1); byteLargeBlockInput.read(&byte, 1);
if (byte != bufferToCompare[i]) { if (byte != bufferToCompare[i]) {
@ -52,11 +52,11 @@ inline void nbt_read_tests(){
bool fstream_large_correct = true; bool fstream_large_correct = true;
for (int i = 0; i < bufferSize; i++) for (int i = 0; i < bufferSize; i++)
buffer.buffer[i] = i+1; buffer[i] = i + 1;
BLT_START_INTERVAL("nbt read", "Raw Write"); BLT_START_INTERVAL("nbt read", "Raw Write");
std::fstream largeOutput("HeyThere.txt", std::ios::out | std::ios::binary); std::fstream largeOutput("HeyThere.txt", std::ios::out | std::ios::binary);
largeOutput.write(buffer.buffer, bufferSize); largeOutput.write(*buffer, bufferSize);
largeOutput.flush(); largeOutput.flush();
largeOutput.close(); largeOutput.close();
BLT_END_INTERVAL("nbt read", "Raw Write"); BLT_END_INTERVAL("nbt read", "Raw Write");
@ -129,7 +129,7 @@ inline void nbt_write_tests(){
blt::scoped_buffer<char> read_buffer{bufferSize}; blt::scoped_buffer<char> read_buffer{bufferSize};
for (int i = 0; i < bufferSize; i++) 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); std::fstream fileOutput("IAmAFile.txt", std::ios::binary | std::ios::out);
for (int i = 0; i < 8; i++) { 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::fs::fstream_block_writer writer(fileOutput, size);
BLT_START_INTERVAL("nbt write block", profiler_string); 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_END_INTERVAL("nbt write block", profiler_string);
BLT_START_INTERVAL("nbt write individual", profiler_string); BLT_START_INTERVAL("nbt write individual", profiler_string);
for (int j = 0; j < bufferSize; j++) { 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); 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 = (size_t) std::pow(2, 11 + i);
auto size_str = std::to_string(size); auto size_str = std::to_string(size);
bool results = true; bool results = true;
fileInput.read(read_buffer.buffer, bufferSize); fileInput.read(*read_buffer, bufferSize);
for (int j = 0; j < bufferSize; j++) { for (int j = 0; j < bufferSize; j++) {
if (buffer[j] != read_buffer[j]) { if (buffer[j] != read_buffer[j]) {
results = false; 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"); BLT_INFO("NBT %s Block Write Correctly? %s;\n", size_str.c_str(), results ? "True" : "False");
results = true; results = true;
fileInput.read(read_buffer.buffer, bufferSize); fileInput.read(*read_buffer, bufferSize);
for (int j = 0; j < bufferSize; j++) { for (int j = 0; j < bufferSize; j++) {
if (buffer[j] != read_buffer[j]) { if (buffer[j] != read_buffer[j]) {
results = false; results = false;