table: PIT iterator dereferences to const shared_ptr<pit::Entry>&

refs #3164

Change-Id: Id857e214a727917009f865c2cfac782077844799
diff --git a/daemon/table/pit-iterator.cpp b/daemon/table/pit-iterator.cpp
index 0cdb395..b3b9afa 100644
--- a/daemon/table/pit-iterator.cpp
+++ b/daemon/table/pit-iterator.cpp
@@ -36,27 +36,24 @@
 static_assert(std::is_default_constructible<Iterator>::value,
               "Iterator must be default-constructible");
 
-Iterator::Iterator()
-  : m_iPitEntry(0)
-{
-}
-
-Iterator::Iterator(const NameTree::const_iterator& ntIt)
+Iterator::Iterator(const NameTree::const_iterator& ntIt, size_t iPitEntry)
   : m_ntIt(ntIt)
-  , m_iPitEntry(0)
+  , m_iPitEntry(iPitEntry)
 {
 }
 
 Iterator&
 Iterator::operator++()
 {
-  ++m_iPitEntry;
-  if (m_iPitEntry < m_ntIt->getPitEntries().size()) {
-    return *this;
+  BOOST_ASSERT(m_ntIt != NameTree::const_iterator());
+  BOOST_ASSERT(m_iPitEntry < m_ntIt->getPitEntries().size());
+
+  if (++m_iPitEntry >= m_ntIt->getPitEntries().size()) {
+    ++m_ntIt;
+    m_iPitEntry = 0;
+    BOOST_ASSERT(m_ntIt == NameTree::const_iterator() || m_ntIt->hasPitEntries());
   }
 
-  ++m_ntIt;
-  m_iPitEntry = 0;
   return *this;
 }
 
diff --git a/daemon/table/pit-iterator.hpp b/daemon/table/pit-iterator.hpp
index 061e077..434dfd0 100644
--- a/daemon/table/pit-iterator.hpp
+++ b/daemon/table/pit-iterator.hpp
@@ -37,13 +37,12 @@
 class Iterator : public std::iterator<std::forward_iterator_tag, const Entry>
 {
 public:
-  Iterator();
-
   /** \brief constructor
    *  \param ntIt a name tree iterator that visits name tree entries with one or more PIT entries
+   *  \param iPitEntry make this iterator to dereference to the i-th PIT entry in name tree entry
    */
   explicit
-  Iterator(const NameTree::const_iterator& ntIt);
+  Iterator(const NameTree::const_iterator& ntIt = NameTree::const_iterator(), size_t iPitEntry = 0);
 
   const Entry&
   operator*() const
@@ -51,10 +50,12 @@
     return *this->operator->();
   }
 
-  shared_ptr<Entry>
+  const shared_ptr<Entry>&
   operator->() const
   {
-    return m_ntIt->getPitEntries().at(m_iPitEntry);
+    BOOST_ASSERT(m_ntIt != NameTree::const_iterator());
+    BOOST_ASSERT(m_iPitEntry < m_ntIt->getPitEntries().size());
+    return m_ntIt->getPitEntries()[m_iPitEntry];
   }
 
   Iterator&
diff --git a/daemon/table/pit.cpp b/daemon/table/pit.cpp
index 6bb246c..bf17e96 100644
--- a/daemon/table/pit.cpp
+++ b/daemon/table/pit.cpp
@@ -130,11 +130,5 @@
     [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); }).begin());
 }
 
-Pit::const_iterator
-Pit::end() const
-{
-  return const_iterator(m_nameTree.end());
-}
-
 } // namespace pit
 } // namespace nfd
diff --git a/daemon/table/pit.hpp b/daemon/table/pit.hpp
index ab22691..1df3380 100644
--- a/daemon/table/pit.hpp
+++ b/daemon/table/pit.hpp
@@ -112,7 +112,10 @@
    *  \sa begin()
    */
   const_iterator
-  end() const;
+  end() const
+  {
+    return Iterator();
+  }
 
 private:
   /** \brief erases a PIT Entry