Skip to content

Commit

Permalink
[Dropper] + expose/export ZonesList structure while hiding it's templ…
Browse files Browse the repository at this point in the history
…ate based implementation based on std::vector #182 #181

Signed-off-by: Gheorghita Mutu <gheorghitamutu@gmail.com>
  • Loading branch information
gheorghitamutu committed Apr 5, 2024
1 parent de9af75 commit 00907c0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 57 deletions.
2 changes: 1 addition & 1 deletion AppCUI
31 changes: 31 additions & 0 deletions GViewCore/include/GView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,37 @@ namespace Utils
virtual uint32 GetSelectionZonesCount() const = 0;
virtual GView::TypeInterface::SelectionZone GetSelectionZone(uint32 index) const = 0;
};

// Structure to represent an interval
struct CORE_EXPORT Zone {
struct Interval {
uint64 low{ INVALID_OFFSET }, high{ INVALID_OFFSET };
} interval{};

AppCUI::Graphics::ColorPair color{ NoColorPair };
AppCUI::Utils::FixSizeString<25> name{};

Zone(uint64 low, uint64 high) : interval{ low, high }
{
}
Zone(uint64 low, uint64 high, ColorPair cp, std::string_view name) : interval{ low, high }, color(cp), name(name)
{
}
Zone() : interval{ INVALID_OFFSET, INVALID_OFFSET }, color(NoColorPair), name(){};
};

class CORE_EXPORT ZonesList
{
void* context{ nullptr };

public:
ZonesList();
~ZonesList();

bool Add(uint64 start, uint64 end, AppCUI::Graphics::ColorPair c, std::string_view txt);
const std::optional<Zone> OffsetToZone(uint64 offset) const;
bool SetCache(const Zone::Interval& interval);
};
} // namespace Utils

namespace Hashes
Expand Down
67 changes: 43 additions & 24 deletions GViewCore/src/Utils/ZonesList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,65 @@
using namespace GView::Utils;
using namespace AppCUI::Graphics;

void ZonesList::Add(uint64 s, uint64 e, ColorPair c, std::string_view txt)
struct ZonesListContext {
std::vector<Zone> zones{};
std::vector<Zone> cache{};
};

ZonesList::ZonesList()
{
context = new ZonesListContext;
}

ZonesList::~ZonesList()
{
if (context != nullptr) {
delete reinterpret_cast<ZonesListContext*>(context);
}
}

bool ZonesList::Add(uint64 s, uint64 e, ColorPair c, std::string_view txt)
{
zones.emplace_back(s, e, c, txt);
CHECK(context != nullptr, false, "");
auto ctx = reinterpret_cast<ZonesListContext*>(this->context);
ctx->zones.emplace_back(s, e, c, txt);
return true;
}

const std::optional<Zone> ZonesList::OffsetToZone(uint64 position) const
{
for (const auto& zone : cache)
{
if (zone.interval.low <= position && position <= zone.interval.high)
{
CHECK(context != nullptr, std::nullopt, "");
auto ctx = reinterpret_cast<ZonesListContext*>(this->context);

for (const auto& zone : ctx->cache) {
if (zone.interval.low <= position && position <= zone.interval.high) {
return zone;
}
}

return std::nullopt;
}

void ZonesList::SetCache(const Zone::Interval& interval)
bool ZonesList::SetCache(const Zone::Interval& interval)
{
cache.clear();
CHECK(context != nullptr, false, "");
auto ctx = reinterpret_cast<ZonesListContext*>(this->context);

for (const auto& zone : zones)
{
ctx->cache.clear();

for (const auto& zone : ctx->zones) {
if ((zone.interval.low >= interval.low && zone.interval.low <= interval.high) ||
interval.low >= zone.interval.low && interval.low <= zone.interval.high)
{
cache.push_back(zone);
interval.low >= zone.interval.low && interval.low <= zone.interval.high) {
ctx->cache.push_back(zone);
}
}

std::sort(
cache.begin(),
cache.end(),
[](const Zone& a, const Zone& b)
{
if (a.interval.low == b.interval.low)
{
return a.interval.high < b.interval.high;
}
return a.interval.low > b.interval.low;
});
std::sort(ctx->cache.begin(), ctx->cache.end(), [](const Zone& a, const Zone& b) {
if (a.interval.low == b.interval.low) {
return a.interval.high < b.interval.high;
}
return a.interval.low > b.interval.low;
});

return true;
}
31 changes: 0 additions & 31 deletions GViewCore/src/include/Internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,37 +94,6 @@ namespace Utils
void CopySetTo(bool ascii[256]);
};

// Structure to represent an interval
struct Zone
{
struct Interval
{
uint64 low{ INVALID_OFFSET }, high{ INVALID_OFFSET };
} interval{};

AppCUI::Graphics::ColorPair color{ NoColorPair };
AppCUI::Utils::FixSizeString<25> name{};

Zone(uint64 low, uint64 high) : interval{ low, high }
{
}
Zone(uint64 low, uint64 high, ColorPair cp, std::string_view name) : interval{ low, high }, color(cp), name(name)
{
}
Zone() : interval{ INVALID_OFFSET, INVALID_OFFSET }, color(NoColorPair), name(){};
};

class ZonesList
{
std::vector<Zone> zones{};
std::vector<Zone> cache{};

public:
void Add(uint64 start, uint64 end, AppCUI::Graphics::ColorPair c, std::string_view txt);
const std::optional<Zone> OffsetToZone(uint64 offset) const;
void SetCache(const Zone::Interval& interval);
};

struct UnicodeString
{
char16* text;
Expand Down
2 changes: 1 addition & 1 deletion GenericPlugins/Dropper/include/Dropper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Instance
public:
bool Init()
{
// dummy init for now
// dummy init for now
std::unique_ptr<IDrop> a = std::make_unique<IpAddress>();
droppers.push_back(std::move(a));

Expand Down

0 comments on commit 00907c0

Please sign in to comment.