Remove UString. And replaced std::map to std::unordered_map
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "ISave.h"
|
#include "ISave.h"
|
||||||
|
|
||||||
#include <map>
|
#include <unordered_map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
@@ -14,22 +14,22 @@ class SaveMap final
|
|||||||
std::shared_ptr<SaveMap> parent_;
|
std::shared_ptr<SaveMap> parent_;
|
||||||
|
|
||||||
// Индивидуальные значения
|
// Индивидуальные значения
|
||||||
std::map<std::string, long long> save_long;
|
std::unordered_map<std::string, long long> save_long;
|
||||||
std::map<std::string, double> save_double;
|
std::unordered_map<std::string, double> save_double;
|
||||||
std::map<std::string, std::string> save_string;
|
std::unordered_map<std::string, std::string> save_string;
|
||||||
std::map<std::string, ISave*> save_objects;
|
std::unordered_map<std::string, ISave*> save_objects;
|
||||||
|
|
||||||
// Массивы
|
// Массивы
|
||||||
std::map<std::string, std::vector<long long>> save_vector_integer;
|
std::unordered_map<std::string, std::vector<long long>> save_vector_integer;
|
||||||
std::map<std::string, std::vector<double>> save_vector_double;
|
std::unordered_map<std::string, std::vector<double>> save_vector_double;
|
||||||
std::map<std::string, std::vector<ISave*>> save_vector_objects;
|
std::unordered_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<std::string>> save_vector_strings;
|
||||||
|
|
||||||
// Связаные списки
|
// Связаные списки
|
||||||
std::map<std::string, std::list<long long>> save_list_integer;
|
std::unordered_map<std::string, std::list<long long>> save_list_integer;
|
||||||
std::map<std::string, std::list<double>> save_list_double;
|
std::unordered_map<std::string, std::list<double>> save_list_double;
|
||||||
std::map<std::string, std::list<ISave*>> save_list_objects;
|
std::unordered_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<std::string>> save_list_strings;
|
||||||
|
|
||||||
static ISave* MakeObjectByName(const std::string& name);
|
static ISave* MakeObjectByName(const std::string& name);
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
|
||||||
}
|
|
||||||
@@ -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_); }
|
|
||||||
};
|
|
||||||
@@ -11,10 +11,8 @@ FetchContent_MakeAvailable(googletest)
|
|||||||
enable_testing()
|
enable_testing()
|
||||||
|
|
||||||
file(GLOB SRC
|
file(GLOB SRC
|
||||||
"UStringTest.cpp"
|
|
||||||
"SaveMapTest.cpp"
|
"SaveMapTest.cpp"
|
||||||
|
|
||||||
"${PROJECT_SOURCE_DIR}/Core/Types/UString.cpp"
|
|
||||||
"${PROJECT_SOURCE_DIR}/Core/Game/SaveMap/SaveMap.cpp"
|
"${PROJECT_SOURCE_DIR}/Core/Game/SaveMap/SaveMap.cpp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
#include <gtest/gtest.h>
|
|
||||||
#include "Types/UString.hpp"
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
TEST(UStringTest, creae_string)
|
|
||||||
{
|
|
||||||
UString str("Hello, World!");
|
|
||||||
EXPECT_EQ(str, "Hello, World!");
|
|
||||||
EXPECT_NE(str, "Hello, UwU!");
|
|
||||||
EXPECT_EQ(str.length(), 13);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST(UStringTest, concatenate_string)
|
|
||||||
{
|
|
||||||
UString str1("Hello, ");
|
|
||||||
UString str2("World!");
|
|
||||||
str1 += str2;
|
|
||||||
EXPECT_EQ(str1, "Hello, World!");
|
|
||||||
EXPECT_EQ(str1.length(), 13);
|
|
||||||
|
|
||||||
UString str3("Hello, ");
|
|
||||||
str3 += "UwU!";
|
|
||||||
EXPECT_EQ(str3, "Hello, UwU!");
|
|
||||||
EXPECT_EQ(str3.length(), 11);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user