-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix LocalVocab merge #1617
base: master
Are you sure you want to change the base?
Fix LocalVocab merge #1617
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,8 +4,10 @@ | |||||
|
||||||
#pragma once | ||||||
|
||||||
#include <algorithm> | ||||||
#include <cstdlib> | ||||||
#include <memory> | ||||||
#include <ranges> | ||||||
#include <span> | ||||||
#include <string> | ||||||
#include <vector> | ||||||
|
@@ -39,7 +41,7 @@ class LocalVocab { | |||||
auto& primaryWordSet() { return *primaryWordSet_; } | ||||||
const auto& primaryWordSet() const { return *primaryWordSet_; } | ||||||
|
||||||
std::optional<ad_utility::BlankNodeManager::LocalBlankNodeManager> | ||||||
std::shared_ptr<ad_utility::BlankNodeManager::LocalBlankNodeManager> | ||||||
localBlankNodeManager_; | ||||||
|
||||||
public: | ||||||
|
@@ -100,6 +102,27 @@ class LocalVocab { | |||||
std::ranges::copy(vocab.otherWordSets_, inserter); | ||||||
*inserter = vocab.primaryWordSet_; | ||||||
} | ||||||
|
||||||
// Also merge the `vocabs` `LocalBlankNodeManager`s, if they exist. | ||||||
using LocalBlankNodeManager = | ||||||
ad_utility::BlankNodeManager::LocalBlankNodeManager; | ||||||
if (auto it = std::ranges::find_if( | ||||||
vocabs, | ||||||
[](const LocalVocab& vocab) -> bool { | ||||||
return vocab.localBlankNodeManager_.get() != nullptr; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Also can be applied in a similar lambda in BlankNodeManager.h. |
||||||
}); | ||||||
it != vocabs.end()) { | ||||||
if (!localBlankNodeManager_) { | ||||||
localBlankNodeManager_ = std::make_shared<LocalBlankNodeManager>( | ||||||
(*it).localBlankNodeManager_->blankNodeManager_); | ||||||
} | ||||||
localBlankNodeManager_->mergeWith( | ||||||
vocabs | std::views::transform( | ||||||
[](const LocalVocab& vocab) | ||||||
-> std::shared_ptr<const LocalBlankNodeManager> { | ||||||
return vocab.localBlankNodeManager_; | ||||||
})); | ||||||
Comment on lines
+120
to
+124
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
} | ||||||
} | ||||||
|
||||||
// Return all the words from all the word sets as a vector. | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
|
||
#include "util/BlankNodeManager.h" | ||
|
||
#include "util/Exception.h" | ||
|
||
namespace ad_utility { | ||
|
||
// _____________________________________________________________________________ | ||
|
@@ -62,9 +64,18 @@ uint64_t BlankNodeManager::LocalBlankNodeManager::getId() { | |
// _____________________________________________________________________________ | ||
bool BlankNodeManager::LocalBlankNodeManager::containsBlankNodeIndex( | ||
uint64_t index) const { | ||
return std::ranges::any_of(blocks_, [index](const Block& block) { | ||
auto containsIndex = [index](const Block& block) { | ||
return index >= block.startIdx_ && index < block.nextIdx_; | ||
}); | ||
}; | ||
auto otherBlocks = std::views::join( | ||
otherManagers_ | | ||
std::views::transform( | ||
[](const std::shared_ptr<const LocalBlankNodeManager>& l) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This copies the blocks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this then also fixes the build failures (otherwise we might have to analyze them further:) |
||
return l->blocks_; | ||
})); | ||
|
||
return std::ranges::any_of(blocks_, containsIndex) || | ||
std::ranges::any_of(otherBlocks, containsIndex); | ||
} | ||
|
||
} // namespace ad_utility |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,6 +162,24 @@ TEST(LocalVocab, merge) { | |
EXPECT_EQ(*indices[1], lit("twoA")); | ||
EXPECT_EQ(*indices[2], lit("oneB")); | ||
EXPECT_EQ(*indices[3], lit("twoB")); | ||
|
||
// Test that the `LocalBlankNodeManager` of vocabs is merged correctly. | ||
LocalVocab vocC, vocD; | ||
ad_utility::BlankNodeManager bnm; | ||
auto id = vocC.getBlankNodeIndex(&bnm); | ||
auto vocabs2 = std::vector{&std::as_const(vocC), &std::as_const(vocD)}; | ||
LocalVocab localVocabMerged2 = LocalVocab::merge(vocabs2); | ||
localVocabMerged2.isBlankNodeIndexContained(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't you miss an |
||
|
||
LocalVocab vocE, vocF; | ||
auto id2 = vocE.getBlankNodeIndex(&bnm); | ||
auto vocabs3 = | ||
std::vector{&std::as_const(localVocabMerged2), &std::as_const(vocF)}; | ||
vocE.mergeWith(vocabs3 | std::views::transform( | ||
[](const LocalVocab* l) -> const LocalVocab& { | ||
return *l; | ||
})); | ||
vocE.isBlankNodeIndexContained(id2); | ||
} | ||
|
||
// _____________________________________________________________________________ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can write the
auto it = ...
outside the loop and then doif (it == end) { return;}
, to reduce the nestedness of the code.