Discussion:
C++ - Most efficient way to remove an element from a Message's repeated array
jaypipes
2009-10-05 17:33:07 UTC
Permalink
Hi all,

I'm wondering what is the most efficient method to remove an element
from a Message's vector of repeated elements? I looked at the
RepeatedFieldPtr API but would appreciate some example code on how to
do this in the most efficient way. I gather from the API that the
underlying storage is a vector and so does not support efficient index-
based, random-access deletion from the vector. Is this correct?

Help is much appreciated,

Jay
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to protobuf+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---
Kenton Varda
2009-10-05 19:56:17 UTC
Permalink
Post by jaypipes
I'm wondering what is the most efficient method to remove an element
from a Message's vector of repeated elements? I looked at the
RepeatedFieldPtr API but would appreciate some example code on how to
do this in the most efficient way. I gather from the API that the
underlying storage is a vector and so does not support efficient index-
based, random-access deletion from the vector. Is this correct?
Yes, that is correct.

The most efficient solution is to permute the contents so that the things
you want to remove are at the end, then use RemoveLast().

Note that the underlying storage is an array of *pointers*, so re-arranging
pointers doesn't require copying whole message objects.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to protobuf+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---
jaypipes
2009-10-05 20:14:28 UTC
Permalink
Gotcha. Thanks for the assistance, Kenton!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to ***@googlegroups.com
To unsubscribe from this group, send email to protobuf+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---

Continue reading on narkive:
Loading...