From 9747d33d08cd3ef069c08b37ff94a971707a5741 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Mon, 15 Aug 2016 11:13:25 -0400 Subject: [PATCH] Derive iterator from std::iterator to reap standarad algorithms. By deriving from std::iterator, iterator_traits will be properly set up for our custom iterator type, thus we can use algorithms from STL with our custom iterators. --- source/opt/iterator.h | 16 +++++++++++----- test/opt/test_ir_loader.cpp | 10 +++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/source/opt/iterator.h b/source/opt/iterator.h index e31fedf9..6ca3dbcc 100644 --- a/source/opt/iterator.h +++ b/source/opt/iterator.h @@ -27,6 +27,7 @@ #ifndef LIBSPIRV_OPT_ITERATOR_H_ #define LIBSPIRV_OPT_ITERATOR_H_ +#include #include #include #include @@ -39,12 +40,17 @@ namespace ir { // std::unique_ptr managed elements in the vector, behaving like we are using // std::vector<|ValueType|>. template -class UptrVectorIterator { +class UptrVectorIterator + : public std::iterator::type> { public: - using pointer = - typename std::conditional::type; - using reference = - typename std::conditional::type; + using super = std::iterator< + std::random_access_iterator_tag, + typename std::conditional::type>; + + using pointer = typename super::pointer; + using reference = typename super::reference; // Type aliases. We need to apply constness properly if |IsConst| is true. using Uptr = std::unique_ptr; diff --git a/test/opt/test_ir_loader.cpp b/test/opt/test_ir_loader.cpp index 7d4cca0e..8717140c 100644 --- a/test/opt/test_ir_loader.cpp +++ b/test/opt/test_ir_loader.cpp @@ -25,6 +25,7 @@ // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. #include +#include #include "opt/libspirv.hpp" @@ -212,11 +213,10 @@ TEST(IrBuilder, OpUndefOutsideFunction) { std::unique_ptr module = t.BuildModule(text); ASSERT_NE(nullptr, module); - int opundef_count = 0; - for (const auto& inst : module->types_values()) { - if (inst.opcode() == SpvOpUndef) ++opundef_count; - } - EXPECT_EQ(3, opundef_count); + const auto opundef_count = std::count_if( + module->types_values_begin(), module->types_values_end(), + [](const ir::Instruction& inst) { return inst.opcode() == SpvOpUndef; }); + EXPECT_EQ(3u, opundef_count); std::vector binary; module->ToBinary(&binary, /* skip_nop = */ false);