未验证 提交 0edd6cf1 编写于 作者: 石晓伟 提交者: GitHub

update the vector_view, test=develop (#3869)

上级 c6b784d2
......@@ -18,6 +18,7 @@
#include <type_traits>
#include <vector>
#include "flatbuffers/flatbuffers.h"
#include "lite/utils/cp_logging.h"
namespace paddle {
namespace lite {
......@@ -32,7 +33,7 @@ struct ElementTraits {
};
template <typename T>
struct ElementTraits<T,
struct ElementTraits<T*,
typename std::enable_if<std::is_class<T>::value>::type> {
typedef flatbuffers::Offset<T> element_type;
};
......@@ -76,6 +77,15 @@ class VectorView {
typename Traits::const_iterator begin() const { return cvec_->begin(); }
typename Traits::const_iterator end() const { return cvec_->end(); }
size_t size() const { return cvec_->size(); }
operator std::vector<T>() {
VLOG(10) << "Copying elements out of VectorView will damage performance.";
std::vector<T> tmp;
tmp.reserve(cvec_->size());
for (auto val : *cvec_) {
tmp.push_back(val);
}
return tmp;
}
~VectorView() = default;
private:
......@@ -90,22 +100,22 @@ struct FBSStrIterator {
VI;
explicit FBSStrIterator(const VI& iter) { iter_ = iter; }
const VI& RawIter() const { return iter_; }
const VI& raw_iter() const { return iter_; }
bool operator==(const FBSStrIterator& other) const {
return iter_ == other.RawIter();
return iter_ == other.raw_iter();
}
bool operator<(const FBSStrIterator& other) const {
return iter_ < other.RawIter();
return iter_ < other.raw_iter();
}
bool operator!=(const FBSStrIterator& other) const {
return iter_ != other.RawIter();
return iter_ != other.raw_iter();
}
ptrdiff_t operator-(const FBSStrIterator& other) const {
return iter_ - other.RawIter();
return iter_ - other.raw_iter();
}
std::string operator*() const { return iter_.operator*()->str(); }
......@@ -144,6 +154,15 @@ class VectorView<std::string, Flatbuffers> {
FBSStrIterator begin() const { return FBSStrIterator(cvec_->begin()); }
FBSStrIterator end() const { return FBSStrIterator(cvec_->end()); }
size_t size() const { return cvec_->size(); }
operator std::vector<std::string>() {
VLOG(10) << "Copying elements out of VectorView will damage performance.";
std::vector<std::string> tmp;
tmp.reserve(cvec_->size());
for (auto val : *cvec_) {
tmp.push_back(val->str());
}
return tmp;
}
~VectorView() = default;
private:
......
......@@ -85,15 +85,15 @@ TEST(VectorView, Flatbuffers) {
auto program = fbs::proto::GetProgramDesc(fbb.GetBufferPointer());
// BlockDesc View
VectorView<proto::BlockDesc> block_view(program->blocks());
VectorView<proto::BlockDesc*> block_view(program->blocks());
EXPECT_EQ(block_view.size(), static_cast<size_t>(2));
EXPECT_EQ(block_view[0]->idx(), 0);
EXPECT_EQ(block_view[1]->idx(), 1);
// OpDesc & Attr View
VectorView<proto::OpDesc> op_view(block_view[0]->ops());
VectorView<proto::OpDesc*> op_view(block_view[0]->ops());
EXPECT_EQ(op_view[0]->type()->str(), std::string("hello!"));
VectorView<proto::OpDesc_::Attr> attr_view(op_view[0]->attrs());
VectorView<proto::OpDesc_::Attr*> attr_view(op_view[0]->attrs());
// int32_t View
VectorView<int32_t> ints_view(attr_view[0]->ints());
......@@ -106,6 +106,10 @@ TEST(VectorView, Flatbuffers) {
for (size_t i = 0; i < ints_view.size(); ++i) {
EXPECT_EQ(ints_view[i], ints[i]);
}
std::vector<int32_t> ints_2(ints_view);
for (size_t i = 0; i < ints_2.size(); ++i) {
EXPECT_EQ(ints_2[i], ints[i]);
}
// String View
VectorView<std::string> strings_view(attr_view[0]->strings());
......@@ -119,6 +123,10 @@ TEST(VectorView, Flatbuffers) {
for (size_t i = 0; i < strings_view.size(); ++i) {
EXPECT_EQ(strings_view[i], strings[i]);
}
std::vector<std::string> string_2(strings_view);
for (size_t i = 0; i < string_2.size(); ++i) {
EXPECT_EQ(string_2[i], strings[i]);
}
}
} // namespace lite
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册