// vi:set ft=cpp: -*- Mode: C++ -*-
/**
 * \file
 * \brief Pair implementation
 */
/*
 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>
 *     economic rights: Technische Universität Dresden (Germany)
 *
 * License: see LICENSE.spdx (in this directory or the directories above)
 */
#pragma once

#include <l4/cxx/type_traits>

namespace cxx {

/**
 * \ingroup cxx_api
 * \brief Pair of two values.
 *
 * Standard container for a pair of values.
 * \param First Type of the first value.
 * \param Second Type of the second value.
 */
template< typename First, typename Second >
struct Pair
{
  /// Type of first value.
  typedef First First_type;
  /// Type of second value.
  typedef Second Second_type;

  /// First value.
  First first;
  /// Second value.
  Second second;

  /**
   * \brief Create a pair from the two values.
   * \param first The first value.
   * \param second The second value.
   */
  template<typename A1, typename A2>
  Pair(A1 &&first, A2 &&second)
  : first(cxx::forward<A1>(first)), second(cxx::forward<A2>(second)) {}

  /**
   * \brief Create a pair, default constructing the second value.
   * \param first The first value.
   */
  template<typename A1>
  Pair(A1 &&first)
  : first(cxx::forward<A1>(first)), second() {}

  /// Default construction.
  Pair() = default;
};

template< typename F, typename S >
Pair<F,S> pair(F const &f, S const &s)
{ return cxx::Pair<F,S>(f,s); }


/**
 * \brief Comparison functor for Pair.
 * \param Cmp Comparison functor for the first value of the pair.
 * \param Typ The pair type.
 *
 * This functor can be used to compare Pair values with respect to the
 * first value.
 */
template< typename Cmp, typename Typ >
class Pair_first_compare
{
private:
  Cmp const &_cmp;

public:
  /**
   * \brief Construction.
   * \param cmp The comparison functor used for the first value.
   */
  Pair_first_compare(Cmp const &cmp = Cmp()) : _cmp(cmp) {}

  /**
   * \brief Do the comparison based on the first value.
   * \param l The lefthand value.
   * \param r The righthand value.
   */
  bool operator () (Typ const &l, Typ const &r) const
  { return _cmp(l.first,r.first); }
};

}

template< typename OS, typename A, typename B >
inline
OS &operator << (OS &os, cxx::Pair<A,B> const &p)
{
  os << p.first << ';' << p.second;
  return os;
}

