Coverage Report

Created: 2026-08-31 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aspell/common/lsort.hpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2004
3
 * Kevin Atkinson
4
 *
5
 * Permission to use, copy, modify, distribute and sell this software
6
 * and its documentation for any purpose is hereby granted without
7
 * fee, provided that the above copyright notice appear in all copies
8
 * and that both that copyright notice and this permission notice
9
 * appear in supporting documentation. I make no representations about
10
 * the suitability of this software for any purpose.  It is provided
11
 * "as is" without express or implied warranty.
12
 *
13
 * This code was originally adopted from the slist implementation
14
 * found in the SGI STL under the following copyright:
15
 *
16
 * Copyright (c) 1997
17
 * Silicon Graphics Computer Systems, Inc.
18
 *
19
 * Permission to use, copy, modify, distribute and sell this software
20
 * and its documentation for any purpose is hereby granted without fee,
21
 * provided that the above copyright notice appear in all copies and
22
 * that both that copyright notice and this permission notice appear
23
 * in supporting documentation.  Silicon Graphics makes no
24
 * representations about the suitability of this software for any
25
 * purpose.  It is provided "as is" without express or implied warranty.
26
 *
27
 */
28
29
#ifndef ACOMMON_LSORT__HPP
30
#define ACOMMON_LSORT__HPP
31
32
namespace acommon {
33
34
using std::swap;
35
36
template <class N>
37
struct Next {
38
25.1M
  N * & operator() (N * n) const {return n->next;}
acommon::Next<aspeller::PfxEntry>::operator()(aspeller::PfxEntry*) const
Line
Count
Source
38
25.4k
  N * & operator() (N * n) const {return n->next;}
acommon::Next<aspeller::SfxEntry>::operator()(aspeller::SfxEntry*) const
Line
Count
Source
38
25.1M
  N * & operator() (N * n) const {return n->next;}
Unexecuted instantiation: readonly_ws.cpp:acommon::Next<(anonymous namespace)::WordData>::operator()((anonymous namespace)::WordData*) const
39
};
40
template <class N>
41
struct Less {
42
  bool operator() (N * x, N * y) const {return x->data < y->data;}
43
};
44
45
46
template <class N, class LT, class NX>
47
static inline N * merge(N * x, N * y, const LT & lt, const NX & nx)
48
487k
{
49
487k
  if (lt(y,x)) swap(x,y);
50
487k
  N * first = x;
51
5.21M
  while (nx(x) && y) {
52
4.72M
    if (lt(y,nx(x))) {
53
2.28M
      N * xn = nx(x);
54
2.28M
      N * yn = nx(y);
55
2.28M
      nx(x) = y;
56
2.28M
      nx(y) = xn;
57
2.28M
      y = yn;
58
2.28M
    }
59
4.72M
    x = nx(x);
60
4.72M
  }
61
487k
  if (y) {
62
397k
    nx(x) = y;
63
397k
  }
64
487k
  return first;
65
487k
}
affix.cpp:aspeller::PfxEntry* acommon::merge<aspeller::PfxEntry, aspeller::AffixLess<aspeller::PfxEntry>, acommon::Next<aspeller::PfxEntry> >(aspeller::PfxEntry*, aspeller::PfxEntry*, aspeller::AffixLess<aspeller::PfxEntry> const&, acommon::Next<aspeller::PfxEntry> const&)
Line
Count
Source
48
2.50k
{
49
2.50k
  if (lt(y,x)) swap(x,y);
50
2.50k
  N * first = x;
51
5.35k
  while (nx(x) && y) {
52
2.84k
    if (lt(y,nx(x))) {
53
1.36k
      N * xn = nx(x);
54
1.36k
      N * yn = nx(y);
55
1.36k
      nx(x) = y;
56
1.36k
      nx(y) = xn;
57
1.36k
      y = yn;
58
1.36k
    }
59
2.84k
    x = nx(x);
60
2.84k
  }
61
2.50k
  if (y) {
62
2.13k
    nx(x) = y;
63
2.13k
  }
64
2.50k
  return first;
65
2.50k
}
affix.cpp:aspeller::SfxEntry* acommon::merge<aspeller::SfxEntry, aspeller::AffixLess<aspeller::SfxEntry>, acommon::Next<aspeller::SfxEntry> >(aspeller::SfxEntry*, aspeller::SfxEntry*, aspeller::AffixLess<aspeller::SfxEntry> const&, acommon::Next<aspeller::SfxEntry> const&)
Line
Count
Source
48
484k
{
49
484k
  if (lt(y,x)) swap(x,y);
50
484k
  N * first = x;
51
5.20M
  while (nx(x) && y) {
52
4.72M
    if (lt(y,nx(x))) {
53
2.28M
      N * xn = nx(x);
54
2.28M
      N * yn = nx(y);
55
2.28M
      nx(x) = y;
56
2.28M
      nx(y) = xn;
57
2.28M
      y = yn;
58
2.28M
    }
59
4.72M
    x = nx(x);
60
4.72M
  }
61
484k
  if (y) {
62
395k
    nx(x) = y;
63
395k
  }
64
484k
  return first;
65
484k
}
Unexecuted instantiation: readonly_ws.cpp:(anonymous namespace)::WordData* acommon::merge<(anonymous namespace)::WordData, (anonymous namespace)::SoundslikeLess, acommon::Next<(anonymous namespace)::WordData> >((anonymous namespace)::WordData*, (anonymous namespace)::WordData*, (anonymous namespace)::SoundslikeLess const&, acommon::Next<(anonymous namespace)::WordData> const&)
66
67
// THIS is SLOWER!!!
68
//  and even slower when condational move is used!!!!
69
template <class N, class LT, class NX>
70
static inline N * merge1(N * x, N * y, const LT & lt, const NX & nx)
71
{
72
  N * * cur = lt(x,y) ? &x : &y;
73
  N * first = *cur;
74
  N * last = *cur;
75
  *cur = nx(*cur);
76
  while (x && y) {
77
    cur = lt(x,y) ? &x : &y;
78
    nx(last) = *cur;
79
    last = *cur;
80
    *cur = nx(*cur);
81
  }
82
  if (x) {nx(last) = x;}
83
  else if (y) {nx(last) = y;}
84
  return first;
85
}
86
87
template <class N, class LT>
88
static inline N * merge(N * x, N * y, const LT & lt)
89
{
90
  return sort(x, y, lt, Next<N>());
91
}
92
93
template <class N>
94
static inline N * merge(N * x, N * y)
95
{
96
  return sort(x, y, Less<N>(), Next<N>());
97
}
98
99
template <class N, class LT, class NX>
100
N * sort(N * first, const LT & lt, const NX & nx)
101
6.92k
{
102
6.92k
  if (!first) return first;
103
104
6.92k
  N * carry = 0;
105
6.92k
  N * counter[sizeof(void *)*8] = {0};
106
6.92k
  int fill = 0;
107
108
500k
  while (first) {
109
110
494k
    N * tmp = nx(first);
111
494k
    nx(first) = carry;
112
494k
    carry = first;
113
494k
    first = tmp;
114
115
494k
    int i = 0;
116
977k
    while (i < fill && counter[i]) {
117
483k
      carry = merge(counter[i], carry, lt, nx);
118
483k
      counter[i] = 0;
119
483k
      ++i;
120
483k
    }
121
122
494k
    swap(carry, counter[i]);
123
124
494k
    if (i == fill) {
125
20.5k
      ++fill;
126
20.5k
    }
127
494k
  }
128
129
20.5k
  for (int i = 1; i < fill; ++i) {
130
13.6k
    if (!counter[i]) counter[i] = counter[i-1];
131
7.87k
    else if (counter[i-1]) counter[i] = merge(counter[i], counter[i-1], lt, nx);
132
13.6k
  }
133
134
6.92k
  return counter[fill-1];
135
6.92k
}
aspeller::PfxEntry* acommon::sort<aspeller::PfxEntry, aspeller::AffixLess<aspeller::PfxEntry>, acommon::Next<aspeller::PfxEntry> >(aspeller::PfxEntry*, aspeller::AffixLess<aspeller::PfxEntry> const&, acommon::Next<aspeller::PfxEntry> const&)
Line
Count
Source
101
907
{
102
907
  if (!first) return first;
103
104
907
  N * carry = 0;
105
907
  N * counter[sizeof(void *)*8] = {0};
106
907
  int fill = 0;
107
108
4.32k
  while (first) {
109
110
3.41k
    N * tmp = nx(first);
111
3.41k
    nx(first) = carry;
112
3.41k
    carry = first;
113
3.41k
    first = tmp;
114
115
3.41k
    int i = 0;
116
5.70k
    while (i < fill && counter[i]) {
117
2.29k
      carry = merge(counter[i], carry, lt, nx);
118
2.29k
      counter[i] = 0;
119
2.29k
      ++i;
120
2.29k
    }
121
122
3.41k
    swap(carry, counter[i]);
123
124
3.41k
    if (i == fill) {
125
2.15k
      ++fill;
126
2.15k
    }
127
3.41k
  }
128
129
2.15k
  for (int i = 1; i < fill; ++i) {
130
1.24k
    if (!counter[i]) counter[i] = counter[i-1];
131
1.03k
    else if (counter[i-1]) counter[i] = merge(counter[i], counter[i-1], lt, nx);
132
1.24k
  }
133
134
907
  return counter[fill-1];
135
907
}
aspeller::SfxEntry* acommon::sort<aspeller::SfxEntry, aspeller::AffixLess<aspeller::SfxEntry>, acommon::Next<aspeller::SfxEntry> >(aspeller::SfxEntry*, aspeller::AffixLess<aspeller::SfxEntry> const&, acommon::Next<aspeller::SfxEntry> const&)
Line
Count
Source
101
6.01k
{
102
6.01k
  if (!first) return first;
103
104
6.01k
  N * carry = 0;
105
6.01k
  N * counter[sizeof(void *)*8] = {0};
106
6.01k
  int fill = 0;
107
108
496k
  while (first) {
109
110
490k
    N * tmp = nx(first);
111
490k
    nx(first) = carry;
112
490k
    carry = first;
113
490k
    first = tmp;
114
115
490k
    int i = 0;
116
971k
    while (i < fill && counter[i]) {
117
480k
      carry = merge(counter[i], carry, lt, nx);
118
480k
      counter[i] = 0;
119
480k
      ++i;
120
480k
    }
121
122
490k
    swap(carry, counter[i]);
123
124
490k
    if (i == fill) {
125
18.4k
      ++fill;
126
18.4k
    }
127
490k
  }
128
129
18.4k
  for (int i = 1; i < fill; ++i) {
130
12.3k
    if (!counter[i]) counter[i] = counter[i-1];
131
6.84k
    else if (counter[i-1]) counter[i] = merge(counter[i], counter[i-1], lt, nx);
132
12.3k
  }
133
134
6.01k
  return counter[fill-1];
135
6.01k
}
Unexecuted instantiation: readonly_ws.cpp:(anonymous namespace)::WordData* acommon::sort<(anonymous namespace)::WordData, (anonymous namespace)::SoundslikeLess, acommon::Next<(anonymous namespace)::WordData> >((anonymous namespace)::WordData*, (anonymous namespace)::SoundslikeLess const&, acommon::Next<(anonymous namespace)::WordData> const&)
136
137
template <class N, class LT>
138
static inline N * sort(N * first, const LT & lt)
139
6.92k
{
140
6.92k
  return sort(first, lt, Next<N>());
141
6.92k
}
affix.cpp:aspeller::PfxEntry* acommon::sort<aspeller::PfxEntry, aspeller::AffixLess<aspeller::PfxEntry> >(aspeller::PfxEntry*, aspeller::AffixLess<aspeller::PfxEntry> const&)
Line
Count
Source
139
907
{
140
907
  return sort(first, lt, Next<N>());
141
907
}
affix.cpp:aspeller::SfxEntry* acommon::sort<aspeller::SfxEntry, aspeller::AffixLess<aspeller::SfxEntry> >(aspeller::SfxEntry*, aspeller::AffixLess<aspeller::SfxEntry> const&)
Line
Count
Source
139
6.01k
{
140
6.01k
  return sort(first, lt, Next<N>());
141
6.01k
}
Unexecuted instantiation: readonly_ws.cpp:(anonymous namespace)::WordData* acommon::sort<(anonymous namespace)::WordData, (anonymous namespace)::SoundslikeLess>((anonymous namespace)::WordData*, (anonymous namespace)::SoundslikeLess const&)
142
143
template <class N>
144
static inline N * sort(N * first)
145
{
146
  return sort(first, Less<N>(), Next<N>());
147
}
148
149
150
template <class N>
151
static inline N * fix_links(N * cur)
152
{
153
  N * prev = 0;
154
  while (cur) {
155
    cur->prev = prev;
156
    prev = cur;
157
    cur = cur->next;
158
  }
159
}
160
161
}
162
163
#endif
164