Skip to content

Commit

Permalink
remove lib jsoncpp | WildcardTreeNode shared_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh committed Mar 29, 2024
1 parent 8dec6f9 commit 540c204
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
# VCPKG
# cmake -DCMAKE_TOOLCHAIN_FILE=/opt/workspace/vcpkg/scripts/buildsystems/vcpkg.cmake ..
# Needed libs is in file vcpkg.json
# Windows required libs: .\vcpkg install --triplet x64-windows asio pugixml spdlog curl jsoncpp protobuf parallel-hashmap magic-enum mio luajit libmariadb mpir abseil
# Windows required libs: .\vcpkg install --triplet x64-windows asio pugixml spdlog curl protobuf parallel-hashmap magic-enum mio luajit libmariadb mpir abseil
if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "")
Expand Down
1 change: 0 additions & 1 deletion cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ find_package(ZLIB REQUIRED)
find_package(absl CONFIG REQUIRED)
find_package(asio CONFIG REQUIRED)
find_package(eventpp CONFIG REQUIRED)
find_package(jsoncpp CONFIG REQUIRED)
find_package(magic_enum CONFIG REQUIRED)
find_package(opentelemetry-cpp CONFIG REQUIRED)
find_package(prometheus-cpp CONFIG REQUIRED)
Expand Down
4 changes: 1 addition & 3 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,14 @@ endif()

if (MSVC)
if(BUILD_STATIC_LIBRARY)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_static)
set(VCPKG_TARGET_TRIPLET "x64-windows-static" CACHE STRING "")
else()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_lib)
set(VCPKG_TARGET_TRIPLET "x64-windows" CACHE STRING "")
endif()

target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${MYSQL_CLIENT_LIBS})
else()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_static Threads::Threads)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC Threads::Threads)
endif (MSVC)

