[英]How to use boost::intrusive_ptr with boost::intrusive::list?
我想一次准確地分配一個 object 並將其推送到幾個列表中。 如何使用boost::intrusive_ptr
和boost::intrusive::list
做到這一點? 或者我應該使用另一個容器和引用計數器?
侵入式列表和 intrusive_ptr 不相關。
你可以使用任何一個。 侵入式容器具有更多的管理功能,但每個容器都需要一個專用的鈎子。
intrusive_ptr
更靈活,因為它可以通過使用引用計數來支持無界容器。
此外,請記住,如果您只想對相同的基礎數據進行多個索引,通常您可以使用 Boost MultiIndex 並獲得兩全其美的效果。
最簡單的演示: Live On Compiler Explorer
#include <string>
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/list_hook.hpp>
// for debug output
#include <fmt/ranges.h>
namespace bi = boost::intrusive;
using BaseHook = bi::list_base_hook<>;
using MemberHook = bi::list_member_hook<>;
struct Item : BaseHook {
std::string data;
Item(std::string data) : data(std::move(data)) {}
MemberHook _secondary, _secondary2;
bool operator<(Item const& rhs) const { return data < rhs.data; }
};
template <> struct fmt::formatter<Item> : fmt::formatter<std::string> {
template <typename Ctx>
auto format(Item const&val, Ctx &ctx) { return fmt::format_to(ctx.out(), "'{}'", val.data); }
};
// primary container uses the base hook
using Items = bi::list<Item>;
using Secondary = bi::list<Item, bi::member_hook<Item, MemberHook, &Item::_secondary >>;
using Third = bi::list<Item, bi::member_hook<Item, MemberHook, &Item::_secondary2 >>;
int main() {
Item elements[] { {"one"}, {"two"}, {"three"} };
Items primary(std::begin(elements), std::end(elements));
Secondary idx1(primary.begin(), primary.end());
Third idx2(primary.begin(), primary.end());
idx1.sort();
idx2.reverse();
fmt::print("primary: {}\n", primary);
fmt::print("idx1: {}\n", idx1);
fmt::print("idx2: {}\n", idx2);
}
印刷
primary: {'one', 'two', 'three'}
idx1: {'one', 'three', 'two'}
idx2: {'three', 'two', 'one'}
intrusive_ptr
請注意,這是如何更加動態的,但因此成本更高。 另請注意,由於容器元素不再是“直接值”,所有操作都因為間接而變得更加復雜:
new
)我在這里使用了各種其他 Boost 助手來節省時間,但請注意,這種復雜性可能會更多地降低您的開發速度,具體取決於您是否了解這些助手位
#include <string>
#include <boost/intrusive_ptr.hpp>
#include <boost/smart_ptr/intrusive_ref_counter.hpp>
#include <list>
// helpers to make handling containers of intrusive pointers easier
#include <boost/range/adaptor/indirected.hpp>
#include <boost/ptr_container/indirect_fun.hpp>
#include <memory>
// for debug output
#include <fmt/ranges.h>
struct Item : boost::intrusive_ref_counter<Item> {
std::string data;
Item(std::string data) : data(std::move(data)) {}
bool operator<(Item const& rhs) const { return data < rhs.data; }
};
template <> struct fmt::formatter<Item> : fmt::formatter<std::string> {
template <typename Ctx>
auto format(Item const&val, Ctx &ctx) { return fmt::format_to(ctx.out(), "'{}'", val.data); }
};
int main() {
using List = std::list<boost::intrusive_ptr<Item> >;
List primary;
for (auto name : {"one","two","three"}) {
primary.emplace_back(new Item(name));
}
List idx1(primary.begin(), primary.end());
List idx2(primary.begin(), primary.end());
idx1.sort(boost::make_indirect_fun(std::less<Item>{}));
idx2.reverse();
fmt::print("primary: {}\n", primary | boost::adaptors::indirected);
fmt::print("idx1: {}\n", idx1 | boost::adaptors::indirected);
fmt::print("idx2: {}\n", idx2 | boost::adaptors::indirected);
}
印刷
primary: {'one', 'two', 'three'}
idx1: {'one', 'three', 'two'}
idx2: {'three', 'two', 'one'}
你沒有要求這個,但給定樣本似乎是一個合乎邏輯的解決方案(我意識到樣本可能不代表你的問題,當然):
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
// for debug output
#include <fmt/ranges.h>
struct Item {
std::string data;
bool operator<(Item const& rhs) const { return data < rhs.data; }
};
template <> struct fmt::formatter<Item> : fmt::formatter<std::string> {
template <typename Ctx>
auto format(Item const&val, Ctx &ctx) { return fmt::format_to(ctx.out(), "'{}'", val.data); }
};
namespace bmi = boost::multi_index;
using Table = bmi::multi_index_container<Item,
bmi::indexed_by<
bmi::sequenced<>, // primary
bmi::sequenced<bmi::tag<struct Index1> >,
bmi::sequenced<bmi::tag<struct Index2> >
> >;
int main() {
Table primary{ {"one"},{"two"},{"three"} };
auto& idx1 = primary.get<Index1>();
auto& idx2 = primary.get<Index2>();
idx1.sort();
idx2.reverse();
fmt::print("primary: {}\n", primary);
fmt::print("idx1: {}\n", idx1);
fmt::print("idx2: {}\n", idx2);
}
印刷
primary: {'one', 'two', 'three'}
idx1: {'one', 'three', 'two'}
idx2: {'three', 'two', 'one'}
后者具有 - 迄今為止 - 最大的靈活性和最高級別的語義(例如,刪除元素適用於所有索引)。 您可以擁有關聯索引、有序鍵、復合鍵等。在這種情況下,它們將在replace
或modify
時自動維護。
當然,如果您的用例不像固定集合的索引,那么您可能需要第一個選項。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.