Question

For below code:

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

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,
                CompareLess
            >
        >
    > MyContainer;

I can easily write a compareless, like below:

struct CompareLess
{   // functor for operator<
    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;
    }
}

But when I want to write something like below:

typedef composite_key
    <double,
    char*,
    char*
    > comp_key;

I have trouble writing the compareLess with the below function

    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));
    }

I don't know how to write some code as "k.value->firstName" to get the char* for comparsion, since the value is no longer a struct, it is only a double. So where can I get the field for comparison? is there something like k.get<0>()?

Was it helpful?

Solution

Composite keys' comparison criteria are specified via composite_key_compare. In your particular case you'd want something like

ordered_unique<
  comp_key,
  composite_key_compare<
     std::less<double>,
     CompareLess,
     Compareless
  >
>

where CompareLess needs only be callable with const char*s:

struct CompareLess
{   
    static inline int compare(const char* left, const char* right)
    {
        return strcmp(left, right);
    }
    inline bool operator()(const char* left, const char* right) const
    {
        return compare(left, right)<0;
    }
};

The rest of the scaffolding is provided by composite_key_compare.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top