Есть ли способ получить информацию о внутреннем контейнере boost::flyweight?
Использование boost::flyweight должно помочь мне сэкономить память. Я ищу способ получить количественную оценку эффективности решения.
Есть ли способ получить размер () внутреннего контейнера? Если это хэш на основе хеш-кода, есть ли способ получить информацию о статусе корзины? хеш-коллизии и т.д?
Любые указатели будут оценены.
2 ответа
Мой предыдущий ответ не дал достаточно подробностей, и решение на самом деле не так просто. Это полный фрагмент, показывающий, как это сделать:
#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_query
) что вы можете адаптировать и расширять в соответствии с вашими потребностями. Решение далеко не элегантное, но может решить вашу проблему.
Посмотрите на исходный код boost::flyweight::hashed_factory_class
: вы можете просто клонировать код, чтобы получить собственную фабрику, заданную пользователем, и предоставить публичную (предпочтительно const
) доступ к внутреннему контейнеру.