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

@ -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;
}
};