Goal is, I've multiple lists of elements available, and I want to be able to store all of these elements into a resultant list in an ordered way.
Some of the ideas that comes to my mind are a) Keep the result as a set (std::set), but the B-tree , needs to rebalanced every now and then. b) Store all the elements in a list and sort the list at the end.
But, I thought, why not store them in a sorted fashion, as and when we add the items to the resultant list.
Here is my function, that does the job of maintaining the results in a sorted way. Is there an efficient way to do the same?
void findItemToInsertAt(std::list<int>& dataSet, int itemToInsert, std::list<int>::iterator& location)
{
std::list<int>::iterator fromBegin = dataSet.begin();
std::list<int>::iterator fromEnd = dataSet.end() ;
// Have two pointers namely end and begin
if ( !dataSet.empty() )
--fromEnd;
// Set the location to the beginning, so that if the dataset is empty, it can return the appropriate value
location = fromBegin;
while ( fromBegin != dataSet.end() )
{
// If the left pointer points to lesser value, move to the next element
if ( *fromBegin < itemToInsert )
{
++fromBegin;
// If the end is greater than the item to be inserted then move to the previous element
if ( *fromEnd > itemToInsert )
{
--fromEnd;
}
else
{
// We move only if the element to be inserted is greater than the end, so that end points to the
// right location
if ( *fromEnd < itemToInsert )
{
location = ++fromEnd;
}
else
{
location = fromEnd;
}
break;
}
}
else
{
location = fromBegin;
break;
}
}
}
And, here is the caller of the function
void storeListToResults(const std::list<int>& dataset, std::list<int>& resultset)
{
std::list<int>::const_iterator curloc;
std::list<int>::iterator insertAt;
// For each item in the data set, find the location to be inserted into
// and insert the item.
for (curloc = dataset.begin(); curloc != dataset.end() ; ++curloc)
{
// Find the iterator to be inserted at
findItemToInsertAt(resultset,*curloc,insertAt);
// If we have reached the end, then the element to be inserted is at the end
if ( insertAt == resultset.end() )
{
resultset.push_back(*curloc);
}
else if ( *insertAt != *curloc ) // If the elements do not exist already, then insert it.
{
resultset.insert(insertAt,*curloc);
}
}
}