Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/libime/core/segmentgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace libime {

SegmentGraphNodeWithData::~SegmentGraphNodeWithData() = default;

struct SegmentGraphNodePairGreater {
bool operator()(const std::pair<const SegmentGraphNode *,
const SegmentGraphNode *> &lhs,
Expand Down Expand Up @@ -107,6 +109,18 @@ void SegmentGraph::merge(SegmentGraph &graph,
std::unordered_set<const SegmentGraphNode *> nodeToDiscard;
for (size_t i = 0; i < since; i++) {
for (auto &node : mutableNodes(i)) {
// Always take data from the new graph, even for reused nodes.
if (auto *dstNode =
dynamic_cast<SegmentGraphNodeWithData *>(&node)) {
if (i < graph.graph_.size() && graph.graph_[i]) {
const auto *srcNode =
dynamic_cast<const SegmentGraphNodeWithData *>(
graph.graph_[i].get());
dstNode->setData(srcNode ? srcNode->data() : std::any{});
} else {
dstNode->setData(std::any{});
}
}
std::vector<SegmentGraphNode *> newNext;
for (auto &next : node.mutableNexts()) {
SegmentGraphNode *n;
Expand Down
15 changes: 14 additions & 1 deletion src/libime/core/segmentgraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// Workaround a boost missing include bug.
#include <boost/type_traits/add_const.hpp>

#include <any>
#include <cassert>
#include <cstddef>
#include <functional>
Expand Down Expand Up @@ -116,6 +117,18 @@ class LIBIMECORE_EXPORT SegmentGraphNode : public fcitx::Element {
size_t start_;
};

class LIBIMECORE_EXPORT SegmentGraphNodeWithData : public SegmentGraphNode {
public:
using SegmentGraphNode::SegmentGraphNode;
~SegmentGraphNodeWithData() override;

void setData(std::any data) { data_ = std::move(data); }
const std::any &data() const { return data_; }

private:
std::any data_;
};

using SegmentGraphPath = std::vector<const SegmentGraphNode *>;
using DiscardCallback =
std::function<void(const std::unordered_set<const SegmentGraphNode *> &)>;
Expand Down Expand Up @@ -358,7 +371,7 @@ class LIBIMECORE_EXPORT SegmentGraph : public SegmentGraphBase {
}

SegmentGraphNode &newNode(size_t idx) {
graph_[idx] = std::make_unique<SegmentGraphNode>(idx);
graph_[idx] = std::make_unique<SegmentGraphNodeWithData>(idx);
return *graph_[idx];
}

Expand Down
Loading