Boost logo

Boost Users :

Subject: Re: [Boost-users] [Flyweight] Enumerating all values in the factory?
From: Joaquin M Lopez Munoz (joaquin_at_[hidden])
Date: 2009-06-26 10:38:31


mailinglists <mailinglists <at> hajo-kirchhoff.de> writes:

>
> Hi, thanks for the flyweight library, looks good.
>
> Is there a way to enumerate all values in a factory?

No, currently there is no such facility. The roadmap
includes an introspection API that could serve this
purpose:

http://www.boost.org/libs/flyweight/doc/future_work.html#instrospection

but this is unavailable for the moment being.

> Consider my use case:
>
> I have a large set of data. Each element has a user defined 'tag',
> implemented as a flyweight<std::string>. Now the user wants to find all
> elements where the 'tag' matches a regex.

Although there are ways (through custom factories) to
tweak Boost.Flyweight into providing access to the
internal factory, I think the following approach is
more robust: when traversing m_data for matches with
regex, maintain two cache structures like this:

  template<typename F>
  struct flyweight_hash
  {
    std::size_t operator()(const F& x)const
    {
      typedef typename F::value_type value_type;
      boost::hash<const value_type*> h;
      return h(&x.get());
    }
  };

  boost::unordered_set<
    flyweight<std::string>,
    flyweight_hash<flyweight<std::string> >
> cache_yes,cache_no;

  for(iterator i=m_some_data.begin();i!=m_some_data.end();++i)
  {
    if(cache_yes.find(i->m_data)!=cache_yes.end()||
       cache_no.find(i->m_data)!=cache_no.end())continue;
    if(regex_match(*i))cache_yes.insert(i->m_data);
    else cache_no.insert(i->m_data);
  }

  return cache_yes;

Note that inserting into the caches is very fast
as flyweight_hash hashes by the addresses of values
referred to by the flyweight objects. Hope this helps.
Thank you for using Boost.Flyweight (I think this is
the first public user question on the lib!)

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net