aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/parallel/base.h
blob: 5a756cd4be9b7e523323b4dbdb9eb5c7d6fe5463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// -*- C++ -*-

// Copyright (C) 2007 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library 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 2, or (at your option) any later
// version.

// This library 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 this library; see the file COPYING.  If not, write to
// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.

// As a special exception, you may use this file as part of a free
// software library without restriction.  Specifically, if other files
// instantiate templates or use macros or inline functions from this
// file, or you compile this file and link it with other files to
// produce an executable, this file does not by itself cause the
// resulting executable to be covered by the GNU General Public
// License.  This exception does not however invalidate any other
// reasons why the executable file might be covered by the GNU General
// Public License.

/** @file parallel/base.h
 *  @brief Sequential helper functions.
 *  This file is a GNU parallel extension to the Standard C++ Library.
 */

// Written by Johannes Singler.

#ifndef _GLIBCXX_PARALLEL_BASE_H
#define _GLIBCXX_PARALLEL_BASE_H 1

#include <parallel/features.h>
#include <functional>
#include <parallel/basic_iterator.h>
#include <parallel/parallel.h>
#include <cstdio>

namespace __gnu_parallel
{
  // XXX remove std::duplicates from here if possible,
  // XXX but keep minimal dependencies.

/** @brief Calculates the rounded-down logarithm of @c n for base 2.
  *  @param n Argument.
  *  @return Returns 0 for argument 0.
  */
template<typename Size>
  inline Size
  log2(Size n)
    {
      Size k;
      for (k = 0; n != 1; n >>= 1)
        ++k;
      return k;
    }

/** @brief Encode two integers into one __gnu_parallel::lcas_t.
  *  @param a First integer, to be encoded in the most-significant @c
  *  lcas_t_bits/2 bits.
  *  @param b Second integer, to be encoded in the least-significant
  *  @c lcas_t_bits/2 bits.
  *  @return __gnu_parallel::lcas_t value encoding @c a and @c b.
  *  @see decode2
  */
inline lcas_t
encode2(int a, int b)	//must all be non-negative, actually
{
  return (((lcas_t)a) << (lcas_t_bits / 2)) | (((lcas_t)b) << 0);
}

/** @brief Decode two integers from one __gnu_parallel::lcas_t.
  *  @param x __gnu_parallel::lcas_t to decode integers from.
  *  @param a First integer, to be decoded from the most-significant
  *  @c lcas_t_bits/2 bits of @c x.
  *  @param b Second integer, to be encoded in the least-significant
  *  @c lcas_t_bits/2 bits of @c x.
  *  @see encode2
  */
inline void
decode2(lcas_t x, int& a, int& b)
{
  a = (int)((x >> (lcas_t_bits / 2)) & lcas_t_mask);
  b = (int)((x >>               0 ) & lcas_t_mask);
}

/** @brief Equivalent to std::min. */
template<typename T>
  const T&
  min(const T& a, const T& b)
  {
    return (a < b) ? a : b;
  };

/** @brief Equivalent to std::max. */
template<typename T>
  const T&
  max(const T& a, const T& b)
  {
    return (a > b) ? a : b;
  };

/** @brief Constructs predicate for equality from strict weak
  *  ordering predicate
  */
// XXX comparator at the end, as per others
template<typename Comparator, typename T1, typename T2>
  class equal_from_less : public std::binary_function<T1, T2, bool>
  {
  private:
    Comparator& comp;

  public:
    equal_from_less(Comparator& _comp) : comp(_comp) { }

