![](/img/trans.png)
[英]boost::multi_index_container: How to use composite_key (x,y) to support rectangular search?
[英]Boost multi_index_container with composite_key and bitwise-and comparison
我想用boost multi_index_container和Composite_key实现这样的事情:
struct LogicalAnd { bool operator()(const int& argVal1, const int& argVal2) const { return int(argVal1 & argVal2) == argVal1; } }; template<class T, class Enable = void> class FooContainer; template <class T> struct FooContainer<T,typename boost::enable_if<boost::is_base_of<IFoo, T> >::type> { typedef multi_index_container< boost::shared_ptr<T>, indexed_by< hashed_non_unique< composite_key< T, const_mem_fun<T,int,&T::getKey1>, const_mem_fun<T,int,&T::getKey2> >, composite_key_equal_to< LogicalAnd, LogicalAnd > > > > shared_ptr_type; };
知道:
namespace CustomKey { typedef enum { VAL1 = 0x00000001, VAL2 = 0x00000002, ALL = 0xFFFFFFFF } type; }
目标是能够执行以下操作:
container.find(boost::make_tuple(CustomKey::VAL1, CustomKey::ALL));
这将使我能够检索LogicalAnd返回true的所有元素。
问题是我无法使LogicalAnd比较器与multi_index_container一起使用。
我可以通过在Composite_key_equal_to之前添加一个Composite_key_hash来构建它:
composite_key_hash< boost::hash<int>, boost::hash<int> >
但是find操作无法按预期工作,因此不会有太大变化。
我已经搜索了boost文档,并尝试了各种实现,但是我被大量的信息淹没了...
任何帮助表示赞赏!
您正在将哈希用于多索引。
这意味着除非在正确的哈希存储桶中找到某些内容,否则永远不会调用LogicalAnd
比较器。
0x01
和0xFF
不返回相同的值, 即使 0x01 & 0xFF == 0x01
。 也就是说, LogicalAnd
将返回true,但从未调用过。
您将需要一个不变的哈希函数。 LogicalAnd
,我认为这是不可能的。 通过设计,大多数散列函数都应将不同键值之间的冲突减到最小。
恐怕您需要提出一些不同的索引系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.