提升multi_index_container composite_key_compare

时间:2013-07-13 01:25:34

标签: c++ boost containers multi-index boost-multi-index

我正在尝试编写一个计算密集型程序。我需要char *作为multi_index_container的composite_key_compare的comparsion字段。但是,它似乎不起作用。代码如下:

struct MyStruct
{
    char* firstName;
    char* secondName;
    int age;
};

struct equal_char
{   // functor for operator<=
    inline bool operator()(const char* left, const char* right) const
    {   // apply operator<= to operands
        bool result=(strcmp(left,right)==0);
        return result;
    }
};

typedef composite_key
    <MyStruct*,
    BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, firstName),
    BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, secondName)
    > comp_key;
typedef multi_index_container
    <
    MyStruct*, 
    indexed_by
        <
        ordered_unique
            <
                comp_key,
                composite_key_compare
                <equal_char, equal_char> 
            >
        >
    > MyContainer;

boost::ptr_vector<MyStruct> vec;
MyStruct* struct1=new MyStruct();
struct1->firstName="Michael";
struct1->secondName="Mike";
struct1->age=20;
vec.push_back(struct1);



MyContainer myContainer;
myContainer.insert(struct1);
char* first="Michael";
char* second="Mike";
auto it=myContainer.find(boost::make_tuple(first, second));
if(it!=myContainer.end())
    cout << (*it)->age << endl;

我确实追踪到了equal_char,发现它确实在“Michael”与“Michael”的第一次比较中返回true,但我也发现equal_char没有被称为“Mike”的第二次比较“麦克风”。谁可以帮我这个?我该如何编写composite_key_compare?

1 个答案:

答案 0 :(得分:0)

我跟踪了程序,发现对于“ordered unique”的比较操作,boost需要类似std :: less()的东西。

所以,我在下面的代码中添加了它,并且它有效。

struct CompareLess
{   // functor for operator<=
#pragma region For 1 variable
    static inline int compare(const char* left, const char* right)
    {
        return strcmp(left, right);
    }
    inline bool operator()(const char* left, const char* right) const
    {   // apply operator<= to operands
        return compare(left, right)<0;
    }

    static inline int compare(const boost::tuple<char*>& x, const char*y)
    {
        return compare(x.get<0>(),y);
    }
    inline bool operator()(const boost::tuple<char*>& x, const char*y) const
    {
        return compare(x,y)<0;
    }

    static inline int compare(const      boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*>& y)
    {
        return -compare(y,(const char*)(k.value->firstName));
    } 
    inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*>& y) const
    {
        return compare(k,y)<0;
    }

    static inline int compare(const boost::tuple<char*>& y, const boost::multi_index::composite_key_result<comp_key>& k)
    {
        return compare(y,(const char*)(k.value->firstName));
    }
    inline bool operator()(const boost::tuple<char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) const
    {
        return compare(y,k) <0;
    }

#pragma endregion For 1 variable
#pragma region For 2 variables
    static inline int compare(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*, char*>& y)
    {
        int val=strcmp(k.value->firstName, y.get<0>());
        if(val!=0)
            return val;
        else
            return compare(y.get<1>(),(const char*)(k.value->secondName));
    }
    inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*, char*>& y) const
    {
        return compare(k,y) <0;
    }

    static inline int compare(const boost::tuple<char*, char*>& y, const boost::multi_index::composite_key_result<comp_key>& k)
    {
        return -compare(k,y);
    }
    inline bool operator()(const boost::tuple<char*, char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) const
    {
        return compare(y,k)<0;
    }
#pragma endregion For 2 variables
#pragma region For all variables
    inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k1, 
        const boost::multi_index::composite_key_result<comp_key>& k2) const
    {
        return CompareLess()((const char*)(k1.value->firstName), (const char*)(k2.value->firstName));
    }
#pragma endregion For all variables



};

typedef multi_index_container
    <
    MyStruct*, 
    indexed_by
        <
        ordered_unique
            <
                comp_key,
                CompareLess
            >
        >
    > MyContainer;

但是我得到了更多的问题,这是一个非常冗长的代码,并且假设如果我有更多的字段,它将会更长。有没有聪明的方法来做到这一点?