    bool operator()(const T1& a, const T2& b)
    {
      return !comp(a, b) && !comp(b, a);
    }
  };


/** @brief Similar to std::binder1st,
  *  but giving the argument types explicitly. */
template<typename _Predicate, typename argument_type>
  class unary_negate
  : public std::unary_function<argument_type, bool>
  {
  protected:
    _Predicate _M_pred;

  public:
    explicit
    unary_negate(const _Predicate& __x) : _M_pred(__x) { }

    bool
    operator()(const argument_type& __x)
    { return !_M_pred(__x); }
  };

/** @brief Similar to std::binder1st,
  *  but giving the argument types explicitly. */
template<
    typename _Operation,
    typename first_argument_type,
    typename second_argument_type,
    typename result_type>
  class binder1st
  : public std::unary_function<second_argument_type, result_type>
  {
  protected:
    _Operation op;
    first_argument_type value;

  public:
    binder1st(const _Operation& __x,
              const first_argument_type& __y)
    : op(__x), value(__y) { }

    result_type
    operator()(const second_argument_type& __x)
    { return op(value, __x); }

    // _GLIBCXX_RESOLVE_LIB_DEFECTS
    // 109.  Missing binders for non-const sequence elements
    result_type
    operator()(second_argument_type& __x) const
    { return op(value, __x); }
  };

/**
  *  @brief Similar to std::binder2nd, but giving the argument types
  *  explicitly.
  */
template<
    typename _Operation,
    typename first_argument_type,
    typename second_argument_type,
    typename result_type>
  class binder2nd
  : public std::unary_function<first_argument_type, result_type>
  {
  protected:
    _Operation op;
    second_argument_type value;

  public:
    binder2nd(const _Operation& __x,
              const second_argument_type& __y)
    : op(__x), value(__y) { }

    result_type
    operator()(const first_argument_type& __x) const
    { return op(__x, value); }

    // _GLIBCXX_RESOLVE_LIB_DEFECTS
    // 109.  Missing binders for non-const sequence elements
    result_type
    operator()(first_argument_type& __x)
    { return op(__x, value); }
  };

/** @brief Similar to std::equal_to, but allows two different types. */
template<typename T1, typename T2>
  struct equal_to : std::binary_function<T1, T2, bool>
  {
    bool operator()(const T1& t1, const T2& t2) const
    { return t1 == t2; }
  };

/** @brief Similar to std::less, but allows two different types. */
template<typename T1, typename T2>
  struct less : std::binary_function<T1, T2, bool>
  {
    bool
    operator()(const T1& t1, const T2& t2) const
    { return t1 < t2; }

    bool
    operator()(const T2& t2, const T1& t1) const
    { return t2 < t1; }
  };

// Partial specialization for one type. Same as std::less.
template<typename _Tp>
struct less<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, bool>
  {
    bool
    operator()(const _Tp& __x, const _Tp& __y) const
    { return __x < __y; }
  };


  /** @brief Similar to std::plus, but allows two different types. */
template<typename _Tp1, typename _Tp2>
  struct plus : public std::binary_function<_Tp1, _Tp2, _Tp1>
  {
    typedef typeof(*static_cast<_Tp1*>(NULL)
                    + *static_cast<_Tp2*>(NULL)) result;

    result
    operator()(const _Tp1& __x, const _Tp2& __y) const
    { return __x + __y; }
  };

// Partial specialization for one type. Same as std::plus.
template<typename _Tp>
  struct plus<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, _Tp>
  {
    typedef typeof(*static_cast<_Tp*>(NULL)
                    + *static_cast<_Tp*>(NULL)) result;

    result
    operator()(const _Tp& __x, const _Tp& __y) const
    { return __x + __y; }
  };


/** @brief Similar to std::multiplies, but allows two different types. */
template<typename _Tp1, typename _Tp2>
  struct multiplies : public std::binary_function<_Tp1, _Tp2, _Tp1>
  {
    typedef typeof(*static_cast<_Tp1*>(NULL)
                    * *static_cast<_Tp2*>(NULL)) result;

    result
    operator()(const _Tp1& __x, const _Tp2& __y) const
    { return __x * __y; }
  };

// Partial specialization for one type. Same as std::multiplies.
template<typename _Tp>
  struct multiplies<_Tp, _Tp> : public std::binary_function<_Tp, _Tp, _Tp>
  {
    typedef typeof(*static_cast<_Tp*>(NULL)
                    * *static_cast<_Tp*>(NULL)) result;

