Boost C++ 库

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Boost.MultiIndex 文档 - 键提取器参考 - Boost C++ 函数库


目录

键提取器

键提取器类被 基于键的索引 用于从 multi_index_container 的元素中获取索引键。一个 CopyConstructibleCopyAssignable 的类 KeyFromValue 被称为从类型 Type 进行键提取,如果

  1. 类型 KeyFromValue::result_type 已定义,
  2. k1(ca) 已定义并且返回一个可转换为 const KeyFromValue::result_type& 的值,
  3. 如果 k2k1 的拷贝,则 k1(ca)k2(ca) 的值相同,
对于所有类型为 const KeyFromValuek1k2,以及类型为 const Type&ca

此外,如果满足以下额外条件,KeyFromValue 是一个读/写键提取器

  1. remove_reference_t<KeyFromValue::result_type> 没有被 const 限定。
  2. k1(a) 已定义并且返回一个可转换为 KeyFromValue::result_type& 的值,
  3. k1(a) 被绑定到与 k1(const_cast<const Type&>(a)) 相同的对象,
对于所有类型为 const KeyFromValuek1 和类型为 Type&a

Boost.MultiIndex 提供了十二种通用键提取器

另外,在支持 C++17 的环境中,提供了一个别名模板 key,允许以非常简洁的方式指定上述提取器(除了 identity)。

链式指针

Boost.MultiIndex 提供的键提取器根据类型 Type 进行模板化,用于提取键,这些键不仅来自类型 Type 的对象,还来自 Boost.Ref 提供的引用包装器,以及指向 Type(或 Type 的引用包装器)的链式指针:链式指针是任何类型 P,对于类型为 const P 的对象 p

也就是说,链式指针是任意指针类对象的组合,最终解引用到 Type&boost::reference_wrapper<Type> 类型的值。

头文件 "boost/multi_index/key_extractors.hpp" 声明

#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <boost/multi_index/composite_key.hpp>

此头文件包含 Boost.MultiIndex 提供的所有键提取器。

头文件 "boost/multi_index/identity.hpp" 声明

namespace boost{

namespace multi_index{

template<typename T> struct identity;

} // namespace boost::multi_index 

} // namespace boost

类模板 identity

identity 是一个 Key Extractor,它表现得像一个不做任何事情的恒等函数。

template<typename Type>
struct identity
{
  typedef Type result_type;

  // only provided if const ChainedPtr& is not convertible to const Type&
  template<typename ChainedPtr> Type& operator()(const ChainedPtr& x)const;

  const Type& operator()(const Type& x)const; 
  Type&       operator()(Type& x)const; // only provided if Type is non-const

  // only provided if Type is non-const
  const Type& operator()(const reference_wrapper<const Type>& x)const; 

  // only provided if Type is const
  Type& operator()(
    const reference_wrapper<typename remove_const<Type>::type>& x)const; 

  Type& operator()(const reference_wrapper<Type>& x)const;
};

identity<Type> 是一个模型

identity 成员

template<typename ChainedPtr> Type& operator()(const ChainedPtr& x)const;
要求: ChainedPtr 是一个指向 Type链式指针 类型。
返回值:x 所指向的对象的引用。
const Type& operator()(const Type& x)const;
返回值: x
Type& operator()(Type& x)const;
返回值: x
const Type& operator()(const reference_wrapper<const Type>& x)const;
返回值: x.get()
Type& operator()(const reference_wrapper<typename remove_const<Type>::type>& x)const;
返回值: x.get()
Type& operator()(const reference_wrapper<Type>& x)const;
返回值: x.get()

头文件 "boost/multi_index/member.hpp" 声明

namespace boost{

namespace multi_index{

template<class Class,typename Type,Type Class::*PtrToMember>
struct member;

template<class Class,typename Type,std::size_t OffsetOfMember>
struct member_offset; // deprecated

#define BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName) implementation defined

} // namespace boost::multi_index 

} // namespace boost

类模板 member

member 是一个 Key Extractor,用于访问类的给定成员。

template<class Class,typename Type,Type Class::*PtrToMember>
struct member
{
  typedef Type result_type;

  // only provided if const ChainedPtr& is not convertible to const Class&
  template<typename ChainedPtr> Type& operator()(const ChainedPtr& x)const;

  const Type& operator()(const Class& x)const;
  Type&       operator()(Class& x)const; // only provided if Type is non-const
  const Type& operator()(const reference_wrapper<const Class>& x)const;
  Type&       operator()(const reference_wrapper<Class>& x)const;
};

PtrToMember 模板参数指定了要提取的成员的特定 Type Class::* 指针。member<Class,Type,PtrToMember> 是一个模型

member 成员

template<typename ChainedPtr> Type& operator()(const ChainedPtr& x)const;
要求: ChainedPtr 是一个指向 Type链式指针 类型。
返回值:x 所指向的对象的引用。
const Type& operator()(const Class& x)const;
返回值: x.*PtrToMember
Type& operator()(Class& x)const;
返回值: x.*PtrToMember
const Type& operator()(const reference_wrapper<const Class>& x)const;
返回值: x.get().*PtrToMember
Type& operator()(const reference_wrapper<Class>& x)const;
返回值: x.get().*PtrToMember

类模板 member_offset

member_offset 是为了克服某些旧版编译器的限制而设计的,目前已弃用。有关更多信息,请参阅 Boost.MultiIndex 的 先前版本

BOOST_MULTI_INDEX_MEMBER

BOOST_MULTI_INDEX_MEMBER(Class,Type,MemberName)

此宏被设计为一种可移植性机制,用于旧版编译器,这些编译器不支持 member。因此,在现代环境中不再需要它,尽管一些用户可能仍然因为它提供了稍微简洁的语法而更倾向于使用它而不是直接使用 member

头文件 "boost/multi_index/mem_fun.hpp" 声明

