Remove UString. And replaced std::map to std::unordered_map

This commit is contained in:
Jiga228
2025-10-15 18:20:27 +07:00
parent 87c72e4a48
commit 189a6d43fe
5 changed files with 13 additions and 203 deletions
+13 -13
View File
@@ -2,7 +2,7 @@
#include "ISave.h"
#include <map>
#include <unordered_map>
#include <memory>
#include <string>
#include <vector>
@@ -14,22 +14,22 @@ class SaveMap final
std::shared_ptr<SaveMap> parent_;
// Индивидуальные значения
std::map<std::string, long long> save_long;
std::map<std::string, double> save_double;
std::map<std::string, std::string> save_string;
std::map<std::string, ISave*> save_objects;
std::unordered_map<std::string, long long> save_long;
std::unordered_map<std::string, double> save_double;
std::unordered_map<std::string, std::string> save_string;
std::unordered_map<std::string, ISave*> save_objects;
// Массивы
std::map<std::string, std::vector<long long>> save_vector_integer;
std::map<std::string, std::vector<double>> save_vector_double;
std::map<std::string, std::vector<ISave*>> save_vector_objects;
std::map<std::string, std::vector<std::string>> save_vector_strings;
std::unordered_map<std::string, std::vector<long long>> save_vector_integer;
std::unordered_map<std::string, std::vector<double>> save_vector_double;
std::unordered_map<std::string, std::vector<ISave*>> save_vector_objects;
std::unordered_map<std::string, std::vector<std::string>> save_vector_strings;
// Связаные списки
std::map<std::string, std::list<long long>> save_list_integer;
std::map<std::string, std::list<double>> save_list_double;
std::map<std::string, std::list<ISave*>> save_list_objects;
std::map<std::string, std::list<std::string>> save_list_strings;
std::unordered_map<std::string, std::list<long long>> save_list_integer;
std::unordered_map<std::string, std::list<double>> save_list_double;
std::unordered_map<std::string, std::list<ISave*>> save_list_objects;
std::unordered_map<std::string, std::list<std::string>> save_list_strings;
static ISave* MakeObjectByName(const std::string& name);
-131
View File
@@ -1,131 +0,0 @@
#include "UString.hpp"
#include <cstring>
unsigned int UString::FNV1aHash(const char* buf)
{
unsigned int h_val = 0x811c9dc5;
while (*buf)
{
h_val ^= static_cast<unsigned int>(*buf++);
h_val *= 0x01000193;
}
return h_val;
}
UString::UString(const char* str)
{
if (str == nullptr)
return;
size_ = strlen(str);
str_ = new char[size_ + 1];
for (size_t i = 0; i < size_; ++i)
str_[i] = str[i];
str_[size_] = '\0';
hash_ = FNV1aHash(str);
}
UString::UString(const UString& str) : size_(str.size_)
{
if (size_ != 0 && str.str_ != nullptr)
{
str_ = new char[str.size_ + 1];
strcpy_s(str_, size_ + 1, str.str_);
}
hash_ = str.hash_;
}
UString::UString(UString&& str) noexcept
{
is_moved_.store(true);
str_ = str.str_;
size_ = str.size_;
hash_ = str.hash_;
}
UString::~UString()
{
if (is_moved_.load() == false)
delete[] str_;
}
UString& UString::operator=(const UString& str)
{
if (&str != this)
{
if (size_ != 0 && str.str_ != nullptr)
{
str_ = new char[str.size_];
strcpy_s(str_, size_, str.str_);
}
}
is_moved_.store(false);
hash_ = FNV1aHash(str_);
return *this;
}
UString& UString::operator=(UString&& str) noexcept
{
str.is_moved_.store(true);
str_ = str.str_;
size_ = str.size_;
is_moved_.store(false);
hash_ = FNV1aHash(str_);
return *this;
}
void UString::operator+=(const UString& str)
{
char* new_str = new char[size_ + str.size_ + 1];
strcpy_s(new_str, size_ + str.size_ + 1, str_);
strcpy_s(new_str + size_, str.size_ + 1, str.str_);
if (is_moved_.load() == false)
delete[] str_;
str_ = new_str;
size_ += str.size_;
is_moved_.store(false);
hash_ = FNV1aHash(str_);
}
void UString::operator+=(const char* str)
{
size_t str_size = strlen(str);
char* new_str = new char[size_ + str_size + 1];
strcpy_s(new_str, size_ + str_size + 1, str_);
strcpy_s(new_str + size_, str_size + 1, str);
if (is_moved_.load() == false)
delete[] str_;
str_ = new_str;
size_ += str_size;
is_moved_.store(false);
hash_ = FNV1aHash(str_);
}
bool UString::operator==(const UString& str) const
{
return hash_ == str.hash_;
}
bool UString::operator==(const char* str) const
{
unsigned int hash = FNV1aHash(str);
return hash_ == hash;
}
bool UString::operator!=(const UString& str) const
{
return !(*this == str);
}
bool UString::operator!=(const char* str) const
{
return !(*this == str);
}
-32
View File
@@ -1,32 +0,0 @@
#pragma once
#include <atomic>
#include <string>
class UString
{
unsigned int hash_ = 0;
std::atomic_bool is_moved_ = false;
char* str_ = nullptr;
size_t size_ = 0;
static unsigned int FNV1aHash (const char *buf);
public:
UString(const char* str);
UString(const UString& str);
UString(UString&& str) noexcept;
~UString();
UString& operator=(const UString& str);
UString& operator=(UString&& str) noexcept;
void operator+=(const UString& str);
void operator+=(const char* str);
bool operator==(const UString& str) const;
bool operator==(const char* str) const;
bool operator!=(const UString& str) const;
bool operator!=(const char* str) const;
size_t length() const { return size_; }
const char* c_str() const { return str_; }
std::string std_str() const { return std::string(str_, size_); }
};