Robot Dreams
Entries tagged as algorithms
Thursday, February 23. 2006
Set, Query, Result
First off, a general update. It's been a slow start to the blog, sorry about that. But I'm still recovering from a total drive failure of the machine I do most of my development work from. So it's been a hard slog to reinstall everything, especially that initial Windows install with at least 15 reboots (I was counting them and that's when I gave up). On to the subject at hand...
One of the data structures in C++ I make use of most are the associative standard containers. I use them because of the bounded algorithmic guarantees they provide. But one of the most nagging problems with them is that it is hard to do any meaningful searching once one has arranged to keep the sorted elements. Sure it's easy to find equivalent elements with for example std::set::equal_range. But if how you are sorting is anything but the element itself as a value it's rather painful to come up with the usual convoluted special value elements to do the searching for you. For example having:
struct A
{
int type;
std::string label;
std::string description;
};
struct S
{
bool operator()(
boost::shared_ptr<A> const & a,
boost::shared_ptr<A> const & b ) const
{
return a->type < b->type;
}
};
std::multiset<boost::shared_ptr<A>,S> db;
And wanting to find out how many A's of type #2 there are. So even though the set is arrange in the most optimal way for getting that answer one is thwarted in using the property of the set only because std::multiset::count, and others, don't take anything but the an element.
Continue reading "Set, Query, Result"


