Deleting a member at particular index in a repeated member

1,519 views
Skip to first unread message

Suraj

unread,
Oct 17, 2011, 2:51:50 AM10/17/11
to Protocol Buffers
Can we delete an entry at a particular index in a repeated member in a
protobuff object?

Igor Gatis

unread,
Oct 17, 2011, 8:10:39 PM10/17/11
to Suraj, Protocol Buffers
foo.mutable_repeated_field()->SwapElements(index_to_remove, foo.repeated_field_size() - 1);
foo.mutable_repeated_field()->RemoveLast();

On Mon, Oct 17, 2011 at 04:51, Suraj <suraj...@gmail.com> wrote:
Can we delete an entry at a particular index in a repeated member in a
protobuff object?

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To post to this group, send email to prot...@googlegroups.com.
To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.


Eyal Farago

unread,
Oct 18, 2011, 2:56:46 AM10/18/11
to Protocol Buffers
this is not doing exactly what Suraj asked for, as it modifies the
order of elements in the repeated field.
assuming order of the elements does matter, you have two options:
1. use the SwapElements() idiom in a loop until the element to be
removed is the last one and than call RemoveLast().
2. get the direct buffer using the mutable_data() method, use
memmove() or something similar, to pull the following elements into
place, place the element to be removed at the end and then call
RemoveLast()

unless you're dealing with extremely large arrays, method #1 should
suffice.

eyal.

Nitish Joshi

unread,
Mar 30, 2015, 1:12:26 PM3/30/15
to prot...@googlegroups.com, suraj...@gmail.com
Hi,
I am facing similar problem to delete particular index in repeated field. 
I saw the code What exactly foo here means?
Reply all
Reply to author
Forward
0 new messages