QoL improvements

This commit is contained in:
Alex
2023-03-27 20:11:32 +03:00
parent 3eb6923374
commit 93afcd2210
59 changed files with 612 additions and 424 deletions

View File

@ -64,11 +64,11 @@ enum MemoryOrder
template <typename T>
class Atomic
{
_Atomic(T) m_Value;
_Atomic(T) Value;
public:
Atomic() : m_Value(0) {}
Atomic(T Init) : m_Value(Init) {}
Atomic() : Value(0) {}
Atomic(T Init) : Value(Init) {}
/**
* @brief Load the value of the atomic variable
@ -78,7 +78,7 @@ public:
*/
T Load(MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic_n(load)(&m_Value, Order);
return builtin_atomic_n(load)(&this->Value, Order);
}
/**
@ -89,7 +89,7 @@ public:
*/
void Store(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic_n(store)(&m_Value, v, Order);
return builtin_atomic_n(store)(&this->Value, v, Order);
}
/**
@ -101,7 +101,7 @@ public:
*/
T Exchange(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic_n(exchange)(&m_Value, v, Order);
return builtin_atomic_n(exchange)(&this->Value, v, Order);
}
/**
@ -115,7 +115,7 @@ public:
*/
bool CompareExchange(T &Expected, T Desired, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic_n(compare_exchange)(&m_Value, &Expected, Desired, true, Order, Order);
return builtin_atomic_n(compare_exchange)(&this->Value, &Expected, Desired, true, Order, Order);
}
/**
@ -127,7 +127,7 @@ public:
*/
T FetchAdd(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic(fetch_add)(&m_Value, v, Order);
return builtin_atomic(fetch_add)(&this->Value, v, Order);
}
/**
@ -139,7 +139,7 @@ public:
*/
T FetchSub(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic(fetch_sub)(&m_Value, v, Order);
return builtin_atomic(fetch_sub)(&this->Value, v, Order);
}
/**
@ -151,7 +151,7 @@ public:
*/
T FetchAnd(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic(fetch_and)(&m_Value, v, Order);
return builtin_atomic(fetch_and)(&this->Value, v, Order);
}
/**
@ -163,7 +163,7 @@ public:
*/
T FetchOr(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic(fetch_or)(&m_Value, v, Order);
return builtin_atomic(fetch_or)(&this->Value, v, Order);
}
/**
@ -175,7 +175,7 @@ public:
*/
T FetchXor(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic(fetch_xor)(&m_Value, v, Order);
return builtin_atomic(fetch_xor)(&this->Value, v, Order);
}
/**
@ -187,7 +187,7 @@ public:
*/
T FetchNand(T v, MemoryOrder Order = MemoryOrder::SeqCst)
{
return builtin_atomic(fetch_nand)(&m_Value, v, Order);
return builtin_atomic(fetch_nand)(&this->Value, v, Order);
}
operator bool() { return this->Load() != 0; }

View File

@ -23,8 +23,8 @@ struct BootInfo
struct FramebufferInfo
{
void *BaseAddress;
__UINT64_TYPE__ Width;
__UINT64_TYPE__ Height;
__UINT32_TYPE__ Width;
__UINT32_TYPE__ Height;
__UINT64_TYPE__ Pitch;
__UINT16_TYPE__ BitsPerPixel;
__UINT8_TYPE__ MemoryModel;

View File

@ -397,7 +397,7 @@ namespace CPU
SafeFunction static inline void wrmsr(uint32_t msr, uint64_t Value)
{
uint32_t Low = Value, High = Value >> 32;
uint32_t Low = s_cst(uint32_t, Value), High = s_cst(uint32_t, Value >> 32);
asmv("wrmsr"
:
: "c"(msr), "a"(Low), "d"(High)

View File

@ -38,7 +38,7 @@ namespace Interrupts
* @param InterruptNumber The interrupt number. NOT the IRQ number! (IRQ0 != 32)
*/
void SetInterruptNumber(int InterruptNumber) { this->InterruptNumber = InterruptNumber; }
int GetInterruptNumber() { return InterruptNumber; }
int GetInterruptNumber() { return this->InterruptNumber; }
/**
* @brief Create a new interrupt handler.

View File

@ -28,7 +28,7 @@ namespace NetworkInterfaceManager
void *DriverCallBackAddress;
/** @brief Reserved */
unsigned int DriverID;
unsigned long DriverID;
};
class Events

View File

@ -16,16 +16,6 @@ static inline void DbgNetwork() { return; }
static inline void DbgDumpData(const char *Description, void *Address, unsigned long Length) { return; }
#endif
// TODO: How i would do this?
typedef __UINT64_TYPE__ uint48_t;
#define b4(x) ((x & 0x0F) << 4 | (x & 0xF0) >> 4)
#define b8(x) ((x)&0xFF)
#define b16(x) __builtin_bswap16(x)
#define b32(x) __builtin_bswap32(x)
#define b48(x) (((((x)&0x0000000000ff) << 40) | (((x)&0x00000000ff00) << 24) | (((x)&0x000000ff0000) << 8) | (((x)&0x0000ff000000) >> 8) | (((x)&0x00ff00000000) >> 24) | (((x)&0xff0000000000) >> 40)))
#define b64(x) __builtin_bswap64(x)
enum Endianness
{
LITTLE_ENDIAN,
@ -149,10 +139,10 @@ struct InternetProtocol
inline uint32_t ToHex()
{
return ((uint64_t)this->Address[0] << 24) |
((uint64_t)this->Address[1] << 16) |
((uint64_t)this->Address[2] << 8) |
((uint64_t)this->Address[3]);
return ((uint32_t)this->Address[0] << 24) |
((uint32_t)this->Address[1] << 16) |
((uint32_t)this->Address[2] << 8) |
((uint32_t)this->Address[3]);
}
inline InternetProtocol::Version4 FromHex(uint32_t Hex)

View File

@ -95,8 +95,8 @@ extern "C"
*
* @param c the single character to print
*/
PRINTF_VISIBILITY
void putchar(char c);
// PRINTF_VISIBILITY
// void putchar(char c);
/**
* An implementation of the C standard's printf/vprintf

View File

@ -31,7 +31,7 @@ struct CPUData
uintptr_t Stack;
/** @brief CPU ID. */
long ID;
int ID;
/** @brief Local CPU error code. */
long ErrorCode;

View File

@ -17,15 +17,15 @@ namespace std
const uint8_t *data = reinterpret_cast<const uint8_t *>(&key);
const size_t size = sizeof(Key);
uint64_t hash = fnv_offset_basis;
uint64_t ret = fnv_offset_basis;
for (size_t i = 0; i < size; ++i)
{
hash ^= static_cast<uint64_t>(data[i]);
hash *= fnv_prime;
ret ^= static_cast<uint64_t>(data[i]);
ret *= fnv_prime;
}
return static_cast<size_t>(hash);
return static_cast<size_t>(ret);
}
};
}

View File

@ -31,37 +31,37 @@ namespace std
template <class T>
class smart_ptr
{
T *m_RealPointer;
T *RealPointer;
public:
explicit smart_ptr(T *Pointer = nullptr)
{
spdbg("Smart pointer created (%#lx)", m_RealPointer);
m_RealPointer = Pointer;
spdbg("Smart pointer created (%#lx)", this->RealPointer);
this->RealPointer = Pointer;
}
~smart_ptr()
{
spdbg("Smart pointer deleted (%#lx)", m_RealPointer);
delete m_RealPointer, m_RealPointer = nullptr;
spdbg("Smart pointer deleted (%#lx)", this->RealPointer);
delete this->RealPointer, this->RealPointer = nullptr;
}
T &operator*()
{
spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer);
return *m_RealPointer;
spdbg("Smart pointer dereferenced (%#lx)", this->RealPointer);
return *this->RealPointer;
}
T *operator->()
{
spdbg("Smart pointer dereferenced (%#lx)", m_RealPointer);
return m_RealPointer;
spdbg("Smart pointer dereferenced (%#lx)", this->RealPointer);
return this->RealPointer;
}
T *get()
{
spdbg("Smart pointer returned (%#lx)", m_RealPointer);
return m_RealPointer;
spdbg("Smart pointer returned (%#lx)", this->RealPointer);
return this->RealPointer;
}
};
@ -87,148 +87,148 @@ namespace std
class counter
{
private:
unsigned int m_RefCount{};
unsigned int RefCount{};
public:
counter() : m_RefCount(0) { spdbg("Counter %#lx created", this); };
counter() : RefCount(0) { spdbg("Counter %#lx created", this); };
counter(const counter &) = delete;
counter &operator=(const counter &) = delete;
~counter() { spdbg("Counter %#lx deleted", this); }
void reset()
{
m_RefCount = 0;
this->RefCount = 0;
spdbg("reset");
}
unsigned int get()
{
return m_RefCount;
return this->RefCount;
spdbg("return");
}
void operator++()
{
m_RefCount++;
this->RefCount++;
spdbg("increment");
}
void operator++(int)
{
m_RefCount++;
this->RefCount++;
spdbg("increment");
}
void operator--()
{
m_RefCount--;
this->RefCount--;
spdbg("decrement");
}
void operator--(int)
{
m_RefCount--;
this->RefCount--;
spdbg("decrement");
}
};
counter *m_ReferenceCounter;
T *m_RealPointer;
counter *ReferenceCounter;
T *RealPointer;
public:
explicit shared_ptr(T *Pointer = nullptr)
{
m_RealPointer = Pointer;
m_ReferenceCounter = new counter();
spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter);
this->RealPointer = Pointer;
this->ReferenceCounter = new counter();
spdbg("[%#lx] Shared pointer created (ptr=%#lx, ref=%#lx)", this, Pointer, this->ReferenceCounter);
if (Pointer)
(*m_ReferenceCounter)++;
(*this->ReferenceCounter)++;
}
shared_ptr(shared_ptr<T> &SPtr)
{
spdbg("[%#lx] Shared pointer copied (ptr=%#lx, ref=%#lx)", this, SPtr.m_RealPointer, SPtr.m_ReferenceCounter);
m_RealPointer = SPtr.m_RealPointer;
m_ReferenceCounter = SPtr.m_ReferenceCounter;
(*m_ReferenceCounter)++;
spdbg("[%#lx] Shared pointer copied (ptr=%#lx, ref=%#lx)", this, SPtr.RealPointer, SPtr.ReferenceCounter);
this->RealPointer = SPtr.RealPointer;
this->ReferenceCounter = SPtr.ReferenceCounter;
(*this->ReferenceCounter)++;
}
~shared_ptr()
{
spdbg("[%#lx] Shared pointer destructor called", this);
(*m_ReferenceCounter)--;
if (m_ReferenceCounter->get() == 0)
(*this->ReferenceCounter)--;
if (this->ReferenceCounter->get() == 0)
{
spdbg("[%#lx] Shared pointer deleted (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter);
delete m_ReferenceCounter, m_ReferenceCounter = nullptr;
delete m_RealPointer, m_RealPointer = nullptr;
spdbg("[%#lx] Shared pointer deleted (ptr=%#lx, ref=%#lx)", this, this->RealPointer, this->ReferenceCounter);
delete this->ReferenceCounter, this->ReferenceCounter = nullptr;
delete this->RealPointer, this->RealPointer = nullptr;
}
}
unsigned int get_count()
{
spdbg("[%#lx] Shared pointer count (%d)", this, m_ReferenceCounter->get());
return m_ReferenceCounter->get();
spdbg("[%#lx] Shared pointer count (%d)", this, this->ReferenceCounter->get());
return this->ReferenceCounter->get();
}
T *get()
{
spdbg("[%#lx] Shared pointer get (%#lx)", this, m_RealPointer);
return m_RealPointer;
spdbg("[%#lx] Shared pointer get (%#lx)", this, this->RealPointer);
return this->RealPointer;
}
T &operator*()
{
spdbg("[%#lx] Shared pointer dereference (ptr*=%#lx)", this, *m_RealPointer);
return *m_RealPointer;
spdbg("[%#lx] Shared pointer dereference (ptr*=%#lx)", this, *this->RealPointer);
return *this->RealPointer;
}
T *operator->()
{
spdbg("[%#lx] Shared pointer dereference (ptr->%#lx)", this, m_RealPointer);
return m_RealPointer;
spdbg("[%#lx] Shared pointer dereference (ptr->%#lx)", this, this->RealPointer);
return this->RealPointer;
}
void reset(T *Pointer = nullptr)
{
if (m_RealPointer == Pointer)
if (this->RealPointer == Pointer)
return;
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, m_ReferenceCounter);
(*m_ReferenceCounter)--;
if (m_ReferenceCounter->get() == 0)
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, Pointer, this->ReferenceCounter);
(*this->ReferenceCounter)--;
if (this->ReferenceCounter->get() == 0)
{
delete m_RealPointer;
delete m_ReferenceCounter;
delete this->RealPointer;
delete this->ReferenceCounter;
}
m_RealPointer = Pointer;
m_ReferenceCounter = new counter();
this->RealPointer = Pointer;
this->ReferenceCounter = new counter();
if (Pointer)
(*m_ReferenceCounter)++;
(*this->ReferenceCounter)++;
}
void reset()
{
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, m_RealPointer, m_ReferenceCounter);
if (m_ReferenceCounter->get() == 1)
spdbg("[%#lx] Shared pointer reset (ptr=%#lx, ref=%#lx)", this, this->RealPointer, this->ReferenceCounter);
if (this->ReferenceCounter->get() == 1)
{
delete m_RealPointer, m_RealPointer = nullptr;
delete m_ReferenceCounter, m_ReferenceCounter = nullptr;
delete this->RealPointer, this->RealPointer = nullptr;
delete this->ReferenceCounter, this->ReferenceCounter = nullptr;
}
else
{
(*m_ReferenceCounter)--;
(*this->ReferenceCounter)--;
}
}
void swap(shared_ptr<T> &Other)
{
spdbg("[%#lx] Shared pointer swap (ptr=%#lx, ref=%#lx <=> ptr=%#lx, ref=%#lx)",
this, m_RealPointer, m_ReferenceCounter, Other.m_RealPointer, Other.m_ReferenceCounter);
T *tempRealPointer = m_RealPointer;
counter *tempReferenceCounter = m_ReferenceCounter;
m_RealPointer = Other.m_RealPointer;
m_ReferenceCounter = Other.m_ReferenceCounter;
Other.m_RealPointer = tempRealPointer;
Other.m_ReferenceCounter = tempReferenceCounter;
this, this->RealPointer, this->ReferenceCounter, Other.RealPointer, Other.ReferenceCounter);
T *tempRealPointer = this->RealPointer;
counter *tempReferenceCounter = this->ReferenceCounter;
this->RealPointer = Other.RealPointer;
this->ReferenceCounter = Other.ReferenceCounter;
Other.RealPointer = tempRealPointer;
Other.ReferenceCounter = tempReferenceCounter;
}
};

