使用boost :: flyweight应该可以帮助我节省内存。我正在寻找方法来对解决方案的有效性进行定量测量。有没有办法获得有关boost :: flyweight内部容器的信息?
有没有办法获得内部容器的大小()?如果它是一个基于散列的flyweight,是否有方法获取有关存储桶状态的信息?散列冲突等?
任何指针将不胜感激。
使用boost :: flyweight应该可以帮助我节省内存。我正在寻找方法来对解决方案的有效性进行定量测量。有没有办法获得有关boost :: flyweight内部容器的信息?
有没有办法获得内部容器的大小()?如果它是一个基于散列的flyweight,是否有方法获取有关存储桶状态的信息?散列冲突等?
任何指针将不胜感激。
看看source code的boost::flyweight::hashed_factory_class
:您可以克隆代码以派生自己的用户定义的工厂并提供对内部容器的公共访问权限(优选const
)。
我以前的答案没有提供足够的细节,解决方案其实并不那么简单。这是展示如何做一个完整的片段:
#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
autoregisters本身通过bucket_query_ptr
暴露了咨询桶的接口数等(bucket_query
),您可以根据自己的需要调整和扩展。该解决方案远非优雅,但可能解决您的问题。
这帮了我很多,但我仍然有问题,我无法解决。我希望能够迭代容器条目并阅读它们的引用计数。我需要能够提供使用轻量级的定量理由。如果引用次数从不超过1,这意味着我从来没有得到任何命中。如果我能得到一个很好的命中/失败率。 Joaquin提供的例子让我想起了那里的方式,但我在其他方面挣扎着。 – dbbd 2012-08-27 09:37:25
我添加到你的例子以下以上:'虚拟无效container_enumerator()const的{ \t \t BOOST_FOREACH(常量条目&E,续){ \t \t \t的std :: COUT <<(的std :: string)在线<< “”<< \t \t \t e.count()<< std :: endl; '并且得到它的工作。我不明白的是结果。使用上面的代码,枚举器打印'hello 3 bye 2',但代码只插入2'hello'和1'bye'。 – dbbd 2012-08-27 14:31:51
@dbbd可能是因为当你打印它时,你增加了ref counter? – krico 2013-01-31 22:12:08
克隆 - 你的意思是复制?我不知道如何从模板中派生出来。 – dbbd 2012-08-01 09:16:44