2012-07-31 146 views
2

使用boost :: flyweight應該可以幫助我節省內存。我正在尋找方法來對解決方案的有效性進行定量測量。有沒有辦法獲得有關boost :: flyweight內部容器的信息?

有沒有辦法獲得內部容器的大小()?如果它是一個基於散列的flyweight,是否有方法獲取有關存儲桶狀態的信息?散列衝突等?

任何指針將不勝感激。

回答

1

看看source codeboost::flyweight::hashed_factory_class:您可以克隆代碼以派生自己的用戶定義的工廠並提供對內部容器的公共訪問權限(優選const)。

+0

克隆 - 你的意思是複製?我不知道如何從模板中派生出來。 – dbbd 2012-08-01 09:16:44

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 autoregisters本身通過bucket_query_ptr暴露了諮詢桶的接口數等(bucket_query),您可以根據自己的需要調整和擴展。該解決方案遠非優雅,但可能解決您的問題。

+0

這幫了我很多,但我仍然有問題,我無法解決。我希望能夠迭代容器條目並閱讀它們的引用計數。我需要能夠提供使用輕量級的定量理由。如果引用次數從不超過1,這意味着我從來沒有得到任何命中。如果我能得到一個很好的命中/失敗率。 Joaquin提供的例子讓我想起了那裏的方式,但我在其他方面掙扎着。 – dbbd 2012-08-27 09:37:25

+0

我添加到你的例子以下以上:'虛擬無效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

+0

@dbbd可能是因爲當你打印它時,你增加了ref counter? – krico 2013-01-31 22:12:08

相關問題