Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarDirectives.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-24 19:41:52 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-24 19:41:52 +1000
commit7bde128a87268751e1f46174a64ffd78b03bf8bc (patch)
treeb27dd239831fa803d019e35f08f209b1d9e091ea /source/core/StarDirectives.cpp
parent51a9de3af37eb8a6844a2dd5f3f568f3956726db (diff)
DirectivesGroup prototype
Diffstat (limited to 'source/core/StarDirectives.cpp')
-rw-r--r--source/core/StarDirectives.cpp236
1 files changed, 81 insertions, 155 deletions
diff --git a/source/core/StarDirectives.cpp b/source/core/StarDirectives.cpp
index 2394915..766b4d9 100644
--- a/source/core/StarDirectives.cpp
+++ b/source/core/StarDirectives.cpp
@@ -2,207 +2,133 @@
#include "StarImageProcessing.hpp"
#include "StarDirectives.hpp"
#include "StarXXHash.hpp"
-#include "StarHash.hpp"
namespace Star {
-NestedDirectives::NestedDirectives() : m_root(nullptr) {}
-NestedDirectives::NestedDirectives(String const& directives) {
- parseDirectivesIntoLeaf(directives);
-}
-NestedDirectives::NestedDirectives(String&& directives) {
- String mine = move(directives); // most useless move constructor in the world
- parseDirectivesIntoLeaf(mine);
-}
-
-void NestedDirectives::parseDirectivesIntoLeaf(String const& directives) {
- Leaf leaf;
- for (String& op : directives.split('?')) {
- if (!op.empty())
- leaf.entries.emplace_back(imageOperationFromString(op), op);
- }
- m_root = std::make_shared<Cell>(move(leaf));
+Directives::Directives() {}
+Directives::Directives(String const& directives) {
+ parse(directives);
}
-inline bool NestedDirectives::empty() const {
- return (bool)m_root;
+Directives::Directives(String&& directives) {
+ String mine = move(directives);
+ parse(mine);
}
-inline bool NestedDirectives::compare(NestedDirectives const& other) const {
- if (m_root == other.m_root)
- return true;
-
- return false;
+void Directives::parse(String const& directives) {
+ List<Entry> newList;
+ StringList split = directives.split('?');
+ newList.reserve(split.size());
+ for (String& str : split) {
+ if (!str.empty()) {
+ ImageOperation operation = imageOperationFromString(str);
+ newList.emplace_back(move(operation), move(str));
+ }
+ }
+ entries = std::make_shared<List<Entry> const>(move(newList));
+ hash = XXH3_64bits(directives.utf8Ptr(), directives.utf8Size());
}
-void NestedDirectives::append(NestedDirectives const& other) {
- convertToBranches().emplace_back(other.branch());
+void Directives::buildString(String& out) const {
+ for (auto& entry : *entries) {
+ out += "?";
+ out += entry.string;
+ }
}
-NestedDirectives& NestedDirectives::operator+=(NestedDirectives const& other) {
- append(other);
- return *this;
+DirectivesGroup::DirectivesGroup() : m_count(0) {}
+DirectivesGroup::DirectivesGroup(String const& directives) {
+ m_directives.emplace_back(directives);
+ m_count = m_directives.back().entries->size();
}
-
-String NestedDirectives::toString() const {
- String string;
- addToString(string);
- return string;
+DirectivesGroup::DirectivesGroup(String&& directives) {
+ m_directives.emplace_back(move(directives));
+ m_count = m_directives.back().entries->size();
}
-void NestedDirectives::addToString(String& string) const {
- if (m_root)
- m_root->buildString(string);
+inline bool DirectivesGroup::empty() const {
+ return m_count == 0;
}
-void NestedDirectives::forEach(LeafCallback callback) const {
- if (m_root)
- m_root->forEach(callback);
-}
+inline bool DirectivesGroup::compare(DirectivesGroup const& other) const {
+ if (m_count != other.m_count)
+ return false;
-void NestedDirectives::forEachPair(LeafPairCallback callback) const {
- if (m_root) {
- LeafCallback pairCallback = [&](Leaf const& leaf) {
- for (auto& entry : leaf.entries)
- callback(entry.operation, entry.string);
- };
- m_root->forEach(pairCallback);
- }
-}
+ if (empty())
+ return true;
-bool NestedDirectives::forEachAbortable(AbortableLeafCallback callback) const {
- if (!m_root)
- return false;
- else
- return m_root->forEachAbortable(callback);
+ return hash() == other.hash();
}
-bool NestedDirectives::forEachPairAbortable(AbortableLeafPairCallback callback) const {
- if (!m_root)
- return false;
- else {
- AbortableLeafCallback pairCallback = [&](Leaf const& leaf) -> bool {
- for (auto& entry : leaf.entries) {
- if (!callback(entry.operation, entry.string))
- return false;
- }
-
- return true;
- };
- return m_root->forEachAbortable(pairCallback);
- }
+inline bool DirectivesGroup::operator==(DirectivesGroup const& other) const {
+ return compare(other);
}
-Image NestedDirectives::apply(Image& image) const {
- Image current = image;
- forEachPair([&](ImageOperation const& operation, String const& string) {
- processImageOperation(operation, current);
- });
- return current;
+inline bool DirectivesGroup::operator!=(DirectivesGroup const& other) const {
+ return !compare(other);
}
-NestedDirectives::Branches& NestedDirectives::convertToBranches() {
- if (!m_root) {
- m_root = std::make_shared<Cell>(Branches());
- }
- else if (m_root->value.is<Branches>())
- return;
-
- Leaf& leaf = m_root->value.get<Leaf>();
- Branches newBranches;
- newBranches.emplace_back(std::make_shared<Cell const>(move(leaf)));
- m_root->value = move(newBranches);
- return m_root->value.get<Branches>();
+void DirectivesGroup::append(Directives const& directives) {
+ m_directives.push_back(directives);
+ m_count += m_directives.back().entries->size();
}
-bool NestedDirectives::Leaf::Entry::operator==(NestedDirectives::Leaf::Entry const& other) const {
- return string == other.string;
+DirectivesGroup& DirectivesGroup::operator+=(Directives const& other) {
+ append(other);
+ return *this;
}
-bool NestedDirectives::Leaf::Entry::operator!=(NestedDirectives::Leaf::Entry const& other) const {
- return string != other.string;
+inline String DirectivesGroup::toString() const {
+ String string;
+ addToString(string);
+ return string;
}
-
-size_t NestedDirectives::Leaf::length() const {
- return entries.size();
+void DirectivesGroup::addToString(String& string) const {
+ for (auto& directives : m_directives)
+ directives.buildString(string);
}
-bool NestedDirectives::Leaf::operator==(NestedDirectives::Leaf const& other) const {
- size_t len = length();
- if (len != other.length())
- return false;
-
- for (size_t i = 0; i != len; ++i) {
- if (entries[i] != other.entries[i])
- return false;
+void DirectivesGroup::forEach(DirectivesCallback callback) const {
+ for (auto& directives : m_directives) {
+ for (auto& entry : *directives.entries)
+ callback(entry);
}
- return true;
-}
-
-bool NestedDirectives::Leaf::operator!=(NestedDirectives::Leaf const& other) const {
- return !(*this == other);
}
-NestedDirectives::Cell::Cell() : value(Leaf()) {};
-NestedDirectives::Cell::Cell(Leaf&& leaf) : value(move(leaf)) {};
-NestedDirectives::Cell::Cell(Branches&& branches) : value(move(branches)) {};
-NestedDirectives::Cell::Cell(const Leaf& leaf) : value(leaf) {};
-NestedDirectives::Cell::Cell(const Branches& branches) : value(branches) {};
-
-/*
-bool NestedDirectives::Cell::operator==(NestedDirectives::Cell const& other) const {
- if (auto leaf = value.ptr<Leaf>()) {
- if (auto otherLeaf = other.value.ptr<Leaf>())
- return *leaf == *otherLeaf;
- else {
-
+bool DirectivesGroup::forEachAbortable(AbortableDirectivesCallback callback) const {
+ for (auto& directives : m_directives) {
+ for (auto& entry : *directives.entries) {
+ if (!callback(entry))
+ return false;
}
}
- else {
- for (auto& branch : value.get<Branches>()) {
- }
- }
+ return true;
}
-
-bool NestedDirectives::Cell::operator!=(NestedDirectives::Cell const& other) const {
- return !(*this == other);
+inline Image DirectivesGroup::applyNewImage(Image const& image) const {
+ Image result = image;
+ applyExistingImage(result);
+ return result;
}
-//*/
-void NestedDirectives::Cell::buildString(String& string) const {
- if (auto leaf = value.ptr<Leaf>())
- for (auto& entry : leaf->entries) {
- string += "?";
- string += entry.string;
- }
- else {
- for (auto& branch : value.get<Branches>())
- branch->buildString(string);
- }
+void DirectivesGroup::applyExistingImage(Image& image) const {
+ forEach([&](auto const& entry) {
+ processImageOperation(entry.operation, image);
+ });
}
-void NestedDirectives::Cell::forEach(LeafCallback& callback) const {
- if (auto leaf = value.ptr<Leaf>())
- callback(*leaf);
- else {
- for (auto& branch : value.get<Branches>())
- branch->forEach(callback);
- }
+inline size_t DirectivesGroup::hash() const {
+ XXHash3 hasher;
+ for (auto& directives : m_directives)
+ hasher.push((const char*)&directives.hash, sizeof(directives.hash));
+
+ return hasher.digest();
}
-bool NestedDirectives::Cell::forEachAbortable(AbortableLeafCallback& callback) const {
- if (auto leaf = value.ptr<Leaf>()) {
- if (!callback(*leaf))
- return false;
- } else {
- for (auto& branch : value.get<Branches>())
- if (!branch->forEachAbortable(callback))
- return false;
- }
- return true;
+inline size_t hash<DirectivesGroup>::operator()(DirectivesGroup const& s) const {
+ return s.hash();
}
} \ No newline at end of file