Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete arbitrary objects in repeated field? (protobuf)

Tags:

I have some entries in the repeated field in my proto. Now I want delete some of them. How can I accomplish this? There is a function to delete the last element, but I want to delete arbitrary elements. I cant just swap them because the order is important.

I could swap with next until end, but isn't there a nicer solution?

like image 484
ManuelSchneid3r Avatar asked Nov 28 '12 11:11

ManuelSchneid3r


2 Answers

For Protobuf v3

iterator RepeatedField::erase(const_iterator position) can delete at arbitrary position.

For Protobuf v2

You can use the DeleteSubrange(int start, int num) in RepeatedPtrField class.

If you want to delete a single element then you have to call this method as DeleteSubrange(index_to_be_del, 1). It will remove the element at that index.

like image 72
jblixr Avatar answered Oct 26 '22 15:10

jblixr


According to the API docs, there isn't a way to arbitrarily remove an element from within a repeated field, just a way to remove the last one.

...
We don't provide a way to remove any element other than the last because it invites inefficient use, such as O(n^2) filtering loops that should have been O(n). If you want to remove an element other than the last, the best way to do it is to re-arrange the elements so that the one you want removed is at the end, then call RemoveLast()
...

like image 27
g19fanatic Avatar answered Oct 26 '22 13:10

g19fanatic