table: move PIT iterator to a separate file

This commit only moves code around and renames variables.
There's no functional change.

refs #3164

Change-Id: Icb6b249cc0c07fcfe42f7a6d6987de31285566e7
diff --git a/daemon/table/pit-iterator.cpp b/daemon/table/pit-iterator.cpp
new file mode 100644
index 0000000..0cdb395
--- /dev/null
+++ b/daemon/table/pit-iterator.cpp
@@ -0,0 +1,72 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "pit-iterator.hpp"
+
+#include <boost/concept/assert.hpp>
+#include <boost/concept_check.hpp>
+#include <type_traits>
+
+namespace nfd {
+namespace pit {
+
+BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Iterator>));
+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)
+  : m_ntIt(ntIt)
+  , m_iPitEntry(0)
+{
+}
+
+Iterator&
+Iterator::operator++()
+{
+  ++m_iPitEntry;
+  if (m_iPitEntry < m_ntIt->getPitEntries().size()) {
+    return *this;
+  }
+
+  ++m_ntIt;
+  m_iPitEntry = 0;
+  return *this;
+}
+
+Iterator
+Iterator::operator++(int)
+{
+  Iterator copy = *this;
+  this->operator++();
+  return copy;
+}
+
+} // namespace pit
+} // namespace nfd
diff --git a/daemon/table/pit-iterator.hpp b/daemon/table/pit-iterator.hpp
new file mode 100644
index 0000000..061e077
--- /dev/null
+++ b/daemon/table/pit-iterator.hpp
@@ -0,0 +1,86 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_TABLE_PIT_ITERATOR_HPP
+#define NFD_DAEMON_TABLE_PIT_ITERATOR_HPP
+
+#include "name-tree.hpp"
+#include "pit-entry.hpp"
+
+namespace nfd {
+namespace pit {
+
+/** \brief PIT iterator
+ */
+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
+   */
+  explicit
+  Iterator(const NameTree::const_iterator& ntIt);
+
+  const Entry&
+  operator*() const
+  {
+    return *this->operator->();
+  }
+
+  shared_ptr<Entry>
+  operator->() const
+  {
+    return m_ntIt->getPitEntries().at(m_iPitEntry);
+  }
+
+  Iterator&
+  operator++();
+
+  Iterator
+  operator++(int);
+
+  bool
+  operator==(const Iterator& other) const
+  {
+    return m_ntIt == other.m_ntIt && m_iPitEntry == other.m_iPitEntry;
+  }
+
+  bool
+  operator!=(const Iterator& other) const
+  {
+    return !this->operator==(other);
+  }
+
+private:
+  NameTree::const_iterator m_ntIt; ///< current name tree entry
+  size_t m_iPitEntry; ///< current PIT entry within m_ntIt->getPitEntries()
+};
+
+} // namespace pit
+} // namespace nfd
+
+#endif // NFD_DAEMON_TABLE_PIT_ITERATOR_HPP
diff --git a/daemon/table/pit.cpp b/daemon/table/pit.cpp
index 6c378dc..6bb246c 100644
--- a/daemon/table/pit.cpp
+++ b/daemon/table/pit.cpp
@@ -24,40 +24,17 @@
  */
 
 #include "pit.hpp"
-#include <type_traits>
-
-#include <boost/concept/assert.hpp>
-#include <boost/concept_check.hpp>
-#include <type_traits>
 
 namespace nfd {
 namespace pit {
 
-#if HAVE_IS_MOVE_CONSTRUCTIBLE
-static_assert(std::is_move_constructible<DataMatchResult>::value,
-              "DataMatchResult must be MoveConstructible");
-#endif // HAVE_IS_MOVE_CONSTRUCTIBLE
-
-} // namespace pit
-
-// http://en.cppreference.com/w/cpp/concept/ForwardIterator
-BOOST_CONCEPT_ASSERT((boost::ForwardIterator<Pit::const_iterator>));
-// boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html,
-// which doesn't require DefaultConstructible
-#ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
-static_assert(std::is_default_constructible<Pit::const_iterator>::value,
-              "Pit::const_iterator must be default-constructible");
-#else
-BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<Pit::const_iterator>));
-#endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
-
 Pit::Pit(NameTree& nameTree)
   : m_nameTree(nameTree)
   , m_nItems(0)
 {
 }
 
