File: sort.hpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (264 lines) | stat: -rw-r--r-- 7,239 bytes parent folder | download
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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Christian Schulte <schulte@gecode.org>
 *
 *  Copyright:
 *     Christian Schulte, 2002
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <algorithm>
#include <climits>

namespace Gecode { namespace Support {

  /// Exchange elements according to order
  template<class Type, class Less>
  forceinline void
  exchange(Type &a, Type &b, Less &less) {
    if (less(b,a)) std::swap(a,b);
  }

  /// Perform quicksort only for more elements
  int const QuickSortCutoff = 20;

  /// Static stack for quicksort
  template<class Type>
  class QuickSortStack {
  private:
    /// Maximal stacksize quicksort ever needs
    static const int maxsize = sizeof(int) * CHAR_BIT;
    /// Top of stack
    Type** tos;
    /// Stack entries (terminated by nullptr entry)
    Type*  stack[2*maxsize+1];
  public:
    /// Initialize stack as empty
    QuickSortStack(void);
    /// Test whether stack is empty
    bool empty(void) const;
    /// Push two positions \a l and \a r
    void push(Type* l, Type* r);
    /// Pop two positions \a l and \a r
    void pop(Type*& l, Type*& r);
  };

  template<class Type>
  forceinline
  QuickSortStack<Type>::QuickSortStack(void) : tos(&stack[0]) {
    *(tos++) = nullptr;
  }

  template<class Type>
  forceinline bool
  QuickSortStack<Type>::empty(void) const {
    return *(tos-1) == nullptr;
  }

  template<class Type>
  forceinline void
  QuickSortStack<Type>::push(Type* l, Type* r) {
    *(tos++) = l; *(tos++) = r;
  }

  template<class Type>
  forceinline void
  QuickSortStack<Type>::pop(Type*& l, Type*& r) {
    r = *(--tos); l = *(--tos);
  }

  /// Standard insertion sort
  template<class Type, class Less>
  forceinline void
  insertion(Type* l, Type* r, Less &less) {
    for (Type* i = r; i > l; i--)
      exchange(*(i-1),*i,less);
    for (Type* i = l+2; i <= r; i++) {
      Type* j = i;
      Type v = *i;
      while (less(v,*(j-1))) {
        *j = *(j-1); j--;
      }
      *j = v;
    }
  }

  /// Standard partitioning
  template<class Type, class Less>
  forceinline Type*
  partition(Type* l, Type* r, Less &less) {
    Type* i = l-1;
    Type* j = r;
    Type v = *r;
    while (true) {
      while (less(*(++i),v)) {}
      while (less(v,*(--j))) if (j == l) break;
        if (i >= j) break;
        std::swap(*i,*j);
    }
    std::swap(*i,*r);
    return i;
  }

  /// Standard quick sort
  template<class Type, class Less>
  inline void
  quicksort(Type* l, Type* r, Less &less) {
    QuickSortStack<Type> s;
    while (true) {
      std::swap(*(l+((r-l) >> 1)),*(r-1));
      exchange(*l,*(r-1),less);
      exchange(*l,*r,less);
      exchange(*(r-1),*r,less);
      Type* i = partition(l+1,r-1,less);
      if (i-l > r-i) {
        if (r-i > QuickSortCutoff) {
          s.push(l,i-1); l=i+1; continue;
        }
        if (i-l > QuickSortCutoff) {
          r=i-1; continue;
        }
      } else {
        if (i-l > QuickSortCutoff) {
          s.push(i+1,r); r=i-1; continue;
        }
        if (r-i > QuickSortCutoff) {
          l=i+1; continue;
        }
      }
      if (s.empty())
        break;
      s.pop(l,r);
    }
  }

  /// Comparison class for sorting using \a <
  template<class Type>
  class Less {
  public:
    bool operator ()(const Type& lhs, const Type& rhs) {
      return lhs < rhs;
    }
  };

  /**
   * \brief Insertion sort
   *
   * Sorts by insertion the \a n first elements of array \a x according
   * to the order \a l as instance of class \a Less. The class
   * \a Less must implement the single member function
   * \code bool operator ()(const Type&, const Type&) \endcode
   * for comparing elements. Note that the order must be strict, that
   * is: comparing an element with itself must return false.
   *
   * The algorithm is largely based on the following book:
   * Robert Sedgewick, Algorithms in C++, 3rd edition, 1998, Addison Wesley.
   *
   * \ingroup FuncSupport
   */
  template<class Type, class Less>
  forceinline void
  insertion(Type* x, int n, Less &l) {
    if (n < 2)
      return;
    assert(!l(x[0],x[0]));
    insertion(x,x+n-1,l);
  }

  /**
   * \brief Insertion sort
   *
   * Sorts by insertion the \a n first elements of array \a x according
   * to the order \a <.
   *
   * The algorithm is largely based on the following book:
   * Robert Sedgewick, Algorithms in C++, 3rd edition, 1998, Addison Wesley.
   *
   * \ingroup FuncSupport
   */
  template<class Type>
  forceinline void
  insertion(Type* x, int n) {
    if (n < 2)
      return;
    Less<Type> l;
    assert(!l(x[0],x[0]));
    insertion(x,x+n-1,l);
  }

  /**
   * \brief Quicksort
   *
   * Sorts with quicksort the \a n first elements of array \a x according
   * to the order \a l as instance of class \a Less. The class
   * \a Less must implement the single member function
   * \code bool operator ()(const Type&, const Type&) \endcode
   * for comparing elements. Note that the order must be strict, that
   * is: comparing an element with itself must return false.
   *
   * The algorithm is largely based on the following book:
   * Robert Sedgewick, Algorithms in C++, 3rd edition, 1998, Addison Wesley.
   *
   * \ingroup FuncSupport
   */
  template<class Type, class Less>
  forceinline void
  quicksort(Type* x, int n, Less &l) {
    if (n < 2)
      return;
    assert(!l(x[0],x[0]));
    if (n > QuickSortCutoff)
      quicksort(x,x+n-1,l);
    insertion(x,x+n-1,l);
  }

  /**
   * \brief Quicksort
   *
   * Sorts with quicksort the \a n first elements of array \a x according
   * to the order \a <.
   *
   * The algorithm is largely based on the following book:
   * Robert Sedgewick, Algorithms in C++, 3rd edition, 1998, Addison Wesley.
   *
   * \ingroup FuncSupport
   */
  template<class Type>
  forceinline void
  quicksort(Type* x, int n) {
    if (n < 2)
      return;
    Less<Type> l;
    assert(!l(x[0],x[0]));
    if (n > QuickSortCutoff)
      quicksort(x,x+n-1,l);
    insertion(x,x+n-1,l);
  }

}}

// STATISTICS: support-any