Implementation:Alibaba MNN Protobuf Repeated Field H
Metadata
| Attribute | Value |
|---|---|
| Implementation File | 3rd_party/protobuf/src/google/protobuf/repeated_field.h
|
| Line Count | 1057 |
| Domains | Serialization, Data_Structures |
| Key Class | RepeatedField<T>
|
| Created | 2026-02-10 |
| Knowledge Sources | Repo, Doc |
Overview
Repeated field container analogous to std::vector for protobuf scalar types. Provides a contiguous, arena-compatible array that stores repeated scalar fields (int32, float, double, bool, enum, etc.).
Usage note: Vendored dependency used internally by MNN for parsing protobuf-based model formats. Not directly imported by end users.
Description
RepeatedField<T> is the container used by generated protobuf code for all repeated scalar fields. It provides:
- STL-like interface --
begin(),end(),size(),empty(),operator[], iterators. - Growth semantics -- Exponential capacity growth similar to
std::vector, with optional arena allocation. - Arena support -- When the owning message is allocated on a protobuf Arena, the
RepeatedFieldstorage is also arena-allocated, avoiding individual heap allocations. - Serialization integration --
data()provides direct pointer access for efficient bulk serialization of packed fields. - Merge and swap --
MergeFromappends elements from anotherRepeatedField;Swapexchanges contents in O(1).
The class is specialized for POD types and does not handle strings or messages (those use RepeatedPtrField).
Usage
#include "google/protobuf/repeated_field.h"
Included by all generated headers that contain repeated scalar fields.
Code Reference
template <typename Element>
class RepeatedField {
public:
RepeatedField();
explicit RepeatedField(Arena* arena);
~RepeatedField();
// Element access.
const Element& Get(int index) const;
Element* Mutable(int index);
void Set(int index, const Element& value);
void Add(const Element& value);
Element* Add();
// Size and capacity.
int size() const;
bool empty() const;
void Reserve(int new_size);
void Resize(int new_size, const Element& value);
void Clear();
// Direct data access for bulk serialization.
const Element* data() const;
Element* mutable_data();
// STL iterator support.
typedef Element* iterator;
typedef const Element* const_iterator;
iterator begin();
iterator end();
// Merge and swap.
void MergeFrom(const RepeatedField& other);
void Swap(RepeatedField* other);
private:
Arena* arena_;
int current_size_;
int total_size_; // capacity
Element* elements_;
};
I/O Contract
| Direction | Type | Description |
|---|---|---|
| Input | Scalar elements | Individual values added via Add() or bulk via MergeFrom()
|
| Output | Contiguous array | Direct pointer access via data() for serialization
|
| Memory | Arena or heap | Allocation strategy depends on owning message's arena status |
Usage Examples
// Working with a repeated int32 field
RepeatedField<int32_t> field;
field.Add(10);
field.Add(20);
field.Add(30);
for (int i = 0; i < field.size(); ++i) {
std::cout << field.Get(i) << std::endl;
}
// Range-based for (STL iterator support)
for (int32_t val : field) {
std::cout << val << std::endl;
}