1// vi:set ft=cpp: -*- Mode: C++ -*-
2/**
3 * \file
4 * \brief Pair implementation
5 */
6/*
7 * (c) 2008-2009 Alexander Warg <warg@os.inf.tu-dresden.de>
8 *     economic rights: Technische Universität Dresden (Germany)
9 *
10 * This file is part of TUD:OS and distributed under the terms of the
11 * GNU General Public License 2.
12 * Please see the COPYING-GPL-2 file for details.
13 *
14 * As a special exception, you may use this file as part of a free software
15 * library without restriction.  Specifically, if other files instantiate
16 * templates or use macros or inline functions from this file, or you compile
17 * this file and link it with other files to produce an executable, this
18 * file does not by itself cause the resulting executable to be covered by
19 * the GNU General Public License.  This exception does not however
20 * invalidate any other reasons why the executable file might be covered by
21 * the GNU General Public License.
22 */
23#pragma once
24
25namespace cxx {
26
27/**
28 * \ingroup cxx_api
29 * \brief Pair of two values.
30 *
31 * Standard container for a pair of values.
32 * \param First Type of the first value.
33 * \param Second Type of the second value.
34 */
35template< typename First, typename Second >
36struct Pair
37{
38  /// Type of first value.
39  typedef First First_type;
40  /// Type of second value.
41  typedef Second Second_type;
42
43  /// First value.
44  First first;
45  /// Second value.
46  Second second;
47
48  /**
49   * \brief Create a pair from the two values.
50   * \param first The first value.
51   * \param second The second value.
52   */
53  template<typename A1, typename A2>
54  Pair(A1 &&first, A2 &&second)
55  : first(first), second(second) {}
56
57  /// Default construction.
58  Pair() = default;
59};
60
61template< typename F, typename S >
62Pair<F,S> pair(F const &f, S const &s)
63{ return cxx::Pair<F,S>(f,s); }
64
65
66/**
67 * \brief Comparison functor for Pair.
68 * \param Cmp Comparison functor for the first value of the pair.
69 * \param Typ The pair type.
70 *
71 * This functor can be used to compare Pair values with respect to the
72 * first value.
73 */
74template< typename Cmp, typename Typ >
75class Pair_first_compare
76{
77private:
78  Cmp const &_cmp;
79
80public:
81  /**
82   * \brief Construction.
83   * \param cmp The comparison functor used for the first value.
84   */
85  Pair_first_compare(Cmp const &cmp = Cmp()) : _cmp(cmp) {}
86
87  /**
88   * \brief Do the comaprison based on the first value.
89   * \param l The lefthand value.
90   * \param r The righthand value.
91   */
92  bool operator () (Typ const &l, Typ const &r) const
93  { return _cmp(l.first,r.first); }
94};
95
96}
97
98template< typename OS, typename A, typename B >
99inline
100OS &operator << (OS &os, cxx::Pair<A,B> const &p)
101{
102  os << p.first << ';' << p.second;
103  return os;
104}
105
106