STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
base.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/parallel/base.h
3  *
4  * Sequential helper functions.
5  * Extracted from MCSTL - http://algo2.iti.uni-karlsruhe.de/singler/mcstl/
6  *
7  * Part of the STXXL. See http://stxxl.sourceforge.net
8  *
9  * Copyright (C) 2007 Johannes Singler <[email protected]>
10  * Copyright (C) 2014 Timo Bingmann <[email protected]>
11  *
12  * Distributed under the Boost Software License, Version 1.0.
13  * (See accompanying file LICENSE_1_0.txt or copy at
14  * http://www.boost.org/LICENSE_1_0.txt)
15  **************************************************************************/
16 
17 #ifndef STXXL_PARALLEL_BASE_HEADER
18 #define STXXL_PARALLEL_BASE_HEADER
19 
20 #include <stxxl/bits/namespace.h>
22 
23 #include <functional>
24 #include <iterator>
25 
27 
28 namespace parallel {
29 
30 /*!
31  * Alternative to std::not2, typedefs first_argument_type and
32  * second_argument_type not needed.
33  */
34 template <class Predicate, typename first_argument_type, typename second_argument_type>
36  : public std::binary_function<first_argument_type, second_argument_type, bool>
37 {
38 protected:
39  Predicate pred;
40 
41 public:
42  explicit
43  binary_negate(const Predicate& _pred) : pred(_pred) { }
44 
45  bool operator () (const first_argument_type& x,
46  const second_argument_type& y) const
47  {
48  return !pred(x, y);
49  }
50 };
51 
52 /*!
53  * Encode two integers into one mcstl::lcas_t.
54  *
55  * \param a First integer, to be encoded in the most-significant \c lcas_t_bits/2 bits.
56  * \param b Second integer, to be encoded in the least-significant \c lcas_t_bits/2 bits.
57  * \return mcstl::lcas_t value encoding \c a and \c b.
58  * \see decode2
59  */
60 static inline lcas_t encode2(int a, int b) // must all be non-negative, actually
61 {
62  return (((lcas_t)a) << (lcas_t_bits / 2)) | (((lcas_t)b) << 0);
63 }
64 
65 /*!
66  * Decode two integers from one mcstl::lcas_t.
67  *
68  * \param x mcstl::lcas_t to decode integers from.
69  * \param a First integer, to be decoded from the most-significant \c lcas_t_bits/2 bits of \c x.
70  * \param b Second integer, to be encoded in the least-significant \c lcas_t_bits/2 bits of \c x.
71  * \see encode2
72  */
73 static inline void decode2(lcas_t x, int& a, int& b)
74 {
75  a = (int)((x >> (lcas_t_bits / 2)) & lcas_t_mask);
76  b = (int)((x >> 0) & lcas_t_mask);
77 }
78 
79 /*!
80  * Constructs predicate for equality from strict weak ordering predicate
81  */
82 template <class Comparator, typename T1, typename T2>
83 class equal_from_less : public std::binary_function<T1, T2, bool>
84 {
85 private:
86  Comparator& comp;
87 
88 public:
89  equal_from_less(Comparator& _comp) : comp(_comp) { }
90 
91  bool operator () (const T1& a, const T2& b)
92  {
93  //FIXME: wrong in general (T1 != T2)
94  return !comp(a, b) && !comp(b, a);
95  }
96 };
97 
98 /*!
99  * Compute the median of three referenced elements, according to \c comp.
100  *
101  * \param a First iterator.
102  * \param b Second iterator.
103  * \param c Third iterator.
104  * \param comp Comparator.
105  */
106 template <typename RandomAccessIterator, typename Comparator>
107 RandomAccessIterator
108 median_of_three_iterators(RandomAccessIterator a, RandomAccessIterator b,
109  RandomAccessIterator c, Comparator& comp)
110 {
111  if (comp(*a, *b))
112  if (comp(*b, *c))
113  return b;
114  else if (comp(*a, *c))
115  return c;
116  else
117  return a;
118  else //just swap a and b
119  if (comp(*a, *c))
120  return a;
121  else if (comp(*b, *c))
122  return c;
123  else
124  return b;
125 }
126 
127 /** Similar to std::equal_to, but allows two different types. */
128 template <typename T1, typename T2>
129 struct equal_to : std::binary_function<T1, T2, bool>
130 {
131  bool operator () (const T1& t1, const T2& t2) const
132  {
133  return t1 == t2;
134  }
135 };
136 
137 /** Similar to std::less, but allows two different types. */
138 template <typename T1, typename T2>
139 struct less : std::binary_function<T1, T2, bool>
140 {
141  bool operator () (const T1& t1, const T2& t2) const
142  {
143  return t1 < t2;
144  }
145 };
146 
147 } // namespace parallel
148 
150 
151 #endif // !STXXL_PARALLEL_BASE_HEADER
RandomAccessIterator median_of_three_iterators(RandomAccessIterator a, RandomAccessIterator b, RandomAccessIterator c, Comparator &comp)
Compute the median of three referenced elements, according to comp.
Definition: base.h:108
int64 lcas_t
Definition: types.h:41
Alternative to std::not2, typedefs first_argument_type and second_argument_type not needed...
Definition: base.h:35
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static lcas_t encode2(int a, int b)
Encode two integers into one mcstl::lcas_t.
Definition: base.h:60
static const lcas_t lcas_t_mask
Definition: types.h:49
static const size_t lcas_t_bits
Definition: types.h:45
equal_from_less(Comparator &_comp)
Definition: base.h:89
Constructs predicate for equality from strict weak ordering predicate.
Definition: base.h:83
binary_negate(const Predicate &_pred)
Definition: base.h:43
static void decode2(lcas_t x, int &a, int &b)
Decode two integers from one mcstl::lcas_t.
Definition: base.h:73
#define STXXL_END_NAMESPACE
Definition: namespace.h:17