有没有一种方法可以获得关于助推的信息::蝇量级内部容器
Is there a way to get information on a boost::flyweight internal container?
使用boost::flyweight应该可以帮助我节省内存。我正在寻找一种方法来定量衡量解决方案的有效性。
有没有办法得到内部容器的size()?如果它是一个基于哈希的轻量级,有没有办法获得bucket状态的信息?散列冲突等?
任何建议都将不胜感激。
我之前的回答没有提供足够的细节,解决方案真的不那么简单。这是一个完整的片段,展示了如何做到这一点:
#include <boost/flyweight/factory_tag.hpp>
#include <boost/flyweight/hashed_factory_fwd.hpp>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/mpl/aux_/lambda_support.hpp>
#include <boost/mpl/if.hpp>
class bucket_query
{
public:
typedef std::size_t size_type;
virtual size_type bucket_count()const=0;
virtual size_type max_bucket_count()const=0;
virtual size_type bucket_size(size_type n)const=0;
};
static bucket_query* bucket_query_ptr=0;
template<
typename Entry,typename Key,
typename Hash=boost::mpl::na,typename Pred=boost::mpl::na,
typename Allocator=boost::mpl::na
>
class accessible_hashed_factory_class:
public boost::flyweights::factory_marker,
public bucket_query
{
struct index_list:
boost::mpl::vector1<
boost::multi_index::hashed_unique<
boost::multi_index::identity<Entry>,
typename boost::mpl::if_<
boost::mpl::is_na<Hash>,
boost::hash<Key>,
Hash
>::type,
typename boost::mpl::if_<
boost::mpl::is_na<Pred>,
std::equal_to<Key>,
Pred
>::type
>
>
{};
typedef boost::multi_index::multi_index_container<
Entry,
index_list,
typename boost::mpl::if_<
boost::mpl::is_na<Allocator>,
std::allocator<Entry>,
Allocator
>::type
> container_type;
public:
typedef const Entry* handle_type;
accessible_hashed_factory_class(){bucket_query_ptr=this;}
handle_type insert(const Entry& x)
{
return &*cont.insert(x).first;
}
void erase(handle_type h)
{
cont.erase(cont.iterator_to(*h));
}
static const Entry& entry(handle_type h){return *h;}
typedef std::size_t size_type;
virtual size_type bucket_count()const{return cont.bucket_count();}
virtual size_type max_bucket_count()const{return cont.max_bucket_count();}
virtual size_type bucket_size(size_type n)const{return cont.bucket_size(n);}
private:
container_type cont;
public:
typedef accessible_hashed_factory_class type;
BOOST_MPL_AUX_LAMBDA_SUPPORT(
5,accessible_hashed_factory_class,(Entry,Key,Hash,Pred,Allocator))
};
template<
typename Hash=boost::mpl::na,typename Pred=boost::mpl::na,
typename Allocator=boost::mpl::na
BOOST_FLYWEIGHT_NOT_A_PLACEHOLDER_EXPRESSION
>
struct accessible_hashed_factory:boost::flyweights::factory_marker
{
template<typename Entry,typename Key>
struct apply:
boost::mpl::apply2<
accessible_hashed_factory_class<
boost::mpl::_1,boost::mpl::_2,Hash,Pred,Allocator
>,
Entry,Key
>
{};
};
/* testing */
#include <boost/flyweight.hpp>
#include <iostream>
#include <string>
int main()
{
typedef boost::flyweight<std::string,accessible_hashed_factory<> > string_fw;
string_fw s1("hello"),s2("hello"),s3("bye");
std::cout<<"number of buckets: "<<bucket_query_ptr->bucket_count()<<std::endl;
}
其想法是:accessible_hashed_factory_class
通过bucket_query_ptr
自动注册自己,公开一个用于查询bucket计数等的接口(bucket_query
),您可以根据需要进行调整和扩展。这个解决方案远非优雅,但可能会解决你的问题。
查看boost::flyweight::hashed_factory_class
的源代码:您只需克隆代码即可派生自己的用户定义工厂,并提供对内部容器的公共(首选const
)访问权限。
相关文章:
- 有什么方法可以包装自动类型扣除的助推"tee"流的构造?
- 如何检查助推融合序列是否为自适应结构
- 助推.测试检查结构是否相等
- 助推.如何在另一个测试单元中运行一个测试组件
- 助推.野兽速率限制
- 如何将助推兽multi_buffer转换为字符串?
- C++:美化助推树json_parser
- 在 MFC dll 中包含助推互斥头文件时断言
- 使用cpp_int构建大型助推unordered_map
- 助推lexical_cast 从双精度到弦精度
- 助推::绑定生病了,无缘无故地抱怨
- 助推.测试崩溃,***异常:MSVC上的其他
- 为什么助推期货会返回不同的值
- 混合单位与助推::单位
- 交叉编译助推 1.60.0 内部依赖问题
- 如何让助推upgrade_to_unique_lock+condition_variable协同工作
- (详细)如何计算从纪元1900到现在使用助推的持续时间
- 助推.原型和复杂变换
- 助推.绑定返回类型
- 有没有一种方法可以获得关于助推的信息::蝇量级内部容器