/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
 * Copyright (c) 2014-2015,  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);

  ~Pit();

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