namespace boost{

namespace multi_index{

template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const>
struct const_mem_fun;

template<
  class Class,typename Type,Type (Class::*PtrToMemberFunction)()const volatile
>
struct cv_mem_fun;

template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const&>
struct cref_mem_fun;

template<
  class Class,typename Type,Type (Class::*PtrToMemberFunction)()const volatile&
>
struct cvref_mem_fun;

template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()>
struct mem_fun;

template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()volatile>
struct volatile_mem_fun;

template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()&>
struct ref_mem_fun;

template<
  class Class,typename Type,Type (Class::*PtrToMemberFunction)()volatile&
>
struct vref_mem_fun;

template<
  class Class,typename Type,
  typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction
>
struct const_mem_fun_explicit; // deprecated

template<
  class Class,typename Type,
  typename PtrToMemberFunctionType,PtrToMemberFunctionType PtrToMemberFunction
>
struct mem_fun_explicit;       // deprecated

#define BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName) \
implementation defined
#define BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName) \
implementation defined

} // namespace boost::multi_index 

} // namespace boost

类模板 const_mem_fun

const_mem_fun 是一个 Key Extractor,它返回通过调用类的给定常量成员函数所产生的结果作为键。

template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()const>
struct const_mem_fun
{
  typedef typename remove_reference<Type>::type result_type;

  // only provided if const ChainedPtr& is not convertible to const Class&
  template<typename ChainedPtr> Type operator()(const ChainedPtr& x)const;

  Type operator()(const Class& x)const;
  Type operator()(const reference_wrapper<const Class>& x)const;
  Type operator()(const reference_wrapper<Class>& x)const;
};

PtrToMemberFunction 模板参数指定了用于提取的常量成员函数的特定 Type (Class::*PtrToMemberFunction)()const 指针。const_mem_fun<Class,Type,PtrToMemberFunction> 是一个模型

并且在所有这些情况下,如果 Type 是指向非 const 限定类型的左值引用,它也是读/写的。

const_mem_fun 成员

template<typename ChainedPtr> Type operator()(const ChainedPtr& x)const;
要求: ChainedPtr 是一个指向 Type链式指针 类型。
返回值: (y.*PtrToMemberFunction)(),其中 yx 所指向的对象。
Type operator()(const Class& x)const;
返回值: (x.*PtrToMemberFunction)()
Type operator()(const reference_wrapper<const Class>& x)const;
返回值: (x.get().*PtrToMemberFunction)()
Type operator()(const reference_wrapper<Class>& x)const;
返回值: (x.get().*PtrToMemberFunction)()

类模板 cv_mem_fun, cref_mem_funcvref_mem_fun

这些 Key Extractorsconst_mem_fun 的变体,用于在传递的成员函数是

除此之外,它们的接口与 const_mem_fun 完全相同。

类模板 mem_fun

mem_fun 是一个 Key Extractor,它返回通过调用类的给定成员函数所产生的结果作为键。

template<class Class,typename Type,Type (Class::*PtrToMemberFunction)()>
struct mem_fun
{
  typedef typename remove_reference<Type>::type result_type;

  // only provided if ChainedPtr& is not convertible to Class&
  template<typename ChainedPtr> Type operator()(const ChainedPtr& x)const;

  Type operator()(Class& x)const;
  Type operator()(const reference_wrapper<Class>& x)const;
};

PtrToMemberFunction 模板参数指定了用于提取的成员函数的特定 Type (Class::*PtrToMemberFunction)() 指针。mem_fun<Class,Type,PtrToMemberFunction> 是一个模型

并且在所有这些情况下,如果 Type 是指向非 const 限定类型的左值引用,它也是读/写的。

mem_fun 成员

template<typename ChainedPtr> Type operator()(const ChainedPtr& x)const;
要求: ChainedPtr 是一个指向 Type链式指针 类型。
返回值: (y.*PtrToMemberFunction)(),其中 yx 所指向的对象。
Type operator()(Class& x)const;
返回值: (x.*PtrToMemberFunction)()
Type operator()(const reference_wrapper<Class>& x)const;
返回值: (x.get().*PtrToMemberFunction)()

类模板 volatile_mem_fun, ref_mem_funvref_mem_fun

这些 Key Extractorsmem_fun 的变体,用于在传递的成员函数是

除此之外,它们的接口与 mem_fun 完全相同。

类模板 const_mem_fun_explicitmem_fun_explicit

这些提取器是为了解决 MSVC++ 6.0 的问题而提供的,现在已弃用。有关更多信息,请参阅 Boost.MultiIndex 的 先前版本

BOOST_MULTI_INDEX_CONST_MEM_FUNBOOST_MULTI_INDEX_MEM_FUN

BOOST_MULTI_INDEX_CONST_MEM_FUN(Class,Type,MemberFunName)
BOOST_MULTI_INDEX_MEM_FUN(Class,Type,MemberFunName)

用于使用 const_mem_funmem_fun 的可移植性宏。虽然现代编译器不再需要它们,但一些用户可能仍会选择使用它们,因为它们提供了稍微简洁的语法。

头文件 "boost/multi_index/global_fun.hpp" 声明

namespace boost{

namespace multi_index{

template<class Value,typename Type,Type (*PtrToFunction)(Value)>
struct global_fun;

} // namespace boost::multi_index 

} // namespace boost

类模板 global_fun

global_fun 是一个 Key Extractor,它基于一个给定的全局或静态成员函数,该函数接受基类型作为参数并返回关联的键。

template<class Value,typename Type,Type (*PtrToFunction)(Value)>
struct global_fun
{
  typedef typename remove_reference<Type>::type result_type;

  // Only provided under the following circumstances:
  //   - If Value is a reference to a constant type, only provided
  //     when const ChainedPtr& is not convertible to Value;
  //   - if Value is a reference to a non-const type, only provided
  //     when ChainedPtr& is not convertible to Value;
  //   - else, only provided when const ChainedPtr& is not
  //     convertible to const Value&.
  Type operator()(const ChainedPtr& x)const;
  
  // only provided if Value is a reference type
  Type operator()(Value x)const;

  // only provided if Value is not a reference type
  Type operator()(const Value& x)const;

