[xsd-users] sorting of a sequence of elements when IDs are
contained, duplicate_id exception
Boris Kolpackov
boris at codesynthesis.com
Fri Feb 13 02:45:58 EST 2009
Hi Jan,
Jan Klimke <jan.klimke at hpi.uni-potsdam.de> writes:
> The problem is when i call
>
> std::sort(tour->getTourSection().begin(), tour->getTourSection().end(),
> compareTourSectionBySectionNumber);
>
> where tourSection is a sequence of gml:FeaturePropertyType, a
> duplicate_id exception is thrown. This probably happens because of the
> gml:id of the entities is copied when a position switch in the sequence
> is necessary.
Hm, that's an interesting problem. I think you are right about what
happens: std::sort() first inserts a new copy and then removes the
old one. So you end up with two objects with the same ID in the tree.
I can't think of anything better than these two solutions:
(1) Implement your own sort where you first remove the old element
from the sequence and then insert the new one.
(2) Copy the contents of the sequence into another, "free-standing"
sequence which is not part of any object model. You can then sort
that sequence and copy the result back. There is quite a bit of
copying with this approach:
TourSectionContainer tmp = tour->getTourSection();
std::sort(tmp.begin(), tmp.end(), compareTourSectionBySectionNumber);
tour->getTourSection() = tmp;
Note that using swap() here won't work.
Boris
More information about the xsd-users
mailing list