Boost logo

Boost Users :

From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2006-09-20 08:30:19


Hello Lukasz,

Lukasz Dobrek ha escrito:

> Hi,
> I know that there are for sure important design decisions explaining why
> all std containers as well as multiindex take a comparison operator,
> as template parameter. This behavior is satisfactory as long, as comparison
> is sort of context independent. The best example say ints, but there exist
> cases when comparison of two object requires some priory knowledge
> about context this objects exist in.
> My favorite example is tenor ( time period ).

tenor?

> It is impossible to say whether 1 month is less then 30 days unless you
> know when you start counting. So in order to compare such objects I need
> to specify some anchor date. Unfortunately I cannot have a std::set of such
> objects, nor a multiindex indexed with this objects.
>
> Note: I am able to sort a vector of such objects using std::sort.

How do you do that? An example with vector can help me better understand
your problem.

> First question: Is there a way to use such objects with multiindex, if not why?

Well, I'm not sure if I'm getting your question, but seems to me STL ordered
containers and Boost.MultiIndex allows for context-dependent comparison
operators: you just have to provide the contextual info as part of the
comparison object internal state:

class object_context_compare
{
public:
  object_context_compare(const context_info& info):info(info){}

  bool operator()(const object& x,const object& y)const
  {
    // compare x and y using info as the comparison context
  }

private:
  context_info info;
};

typedef std::set<object,object_context_compare> object_set;

...

// construct an object_set with the appropriate contextual information
// for comparison purposes
object_set os(context_info(...));

The same tecnhique can be used with Boost.MultiIndex, although here
the passing of comparison objects and other auxiliary objects at
construction time is more cumbersome due to the presence of multiple
indices. See

http://boost-consulting.com/boost/libs/multi_index/doc/tutorial/creation.html#ctor_args_list

for a complete explanation of how to do it.

> Second question: I cannot be the only bloke disturbed by this issue,
> how are you dealing with it?

Hopefully answered above. Is this what you were after?

> Regards,
> Lukasz Dobrek

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net