  // only provided if Value is not a reference type
  Type operator()(const reference_wrapper<const Value>& x)const;

  // only provided if Value is a reference type
  Type operator()(
    const reference_wrapper<
      remove_reference<Value>::type>& x)const;

  // only provided if Value is not a reference type or is
  // a reference to a constant type
  Type operator()(
    const reference_wrapper<
      typename remove_const<
        typename remove_reference<Value>::type>::type>& x)const;
};

PtrToFunction 指定了用于从某个 BaseType 中提取类型为 Type 的键的特定函数。global_fun 支持以下函数签名

global_fun<Type,Value,PtrToFunction> 是一个模型ValueBaseTypeconst BaseType& 时,global_fun<Type,Value,PtrToFunction> 也是一个模型在以上所有五种情况下,如果 Type 是指向非 const 限定类型的左值引用,global_fun<Type,Value,PtrToFunction> 也是读/写的。

global_fun 成员

template<typename ChainedPtr> Type operator()(const ChainedPtr& x)const;
要求: ChainedPtr 是一个指向 Value链式指针 类型。
返回值: PtrToFunction)(y),其中 yx 所指向的对象。
Type operator()(Value x)const;
返回值: PtrToFunction(x)
Type operator()(const Value& x)const;
返回值: PtrToFunction(x)
Type operator()(const reference_wrapper<const Value>& x)const;
返回值: PtrToFunction(x.get())
Type operator()(
  const reference_wrapper<remove_reference<Value>::type>& x)const;
返回值: PtrToFunction(x.get())
Type operator()(
  const reference_wrapper<
    typename remove_const<
      typename remove_reference<Value>::type>::type>& x)const;
返回值: PtrToFunction(x.get())

头文件 "boost/multi_index/composite_key.hpp" 声明

namespace boost{

namespace multi_index{

template<typename Value,typename KeyFromValue0,...,typename KeyFromValuen>
struct composite_key;

template<typename CompositeKey>
struct composite_key_result;
  
// comparison operators for composite_key_result:

// OP is any of ==,<,!=,>,>=,<=

template<typename CompositeKey1,typename CompositeKey2>
bool operator OP(
  const composite_key_result<CompositeKey1>& x,
  const composite_key_result<CompositeKey2>& y);

template<typename CompositeKey,typename... Values>
bool operator OP(
  const composite_key_result<CompositeKey>& x,
  const std::tuple<Values...>& y);

template<typename CompositeKey,typename... Values>
bool operator OP(
  const std::tuple<Values...>& x,
  const composite_key_result<CompositeKey>& y);

template<typename CompositeKey,typename Value0,...,typename Valuen>
bool operator OP(
  const composite_key_result<CompositeKey>& x,
  const boost::tuple<Value0,...,Valuen>& y);

template<typename Value0,...,typename Valuen,typename CompositeKey>
bool operator OP(
  const boost::tuple<Value0,...,Valuen>& x,
  const composite_key_result<CompositeKey>& y);

// equality functors:

template<typename Pred0,...,typename Predn>
struct composite_key_equal_to;
  
template<typename CompositeKeyResult>
struct composite_key_result_equal_to; // deprecated

// comparison functors:

template<typename Compare0,...,typename Comparen>
struct composite_key_compare;
  
template<typename CompositeKeyResult>
struct composite_key_result_less;     // deprecated

template<typename CompositeKeyResult>
struct composite_key_result_greater;  // deprecated

// hash functors:

template<typename Hash0,...,typename Hashn>
struct composite_key_hash;
  
template<typename CompositeKeyResult>
struct composite_key_result_hash;     // deprecated

} // namespace boost::multi_index

} // namespace boost

// specializations of external functors for composite_key_result:

namespace std{

template<typename CompositeKey>
struct equal_to<boost::multi_index::composite_key_result<CompositeKey> >;

template<typename CompositeKey>
struct less<boost::multi_index::composite_key_result<CompositeKey> >;

template<typename CompositeKey>
struct greater<boost::multi_index::composite_key_result<CompositeKey> >;

} // namespace std

namespace boost{

template<typename CompositeKey>
struct hash<boost::multi_index::composite_key_result<CompositeKey> >;

} // namespace boost

类模板 composite_key

composite_key 是一个 Key Extractor,它返回多个编译时指定的键提取器的组合值。返回的对象类型为 composite_key_result<composite_key>

template<typename Value,typename KeyFromValue0,...,typename KeyFromValuen>
struct composite_key
{
  typedef boost::tuple<KeyFromValue0,...,KeyFromValuen> key_extractor_tuple;
  typedef Value                                         value_type;
  typedef composite_key_result<composite_key>           result_type;

  composite_key(
    const KeyFromValue0& k0=KeyFromValue0(),
    ...
    const KeyFromValuen& kn=KeyFromValuen());

  composite_key(const key_extractor_tuple& x);

  const key_extractor_tuple& key_extractors()const;
  key_extractor_tuple&       key_extractors()

  // only provided if const ChainedPtr& is not convertible to const value_type&
  template<typename ChainedPtr>
  result_type operator()(const ChainedPtr& x)const;
  
  result_type operator()(const value_type& x)const;
  result_type operator()(const reference_wrapper<const value_type>& x)const;
  result_type operator()(const reference_wrapper<value_type>& x)const;
};

KeyFromValue0, ... , KeyFromValuen 是组合键的键提取器类型。这些类型中的每一种都必须是 ValueKey Extractor。至少需要提供一个键提取器。composite_key 实例化的最大键提取器数量是实现定义的。composite_key 内部存储了每个组成键提取器的对象。composite_key<Value,KeyFromValue0,...,KeyFromValuen> 是一个模型

composite_key 成员

composite_key(
  const KeyFromValue0& k0=KeyFromValue0(),
  ...
  const KeyFromValuen& kn=KeyFromValuen());
