/* -*- 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_HPP
#define NFD_DAEMON_TABLE_PIT_HPP

#include "name-tree.hpp"
#include "pit-entry.hpp"

namespace nfd {
namespace pit {

/** \class DataMatchResult
 *  \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()
 */
typedef std::vector<shared_ptr<pit::Entry>> DataMatchResult;

} // namespace pit

/** \brief represents the Interest Table
 */
class Pit : noncopyable
{
public:
  explicit
  Pit(NameTree& nameTree);

  /** \return number of entries
   */
  size_t
  size() const;

  /** \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;

  /** \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);

  /** \brief performs a Data match
   *  \return an iterable of all PIT entries matching data
   */
  pit::DataMatchResult
  findAllDataMatches(const Data& data) const;

  /**
   *  \brief erases a PIT Entry
   */
  void
  erase(shared_ptr<pit::Entry> pitEntry);

public: // enumeration
  class const_iterator;

  /** \brief returns an iterator pointing to the first PIT entry
   *  \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
   */
  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
   */
  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 finds or inserts a PIT entry for Interest
   *  \param interest the Interest; must be created with make_shared if allowInsert
   *  \param allowInsert whether inserting new entry is allowed.
   *  \return if allowInsert, a new or existing entry with same Name+Selectors,
   *          and true for new entry, false for existing entry;
   *          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>
  findOrInsert(const Interest& interest, bool allowInsert);

private:
  NameTree& m_nameTree;
  size_t m_nItems;
};

inline size_t
Pit::size() const
{
  return m_nItems;
}

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);
}

} // namespace nfd

#endif // NFD_DAEMON_TABLE_PIT_HPP
