table: rename cs::iterator to cs::Table::const_iterator

refs #4914

Change-Id: I0d81a0983c6bbf8ad2bdf78eec4f37ac73ec3d2d
diff --git a/daemon/table/cs-entry.hpp b/daemon/table/cs-entry.hpp
index eadb9f6..5dd931d 100644
--- a/daemon/table/cs-entry.hpp
+++ b/daemon/table/cs-entry.hpp
@@ -115,7 +115,11 @@
  */
 using Table = std::set<Entry, std::less<>>;
 
-using iterator = Table::const_iterator;
+inline bool
+operator<(Table::const_iterator lhs, Table::const_iterator rhs)
+{
+  return *lhs < *rhs;
+}
 
 } // namespace cs
 } // namespace nfd
diff --git a/daemon/table/cs-policy-lru.cpp b/daemon/table/cs-policy-lru.cpp
index 5b2f104..56b21ec 100644
--- a/daemon/table/cs-policy-lru.cpp
+++ b/daemon/table/cs-policy-lru.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -39,26 +39,26 @@
 }
 
 void
-LruPolicy::doAfterInsert(iterator i)
+LruPolicy::doAfterInsert(EntryRef i)
 {
   this->insertToQueue(i, true);
   this->evictEntries();
 }
 
 void
-LruPolicy::doAfterRefresh(iterator i)
+LruPolicy::doAfterRefresh(EntryRef i)
 {
   this->insertToQueue(i, false);
 }
 
 void
-LruPolicy::doBeforeErase(iterator i)
+LruPolicy::doBeforeErase(EntryRef i)
 {
   m_queue.get<1>().erase(i);
 }
 
 void
-LruPolicy::doBeforeUse(iterator i)
+LruPolicy::doBeforeUse(EntryRef i)
 {
   this->insertToQueue(i, false);
 }
@@ -69,18 +69,18 @@
   BOOST_ASSERT(this->getCs() != nullptr);
   while (this->getCs()->size() > this->getLimit()) {
     BOOST_ASSERT(!m_queue.empty());
-    iterator i = m_queue.front();
+    EntryRef i = m_queue.front();
     m_queue.pop_front();
     this->emitSignal(beforeEvict, i);
   }
 }
 
 void