View File

@ -12,7 +12,7 @@ namespace std
public:
runtime_error(const char *what_arg) : m_what(what_arg) {}
const char *what() const { return m_what; }
const char *what() const { return this->m_what; }
};
}

View File

@ -25,85 +25,85 @@ namespace std
class string
{
private:
char *m_Data;
size_t m_Length;
size_t m_Capacity;
char *Data;
size_t Length;
size_t Capacity;
public:
static const size_t npos = -1;
string(const char *Str = "")
{
this->m_Length = strlen(Str);
this->m_Capacity = this->m_Length + 1;
this->m_Data = new char[m_Capacity];
strcpy(m_Data, Str);
strdbg("New string created: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length = strlen(Str);
this->Capacity = this->Length + 1;
this->Data = new char[this->Capacity];
strcpy(this->Data, Str);
strdbg("New string created: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
~string()
{
strdbg("String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
delete[] this->m_Data, this->m_Data = nullptr;
strdbg("String deleted: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
delete[] this->Data, this->Data = nullptr;
}
int length() const
size_t length() const
{
strdbg("String length: %d", this->m_Length);
return this->m_Length;
strdbg("String length: %d", this->Length);
return this->Length;
}
int capacity() const
size_t capacity() const
{
strdbg("String capacity: %d", this->m_Capacity);
return this->m_Capacity;
strdbg("String capacity: %d", this->Capacity);
return this->Capacity;
}
const char *c_str() const
{
strdbg("String data: \"%s\"", this->m_Data);
return this->m_Data;
strdbg("String data: \"%s\"", this->Data);
return this->Data;
}
void resize(size_t NewLength)
{
strdbg("String resize: %d", NewLength);
if (NewLength > this->m_Capacity)
if (NewLength > this->Capacity)
{
size_t newCapacity = NewLength + 1;
char *newData = new char[newCapacity];
strcpy(newData, this->m_Data);
strcpy(newData, this->Data);
strdbg("old: %#lx, new: %#lx", this->m_Data, newData);
delete[] this->m_Data;
this->m_Data = newData;
this->m_Capacity = newCapacity;
strdbg("old: %#lx, new: %#lx", this->Data, newData);
delete[] this->Data;
this->Data = newData;
this->Capacity = newCapacity;
}
this->m_Length = NewLength;
this->m_Data[m_Length] = '\0';
strdbg("String resized: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length = NewLength;
this->Data[this->Length] = '\0';
strdbg("String resized: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
void concat(const string &Other)
{
int NewLength = this->m_Length + Other.m_Length;
size_t NewLength = this->Length + Other.Length;
this->resize(NewLength);
strcat(m_Data, Other.m_Data);
strdbg("String concatenated: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strcat(this->Data, Other.Data);
strdbg("String concatenated: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
bool empty() const
{
strdbg("String empty: %d", this->m_Length == 0);
return this->m_Length == 0;
strdbg("String empty: %d", this->Length == 0);
return this->Length == 0;
}
size_t size() const
{
strdbg("String size: %d", this->m_Length);
return this->m_Length;
strdbg("String size: %d", this->Length);
return this->Length;
}
void clear()
@ -115,15 +115,15 @@ namespace std
size_t find(const char *Str, size_t Pos = 0) const
{
strdbg("String find: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = true;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i + j] != Str[j])
if (this->Data[i + j] != Str[j])
{
found = false;
break;
@ -144,35 +144,35 @@ namespace std
void erase(int Index, int Count = 1)
{
strdbg("String erase: %d, %d", Index, Count);
if (Index < 0 || (size_t)Index >= this->m_Length)
if (Index < 0 || (size_t)Index >= this->Length)
return;
if (Count < 0)
return;
if ((size_t)(Index + Count) > this->m_Length)
Count = this->m_Length - Index;
if ((size_t)(Index + Count) > this->Length)
Count = (int)this->Length - Index;
for (size_t i = Index; i < this->m_Length - Count; i++)
this->m_Data[i] = this->m_Data[i + Count];
for (size_t i = Index; i < this->Length - Count; i++)
this->Data[i] = this->Data[i + Count];
this->m_Length -= Count;
this->m_Data[m_Length] = '\0';
strdbg("String erased: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length -= Count;
this->Data[this->Length] = '\0';
strdbg("String erased: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
size_t find_last_not_of(const char *Str, size_t Pos = npos) const
{
strdbg("String find_last_not_of: \"%s\", %d", Str, Pos);
if (Pos == npos)
Pos = this->m_Length - 1;
Pos = this->Length - 1;
for (int i = (int)Pos; i >= 0; i--)
{
bool found = false;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -187,15 +187,15 @@ namespace std
size_t find_first_not_of(const char *Str, size_t Pos = 0) const
{
strdbg("String find_first_not_of: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = false;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -210,15 +210,15 @@ namespace std
size_t find_first_of(const char *Str, size_t Pos = 0) const
{
strdbg("String find_first_of: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = false;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -234,14 +234,14 @@ namespace std
{
strdbg("String find_last_of: \"%s\", %d", Str, Pos);
if (Pos == npos)
Pos = this->m_Length - 1;
Pos = this->Length - 1;
for (int i = (int)Pos; i >= 0; i--)
{
bool found = false;
for (int j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i] == Str[j])
if (this->Data[i] == Str[j])
{
found = true;
break;
@ -256,12 +256,12 @@ namespace std
size_t find_first_of(char C, size_t Pos = 0) const
{
strdbg("String find_first_of: '%c', %d", C, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
if (this->m_Data[i] == C)
if (this->Data[i] == C)
return i;
}
return npos;
@ -271,11 +271,11 @@ namespace std
{
strdbg("String find_last_of: '%c', %d", C, Pos);
if (Pos == npos)
Pos = this->m_Length - 1;
Pos = this->Length - 1;
for (int i = (int)Pos; i >= 0; i--)
{
if (this->m_Data[i] == C)
if (this->Data[i] == C)
return i;
}
return npos;
@ -284,15 +284,15 @@ namespace std
size_t substr(const char *Str, size_t Pos = 0) const
{
strdbg("String substr: \"%s\", %d", Str, Pos);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return npos;
for (size_t i = Pos; i < this->m_Length; i++)
for (size_t i = Pos; i < this->Length; i++)
{
bool found = true;
for (size_t j = 0; Str[j] != '\0'; j++)
{
if (this->m_Data[i + j] != Str[j])
if (this->Data[i + j] != Str[j])
{
found = false;
break;
@ -313,76 +313,76 @@ namespace std
string substr(size_t Pos = 0, size_t Count = npos) const
{
strdbg("String substr: %d, %d", Pos, Count);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return string();
if (Count == npos)
Count = this->m_Length - Pos;
Count = this->Length - Pos;
if (Pos + Count > this->m_Length)
Count = this->m_Length - Pos;
if (Pos + Count > this->Length)
Count = this->Length - Pos;
string ret;
ret.resize(Count);
for (size_t i = 0; i < Count; i++)
ret.m_Data[i] = this->m_Data[Pos + i];
ret.m_Data[Count] = '\0';
ret.Data[i] = this->Data[Pos + i];
ret.Data[Count] = '\0';
return ret;
}
void replace(size_t Pos, size_t Count, const char *Str)
{
strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str);
if (Pos >= this->m_Length)
if (Pos >= this->Length)
return;
if ((int64_t)Count < 0)
return;
if (Pos + Count > this->m_Length)
Count = this->m_Length - Pos;
if (Pos + Count > this->Length)
Count = this->Length - Pos;
int NewLength = this->m_Length - Count + strlen(Str);
size_t NewLength = this->Length - Count + strlen(Str);
this->resize(NewLength);
for (size_t i = this->m_Length - 1; i >= Pos + strlen(Str); i--)
this->m_Data[i] = this->m_Data[i - strlen(Str) + Count];
for (size_t i = this->Length - 1; i >= Pos + strlen(Str); i--)
this->Data[i] = this->Data[i - strlen(Str) + Count];
for (unsigned long i = 0; i < strlen(Str); i++)
this->m_Data[Pos + i] = Str[i];
this->Data[Pos + i] = Str[i];
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
void replace(size_t Pos, size_t Count, const string &Str)
{
strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str.m_Data);
if (Pos >= this->m_Length)
strdbg("String replace: %d, %d, \"%s\"", Pos, Count, Str.Data);
if (Pos >= this->Length)
return;
if ((int64_t)Count < 0)
return;
if (Pos + Count > this->m_Length)
Count = this->m_Length - Pos;
if (Pos + Count > this->Length)
Count = this->Length - Pos;
int NewLength = this->m_Length - Count + Str.m_Length;
size_t NewLength = this->Length - Count + Str.Length;
this->resize(NewLength);
for (size_t i = this->m_Length - 1; i >= Pos + Str.m_Length; i--)
this->m_Data[i] = this->m_Data[i - Str.m_Length + Count];
for (size_t i = this->Length - 1; i >= Pos + Str.Length; i--)
this->Data[i] = this->Data[i - Str.Length + Count];
for (size_t i = 0; i < Str.m_Length; i++)
this->m_Data[Pos + i] = Str.m_Data[i];
for (size_t i = 0; i < Str.Length; i++)
this->Data[Pos + i] = Str.Data[i];
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String replaced: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
}
string operator+(const string &Other) const
{
string result = *this;
result.concat(Other);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.Data, result.Data, result.Length, result.Capacity);
return result;
}
@ -390,21 +390,21 @@ namespace std
{
string result = *this;
result.concat(Other);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.m_Data, result.m_Data, result.m_Length, result.m_Capacity);
strdbg("String added: \"%s\" (data: %#lx, length: %d, capacity: %d)", result.Data, result.Data, result.Length, result.Capacity);
return result;
}
string &operator+=(const string &Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
string &operator+=(const char *Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
@ -415,120 +415,120 @@ namespace std
// {
// if (this != &Other)
// {
// delete[] this->m_Data;
// this->m_Data = Other.m_Data;
// this->m_Length = Other.m_Length;
// this->m_Capacity = Other.m_Capacity;
// strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
// delete[] this->Data;
// this->Data = Other.Data;
// this->Length = Other.Length;
// this->Capacity = Other.Capacity;
// strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
// }
// return *this;
// }
string &operator=(const char *Other)
{
this->m_Length = strlen(Other);
this->m_Capacity = this->m_Length + 1;
delete[] this->m_Data;
this->m_Data = new char[m_Capacity];
strcpy(m_Data, Other);
strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
this->Length = strlen(Other);
this->Capacity = this->Length + 1;
delete[] this->Data;
this->Data = new char[this->Capacity];
strcpy(this->Data, Other);
strdbg("String assigned: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
string &operator<<(const string &Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
string &operator<<(const char *Other)
{
this->concat(Other);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->m_Data, this->m_Data, this->m_Length, this->m_Capacity);
strdbg("String appended: \"%s\" (data: %#lx, length: %d, capacity: %d)", this->Data, this->Data, this->Length, this->Capacity);
return *this;
}
char &operator[](int Index)
{
strdbg("String index: %d", Index);
return this->m_Data[Index];
return this->Data[Index];
}
const char &operator[](int Index) const
{
strdbg("String index: %d", Index);
return this->m_Data[Index];
return this->Data[Index];
}
bool operator==(const string &Other) const
{
strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other.m_Data);
return strcmp(this->m_Data, Other.m_Data) == 0;
strdbg("String compared: \"%s\" == \"%s\"", this->Data, Other.Data);
return strcmp(this->Data, Other.Data) == 0;
}
bool operator!=(const char *Other) const
{
strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other);
return strcmp(this->m_Data, Other) != 0;
strdbg("String compared: \"%s\" != \"%s\"", this->Data, Other);
return strcmp(this->Data, Other) != 0;
}
bool operator!=(const string &Other) const
{
strdbg("String compared: \"%s\" != \"%s\"", this->m_Data, Other.m_Data);
return strcmp(this->m_Data, Other.m_Data) != 0;
strdbg("String compared: \"%s\" != \"%s\"", this->Data, Other.Data);
return strcmp(this->Data, Other.Data) != 0;
}
bool operator==(const char *Other) const
{
strdbg("String compared: \"%s\" == \"%s\"", this->m_Data, Other);
return strcmp(this->m_Data, Other) == 0;
strdbg("String compared: \"%s\" == \"%s\"", this->Data, Other);
return strcmp(this->Data, Other) == 0;
}
class iterator
{
private:
char *m_Pointer;
char *Pointer;
public:
iterator(char *Pointer) : m_Pointer(Pointer) {}
iterator(char *Pointer) : Pointer(Pointer) {}
iterator &operator++()
{
++this->m_Pointer;
strdbg("String iterator incremented: %#lx", this->m_Pointer);
++this->Pointer;
strdbg("String iterator incremented: %#lx", this->Pointer);
return *this;
}
char &operator*()
{
strdbg("String iterator dereferenced: %#lx", this->m_Pointer);
return *this->m_Pointer;
strdbg("String iterator dereferenced: %#lx", this->Pointer);
return *this->Pointer;
}
bool operator!=(const iterator &Other) const
{
strdbg("String iterator compared: %#lx != %#lx", this->m_Pointer, Other.m_Pointer);
return this->m_Pointer != Other.m_Pointer;
strdbg("String iterator compared: %#lx != %#lx", this->Pointer, Other.Pointer);
return this->Pointer != Other.Pointer;
}
bool operator==(const iterator &Other) const
{
strdbg("String iterator compared: %#lx == %#lx", this->m_Pointer, Other.m_Pointer);
return this->m_Pointer == Other.m_Pointer;
strdbg("String iterator compared: %#lx == %#lx", this->Pointer, Other.Pointer);
return this->Pointer == Other.Pointer;
}
};
iterator begin()
{
strdbg("String iterator begin: %#lx", this->m_Data);
return iterator(this->m_Data);
strdbg("String iterator begin: %#lx", this->Data);
return iterator(this->Data);
}
iterator end()
{
strdbg("String iterator end: %#lx", this->m_Data + this->m_Length);
return iterator(this->m_Data + this->m_Length);
strdbg("String iterator end: %#lx", this->Data + this->Length);
return iterator(this->Data + this->Length);
}
};
}

View File

@ -19,23 +19,23 @@ namespace std
private:
static const size_t DEFAULT_NUM_BUCKETS = 10;
std::vector<bucket> m_buckets;
std::vector<bucket> bkts;
size_t hash(const key_type &key) const
{
std::hash<key_type> hash_function;
return hash_function(key) % m_buckets.size();
return hash_function(key) % this->bkts.size();
}
public:
unordered_map() : m_buckets(DEFAULT_NUM_BUCKETS) {}
unordered_map(size_t num_buckets) : m_buckets(num_buckets) {}
unordered_map() : bkts(DEFAULT_NUM_BUCKETS) {}
unordered_map(size_t num_buckets) : bkts(num_buckets) {}
void insert(const key_value_pair &pair)
{
size_t bucket_index = hash(pair.first);
bucket &bucket = m_buckets[bucket_index];
for (auto it = bucket.begin(); it != bucket.end(); ++it)
bucket &bkt = this->bkts[bucket_index];
for (auto it = bkt.begin(); it != bkt.end(); ++it)
{
if (it->first == pair.first)
{
@ -43,14 +43,14 @@ namespace std
return;
}
}
bucket.push_back(pair);
bkt.push_back(pair);
}
bool contains(const key_type &key) const
{
size_t bucket_index = hash(key);
const bucket &bucket = m_buckets[bucket_index];
for (auto it = bucket.begin(); it != bucket.end(); ++it)
const bucket &bkt = this->bkts[bucket_index];
for (auto it = bkt.begin(); it != bkt.end(); ++it)
{
if (it->first == key)
{
@ -63,34 +63,34 @@ namespace std
iterator find(const key_type &k)
{
size_t bucket_index = hash(k);
bucket &bucket = m_buckets[bucket_index];
for (auto it = bucket.begin(); it != bucket.end(); ++it)
bucket &bkt = this->bkts[bucket_index];
for (auto it = bkt.begin(); it != bkt.end(); ++it)
{
if (it->first == k)
return it;
}
return bucket.end();
return bkt.end();
}
const_iterator find(const key_type &k) const
{
size_t bucket_index = hash(k);
const bucket &bucket = m_buckets[bucket_index];
for (auto it = bucket.begin(); it != bucket.end(); ++it)
const bucket &bkt = this->bkts[bucket_index];
for (auto it = bkt.begin(); it != bkt.end(); ++it)
{
if (it->first == k)
return it;
}
return bucket.end();
return bkt.end();
}
iterator end() noexcept { return m_buckets.end(); }
iterator end() noexcept { return this->bkts.end(); }
size_t size() const
{
size_t count = 0;
for (const auto &bucket : m_buckets)
count += bucket.size();
foreach (const auto &bkt in this->bkts)
count += bkt.size();
return count;
}
@ -98,14 +98,14 @@ namespace std
value_type &operator[](const key_type &key)
{
size_t bucket_index = hash(key);
bucket &bucket = m_buckets[bucket_index];
for (auto it = bucket.begin(); it != bucket.end(); ++it)
bucket &bkt = this->bkts[bucket_index];
for (auto it = bkt.begin(); it != bkt.end(); ++it)
{
if (it->first == key)
return it->second;
}
bucket.emplace_back(key, value_type());
return bucket.back().second;
bkt.emplace_back(key, value_type());
return bkt.back().second;
}
};
}

View File

@ -52,17 +52,17 @@ namespace std
VectorBuffer[i] = Initial;
}
NIF vector(const vector<T> &vector)
NIF vector(const vector<T> &v)
{
VectorSize = vector.VectorSize;
VectorCapacity = vector.VectorCapacity;
VectorSize = v.VectorSize;
VectorCapacity = v.VectorCapacity;
#ifdef DEBUG_MEM_ALLOCATION
debug("VECTOR INIT: vector( <vector> )->Size: %lld", VectorSize);
#endif
assert(VectorSize > 0);
VectorBuffer = new T[VectorSize];
for (size_t i = 0; i < VectorSize; i++)
VectorBuffer[i] = vector.VectorBuffer[i];
VectorBuffer[i] = v.VectorBuffer[i];
}
NIF ~vector()
@ -248,17 +248,17 @@ namespace std
return VectorBuffer[Index];
}
NIF vector<T> &operator=(const vector<T> &vector)
NIF vector<T> &operator=(const vector<T> &v)
{
delete[] VectorBuffer;
VectorSize = vector.VectorSize;
VectorCapacity = vector.VectorCapacity;
VectorSize = v.VectorSize;
VectorCapacity = v.VectorCapacity;
#ifdef DEBUG_MEM_ALLOCATION
debug("VECTOR ALLOCATION: operator=( <vector> )->Size:%lld", VectorSize);
#endif
VectorBuffer = new T[VectorSize];
for (size_t i = 0; i < VectorSize; i++)
VectorBuffer[i] = vector.VectorBuffer[i];
VectorBuffer[i] = v.VectorBuffer[i];
return *this;
}
};

View File

@ -8,18 +8,18 @@
{
#define END_EXTERNC \
}
#else
#else // __cplusplus
#define EXTERNC
#define START_EXTERNC
#define END_EXTERNC
#endif
#endif // __cplusplus
#ifdef __cplusplus
#define NULL 0
#else
#else // __cplusplus
#define NULL ((void *)0)
#define bool _Bool
#endif
#endif // __cplusplus
#define asm __asm__
#define asmv __asm__ volatile
@ -30,7 +30,12 @@
#ifdef __cplusplus
#define foreach for
#define in :
#endif
#define r_cst(t, v) reinterpret_cast<t>(v)
#define c_cst(t, v) const_cast<t>(v)
#define s_cst(t, v) static_cast<t>(v)
#define d_cst(t, v) dynamic_cast<t>(v)
#endif // __cplusplus
#define UNUSED(x) (void)(x)
#define CONCAT(x, y) x##y
@ -231,11 +236,32 @@ typedef intptr_t ssize_t;
: "memory");
#endif
#ifdef __INT48_TYPE__
typedef __INT48_TYPE__ int48_t;
typedef __UINT48_TYPE__ uint48_t;
typedef int48_t int_least48_t;
typedef uint48_t uint_least48_t;
typedef int48_t int_fast48_t;
typedef uint48_t uint_fast48_t;
#else // __INT48_TYPE__
typedef __INT64_TYPE__ int48_t;
typedef __UINT64_TYPE__ uint48_t;
typedef int48_t int_least48_t;
typedef uint48_t uint_least48_t;
typedef int48_t int_fast48_t;
typedef uint48_t uint_fast48_t;
#endif // __INT48_TYPE__
#define b4(x) ((x & 0x0F) << 4 | (x & 0xF0) >> 4)
#define b8(x) ((x)&0xFF)
#define b16(x) __builtin_bswap16(x)
#define b32(x) __builtin_bswap32(x)
#define b48(x) (((((x)&0x0000000000ff) << 40) | (((x)&0x00000000ff00) << 24) | (((x)&0x000000ff0000) << 8) | (((x)&0x0000ff000000) >> 8) | (((x)&0x00ff00000000) >> 24) | (((x)&0xff0000000000) >> 40)))
#define b48(x) (((((x)&0x0000000000ff) << 40) | \
(((x)&0x00000000ff00) << 24) | \
(((x)&0x000000ff0000) << 8) | \
(((x)&0x0000ff000000) >> 8) | \
(((x)&0x00ff00000000) >> 24) | \
(((x)&0xff0000000000) >> 40)))
#define b64(x) __builtin_bswap64(x)
/* https://gcc.gnu.org/onlinedocs/gcc-9.5.0/gnat_ugn/Optimization-Levels.html */