File: lsort.hpp

package info (click to toggle)
aspell 0.60.8.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,336 kB
  • sloc: cpp: 24,378; sh: 12,340; perl: 1,924; ansic: 1,661; makefile: 852; sed: 16
file content (164 lines) | stat: -rw-r--r-- 3,629 bytes parent folder | download | duplicates (11)
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
/*
 * Copyright (c) 2004
 * Kevin Atkinson
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation. I make no representations about
 * the suitability of this software for any purpose.  It is provided
 * "as is" without express or implied warranty.
 *
 * This code was originally adopted from the slist implementation
 * found in the SGI STL under the following copyright:
 *
 * Copyright (c) 1997
 * Silicon Graphics Computer Systems, Inc.
 *
 * Permission to use, copy, modify, distribute and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appear in all copies and
 * that both that copyright notice and this permission notice appear
 * in supporting documentation.  Silicon Graphics makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 */

#ifndef ACOMMON_LSORT__HPP
#define ACOMMON_LSORT__HPP

namespace acommon {

using std::swap;

template <class N>
struct Next {
  N * & operator() (N * n) const {return n->next;}
};
template <class N>
struct Less {
  bool operator() (N * x, N * y) const {return x->data < y->data;}
};


template <class N, class LT, class NX>
static inline N * merge(N * x, N * y, const LT & lt, const NX & nx)
{
  if (lt(y,x)) swap(x,y);
  N * first = x;
  while (nx(x) && y) {
    if (lt(y,nx(x))) {
      N * xn = nx(x);
      N * yn = nx(y);
      nx(x) = y;
      nx(y) = xn;
      y = yn;
    }
    x = nx(x);
  }
  if (y) {
    nx(x) = y;
  }
  return first;
}

// THIS is SLOWER!!!
//  and even slower when condational move is used!!!!
template <class N, class LT, class NX>
static inline N * merge1(N * x, N * y, const LT & lt, const NX & nx)
{
  N * * cur = lt(x,y) ? &x : &y;
  N * first = *cur;
  N * last = *cur;
  *cur = nx(*cur);
  while (x && y) {
    cur = lt(x,y) ? &x : &y;
    nx(last) = *cur;
    last = *cur;
    *cur = nx(*cur);
  }
  if (x) {nx(last) = x;}
  else if (y) {nx(last) = y;}
  return first;
}

template <class N, class LT>
static inline N * merge(N * x, N * y, const LT & lt)
{
  return sort(x, y, lt, Next<N>());
}

template <class N>
static inline N * merge(N * x, N * y)
{
  return sort(x, y, Less<N>(), Next<N>());
}

template <class N, class LT, class NX>
N * sort(N * first, const LT & lt, const NX & nx)
{
  if (!first) return first;

  N * carry = 0;
  N * counter[sizeof(void *)*8] = {0};
  int fill = 0;

  while (first) {

    N * tmp = nx(first);
    nx(first) = carry;
    carry = first;
    first = tmp;

    int i = 0;
    while (i < fill && counter[i]) {
      carry = merge(counter[i], carry, lt, nx);
      counter[i] = 0;
      ++i;
    }

    swap(carry, counter[i]);

    if (i == fill) {
      ++fill;
    }
  }

  for (int i = 1; i < fill; ++i) {
    if (!counter[i]) counter[i] = counter[i-1];
    else if (counter[i-1]) counter[i] = merge(counter[i], counter[i-1], lt, nx);
  }

  return counter[fill-1];
}

template <class N, class LT>
static inline N * sort(N * first, const LT & lt)
{
  return sort(first, lt, Next<N>());
}

template <class N>
static inline N * sort(N * first)
{
  return sort(first, Less<N>(), Next<N>());
}


template <class N>
static inline N * fix_links(N * cur)
{
  N * prev = 0;
  while (cur) {
    cur->prev = prev;
    prev = cur;
    cur = cur->next;
  }
}

}

#endif