[multi-index] Key extractor for 1:n relationship?
All the key extractors in the documentation of Boost.MultiIndex return one
value for an element in a container. Is it somehow possible to use a 1:n
relationship between element and value?
The documentation uses this container definition (among others):
typedef multi_index_container<
employee,
indexed_by<
hashed_unique<
const_mem_fun
employee_set;
Let's assume an employee can have more than one name (std::string becomes std::vectorstd::string). Now if someone searches for Tom or John the same employee should be returned by the container. Is this possible? Boris
Boris Schaeling wrote:
All the key extractors in the documentation of Boost.MultiIndex return one value for an element in a container. Is it somehow possible to use a 1:n relationship between element and value?
The documentation uses this container definition (among others):
typedef multi_index_container< employee, indexed_by< hashed_unique< const_mem_fun
> employee_set;
Let's assume an employee can have more than one name (std::string becomes std::vectorstd::string). Now if someone searches for Tom or John the same employee should be returned by the container. Is this possible?
A vector of strings is still an object, and hashed_unique indexes are
unique. For indexes that allow duplicate items you want "equal_range" like:
typedef pmt_t::index<pid>::type pid_idx_t;
pair
Boris
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Fri, 17 Jul 2009 07:12:00 +0200, plarroy
Boris Schaeling wrote:
All the key extractors in the documentation of Boost.MultiIndex return one value for an element in a container. Is it somehow possible to use a 1:n relationship between element and value?
The documentation uses this container definition (among others):
typedef multi_index_container< employee, indexed_by< hashed_unique< const_mem_fun
> employee_set;
Let's assume an employee can have more than one name (std::string becomes std::vectorstd::string). Now if someone searches for Tom or John the same employee should be returned by the container. Is this possible?
A vector of strings is still an object, and hashed_unique indexes are unique.
Either you misunderstand me or I misunderstand you. :) The hash value would be created for the entire vector? I want to use a string in the vector as a key, not the entire vector itself. If an employee has the names Tom and John I want to find the employee also if I only search for Tom. There would be one employee object in the container but two hash values would need to be generated. Not sure if this is possible at all? Boris
I remember to ask a similar question. I was told that it is not supported.
Finally I reconsidered my design, which was more optimal at all. On the
other hand, it is not said that hash works optimal in that case. You might
better do a lexicographical_compare to the keys and find first one of the
keys from the set, as to hash all and look-up for hash values. Therefore a
multi-map or multi-set as part of your multi-index might be a better choice.
Otherwise, you need to hash all possible tuple combinations, which is an
NP-complete problem.
There is a nice article in Dr.Dobb's on what is more efficient hash based
container or stl::set or map.
http://www.ddj.com/cpp/198800559?pgno=1
Hope that helps,
Regards,
Ovanes
On Fri, Jul 17, 2009 at 2:15 PM, Boris Schaeling
On Fri, 17 Jul 2009 07:12:00 +0200, plarroy
wrote: Either you misunderstand me or I misunderstand you. :) The hash value would be created for the entire vector? I want to use a string in the vector as a key, not the entire vector itself. If an employee has the names Tom and John I want to find the employee also if I only search for Tom. There would be one employee object in the container but two hash values would need to be generated. Not sure if this is possible at all?
Boris
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Boris Schaeling
All the key extractors in the documentation of Boost.MultiIndex return one value for an element in a container. Is it somehow possible to use a 1:n relationship between element and value?
The documentation uses this container definition (among others):
typedef multi_index_container< employee, indexed_by< hashed_unique< const_mem_fun
> employee_set;
Let's assume an employee can have more than one name (std::string becomes std::vectorstd::string). Now if someone searches for Tom or John the same employee should be returned by the container. Is this possible?
No, I don't think it's possible in general. Take into account that when you insert an element x into a multi_index_container m, then x will appear exactly once when traversing any index of m. So if you want something like multiple retrieval within one index you'd have to insert x multiple times. I'm afraid you'll need a separate container to hold this 1:n relationship. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (4)
-
Boris Schaeling
-
Joaquin M Lopez Munoz
-
Ovanes Markarian
-
plarroy