Could we please have a ‘replacesorted’ method in ReferenceCountedArray (or a better place). The idea is that when we’re sorting, the comparator is a better judge of whether something is the same than pointer comparison.
It’s sort of addReplacingIfAlreadyAsThereDefinedByComparator(), if you get my gist.
I’m trying this:
[code] template
void replaceSorted (ElementComparator& comparator,
ObjectClass* newObject) throw()
{
lock.enter();
int index = findInsertIndexInSortedArray (comparator, this->elements, newObject, 0, numUsed);
// Check the previous item, and if it’s a match, delete it first
if (index > 0 && index - 1 < numUsed && comparator.compareElements (newObject, this->elements [index - 1]) == 0)
{
–index;
remove (index);
}
if (index > 0 && comparator.compareElements (newObject, this->elements [index - 1]) == 0)
set (index - 1, newObject); // replace an existing object that matches
else
insert (index, newObject); // no match, so insert the new one
lock.exit();
}