效果: 构建一个 composite_key,它存储所提供键提取器对象的副本。
composite_key(const key_extractor_tuple& x);
效果: 构建一个 composite_key,它存储 x 中所提供的键提取器对象的副本。
const key_extractor_tuple& key_extractors()const;
返回值: 对包含 composite_key 内部存储的键提取器的元组的常量引用。
key_extractor_tuple& key_extractors();
返回值: 对包含 composite_key 内部存储的键提取器的元组的引用。
template<typename ChainedPtr>
result_type operator()(const ChainedPtr& x)const;
要求: ChainedPtr 是一个指向 result_type链式指针 类型。
返回值: 一个 result_type 对象,该对象取决于 *thisy,其中 yx 所指向的对象。
result_type operator()(const value_type& x)const;
返回值: 一个 result_type 对象,该对象取决于 *thisx
result_type operator()(const reference_wrapper<const value_type>& x)const;
返回值: 一个 result_type 对象,该对象取决于 *thisx.get()
result_type operator()(const reference_wrapper<value_type>& x)const;
返回值: 一个 result_type 对象,该对象取决于 *thisx.get()

类模板 composite_key_result

这是一个由 composite_key 实例返回的、作为其提取键的不透明类型。

template<typename CompositeKey>
struct composite_key_result
{
  no public interface available
};

// comparison:
  
// OP is any of ==,<,!=,>,>=,<=

template<typename CompositeKey1,typename CompositeKey2>
bool operator OP(
  const composite_key_result<CompositeKey1>& x,
  const composite_key_result<CompositeKey2>& y);

template<typename CompositeKey,typename... Values>
bool operator OP(
  const composite_key_result<CompositeKey>& x,
  const std::tuple<Values...>& y);

template<typename CompositeKey,typename... Values>
bool operator OP(
  const std::tuple<Values...>& x,
  const composite_key_result<CompositeKey>& y);

template<typename CompositeKey,typename Value0,...,typename Valuen>
bool operator OP(
  const composite_key_result<CompositeKey>& x,
  const boost::tuple<Value0,...,Valuen>& y);

template<typename Value0,...,typename Valuen,typename CompositeKey>
bool operator OP(
  const boost::tuple<Value0,...,Valuen>& x,
  const composite_key_result<CompositeKey>& y);
CompositeKey 是与 composite_key_result 类型关联的 composite_key 实例。由复合键返回的 composite_key_result 类型对象必须始终被视为临时对象,即不应存储或复制它们。composite_key_result保证是 DefaultConstructibleCopyAssignable。每个 composite_key_result<CompositeKey> 类型对象都内部关联到它返回的 CompositeKey 以及已应用于复合键的 CompositeKey::value_type 类型对象。

符号约定

给定一个类型为 composite_key_result<CompositeKey>x,我们使用以下表示法

此外,如果 y 是一个 std::tupleboost::tuple 类型的值,我们定义

比较运算符

template<typename CompositeKey1,typename CompositeKey2>
bool operator==(
  const composite_key_result<CompositeKey1>& x,
  const composite_key_result<CompositeKey2>& y);
template<typename CompositeKey,typename... Values>
bool operator==(
  const composite_key_result<CompositeKey>& x,
  const std::tuple<Values...>& y);
template<typename CompositeKey,typename... Values>
bool operator==(
  const std::tuple<Values...>& x,
  const composite_key_result<CompositeKey>& y);
template<typename CompositeKey,typename Value0,...,typename Valuen>
bool operator==(
  const composite_key_result<CompositeKey>& x,
  const boost::tuple<Value0,...,Valuen>& y);
template<typename Value0,...,typename Valuen,typename CompositeKey>
bool operator==(
  const boost::tuple<Value0,...,Valuen>& x,
  const composite_key_result<CompositeKey>& y);
要求: length(x)==length(y)。表达式 xi==yi 对所有 i[0,length(x)) 范围内有效。
返回值: 当且仅当
xi==yi 对所有 i[0,length(x)) 范围内成立。
复杂度: 进行的键提取操作和比较操作不超过评估上述表达式所必需的操作,从 i==0 开始。一旦结果确定为 false,评估就会短路。
template<typename CompositeKey1,typename CompositeKey2>
bool operator<(
  const composite_key_result<CompositeKey1>& x,
  const composite_key_result<CompositeKey2>& y);
template<typename CompositeKey,typename... Values>
bool operator<(
  const composite_key_result<CompositeKey>& x,
  const std::tuple<Values...>& y);
template<typename CompositeKey,typename... Values>
bool operator<(
  const std::tuple<Values...>& x,
  const composite_key_result<CompositeKey>& y);
template<typename CompositeKey,typename Value0,...,typename Valuen>
bool operator<(
  const composite_key_result<CompositeKey>& x,
  const boost::tuple<Value0,...,Valuen>& y);
template<typename Value0,...,typename Valuen,typename CompositeKey>
bool operator<(
  const boost::tuple<Value0,...,Valuen>& x,
  const composite_key_result<CompositeKey>& y);
要求: 表达式 xi<yiyi<xi 对所有 i[0,min(length(x),length(y))) 范围内有效。
返回值: 当且仅当存在某个 j 在范围 [0,min(length(x),length(y))) 内,使得
!(xi<yi) && !(yi<xi) 对所有 i[0,j) 范围内成立,
  xj<yj.
复杂度: 进行的键提取操作和比较操作不超过评估上述表达式所必需的操作,从 i==0 开始。一旦结果确定为 false,评估就会短路。
template<typename CompositeKey1,typename CompositeKey2>
bool operator OP(
  const composite_key_result<CompositeKey1>& x,
  const composite_key_result<CompositeKey2>& y);
template<typename CompositeKey,typename... Values>
bool operator OP(
  const composite_key_result<CompositeKey>& x,
  const std::tuple<Values...>& y);
template<typename CompositeKey,typename... Values>
bool operator OP(
  const std::tuple<Values...>& x,
  const composite_key_result<CompositeKey>& y);
template<typename CompositeKey,typename Value0,...,typename Valuen>
bool operator OP(
  const composite_key_result<CompositeKey>& x,
  const boost::tuple<Value0,...,Valuen>& y);