# === OpenMP ===
Expand Down
8 changes: 5 additions & 3 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ Game::Game() {
// Create instance of IOWheel to Game class
m_IOWheel = std::make_unique<IOWheel>();

wildcardTree = std::make_shared<WildcardTreeNode>(false);

m_highscoreCategoriesNames = {
{ static_cast<uint8_t>(HighscoreCategories_t::ACHIEVEMENTS), "Achievement Points" },
{ static_cast<uint8_t>(HighscoreCategories_t::AXE_FIGHTING), "Axe Fighting" },
Expand Down Expand Up @@ -886,7 +888,7 @@ ReturnValue Game::getPlayerByNameWildcard(const std::string &s, std::shared_ptr<
if (s.back() == '~') {
const std::string &query = asLowerCaseString(s.substr(0, strlen - 1));
std::string result;
ReturnValue ret = wildcardTree.findOne(query, result);
ReturnValue ret = wildcardTree->findOne(query, result);
if (ret != RETURNVALUE_NOERROR) {
return ret;
}
Expand Down Expand Up @@ -9689,14 +9691,14 @@ void Game::updatePlayerSaleItems(uint32_t playerId) {
void Game::addPlayer(std::shared_ptr<Player> player) {
const std::string &lowercase_name = asLowerCaseString(player->getName());
mappedPlayerNames[lowercase_name] = player;
wildcardTree.insert(lowercase_name);
wildcardTree->insert(lowercase_name);
players[player->getID()] = player;
}

void Game::removePlayer(std::shared_ptr<Player> player) {
const std::string &lowercase_name = asLowerCaseString(player->getName());
mappedPlayerNames.erase(lowercase_name);
wildcardTree.remove(lowercase_name);
wildcardTree->remove(lowercase_name);
players.erase(player->getID());
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ class Game {
size_t lastBucket = 0;
size_t lastImbuedBucket = 0;

WildcardTreeNode wildcardTree { false };
std::shared_ptr<WildcardTreeNode> wildcardTree;

std::map<uint32_t, std::shared_ptr<Npc>> npcs;
std::map<uint32_t, std::shared_ptr<Monster>> monsters;
Expand Down
4 changes: 1 addition & 3 deletions src/pch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <numeric>
#include <cmath>
#include <mutex>
#include <stack>

// --------------------
// System Includes
Expand Down Expand Up @@ -95,9 +96,6 @@ struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<E>, char>> : formatter<
// GMP
#include <gmp.h>

// JSON
#include <json/json.h>

// LUA
#if __has_include("luajit/lua.hpp")
#include <luajit/lua.hpp>
Expand Down
26 changes: 13 additions & 13 deletions src/utils/wildcardtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@

#include "utils/wildcardtree.hpp"

WildcardTreeNode* WildcardTreeNode::getChild(char ch) {
std::shared_ptr<WildcardTreeNode> WildcardTreeNode::getChild(char ch) {
auto it = children.find(ch);
if (it == children.end()) {
return nullptr;
}
return &it->second;
return it->second;
}

const WildcardTreeNode* WildcardTreeNode::getChild(char ch) const {
std::shared_ptr<WildcardTreeNode> WildcardTreeNode::getChild(char ch) const {
auto it = children.find(ch);
if (it == children.end()) {
return nullptr;
}
return &it->second;
return it->second;
}

WildcardTreeNode* WildcardTreeNode::addChild(char ch, bool breakp) {
WildcardTreeNode* child = getChild(ch);
std::shared_ptr<WildcardTreeNode> WildcardTreeNode::addChild(char ch, bool breakp) {
std::shared_ptr<WildcardTreeNode> child = getChild(ch);
if (child) {
if (breakp && !child->breakpoint) {
child->breakpoint = true;
}
} else {
auto pair = children.emplace(std::piecewise_construct, std::forward_as_tuple(ch), std::forward_as_tuple(breakp));
child = &pair.first->second;
auto pair = children.emplace(std::piecewise_construct, std::forward_as_tuple(ch), std::forward_as_tuple(std::make_shared<WildcardTreeNode>(breakp)));
child = pair.first->second;
}
return child;
}

void WildcardTreeNode::insert(const std::string &str) {
WildcardTreeNode* cur = this;
std::shared_ptr<WildcardTreeNode> cur = static_self_cast<WildcardTreeNode>();

size_t length = str.length() - 1;
for (size_t pos = 0; pos < length; ++pos) {
Expand All @@ -52,9 +52,9 @@ void WildcardTreeNode::insert(const std::string &str) {
}

void WildcardTreeNode::remove(const std::string &str) {
WildcardTreeNode* cur = this;
std::shared_ptr<WildcardTreeNode> cur = static_self_cast<WildcardTreeNode>();

std::stack<WildcardTreeNode*> path;
std::stack<std::shared_ptr<WildcardTreeNode>> path;
path.push(cur);
size_t len = str.length();
for (size_t pos = 0; pos < len; ++pos) {
Expand Down Expand Up @@ -85,7 +85,7 @@ void WildcardTreeNode::remove(const std::string &str) {
}

ReturnValue WildcardTreeNode::findOne(const std::string &query, std::string &result) const {
const WildcardTreeNode* cur = this;
auto cur = static_self_cast<const WildcardTreeNode>();
for (char pos : query) {
cur = cur->getChild(pos);
if (!cur) {
Expand All @@ -105,6 +105,6 @@ ReturnValue WildcardTreeNode::findOne(const std::string &query, std::string &res

auto it = cur->children.begin();
result += it->first;
cur = &it->second;
cur = it->second;
} while (true);
}
12 changes: 6 additions & 6 deletions src/utils/wildcardtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@

#include "declarations.hpp"

class WildcardTreeNode {
class WildcardTreeNode : public SharedObject {
public:
explicit WildcardTreeNode(bool initBreakpoint) :
breakpoint(initBreakpoint) { }
WildcardTreeNode(WildcardTreeNode &&other) = default;
WildcardTreeNode(WildcardTreeNode &&other) noexcept = default;

// non-copyable
WildcardTreeNode(const WildcardTreeNode &) = delete;
WildcardTreeNode &operator=(const WildcardTreeNode &) = delete;

WildcardTreeNode* getChild(char ch);
const WildcardTreeNode* getChild(char ch) const;
WildcardTreeNode* addChild(char ch, bool breakpoint);
std::shared_ptr<WildcardTreeNode> getChild(char ch);
std::shared_ptr<WildcardTreeNode> getChild(char ch) const;
std::shared_ptr<WildcardTreeNode> addChild(char ch, bool breakpoint);

void insert(const std::string &str);
void remove(const std::string &str);

ReturnValue findOne(const std::string &query, std::string &result) const;

private:
std::map<char, WildcardTreeNode> children;
std::map<char, std::shared_ptr<WildcardTreeNode>> children;
bool breakpoint;
};
1 change: 0 additions & 1 deletion vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"bext-ut",
"curl",
"eventpp",
"jsoncpp",
"luajit",
"magic-enum",
"mio",
Expand Down
2 changes: 0 additions & 2 deletions vcproj/settings.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
libcurl.lib;
fmt.lib;
spdlog.lib;
jsoncpp.lib;
abseil_dll.lib;
argon2.lib;
opentelemetry_common.lib;
Expand Down Expand Up @@ -60,7 +59,6 @@
libcurl-d.lib;
fmtd.lib;
spdlogd.lib;
jsoncpp.lib;
abseil_dll.lib;
argon2.lib;
opentelemetry_common.lib;
Expand Down

0 comments on commit 540c204

Please sign in to comment.