Jpp  15.0.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPermutation.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JPERMUTATION__
2 #define __JTOOLS__JPERMUTATION__
3 
4 #include <iterator>
5 
6 
7 /**
8  * \author mdejong
9  */
10 
11 namespace JTOOLS {}
12 namespace JPP { using namespace JTOOLS; }
13 
14 namespace JTOOLS {
15 
16  /**
17  * Implementation of method next_permutation for bidirectional iterators.
18  */
19  template<class T, class JComparator_t>
20  inline bool next_permutation(T __begin,
21  T __last,
22  T __end,
23  JComparator_t compare,
24  std::bidirectional_iterator_tag)
25  {
26  using namespace std;
27 
28  T __rend = __begin;
29  --__rend;
30 
31  for (T __i = __last; --__i != __rend; ) {
32 
33  T __j;
34 
35  for (__j = __last; __j != __end && !compare(*__i,*__j); ++__j) {}
36 
37  if (__j != __end) {
38 
39  iter_swap(__i,__j);
40 
41  if (++__i != __last && ++__j != __end) {
42 
43  reverse(__i,__last);
44  reverse(__j,__end);
45 
46  T __k = __end;
47 
48  while (__k != __j && __i != __last)
49  iter_swap(__i++,--__k);
50 
51  reverse(__i,__last);
52  reverse(__j,__k);
53  }
54 
55  return true;
56  }
57  }
58 
59  reverse(__begin, __last);
60  reverse(__last, __end);
61  reverse(__begin, __end);
62 
63  return false;
64  }
65 
66 
67  /**
68  * Permutations of sub-set of data.
69  * The first template argument refers to a bidirectional iterator and
70  * the second template argument to the comparator.
71  *
72  * \param __begin begin of input data
73  * \param __last end of output data
74  * \param __end end of input data
75  * \param compare comparison operator
76  * \return true if another permutation exists; else false
77  */
78  template<class T, class JComparator_t>
79  inline bool next_permutation(T __begin,
80  T __last,
81  T __end,
82  JComparator_t compare)
83  {
84  return next_permutation(__begin, __last, __end, compare, typename std::iterator_traits<T>::iterator_category());
85  }
86 }
87 
88 #endif
do set_variable OUTPUT_DIRECTORY $WORKDIR T
bool next_permutation(T __begin, T __last, T __end, JComparator_t compare, std::bidirectional_iterator_tag)
Implementation of method next_permutation for bidirectional iterators.
Definition: JPermutation.hh:20