[multi_index and flyweight]
Hi, I'm currently facing some limitations from flyweight and multi_index. Let's begin with multi_index: I'd want to have a multi_index storing containers, and have one index based on all the contained elements. For example: two vectors [1 2 3] and [3 4] would be indexed as 1 -> [1 2 3] 2 -> [1 2 3] 3 -> [1 2 3] [3 4] 4 -> [3 4] How would you do that ? Flyweight: I'd like to have a scoped flyweight, without tracking, i.e.: { ... // create some elements } // All elements are deleted when leaving the scope. However, currently I didn't see a method to clear the internal container. One way I could do that would be copying - pasting the hashed_factory and adding a clear method. It would be easier to extend the class, just having the private container be protected. Would that be possible to do one a future release ? Or is there another easy way to do so ? Regards, Samuel
Samuel Charron
Hi,
I'm currently facing some limitations from flyweight and multi_index.
Let's begin with multi_index:
I'd want to have a multi_index storing containers, and have one index based on all the contained elements. For example: two vectors [1 2 3] and [3 4] would be indexed as 1 -> [1 2 3] 2 -> [1 2 3] 3 -> [1 2 3] [3 4] 4 -> [3 4]
How would you do that ?
If I'm understanding your question well, you can do like
this:
struct container_compare
{
template<typename Container>
bool operator()(const Container& x,const Container& y)const
{
return std::lexicographical_compare(
x.begin(),x.end(),
y.begin(),y.end());
}
};
typedef std::vector<int> value_type;
typedef multi_index_container<
value_type,
indexed_by<
ordered_unique<
identity
multi_t;
Flyweight: I'd like to have a scoped flyweight, without tracking, i.e.: { ... // create some elements } // All elements are deleted when leaving the scope.
However, currently I didn't see a method to clear the internal container. One way I could do that would be copying - pasting the hashed_factory and adding a clear method. It would be easier to extend the class, just having the private container be protected. Would that be possible to do one a future release ? Or is there another easy way to do so ?
There's currently no way to do what you want save the trick you describe, which is perfectly OK. The roadmap for Boost.Flyweight includes features that will allow you to control the factory in the way you describe, see http://www.boost.org/doc/libs/1_41_0/libs/flyweight/doc/future_work.html#ins... but I don't think I'll be able to begin working on new features for this lib until some time in 2010. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Hi,
On Fri, Dec 4, 2009 at 9:58 PM, Joaquin M Lopez Munoz
Samuel Charron
writes: Hi,
I'm currently facing some limitations from flyweight and multi_index.
Let's begin with multi_index:
I'd want to have a multi_index storing containers, and have one index based on all the contained elements. For example: two vectors [1 2 3] and [3 4] would be indexed as 1 -> [1 2 3] 2 -> [1 2 3] 3 -> [1 2 3] [3 4] 4 -> [3 4]
How would you do that ?
If I'm understanding your question well, you can do like this:
struct container_compare { template<typename Container> bool operator()(const Container& x,const Container& y)const { return std::lexicographical_compare( x.begin(),x.end(), y.begin(),y.end()); } };
typedef std::vector<int> value_type;
typedef multi_index_container< value_type, indexed_by< ordered_unique< identity
,container_compare > > > multi_t;
With your solution, my containers are sorted using all their values, it's not what I meant. My problem is that from any element from the container, I want to be able to retrieve the whole container. My index type would be the element contained, and the type of element contained in the multi_index would be the container type. The indices inserted would be all elements from my container, and the element would be one occurence of the container. something like: foreach (element in container) multi_index.insert(element, container); Is this a better explaination ?
Flyweight: I'd like to have a scoped flyweight, without tracking, i.e.: { ... // create some elements } // All elements are deleted when leaving the scope.
However, currently I didn't see a method to clear the internal container. One way I could do that would be copying - pasting the hashed_factory and adding a clear method. It would be easier to extend the class, just having the private container be protected. Would that be possible to do one a future release ? Or is there another easy way to do so ?
There's currently no way to do what you want save the trick you describe, which is perfectly OK. The roadmap for Boost.Flyweight includes features that will allow you to control the factory in the way you describe, see
http://www.boost.org/doc/libs/1_41_0/libs/flyweight/doc/future_work.html#ins...
but I don't think I'll be able to begin working on new features for this lib until some time in 2010.
Ok I'll wait ! Thanks ! Regards, Samuel
Samuel Charron
On Fri, Dec 4, 2009 at 9:58 PM, Joaquin M Lopez Munoz
wrote: Samuel Charron
writes: I'd want to have a multi_index storing containers, and have one index based on all the contained elements. For example: two vectors [1 2 3] and [3 4] would be indexed as 1 -> [1 2 3] 2 -> [1 2 3] 3 -> [1 2 3] [3 4] 4 -> [3 4]
How would you do that ?
If I'm understanding your question well, you can do like this: [...]
With your solution, my containers are sorted using all their values, it's not what I meant.
My problem is that from any element from the container, I want to be able to retrieve the whole container.
My index type would be the element contained, and the type of element contained in the multi_index would be the container type. The indices inserted would be all elements from my container, and the element would be one occurence of the container.
Ok, now I got it. The short ansewer is that Boost.MultiIndex
does not provide any off-the-shelf way to do what you want,
basically because each element (each vector in your case)
appears exactly once in any given index.
Long answer: you can approach your scenario by storing
"container positions" rather than containers and manually
inserting for each new conainer as many different positions
as elements has the container. Kind of as sketched in the
following program:
#include
multi_t;
template
On Tue, Dec 8, 2009 at 9:01 PM, Joaquin M Lopez Munoz
Ok, now I got it. The short ansewer is that Boost.MultiIndex does not provide any off-the-shelf way to do what you want, basically because each element (each vector in your case) appears exactly once in any given index.
Long answer: you can approach your scenario by storing "container positions" rather than containers and manually inserting for each new conainer as many different positions as elements has the container. Kind of as sketched in the following program:
<code snippet>
Is this what you're after?
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Yes that's it. I tried something similar, using boost::flyweight (hence my question about scoped flyweights). I tried to write another kind of index for BMI some time ago, and if I remember correctly, there is no way to map an element to multiple values of an index. Do you think of anything to point into the code of BMI to try doing it ? Do you have any kind of documentation about writing an index for BMI (to be sure what I'm writing is safe) ? Thanks for this library ! Samuel
Samuel Charron escribió:
On Tue, Dec 8, 2009 at 9:01 PM, Joaquin M Lopez Munoz
wrote: Ok, now I got it. The short ansewer is that Boost.MultiIndex does not provide any off-the-shelf way to do what you want, basically because each element (each vector in your case) appears exactly once in any given index.
Long answer: you can approach your scenario by storing "container positions" rather than containers and manually inserting for each new conainer as many different positions as elements has the container. Kind of as sketched in the following program:
<code snippet>
Is this what you're after?
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Yes that's it. I tried something similar, using boost::flyweight (hence my question about scoped flyweights).
I tried to write another kind of index for BMI some time ago, and if I remember correctly, there is no way to map an element to multiple values of an index. Do you think of anything to point into the code of BMI to try doing it ?
I don't think BMI can be tweaked in that way. The idea that each element is mapped exactly once into each index is a fundamental one in the design of the library.
Do you have any kind of documentation about writing an index for BMI (to be sure what I'm writing is safe) ?
No, I'm afraid I don't. If you plan on writing a custom index of your own do not hesitate to come back for help. Others have done in the past, it's not particularly difficult if you take one of the current indices as your starting point. Anyway, I doubt there's a nice way to implement your multiple-value idea. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (4)
-
Joaquin M Lopez Munoz
-
joaquin@tid.es
-
Samuel Charron
-
Samuel Charron