template<typename Value0,...,typename Valuen,typename CompositeKey>
bool operator OP(
  const boost::tuple<Value0,...,Valuen>& x,
  const composite_key_result<CompositeKey>& y);

(OP!=, >, >=, <= 中的任何一个。)

要求: 下列表达式对于考虑的特定 OP 有效。
返回值: 当且仅当
!(x==y) (OP!=),
  y< x  (OP),
!(x< y) (OP>=),
!(y< x) (OP<=).

相等性

类模板 composite_key_equal_to

composite_key_equal_to 使用内部存储的多个基本相等谓词集合来测试 composite_key_result 实例之间的相等性,以及这些实例与值元组之间的相等性。

template<typename Pred0,...,typename Predn>
struct composite_key_equal_to
{
  typedef boost::tuple<Pred0,...,Predn> key_eq_tuple;

  composite_key_equal_to(
    const Pred0& p0=Pred0(),
    ...
    const Predn& pn=Predn());

  composite_key_equal_to(const key_eq_tuple& x);

  const key_eq_tuple& key_eqs()const;
  key_eq_tuple&       key_eqs();

  template<typename CompositeKey1,typename CompositeKey2>
  bool operator()(
    const composite_key_result<CompositeKey1> & x,
    const composite_key_result<CompositeKey2> & y)const;
  
  template<typename CompositeKey,typename... Values>
  bool operator()(
    const composite_key_result<CompositeKey>& x,
    const std::tuple<Values...>& y)const;

  template<typename CompositeKey,typename... Values>
  bool operator()(
    const std::tuple<Values...>& x,
    const composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value0,...,typename Valuen>
  bool operator()(
    const composite_key_result<CompositeKey>& x,
    const boost::tuple<Value0,...,Valuen>& y)const;

  template<typename Value0,...,typename Valuen,typename CompositeKey>
  bool operator()(
    const boost::tuple<Value0,...,Valuen>& x,
    const composite_key_result<CompositeKey>& y)const;
};

Pred0, ... , Predncomposite_key_equal_to 存储的相等二元谓词的类型。这些谓词中的每一个都必须是 CopyConstructibleCopyAssignable。至少需要提供一个相等谓词。composite_key_equal_to 实例化的最大相等谓词数量是实现定义的。composite_key_equal_toCopyConstructibleCopyAssignable。如果每个 Predi 本身是 DefaultConstructible,它也是 DefaultConstructible

请注意,形式上并不要求 Predi 类型以任何确定的方式表现为相等谓词。然而,如果确实如此,composite_key_equal_to 的语义是明确的,如 composite_key_result 语义 部分所述。

符号约定

在下面的内容中,我们使用与 composite_key_result 相同的 表示法

composite_key_equal_to 成员

composite_key_equal_to(
  const Pred0& p0=Pred0(),
  ...
  const Predn& pn=Predn());
效果: 构建一个 composite_key_equal_to,它存储所提供的相等谓词的副本。
composite_key_equal_to(const key_eq_tuple& x);
效果: 构建一个 composite_key_equal_to,它存储 x 中所提供的相等谓词对象的副本。
const key_eq_tuple& key_eqs()const;
返回值: 对包含 composite_key_equal_to 内部存储的相等谓词对象的元组的常量引用。
key_eq_tuple& key_eqs();
返回值: 对包含 composite_key_equal_to 内部存储的相等谓词对象的元组的引用。
template<typename CompositeKey1,typename CompositeKey2>
bool operator()(
  const composite_key_result<CompositeKey1> & x,
  const composite_key_result<CompositeKey2> & y)const;
template<typename CompositeKey,typename Values...>
bool operator()(
  const composite_key_result<CompositeKey>& x,
  const std::tuple<Values...>& y)const;
template<typename CompositeKey,typename Values...>
bool operator()(
  const std::tuple<Values...>& x,
  const composite_key_result<CompositeKey>& y)const;
template<typename CompositeKey,typename Value0,...,typename Valuen>
bool operator()(
  const composite_key_result<CompositeKey>& x,
  const boost::tuple<Value0,...,Valuen>& y)const;
template<typename Value0,...,typename Valuen,typename CompositeKey>
bool operator()(
  const boost::tuple<Value0,...,Valuen>& x,
  const composite_key_result<CompositeKey>& y)const;
要求: length(x)==length(y)。表达式 key_eqs().get<i>()(xi,yi)key_eqs().get<i>()(yi,xi) 对所有 i[0,length(x)) 范围内有效。
返回值: 当且仅当
key_eqs().get<i>()(xi,yi) 对所有 i[0,length(x)) 范围内成立。
复杂度: 进行的键提取操作和比较操作不超过评估上述表达式所必需的操作,从 i==0 开始。一旦结果确定为 false,评估就会短路。

类模板 composite_key_result_equal_to

已弃用。请使用 std::equal_to<CompositeKeyResult>

std::equal_tocomposite_key 结果的特化

std::equal_to<CompositeKeyResult>,其中 CompositeKeyResultcomposite_key_result 的一个实例,表现得像 composite_key_equal_to 的一个特化,其中提供的所有比较谓词都是 std::equal_to 的实例。

namespace std{

template<typename CompositeKey>
struct equal_to<boost::multi_index::composite_key_result<CompositeKey> >
{
  typedef 
    boost::multi_index::composite_key_result<
      CompositeKey>                           first_argument_type;
  typedef first_argument_type                 second_argument_type;
  typedef bool                                result_type;

  template<typename CompositeKey1,typename CompositeKey2>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey1> & x,
    const boost::multi_index::composite_key_result<CompositeKey2> & y)const;
  
  template<typename CompositeKey,typename... Values>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const std::tuple<Values...>& y)const;

  template<typename CompositeKey,typename... Values>
  bool operator()(
    const std::tuple<Values...>& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value0,...,typename Valuen>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const boost::tuple<Value0,...,Valuen>& y)const;

  template<typename Value0,...,typename Valuen,typename CompositeKey>
  bool operator()(
    const boost::tuple<Value0,...,Valuen>& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;
};

} // namespace std