-LruPolicy::insertToQueue(iterator i, bool isNewEntry)
+LruPolicy::insertToQueue(EntryRef i, bool isNewEntry)
 {
   Queue::iterator it;
   bool isNew = false;
-  // push_back only if iterator i does not exist
+  // push_back only if i does not exist
   std::tie(it, isNew) = m_queue.push_back(i);
 
   BOOST_ASSERT(isNew == isNewEntry);
diff --git a/daemon/table/cs-policy-lru.hpp b/daemon/table/cs-policy-lru.hpp
index 59e3e57..017de7e 100644
--- a/daemon/table/cs-policy-lru.hpp
+++ b/daemon/table/cs-policy-lru.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2016,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2019,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -30,36 +30,21 @@
 
 #include <boost/multi_index_container.hpp>
 #include <boost/multi_index/sequenced_index.hpp>
-#include <boost/multi_index/hashed_index.hpp>
+#include <boost/multi_index/ordered_index.hpp>
 
 namespace nfd {
 namespace cs {
 namespace lru {
 
-struct EntryItComparator
-{
-  bool
-  operator()(const iterator& a, const iterator& b) const
-  {
-    return *a < *b;
-  }
-};
+using Queue = boost::multi_index_container<
+                Policy::EntryRef,
+                boost::multi_index::indexed_by<
+                  boost::multi_index::sequenced<>,
+                  boost::multi_index::ordered_unique<boost::multi_index::identity<Policy::EntryRef>>
+                >
+              >;
 
-typedef boost::multi_index_container<
-    iterator,
-    boost::multi_index::indexed_by<
-      boost::multi_index::sequenced<>,
-      boost::multi_index::ordered_unique<
-        boost::multi_index::identity<iterator>, EntryItComparator
-      >
-    >
-  > Queue;
-
-/** \brief LRU cs replacement policy
- *
- * The least recently used entries get removed first.
- * Everytime when any entry is used or refreshed, Policy should witness the usage
- * of it.
+/** \brief Least-Recently-Used (LRU) replacement policy
  */
 class LruPolicy : public Policy
 {
@@ -70,26 +55,26 @@
   static const std::string POLICY_NAME;
 
 private:
-  virtual void
-  doAfterInsert(iterator i) override;
+  void
+  doAfterInsert(EntryRef i) override;
 
-  virtual void
-  doAfterRefresh(iterator i) override;
+  void
+  doAfterRefresh(EntryRef i) override;
 
-  virtual void
-  doBeforeErase(iterator i) override;
+  void
+  doBeforeErase(EntryRef i) override;
 
-  virtual void
-  doBeforeUse(iterator i) override;
+  void
+  doBeforeUse(EntryRef i) override;
 
-  virtual void
+  void
   evictEntries() override;
 
 private:
   /** \brief moves an entry to the end of queue
    */
   void
-  insertToQueue(iterator i, bool isNewEntry);
+  insertToQueue(EntryRef i, bool isNewEntry);
 
 private:
   Queue m_queue;
diff --git a/daemon/table/cs-policy-priority-fifo.cpp b/daemon/table/cs-policy-priority-fifo.cpp
index 3c604ce..d037175 100644
--- a/daemon/table/cs-policy-priority-fifo.cpp
+++ b/daemon/table/cs-policy-priority-fifo.cpp
@@ -47,27 +47,27 @@
 }
 
 void
-PriorityFifoPolicy::doAfterInsert(iterator i)
+PriorityFifoPolicy::doAfterInsert(EntryRef i)
 {
   this->attachQueue(i);
   this->evictEntries();
 }
 
 void
-PriorityFifoPolicy::doAfterRefresh(iterator i)
+PriorityFifoPolicy::doAfterRefresh(EntryRef i)
 {
   this->detachQueue(i);
   this->attachQueue(i);
 }
 
 void
-PriorityFifoPolicy::doBeforeErase(iterator i)
+PriorityFifoPolicy::doBeforeErase(EntryRef i)
 {
   this->detachQueue(i);
 }
 
 void
-PriorityFifoPolicy::doBeforeUse(iterator i)
+PriorityFifoPolicy::doBeforeUse(EntryRef i)
 {
   BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
 }
@@ -89,7 +89,7 @@
                !m_queues[QUEUE_STALE].empty() ||
                !m_queues[QUEUE_FIFO].empty());
 
-  iterator i;
+  EntryRef i;
   if (!m_queues[QUEUE_UNSOLICITED].empty()) {
     i = m_queues[QUEUE_UNSOLICITED].front();
   }
@@ -105,7 +105,7 @@
 }
 
 void
-PriorityFifoPolicy::attachQueue(iterator i)
+PriorityFifoPolicy::attachQueue(EntryRef i)
 {
   BOOST_ASSERT(m_entryInfoMap.find(i) == m_entryInfoMap.end());
 
@@ -128,7 +128,7 @@
 }
 
 void
-PriorityFifoPolicy::detachQueue(iterator i)
+PriorityFifoPolicy::detachQueue(EntryRef i)
 {
   BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
 
@@ -143,7 +143,7 @@
 }
 
 void
-PriorityFifoPolicy::moveToStaleQueue(iterator i)
+PriorityFifoPolicy::moveToStaleQueue(EntryRef i)
 {
   BOOST_ASSERT(m_entryInfoMap.find(i) != m_entryInfoMap.end());
 
diff --git a/daemon/table/cs-policy-priority-fifo.hpp b/daemon/table/cs-policy-priority-fifo.hpp
index 39b456f..9671408 100644
--- a/daemon/table/cs-policy-priority-fifo.hpp
+++ b/daemon/table/cs-policy-priority-fifo.hpp
@@ -34,8 +34,7 @@
 namespace cs {
 namespace priority_fifo {
 
-typedef std::list<iterator> Queue;
-typedef Queue::iterator QueueIt;
+using Queue = std::list<Policy::EntryRef>;
 
 enum QueueType {
   QUEUE_UNSOLICITED,
@@ -47,30 +46,19 @@
 struct EntryInfo
 {
   QueueType queueType;
-  QueueIt queueIt;
+  Queue::iterator queueIt;
   scheduler::EventId moveStaleEventId;
 };
 
-struct EntryItComparator
-{
-  bool
-  operator()(const iterator& a, const iterator& b) const
-  {
-    return *a < *b;
-  }
-};
-
-typedef std::map<iterator, EntryInfo*, EntryItComparator> EntryInfoMapFifo;
-
 /** \brief Priority FIFO replacement policy
  *
  *  This policy maintains a set of cleanup queues to decide the eviction order of CS entries.
- *  The cleanup queues are three doubly linked lists that store Table iterators.
+ *  The cleanup queues are three doubly linked lists that store EntryRefs.
  *  The three queues keep track of unsolicited, stale, and fresh Data packet, respectively.
- *  Table iterator is placed into, removed from, and moved between suitable queues
+ *  EntryRef is placed into, removed from, and moved between suitable queues
  *  whenever an Entry is added, removed, or has other attribute changes.
- *  The Table iterator of an Entry should be in exactly one queue at any moment.
- *  Within each queue, the iterators are kept in first-in-first-out order.
+ *  Each Entry should be in exactly one queue at any moment.
+ *  Within each queue, the EntryRefs are kept in first-in-first-out order.
  *  Eviction procedure exhausts the first queue before moving onto the next queue,
  *  in the order of unsolicited, stale, and fresh queue.
  */
@@ -86,16 +74,16 @@
 
 private:
   void
-  doAfterInsert(iterator i) override;
+  doAfterInsert(EntryRef i) override;
 
   void
-  doAfterRefresh(iterator i) override;
+  doAfterRefresh(EntryRef i) override;
 
   void
-  doBeforeErase(iterator i) override;
+  doBeforeErase(EntryRef i) override;
 
   void
-  doBeforeUse(iterator i) override;
+  doBeforeUse(EntryRef i) override;
 
   void
   evictEntries() override;
@@ -111,22 +99,22 @@
    *  \pre the entry is not in any queue
    */
   void
-  attachQueue(iterator i);
+  attachQueue(EntryRef i);
 
   /** \brief detaches the entry from its current queue
    *  \post the entry is not in any queue
    */
   void
-  detachQueue(iterator i);
+  detachQueue(EntryRef i);
 
   /** \brief moves an entry from FIFO queue to STALE queue
    */
   void
-  moveToStaleQueue(iterator i);
+  moveToStaleQueue(EntryRef i);
 
 private:
   Queue m_queues[QUEUE_MAX];
-  EntryInfoMapFifo m_entryInfoMap;
+  std::map<EntryRef, EntryInfo*> m_entryInfoMap;
 };
 
 } // namespace priority_fifo
diff --git a/daemon/table/cs-policy.cpp b/daemon/table/cs-policy.cpp
index 4930af8..b51aac1 100644
--- a/daemon/table/cs-policy.cpp
+++ b/daemon/table/cs-policy.cpp
@@ -73,28 +73,28 @@
 }
 
 void
-Policy::afterInsert(iterator i)
+Policy::afterInsert(EntryRef i)
 {
   BOOST_ASSERT(m_cs != nullptr);
   this->doAfterInsert(i);
 }
 
 void
-Policy::afterRefresh(iterator i)
+Policy::afterRefresh(EntryRef i)
 {
   BOOST_ASSERT(m_cs != nullptr);
   this->doAfterRefresh(i);
 }
 
 void
-Policy::beforeErase(iterator i)
+Policy::beforeErase(EntryRef i)
 {
   BOOST_ASSERT(m_cs != nullptr);
   this->doBeforeErase(i);
 }
 
 void
-Policy::beforeUse(iterator i)
+Policy::beforeUse(EntryRef i)
 {
   BOOST_ASSERT(m_cs != nullptr);
   this->doBeforeUse(i);
diff --git a/daemon/table/cs-policy.hpp b/daemon/table/cs-policy.hpp
index f50f854..fb59990 100644
--- a/daemon/table/cs-policy.hpp
+++ b/daemon/table/cs-policy.hpp
@@ -71,8 +71,6 @@
     return m_policyName;
   }
 
-
-public:
   /** \brief gets cs
    */
   Cs*
@@ -106,12 +104,18 @@
   void
   setLimit(size_t nMaxEntries);
 
+public:
+  /** \brief a reference to an CS entry
+   *  \note operator< of EntryRef compares the Data name enclosed in the Entry.
+   */
+  using EntryRef = Table::const_iterator;
+
   /** \brief emits when an entry is being evicted
    *
-   *  A policy implementation should emit this signal to cause CS to erase the entry from its index.
+   *  A policy implementation should emit this signal to cause CS to erase an entry from its index.
    *  CS should connect to this signal and erase the entry upon signal emission.
    */
-  signal::Signal<Policy, iterator> beforeEvict;
+  signal::Signal<Policy, EntryRef> beforeEvict;
 
   /** \brief invoked by CS after a new entry is inserted
    *  \post cs.size() <= getLimit()
@@ -120,27 +124,27 @@
    *  During this process, \p i might be evicted.
    */
   void
-  afterInsert(iterator i);
+  afterInsert(EntryRef i);
 
   /** \brief invoked by CS after an existing entry is refreshed by same Data
    *
    *  The policy may witness this refresh to make better eviction decisions in the future.
    */
   void
-  afterRefresh(iterator i);
+  afterRefresh(EntryRef i);
 
   /** \brief invoked by CS before an entry is erased due to management command
    *  \warning CS must not invoke this method if an entry is erased due to eviction.
    */
   void
-  beforeErase(iterator i);
+  beforeErase(EntryRef i);
 
   /** \brief invoked by CS before an entry is used to match a lookup
    *
    *  The policy may witness this usage to make better eviction decisions in the future.
    */
   void
-  beforeUse(iterator i);
+  beforeUse(EntryRef i);
 
 protected:
   /** \brief invoked after a new entry is created in CS
@@ -152,7 +156,7 @@
    *  in order to keep CS size under limit.
    */
   virtual void
-  doAfterInsert(iterator i) = 0;
+  doAfterInsert(EntryRef i) = 0;
 
   /** \brief invoked after an existing entry is refreshed by same Data
    *
@@ -160,7 +164,7 @@
    *  and adjust its cleanup index.
    */
   virtual void
-  doAfterRefresh(iterator i) = 0;
+  doAfterRefresh(EntryRef i) = 0;
 
   /** \brief invoked before an entry is erased due to management command
    *  \note This will not be invoked for an entry being evicted by policy.
@@ -169,7 +173,7 @@
    *  from its cleanup index without emitted \p afterErase signal.
    */
   virtual void
-  doBeforeErase(iterator i) = 0;
+  doBeforeErase(EntryRef i) = 0;
 
   /** \brief invoked before an entry is used to match a lookup
    *
@@ -177,7 +181,7 @@
    *  and adjust its cleanup index.
    */
   virtual void
-  doBeforeUse(iterator i) = 0;
+  doBeforeUse(EntryRef i) = 0;
 
   /** \brief evicts zero or more entries
    *  \post CS size does not exceed hard limit
@@ -189,8 +193,8 @@
   DECLARE_SIGNAL_EMIT(beforeEvict)
 
 private: // registry
-  typedef std::function<unique_ptr<Policy>()> CreateFunc;
-  typedef std::map<std::string, CreateFunc> Registry; // indexed by policy name
+  using CreateFunc = std::function<unique_ptr<Policy>()>;
+  using Registry = std::map<std::string, CreateFunc>; // indexed by policy name
 
   static Registry&
   getRegistry();
diff --git a/daemon/table/cs.cpp b/daemon/table/cs.cpp
index a73303f..b240e3d 100644
--- a/daemon/table/cs.cpp
+++ b/daemon/table/cs.cpp
@@ -33,8 +33,6 @@
 namespace nfd {
 namespace cs {
 
-NDN_CXX_ASSERT_FORWARD_ITERATOR(Cs::const_iterator);
-
 NFD_LOG_INIT(ContentStore);
 
 static unique_ptr<Policy>
@@ -66,7 +64,7 @@
     }
   }
 
-  iterator it;
+  const_iterator it;
   bool isNewEntry = false;
   std::tie(it, isNewEntry) = m_table.emplace(data.shared_from_this(), isUnsolicited);
   Entry& entry = const_cast<Entry&>(*it);
@@ -86,11 +84,11 @@
   }
 }
 
-std::pair<iterator, iterator>
+std::pair<Cs::const_iterator, Cs::const_iterator>
 Cs::findPrefixRange(const Name& prefix) const
 {
-  iterator first = m_table.lower_bound(prefix);
-  iterator last = m_table.end();
+  auto first = m_table.lower_bound(prefix);
+  auto last = m_table.end();
   if (prefix.size() > 0) {
     last = m_table.lower_bound(prefix.getSuccessor());
   }
@@ -100,7 +98,7 @@
 size_t
 Cs::eraseImpl(const Name& prefix, size_t limit)
 {
-  iterator i, last;
+  const_iterator i, last;
   std::tie(i, last) = findPrefixRange(prefix);
 
   size_t nErased = 0;
@@ -112,7 +110,7 @@
   return nErased;
 }
 
-iterator
+Cs::const_iterator
 Cs::findImpl(const Interest& interest) const
 {
   if (!m_shouldServe || m_policy->getLimit() == 0) {
@@ -157,9 +155,7 @@
 {
   NFD_LOG_DEBUG("set-policy " << policy->getName());
   m_policy = std::move(policy);
-  m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (iterator it) {
-      m_table.erase(it);
-    });
+  m_beforeEvictConnection = m_policy->beforeEvict.connect([this] (auto it) { m_table.erase(it); });
 
   m_policy->setCs(this);
   BOOST_ASSERT(m_policy->getCs() == this);
diff --git a/daemon/table/cs.hpp b/daemon/table/cs.hpp
index 1d15294..88f171d 100644
--- a/daemon/table/cs.hpp
+++ b/daemon/table/cs.hpp
@@ -80,7 +80,7 @@
   void
   find(const Interest& interest, HitCallback&& hit, MissCallback&& miss) const
   {
-    iterator match = findImpl(interest);
+    auto match = findImpl(interest);
     if (match == m_table.end()) {
       miss(interest);
       return;
@@ -158,9 +158,7 @@
   enableServe(bool shouldServe);
 
 public: // enumeration
-  /** \brief ContentStore iterator (public API)
-   */
-  using const_iterator = iterator;
+  using const_iterator = Table::const_iterator;
 
   const_iterator
   begin() const
@@ -175,13 +173,13 @@
   }
 
 private:
-  std::pair<iterator, iterator>
+  std::pair<const_iterator, const_iterator>
   findPrefixRange(const Name& prefix) const;
 
   size_t
   eraseImpl(const Name& prefix, size_t limit);
 
-  iterator
+  const_iterator
   findImpl(const Interest& interest) const;
 
   void