    result
    operator()(const _Tp& __x, const _Tp& __y) const
    { return __x * __y; }
  };


template<typename T, typename _DifferenceTp>
  class pseudo_sequence;

/** @brief Iterator associated with __gnu_parallel::pseudo_sequence.
  *  If features the usual random-access iterator functionality.
  *  @param T Sequence value type.
  *  @param difference_type Sequence difference type.
  */
template<typename T, typename _DifferenceTp>
  class pseudo_sequence_iterator
  {
  public:
    typedef _DifferenceTp difference_type;

  private:
    typedef pseudo_sequence_iterator<T, _DifferenceTp> type;

    const T& val;
    difference_type pos;

  public:
    pseudo_sequence_iterator(const T& val, difference_type pos)
    : val(val), pos(pos) { }

    // Pre-increment operator.
    type&
    operator++()
    {
      ++pos;
      return *this;
    }

    // Post-increment operator.
    const type
    operator++(int)
    { return type(pos++); }

    const T&
    operator*() const
    { return val; }

    const T&
    operator[](difference_type) const
    { return val; }

    bool
    operator==(const type& i2)
    { return pos == i2.pos; }

    difference_type
    operator!=(const type& i2)
    { return pos != i2.pos; }

    difference_type
    operator-(const type& i2)
    { return pos - i2.pos; }
  };

/** @brief Sequence that conceptually consists of multiple copies of
    the same element.
  *  The copies are not stored explicitly, of course.
  *  @param T Sequence value type.
  *  @param difference_type Sequence difference type.
  */
template<typename T, typename _DifferenceTp>
  class pseudo_sequence
  {
    typedef pseudo_sequence<T, _DifferenceTp> type;

  public:
    typedef _DifferenceTp difference_type;

    // Better case down to uint64, than up to _DifferenceTp.
    typedef pseudo_sequence_iterator<T, uint64> iterator;

    /** @brief Constructor.
      *  @param val Element of the sequence.
      *  @param count Number of (virtual) copies.
      */
    pseudo_sequence(const T& val, difference_type count)
    : val(val), count(count)  { }

    /** @brief Begin iterator. */
    iterator
    begin() const
    { return iterator(val, 0); }

    /** @brief End iterator. */
    iterator
    end() const
    { return iterator(val, count); }

  private:
    const T& val;
    difference_type count;
  };

/** @brief Functor that does nothing */
template<typename _ValueTp>
  class void_functor
  {
    inline void
    operator()(const _ValueTp& v) const { }
  };

/** @brief Compute the median of three referenced elements,
    according to @c comp.
  *  @param a First iterator.
  *  @param b Second iterator.
  *  @param c Third iterator.
  *  @param comp Comparator.
  */
template<typename RandomAccessIterator, typename Comparator>
RandomAccessIterator
  median_of_three_iterators(RandomAccessIterator a, RandomAccessIterator b,
                            RandomAccessIterator c, Comparator& comp)
  {
    if (comp(*a, *b))
      if (comp(*b, *c))
        return b;
      else
        if (comp(*a, *c))
          return c;
        else
          return a;
    else
      {
        // Just swap a and b.
        if (comp(*a, *c))
          return a;
        else
          if (comp(*b, *c))
            return c;
          else
            return b;
      }
  }

// Avoid the use of assert, because we're trying to keep the <cassert>
// include out of the mix. (Same as debug mode).
inline void
__replacement_assert(const char* __file, int __line,
                     const char* __function, const char* __condition)
{
  std::printf("%s:%d: %s: Assertion '%s' failed.\n", __file, __line,
              __function, __condition);
  __builtin_abort();
}

#define _GLIBCXX_PARALLEL_ASSERT(_Condition)                            \
do 								        \
  {									\
    if (!(_Condition))						\
      __gnu_parallel::__replacement_assert(__FILE__, __LINE__,	\
                                  __PRETTY_FUNCTION__, #_Condition);	\
  } while (false)

} //namespace __gnu_parallel

#endif