CompositeKeyResult 必须是 composite_key<KeyFromValue0,...,KeyFromValuen> 类型的某个实例。然后,std::equal_to<CompositeKeyResult>::operator() 等同于 composite_key_equal_to<Pred0,...,Predn>::operator(),其中

Predi = std::equal_to<KeyFromValuei::result_type> 对所有 i = 0,...,n

除了 composite_key_equal_toPredi 的要求外,每种类型都必须是 DefaultConstructiblestd::equal_to<CompositeKeyResult>DefaultConstructibleCopyConstructibleCopyAssignable

比较

类模板 composite_key_compare

composite_key_compare 使用内部存储的多个基本比较谓词集合来比较 composite_key_result 实例之间以及这些实例与值元组之间的关系。

template<typename Compare0,...,typename Comparen>
struct composite_key_compare
{
  typedef boost::tuple<Compare0,...,Comparen> key_comp_tuple;

  composite_key_compare(
    const Compare0& c0=Compare0(),
    ...
    const Comparen& cn=Comparen());

  composite_key_compare(const key_comp_tuple& x);

  const key_comp_tuple& key_comps()const;
  key_comp_tuple&       key_comps();

  template<typename CompositeKey1,typename CompositeKey2>
  bool operator()(
    const composite_key_result<CompositeKey1> & x,
    const composite_key_result<CompositeKey2> & y)const;
  
  template<typename CompositeKey,typename... Values>
  bool operator()(
    const composite_key_result<CompositeKey>& x,
    const std::tuple<Values...>& y)const;

  template<typename CompositeKey,typename... Values>
  bool operator()(
    const std::tuple<Values...>& x,
    const composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value0,...,typename Valuen>
  bool operator()(
    const composite_key_result<CompositeKey>& x,
    const boost::tuple<Value0,...,Valuen>& y)const;

  template<typename Value0,...,typename Valuen,typename CompositeKey>
  bool operator()(
    const boost::tuple<Value0,...,Valuen>& x,
    const composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value>
  bool operator()(
    const composite_key_result<CompositeKey>& x,const Value& y)const;

  template<typename Value,typename CompositeKey>
  bool operator()(
    const Value& x,const composite_key_result<CompositeKey>& y)const;
};

Compare0, ... , Comparencomposite_key_compare 存储的比较二元谓词的类型。这些谓词中的每一个都必须是 CopyConstructibleCopyAssignable。至少需要提供一个比较谓词。composite_key_compare 实例化的最大比较谓词数量是实现定义的。composite_key_compareCopyConstructibleCopyAssignable。如果每个 Comparei 本身是 DefaultConstructible,它也是 DefaultConstructible

请注意,形式上并不要求 Comparei 类型以任何确定的方式表现为比较谓词。然而,如果确实如此,composite_key_compare 的语义是明确的,如 composite_key_result 语义 部分所述。

符号约定

在下面的内容中,我们使用与 composite_key_result 相同的 表示法

composite_key_compare 成员

composite_key_compare(
  const Compare0& c0=Compare0(),
  ...
  const Comparen& cn=Comparen());
效果: 构建一个 composite_key_compare,它存储所提供的比较谓词的副本。
composite_key_compare(const key_comp_tuple& x);
效果: 构建一个 composite_key_compare,它存储 x 中所提供的比较谓词对象的副本。
const key_comp_tuple& key_comps()const;
返回值: 对包含 composite_key_compare 内部存储的比较谓词对象的元组的常量引用。
key_comp_tuple& key_comps();
返回值: 对包含 composite_key_compare 内部存储的比较谓词对象的元组的引用。
template<typename CompositeKey1,typename CompositeKey2>
bool operator()(
  const composite_key_result<CompositeKey1> & x,
  const composite_key_result<CompositeKey2> & y)const;
template<typename CompositeKey,typename... Values>
bool operator()(
  const composite_key_result<CompositeKey>& x,
  const std::tuple<Values...>& y)const;
template<typename CompositeKey,typename... Values>
bool operator()(
  const std::tuple<Values...>& x,
  const composite_key_result<CompositeKey>& y)const;
template<typename CompositeKey,typename Value0,...,typename Valuen>
bool operator()(
  const composite_key_result<CompositeKey>& x,
  const boost::tuple<Value0,...,Valuen>& y)const;
template<typename Value0,...,typename Valuen,typename CompositeKey>
bool operator()(
  const boost::tuple<Value0,...,Valuen>& x,
  const composite_key_result<CompositeKey>& y)const;
要求: 表达式 key_comps().get<i>()(xi,yi)key_comps().get<i>()(yi,xi) 对所有 i[0,min(length(x),length(y))) 范围内有效。
返回值: 当且仅当存在某个 j 在范围 [0,min(length(x),length(y))) 内,使得
!key_comps().get<i>()(xi,yi) && !key_comps().get<i>()(yi,xi) 对所有 i[0,j) 范围内成立,
 key_comps().get<j>()(xj,yj).
复杂度: 进行的键提取操作和比较操作不超过评估上述表达式所必需的操作,从 i==0 开始。一旦结果确定为 false,评估就会短路。
template<typename CompositeKey,typename Value>
bool operator()(
  const composite_key_result<CompositeKey>& x,const Value& y)const;
效果
return operator()(x,boost::make_tuple(boost::cref(y)));

template<typename Value,typename CompositeKey>
bool operator()(
  const Value& x,const composite_key_result<CompositeKey>& y)const;
效果
return operator()(boost::make_tuple(boost::cref(x)),y);

类模板 composite_key_result_less

已弃用。请使用 std::less<CompositeKeyResult>

类模板 composite_key_result_greater

已弃用。请使用 std::greater<CompositeKeyResult>

std::lesscomposite_key 结果的特化

std::less<CompositeKeyResult>,其中 CompositeKeyResultcomposite_key_result 的一个实例,表现得像 composite_key_compare 的一个特化,其中提供的所有比较谓词都是 std::less 的实例。

namespace std{

template<typename CompositeKey>
struct less<boost::multi_index::composite_key_result<CompositeKey> >
{
  typedef
    boost::multi_index::composite_key_result<
      CompositeKey>                           first_argument_type;
  typedef first_argument_type                 second_argument_type;
  typedef bool                                result_type;

  template<typename CompositeKey1,typename CompositeKey2>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey1> & x,
    const boost::multi_index::composite_key_result<CompositeKey2> & y)const;
  
  template<typename CompositeKey,typename... Values>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const std::tuple<Values...>& y)const;

  template<typename CompositeKey,typename... Values>
  bool operator()(
    const std::tuple<Values...>& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value0,...,typename Valuen>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const boost::tuple<Value0,...,Valuen>& y)const;

  template<typename Value0,...,typename Valuen,typename CompositeKey>
  bool operator()(
    const boost::tuple<Value0,...,Valuen>& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const Value& y)const;

  template<typename Value,typename CompositeKey>
  bool operator()(
    const Value& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;
};

} // namespace std

CompositeKeyResult 必须是 composite_key<KeyFromValue0,...,KeyFromValuen> 类型的某个实例。然后,std::less<CompositeKeyResult>::operator() 等同于 composite_key_compare<Compare0,...,Comparen>::operator(),其中

Comparei = std::less<KeyFromValuei::result_type> 对所有 i = 0,...,n

除了 composite_key_compareComparei 的要求外,每种类型都必须是 DefaultConstructiblestd::less<CompositeKeyResult>DefaultConstructibleCopyConstructibleCopyAssignable

std::greatercomposite_key 结果的特化

std::greater<CompositeKeyResult>,其中 CompositeKeyResultcomposite_key_result 的一个实例,表现得像 composite_key_compare 的一个特化,其中提供的所有比较谓词都是 std::greater 的实例。

namespace std{

template<typename CompositeKey>
struct greater<boost::multi_index::composite_key_result<CompositeKey> >
{
  typedef
    boost::multi_index::composite_key_result<
      CompositeKey>                           first_argument_type;
  typedef first_argument_type                 second_argument_type;
  typedef bool                                result_type;

