Skip to content

Commit

Permalink
Simplify attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Hammie committed Apr 14, 2024
1 parent 26405ab commit 92db72f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
9 changes: 3 additions & 6 deletions src/libs/core/include/attributes.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <functional>
#include <optional>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -28,14 +29,9 @@ bool MatchAttributePath(const std::string_view &pattern, const ATTRIBUTES &attri

class ATTRIBUTES final
{
// TODO: remove with another iteration of rewriting this
friend class COMPILER;

class LegacyProxy;

public:
[[deprecated("Pass StringCodec by reference instead")]] explicit ATTRIBUTES(VSTRING_CODEC *p);

explicit ATTRIBUTES(VSTRING_CODEC &p);
ATTRIBUTES(const ATTRIBUTES &) = delete;
ATTRIBUTES(ATTRIBUTES &&other) noexcept;
Expand Down Expand Up @@ -86,11 +82,12 @@ class ATTRIBUTES final
void SetNameCode(uint32_t n) noexcept;
[[nodiscard]] VSTRING_CODEC &GetStringCodec() const noexcept;

void Sort(const std::function<bool(const std::unique_ptr<ATTRIBUTES> &lhs, const std::unique_ptr<ATTRIBUTES> &rhs)>& pred);

private:
ATTRIBUTES(VSTRING_CODEC &string_codec, ATTRIBUTES *parent, const std::string_view &name);
ATTRIBUTES(VSTRING_CODEC &string_codec, ATTRIBUTES *parent, uint32_t name_code);

void Release() const noexcept;
ATTRIBUTES *CreateNewAttribute(uint32_t name_code);

VSTRING_CODEC &stringCodec_;
Expand Down
39 changes: 17 additions & 22 deletions src/libs/core/src/attributes.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#include "attributes.h"

#include "string_compare.hpp"
#include "platform/platform.hpp"
#include <execution>

ATTRIBUTES::ATTRIBUTES(VSTRING_CODEC *p): ATTRIBUTES(*p)
{
}
#include "string_compare.hpp"

ATTRIBUTES::ATTRIBUTES(ATTRIBUTES &&other) noexcept
: stringCodec_(other.stringCodec_), nameCode_(other.stringCodec_.Convert("root")), value_(std::move(other.value_)),
Expand All @@ -18,17 +15,20 @@ ATTRIBUTES & ATTRIBUTES::operator=(ATTRIBUTES &&other) noexcept
stringCodec_ = other.stringCodec_;
// Do not update name code
// nameCode_ = other.nameCode_;
other.nameCode_ = 1337;
value_ = std::move(other.value_);
attributes_ = std::move(other.attributes_);
// Do not update parent
// parent_ = other.parent_;
other.parent_ = (ATTRIBUTES*)0x1;
break_ = other.break_;
return *this;
}

ATTRIBUTES::~ATTRIBUTES()
{
Release();
if (break_)
stringCodec_.VariableChanged();
}

void ATTRIBUTES::SetBreak(bool set_break)
Expand Down Expand Up @@ -265,19 +265,14 @@ bool ATTRIBUTES::DeleteAttributeClassX(ATTRIBUTES *pA)
}
else
{
// auto removed_it = std::remove_if(pAttributes.begin(), pAttributes.end(), [&](const auto&
// item){
// return item.get() == pA;
// });
// return removed_it != pAttributes.end();
for (uint32_t n = 0; n < attributes_.size(); n++)
{
if (attributes_[n].get() == pA)
auto it = std::remove_if(attributes_.begin(), attributes_.end(), [&pA](const auto &attr) {
return attr.get() == pA;
});
if (it != attributes_.end() )
{
for (auto i = n; i < attributes_.size() - 1; i++)
attributes_[i] = std::move(attributes_[i + 1]);

attributes_.pop_back();
attributes_.erase(it);
return true;
}
if (attributes_[n]->DeleteAttributeClassX(pA))
Expand Down Expand Up @@ -446,6 +441,12 @@ VSTRING_CODEC & ATTRIBUTES::GetStringCodec() const noexcept
return stringCodec_;
}

void ATTRIBUTES::Sort(
const std::function<bool(const std::unique_ptr<ATTRIBUTES> &, const std::unique_ptr<ATTRIBUTES> &)>& pred)
{
std::sort(std::execution::seq, std::begin(attributes_), std::end(attributes_), pred);
}

ATTRIBUTES * ATTRIBUTES::CreateNewAttribute(uint32_t name_code)
{
const std::unique_ptr<ATTRIBUTES> &attr = attributes_.emplace_back(new ATTRIBUTES(stringCodec_, this, name_code));
Expand Down Expand Up @@ -480,12 +481,6 @@ ATTRIBUTES ATTRIBUTES::Copy() const
return result;
}

void ATTRIBUTES::Release() const noexcept
{
if (break_)
stringCodec_.VariableChanged();
}

// MatchAttributePath("equipment.*.locator", attribute)
bool MatchAttributePath(const std::string_view &pattern, const ATTRIBUTES &attribute)
{
Expand Down
7 changes: 3 additions & 4 deletions src/libs/core/src/internal_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2569,10 +2569,9 @@ DATA *COMPILER::BC_CallIntFunction(uint32_t func_code, DATA *&pVResult, uint32_t
}
pA = pV->GetAClass();

std::sort(std::execution::seq, std::begin(pA->attributes_), std::end(pA->attributes_),
[](const std::unique_ptr<ATTRIBUTES> &lhs, const std::unique_ptr<ATTRIBUTES> &rhs) {
return strcmp(lhs->GetThisName(), rhs->GetThisName()) < 0;
});
pA->Sort([](const std::unique_ptr<ATTRIBUTES> &lhs, const std::unique_ptr<ATTRIBUTES> &rhs) {
return strcmp(lhs->GetThisName(), rhs->GetThisName()) < 0;
});

break;
}
Expand Down
25 changes: 14 additions & 11 deletions src/libs/core/src/string_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@

struct HTSUBELEMENT
{
char *pStr;
uint32_t dwHashCode;
char *pStr = nullptr;
uint32_t dwHashCode{};
};

struct HTELEMENT
{
HTSUBELEMENT *pElements;
uint32_t nStringsNum;
HTSUBELEMENT *pElements = nullptr;
uint32_t nStringsNum{};
};

class STRING_CODEC : public VSTRING_CODEC
{
uint32_t nHTIndex;
uint32_t nHTEIndex;
uint32_t nStringsNum;
uint32_t nHTIndex{};
uint32_t nHTEIndex{};
uint32_t nStringsNum{};

HTELEMENT HTable[HASH_TABLE_SIZE]{};

public:
STRING_CODEC() : nHTIndex(0), nHTEIndex(0)
{
nStringsNum = 0;
}
STRING_CODEC() = default;

~STRING_CODEC() override
{
Expand Down Expand Up @@ -132,6 +129,12 @@ class STRING_CODEC : public VSTRING_CODEC
{
return "INVALID SCC";
}
if (HTable[nTableIndex].pElements == nullptr) {
return "INVALID SC ELEMENTS";
}
if (HTable[nTableIndex].pElements[n].pStr == nullptr) {
return "INVALID SC VALUE";
}
return HTable[nTableIndex].pElements[n].pStr;
}

Expand Down

0 comments on commit 92db72f

Please sign in to comment.