-std::pair<shared_ptr<pit::Entry>, bool>
+std::pair<shared_ptr<Entry>, bool>
 Pit::findOrInsert(const Interest& interest, bool allowInsert)
 {
   // determine which NameTree entry should the PIT entry be attached onto
@@ -79,9 +56,9 @@
 
   // check if PIT entry already exists
   size_t nteNameLen = nteName.size();
-  const std::vector<shared_ptr<pit::Entry>>& pitEntries = nte->getPitEntries();
+  const std::vector<shared_ptr<Entry>>& pitEntries = nte->getPitEntries();
   auto it = std::find_if(pitEntries.begin(), pitEntries.end(),
-    [&interest, nteNameLen] (const shared_ptr<pit::Entry>& entry) -> bool {
+    [&interest, nteNameLen] (const shared_ptr<Entry>& entry) -> bool {
       // initial part of the name is guaranteed to be the same
       BOOST_ASSERT(entry->getInterest().getName().compare(0, nteNameLen,
                    interest.getName(), 0, nteNameLen) == 0);
@@ -99,21 +76,21 @@
     return {nullptr, true};
   }
 
-  auto entry = make_shared<pit::Entry>(interest);
+  auto entry = make_shared<Entry>(interest);
   nte->insertPitEntry(entry);
   ++m_nItems;
   return {entry, true};
 }
 
-pit::DataMatchResult
+DataMatchResult
 Pit::findAllDataMatches(const Data& data) const
 {
   auto&& ntMatches = m_nameTree.findAllMatches(data.getName(),
     [] (const name_tree::Entry& entry) { return entry.hasPitEntries(); });
 
-  pit::DataMatchResult matches;
+  DataMatchResult matches;
   for (const name_tree::Entry& nte : ntMatches) {
-    for (const shared_ptr<pit::Entry>& pitEntry : nte.getPitEntries()) {
+    for (const shared_ptr<Entry>& pitEntry : nte.getPitEntries()) {
       if (pitEntry->getInterest().matchesData(data))
         matches.emplace_back(pitEntry);
     }
@@ -123,13 +100,7 @@
 }
 
 void
-Pit::erase(shared_ptr<pit::Entry> entry)
-{
-  this->erase(entry, true);
-}
-
-void
-Pit::erase(shared_ptr<pit::Entry> entry, bool canDeleteNte)
+Pit::erase(shared_ptr<Entry> entry, bool canDeleteNte)
 {
   name_tree::Entry* nte = m_nameTree.getEntry(*entry);
   BOOST_ASSERT(nte != nullptr);
@@ -142,7 +113,7 @@
 }
 
 void
-Pit::deleteInOutRecords(shared_ptr<pit::Entry> entry, const Face& face)
+Pit::deleteInOutRecords(shared_ptr<Entry> entry, const Face& face)
 {
   BOOST_ASSERT(entry != nullptr);
 
@@ -159,4 +130,11 @@
     [] (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 cbfac86..ab22691 100644
--- a/daemon/table/pit.hpp
+++ b/daemon/table/pit.hpp
@@ -26,8 +26,8 @@
 #ifndef NFD_DAEMON_TABLE_PIT_HPP
 #define NFD_DAEMON_TABLE_PIT_HPP
 
-#include "name-tree.hpp"
 #include "pit-entry.hpp"
+#include "pit-iterator.hpp"
 
 namespace nfd {
 namespace pit {
@@ -36,12 +36,10 @@
  *  \brief an unordered iterable of all PIT entries matching Data
  *
  *  This type shall support:
- *    iterator<shared_ptr<pit::Entry>> begin()
- *    iterator<shared_ptr<pit::Entry>> end()
+ *    iterator<shared_ptr<Entry>> begin()
+ *    iterator<shared_ptr<Entry>> end()
  */
-typedef std::vector<shared_ptr<pit::Entry>> DataMatchResult;
-
-} // namespace pit
+typedef std::vector<shared_ptr<Entry>> DataMatchResult;
 
 /** \brief represents the Interest Table
  */
@@ -54,102 +52,73 @@
   /** \return number of entries
    */
   size_t
-  size() const;
+  size() const
+  {
+    return m_nItems;
+  }
 
   /** \brief finds a PIT entry for Interest
    *  \param interest the Interest
    *  \return an existing entry with same Name and Selectors; otherwise nullptr
    */
-  shared_ptr<pit::Entry>
-  find(const Interest& interest) const;
+  shared_ptr<Entry>
+  find(const Interest& interest) const
+  {
+    return const_cast<Pit*>(this)->findOrInsert(interest, false).first;
+  }
 
   /** \brief inserts a PIT entry for Interest
    *  \param interest the Interest; must be created with make_shared
    *  \return a new or existing entry with same Name and Selectors,
    *          and true for new entry, false for existing entry
    */
-  std::pair<shared_ptr<pit::Entry>, bool>
-  insert(const Interest& interest);
+  std::pair<shared_ptr<Entry>, bool>
+  insert(const Interest& interest)
+  {
+    return this->findOrInsert(interest, true);
+  }
 
   /** \brief performs a Data match
    *  \return an iterable of all PIT entries matching data
    */
-  pit::DataMatchResult
+  DataMatchResult
   findAllDataMatches(const Data& data) const;
 
-  /**
-   *  \brief erases a PIT Entry
+  /** \brief erases a PIT Entry
    */
   void
-  erase(shared_ptr<pit::Entry> entry);
+  erase(shared_ptr<Entry> entry)
+  {
+    this->erase(entry, true);
+  }
 
   /** \brief deletes in-record and out-record for face
    */
   void
-  deleteInOutRecords(shared_ptr<pit::Entry> entry, const Face& face);
+  deleteInOutRecords(shared_ptr<Entry> entry, const Face& face);
 
 public: // enumeration
-  class const_iterator;
+  typedef Iterator const_iterator;
 
-  /** \brief returns an iterator pointing to the first PIT entry
+  /** \return an iterator to the beginning
    *  \note Iteration order is implementation-specific and is undefined
-   *  \note The returned iterator may get invalidated if PIT or another NameTree-based
-   *        table is modified
+   *  \warning Undefine behavior may occur if a FIB/PIT/Measurements/StrategyChoice entry
+   *           is inserted or deleted during PIT enumeration.
    */
   const_iterator
   begin() const;
 
-  /** \brief returns an iterator referring to the past-the-end PIT entry
-   *  \note The returned iterator may get invalidated if PIT or another NameTree-based
-   *        table is modified
+  /** \return an iterator to the end
+   *  \sa begin()
    */
   const_iterator
   end() const;
 
-  class const_iterator : public std::iterator<std::forward_iterator_tag, const pit::Entry>
-  {
-  public:
-    const_iterator();
-
-    explicit
-    const_iterator(const NameTree::const_iterator& it);
-
-    ~const_iterator();
-
-    const pit::Entry&
-    operator*() const;
-
-    shared_ptr<pit::Entry>
-    operator->() const;
-
-    const_iterator&
-    operator++();
-
-    const_iterator
-    operator++(int);
-
-    bool
-    operator==(const const_iterator& other) const;
-
-    bool
-    operator!=(const const_iterator& other) const;
-
-  private:
-    NameTree::const_iterator m_nameTreeIterator;
-    /** \brief Index of the current visiting PIT entry in NameTree node
-     *
-     * Index is used to ensure that dereferencing of m_nameTreeIterator happens only when
-     * const_iterator is dereferenced or advanced.
-     */
-    size_t m_iPitEntry;
-  };
-
 private:
-  /**
-   *  \brief erases a PIT Entry
+  /** \brief erases a PIT Entry
    */
   void
-  erase(shared_ptr<pit::Entry> pitEntry, bool canDeleteNte);
+  erase(shared_ptr<Entry> pitEntry, bool canDeleteNte);
 
   /** \brief finds or inserts a PIT entry for Interest
    *  \param interest the Interest; must be created with make_shared if allowInsert
@@ -159,7 +128,7 @@
    *          if not allowInsert, an existing entry with same Name+Selectors and false,
    *          or {nullptr, true} if there's no existing entry
    */
-  std::pair<shared_ptr<pit::Entry>, bool>
+  std::pair<shared_ptr<Entry>, bool>
   findOrInsert(const Interest& interest, bool allowInsert);
 
 private:
@@ -167,93 +136,9 @@
   size_t m_nItems;
 };
 
-inline size_t
-Pit::size() const
-{
-  return m_nItems;
-}
+} // namespace pit
 
-inline shared_ptr<pit::Entry>
-Pit::find(const Interest& interest) const
-{
-  return const_cast<Pit*>(this)->findOrInsert(interest, false).first;
-}
-
-inline std::pair<shared_ptr<pit::Entry>, bool>
-Pit::insert(const Interest& interest)
-{
-  return this->findOrInsert(interest, true);
-}
-
-inline Pit::const_iterator
-Pit::end() const
-{
-  return const_iterator(m_nameTree.end());
-}
-
-inline
-Pit::const_iterator::const_iterator()
-  : m_iPitEntry(0)
-{
-}
-
-inline
-Pit::const_iterator::const_iterator(const NameTree::const_iterator& it)
-  : m_nameTreeIterator(it)
-  , m_iPitEntry(0)
-{
-}
-
-inline
-Pit::const_iterator::~const_iterator()
-{
-}
-
-inline Pit::const_iterator
-Pit::const_iterator::operator++(int)
-{
-  Pit::const_iterator temp(*this);
-  ++(*this);
-  return temp;
-}
-
-inline Pit::const_iterator&
-Pit::const_iterator::operator++()
-{
-  ++m_iPitEntry;
-  if (m_iPitEntry < m_nameTreeIterator->getPitEntries().size()) {
-    return *this;
-  }
-
-  ++m_nameTreeIterator;
-  m_iPitEntry = 0;
-  return *this;
-}
-
-inline const pit::Entry&
-Pit::const_iterator::operator*() const
-{
-  return *(this->operator->());
-}
-
-inline shared_ptr<pit::Entry>
-Pit::const_iterator::operator->() const
-{
-  return m_nameTreeIterator->getPitEntries().at(m_iPitEntry);
-}
-
-inline bool
-Pit::const_iterator::operator==(const Pit::const_iterator& other) const
-{
-  return m_nameTreeIterator == other.m_nameTreeIterator &&
-         m_iPitEntry == other.m_iPitEntry;
-}
-
-inline bool
-Pit::const_iterator::operator!=(const Pit::const_iterator& other) const
-{
-  return !(*this == other);
-}
+using pit::Pit;
 
 } // namespace nfd