  template<typename CompositeKey1,typename CompositeKey2>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey1> & x,
    const boost::multi_index::composite_key_result<CompositeKey2> & y)const;
  
  template<typename CompositeKey,typename... Values>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const std::tuple<Values...>& y)const;

  template<typename CompositeKey,typename... Values>
  bool operator()(
    const std::tuple<Values...>& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value0,...,typename Valuen>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const boost::tuple<Value0,...,Valuen>& y)const;

  template<typename Value0,...,typename Valuen,typename CompositeKey>
  bool operator()(
    const boost::tuple<Value0,...,Valuen>& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;

  template<typename CompositeKey,typename Value>
  bool operator()(
    const boost::multi_index::composite_key_result<CompositeKey>& x,
    const Value& y)const;

  template<typename Value,typename CompositeKey>
  bool operator()(
    const Value& x,
    const boost::multi_index::composite_key_result<CompositeKey>& y)const;
};

} // namespace std

CompositeKeyResult 必须是 composite_key<KeyFromValue0,...,KeyFromValuen> 类型的某个实例。然后,std::greater<CompositeKeyResult>::operator() 等同于 composite_key_compare<Compare0,...,Comparen>::operator(),其中

Comparei = std::greater<KeyFromValuei::result_type> 对所有 i = 0,...,n

除了 composite_key_compareComparei 的要求外,每种类型都必须是 DefaultConstructiblestd::greater<CompositeKeyResult>DefaultConstructibleCopyConstructibleCopyAssignable

哈希

类模板 composite_key_hash

composite_key_hash 基于一组基本哈希函数对象生成 composite_key_result 实例的哈希值。

template<typename Hash0,...,typename Hashn>
struct composite_key_hash
{
  typedef boost::tuple<Hash0,...,Hashn> key_hasher_tuple;

  composite_key_hash(
    const Hash0& h0=Hash0(),
    ...
    const Hashn& hn=Hashn());

  composite_key_hash(const key_hasher_tuple& x);

  const key_hasher_tuple& key_hash_functions()const;
  key_hasher_tuple&       key_hash_functions();

  template<typename CompositeKey>
  std::size_t operator()(
    const composite_key_result<CompositeKey>& x)const;
  
  template<typename... Values>
  std::size_t operator()(
    const std::tuple<Values...>& x)const;

  template<typename Value0,...,typename Valuen>
  std::size_t operator()(
    const boost::tuple<Value0,...,Valuen>& x)const;
};

Hash0, ... , Hashncomposite_key_hash 存储的哈希一元函数对象的类型。这些对象中的每一个都必须是 CopyConstructibleCopyAssignable,并且返回一个类型为 std::size_t 且范围在 [0, std::numeric_limits<std::size_t>::max())) 内的值。至少需要提供一个哈希函数。composite_key_hash 实例化的最大哈希函数数量是实现定义的。composite_key_hashCopyConstructibleCopyAssignable。如果每个 Hashi 本身是 DefaultConstructible,它也是 DefaultConstructible

符号约定

在下面的内容中,我们使用与 composite_key_result 相同的 表示法

composite_key_hash 成员

composite_key_hash(
  const Hash0& h0=Hash0(),
  ...
  const Hashn& hn=Hashn());
效果: 构建一个 composite_key_hash,它存储所提供的哈希函数对象的副本。
composite_key_hash(const key_hasher_tuple& x);
效果: 构建一个 composite_key_hash,它存储 x 中所提供的哈希函数对象的副本。
const key_hasher_tuple& key_hash_functions()const;
返回值: 对包含 composite_key_hash 内部存储的哈希函数对象的元组的常量引用。
key_hasher_tuple& key_hash_functions();
返回值: 对包含 composite_key_hash 内部存储的哈希函数对象的元组的引用。
template<typename CompositeKey>
bool operator()(
  const composite_key_result<CompositeKey>& x)const;
template<typename... Values>
bool operator()(
  const std::tuple<Values...>& x)const;
