[Flyweight] Enumerating all values in the factory?
Hi, thanks for the flyweight library, looks good.
Is there a way to enumerate all values in a factory? Consider my use case:
I have a large set of data. Each element has a user defined 'tag',
implemented as a flyweightstd::string. Now the user wants to find all
elements where the 'tag' matches a regex.
Calculating the regex match for all elements in the dataset is
expensive. If I could find all matching tags in the flyweight factory
first, searching the elements is reduced to comparing two flyweights
rather than doing a regex match.
Consider:
struct data {
flyweightstd::string m_tag;
};
vector<data> m_some_data;
The straight forward approach is costly:
for (iterator i=m_some_data.begin();
i!=m_some_data.end() && regex_match(*i);
++i)
;
with the regex match calculated for every element in the vector.
A much more efficient solution is to find all elements in the flyweight
factory that match the regex and then compare the flyweights.
vector
mailinglists
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 flyweightstd::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
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
participants (2)
-
Joaquin M Lopez Munoz
-
mailinglists