正如在this question中回答的那样
请注意,Cap结构中的单个列表值的限制为2^29-1元素。
由于对单个列表的限制,我试图将包含超过此数量的项目的一个列表拆分为一个列表映射。为此,我使用以下模式:
struct Map64UInt{
entries @0: List(Entry);
struct Entry{
key @0: UInt64;
value @1: List(UInt64);
}
}我一直在研究Proto的所有例子,但是我找不到一个包含关于如何创建和将itens添加到Capn'Proto列表中的示例,然后将这个列表添加到Capn Proto Map中。作为一个例子,请考虑以下代码:
void addVecToCapnprotoMap(std::vector<uint64_t> &v){
unsigned int key = 0;
//Here: how do I create a Capn' Proto List of uint64_t
//and add it to a Capn Proto map that follows the schema
//described above?
}发布于 2019-08-18 00:12:11
不幸的是,您不能动态地向列表中添加新元素;您必须在第一次创建列表时指定列表的完整大小。这是普罗托船长的零拷贝性质的副作用。由于列表是直接分配到最终消息缓冲区中的,因此不能在以后调整大小。
相反,您可以做的是分别维护一张地图,然后作为最后一步编写最终列表,如下所示:
// WARNING: Not tested, probably has typos.
class MapHelper {
public:
MapHelper(Map64UInt::Builder finalBuilder)
: finalBuilder(finalBuilder),
orphanage(capnp::Orphanage::getForMessageContaining(finalBuilder)) {}
void add(uint64_t key, const std::vector<unit64_t>& v) {
auto orphan = orphanage.newOrphan<capnp::List<uint64_t>>(v.size());
auto listBuilder = orphan.get();
for (size_t i = 0; i < v.size(); i++) {
listBuilder[i] = v[i];
}
contents.insert(std::make_pair(key, kj::mv(orphan)));
}
void finish() {
// Write all entries to the final map.
auto entriesBuilder = finalBuilder.initEntries(contents.size());
size_t i = 0;
for (auto& entry: contents) {
auto entryBuilder = entriesBuilder[i];
entryBuilder.setKey(entry.first);
entryBuilder.adoptValue(kj::mv(entry.second));
++i;
}
}
private:
Map64UInt::Builder finalBuilder;
capnp::Orphanage orphanage;
std::map<uint64_t, capnp::Orphan<capnp::List<uint64_t>>> contents;
};https://stackoverflow.com/questions/57516568
复制相似问题