template<typename Value0,...,typename Valuen>
bool operator()(
  const boost::tuple<Value0,...,Valuen>& x)const;
要求: length(x)==length(key_hash_functions())。表达式 key_hash_functions().get<i>()(xi) 对所有 i[0,length(x)) 范围内有效。
返回值: 一个范围在 [0, std::numeric_limits<std::size_t>::max()) 内的值,该值仅取决于数值元组
(key_hash_functions().get<0>()(x0), ... , key_hash_functions().get<N>()(xN)),其中 N=length(x)-1

类模板 composite_key_result_hash

已弃用。请使用 boost::hash<CompositeKeyResult>

boost::hashcomposite_key 结果的特化

boost::hash<CompositeKeyResult>,其中 CompositeKeyResultcomposite_key_result 的一个实例,表现得像 composite_key_hash 的一个特化,其中提供的所有哈希函数都是 boost::hash 的实例。

namespace boost{

template<typename CompositeKey>
struct hash<multi_index::composite_key_result<CompositeKey> >
{
  typedef multi_index::composite_key_result<CompositeKey> argument_type;
  typedef std::size_t                                     result_type;

  template<typename CompositeKey>
  std::size_t operator()(
    const multi_index::composite_key_result<CompositeKey>& x)const;
  
  template<typename... Values>
  std::size_t operator()(
    const std::tuple<Values...>& x)const;

  template<typename Value0,...,typename Valuen>
  std::size_t operator()(
    const boost::tuple<Value0,...,Valuen>& x)const;
};

} // namespace boost

CompositeKeyResult 必须是 composite_key<KeyFromValue0,...,KeyFromValuen> 类型的某个实例。然后,boost::hash<CompositeKeyResult>::operator() 等同于 composite_key_hash<Hash0,...,Hashn>::operator(),其中

Hashi = boost::hash<KeyFromValuei::result_type> 对所有 i = 0,...,n

除了 composite_key_hashHashi 的要求外,每种类型都必须是 DefaultConstructibleboost::hash<CompositeKeyResult>DefaultConstructibleCopyConstructibleCopyAssignable

composite_key_result 的语义

composite_key_result 的相等、比较和哈希操作的设计基于以下原理:composite_key_result 被视为一个“虚拟”元组,它的每个元素都是相应基本键提取器的结果。因此,任何给定的操作都归结为相应元素操作的组合。这种映射保留了基本操作的基本属性;例如,如果基本谓词本身产生等价关系,则定义了一个真正的等价关系。我们可以形式地陈述这些事实如下。

考虑一个 composite_key_equal_to 实例,其类型为 Pred0, ... , Predn,使得每个 Predi 在某个类型 Ti 上产生一个等价关系,并且 CompositeKey 是一个类型 composite_key<Value,KeyFromValue0,...,KeyFromValuej> 的形式,其中 j <= n,使得

KeyFromValuei::result_type = Ti,对所有 i = 0,...,j
那么,composite_key_equal_to 在类型为 composite_key_result<CompositeKey> 的对象上产生一个等价关系;如果它们的所有基本键提取器值也等价,则这两个对象是等价的。此外,给定一个 composite_key_hash<Hash0,...,Hashj> 实例,以下类型是 (composite_key_hash, composite_key_equal_to) 关于 composite_key_result<CompositeKey>Compatible Keys
tuple<Q0,...,Qj>,
composite_key_result<composite_key<K0,...,Kj> >,其中 Ki::result_type = Qi 对所有 i = 0,...,j
前提是每个 Qi 要么是 Ti,要么是 (Hashi, Predi) 的 Compatible Key

至于比较,考虑一个 composite_key_compare 实例,其类型为 Compare0, ... , Comparen,使得每个 Comparei 在类型 Ti 上产生一个严格弱序。然后,对于上面定义的 CompositeKey 类型,composite_key_compare 在类型为 composite_key_result<CompositeKey> 的对象上产生一个严格弱序,并且诱导的顺序是字典序。此外,以下类型是 composite_key_compare 关于 composite_key_result<CompositeKey>Compatible Keys

tuple<Q0,...,Qk>, k <= n
composite_key_result<composite_key<K0,...,Kk> >, 其中 Ki::result_type = Qi 对所有 i = 0,...,k
前提是在这种情况下,比较仅在前 1+min(j,k) 个元素上进行字典序比较。

composite_key_result 的相等和比较运算符也具有类似的属性。但请注意,相等性仅对相同长度的对象定义,而比较则考虑操作数的最小长度。因此,x==y 诱导的等价类是与 !(x<y)&&!(y<x) 关联的等价类的子集。

头文件 "boost/multi_index/key.hpp" 声明

#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <boost/multi_index/composite_key.hpp>

#if implementation defined /* auto non-type template parameters supported */

#define BOOST_MULTI_INDEX_KEY_SUPPORTED

namespace boost{

namespace multi_index{

template<auto... Keys>
using key=implementation defined;

} /* namespace multi_index */

} /* namespace boost */

#endif

别名模板 key

在支持 C++17 的环境中,key 为 Boost.MultiIndex 预定义键提取器的规范提供了非常简洁的语法。传递给 key 的模板参数数量必须大于零且不超过 composite_key 所接受的最大键提取器数量。key<Key> 解析为

否则会导致未定义行为。当传递两个或更多参数时,key<Key0,...,Keyn> 解析为 composite_key<Value,KeyFromValue0,...,KeyFromValuen>,其中其中 Valuei 对应于 KeyFromValuei 的相关 ClassValue 类型,而 TQTQ 之间的最不通用类型,定义为:如果 std::is_convertible_v<const T&,const Q&>,则为 T;如果 std::is_convertible_v<const Q&,const T&>,则为 Q;否则是未定义的。


最后修订于 2020 年 4 月 19 日

© Copyright 2003-2020 Joaquín M López Muñoz. Distributed under the Boost Software License, Version 1.0. (请参阅随附文件 LICENSE_1_0.txt 或复制自 https://boost.ac.cn/LICENSE_1_0.txt)