Coverage Report

Created: 2026-08-13 06:28

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
29.3M
  N * & operator() (N * n) const {return n->next;}
acommon::Next<aspeller::PfxEntry>::operator()(aspeller::PfxEntry*) const
Line
Count
Source
38
29.3k
  N * & operator() (N * n) const {return n->next;}
acommon::Next<aspeller::SfxEntry>::operator()(aspeller::SfxEntry*) const
Line
Count
Source
38
29.3M
  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
566k
{
49
566k
  if (lt(y,x)) swap(x,y);
50
566k
  N * first = x;
51
6.07M
  while (nx(x) && y) {
52
5.51M
    if (lt(y,nx(x))) {
53
2.66M
      N * xn = nx(x);
54
2.66M
      N * yn = nx(y);
55
2.66M
      nx(x) = y;
56
2.66M
      nx(y) = xn;
57
2.66M
      y = yn;
58
2.66M
    }
59
5.51M
    x = nx(x);
60
5.51M
  }
61
566k
  if (y) {
62
461k
    nx(x) = y;
63
461k
  }
64
566k
  return first;
65
566k
}
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.86k
{
49
2.86k
  if (lt(y,x)) swap(x,y);
50
2.86k
  N * first = x;
51
6.18k
  while (nx(x) && y) {
52
3.31k
    if (lt(y,nx(x))) {
53
1.59k
      N * xn = nx(x);
54
1.59k
      N * yn = nx(y);
55
1.59k
      nx(x) = y;
56
1.59k
      nx(y) = xn;
57
1.59k
      y = yn;
58
1.59k
    }
59
3.31k
    x = nx(x);
60
3.31k
  }
61
2.86k
  if (y) {
62
2.42k
    nx(x) = y;
63
2.42k
  }
64
2.86k
  return first;
65
2.86k
}
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
563k
{
49
563k
  if (lt(y,x)) swap(x,y);
50
563k
  N * first = x;
51
6.07M
  while (nx(x) && y) {
52
5.50M
    if (lt(y,nx(x))) {
53
2.65M
      N * xn = nx(x);
54
2.65M
      N * yn = nx(y);
55
2.65M
      nx(x) = y;
56
2.65M
      nx(y) = xn;
57
2.65M
      y = yn;
58
2.65M
    }
59
5.50M
    x = nx(x);
60
5.50M
  }
61
563k
  if (y) {
62
459k
    nx(x) = y;
63
459k
  }
64
563k
  return first;
65
563k
}
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
7.52k
{
102
7.52k
  if (!first) return first;
103
104
7.52k
  N * carry = 0;
105
7.52k
  N * counter[sizeof(void *)*8] = {0};
106
7.52k
  int fill = 0;
107
108
581k
  while (first) {
109
110
573k
    N * tmp = nx(first);
111
573k
    nx(first) = carry;
112
573k
    carry = first;
113
573k
    first = tmp;
114
115
573k
    int i = 0;
116
1.13M
    while (i < fill && counter[i]) {
117
561k
      carry = merge(counter[i], carry, lt, nx);
118
561k
      counter[i] = 0;
119
561k
      ++i;
120
561k
    }
121
122
573k
    swap(carry, counter[i]);
123
124
573k
    if (i == fill) {
125
22.4k
      ++fill;
126
22.4k
    }
127
573k
  }
128
129
22.4k
  for (int i = 1; i < fill; ++i) {
130
14.9k
    if (!counter[i]) counter[i] = counter[i-1];
131
8.63k
    else if (counter[i-1]) counter[i] = merge(counter[i], counter[i-1], lt, nx);
132
14.9k
  }
133
134
7.52k
  return counter[fill-1];
135
7.52k
}
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
997
{
102
997
  if (!first) return first;
103
104
997
  N * carry = 0;
105
997
  N * counter[sizeof(void *)*8] = {0};
106
997
  int fill = 0;
107
108
4.86k
  while (first) {
109
110
3.86k
    N * tmp = nx(first);
111
3.86k
    nx(first) = carry;
112
3.86k
    carry = first;
113
3.86k
    first = tmp;
114
115
3.86k
    int i = 0;
116
6.47k
    while (i < fill && counter[i]) {
117
2.61k
      carry = merge(counter[i], carry, lt, nx);
118
2.61k
      counter[i] = 0;
119
2.61k
      ++i;
120
2.61k
    }
121
122
3.86k
    swap(carry, counter[i]);
123
124
3.86k
    if (i == fill) {
125
2.39k
      ++fill;
126
2.39k
    }
127
3.86k
  }
128
129
2.39k
  for (int i = 1; i < fill; ++i) {
130
1.39k
    if (!counter[i]) counter[i] = counter[i-1];
131
1.14k
    else if (counter[i-1]) counter[i] = merge(counter[i], counter[i-1], lt, nx);
132
1.39k
  }
133
134
997
  return counter[fill-1];
135
997
}
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.52k
{
102
6.52k
  if (!first) return first;
103
104
6.52k
  N * carry = 0;
105
6.52k
  N * counter[sizeof(void *)*8] = {0};
106
6.52k
  int fill = 0;
107
108
576k
  while (first) {
109
110
569k
    N * tmp = nx(first);
111
569k
    nx(first) = carry;
112
569k
    carry = first;
113
569k
    first = tmp;
114
115
569k
    int i = 0;
116
1.12M
    while (i < fill && counter[i]) {
117
559k
      carry = merge(counter[i], carry, lt, nx);
118
559k
      counter[i] = 0;
119
559k
      ++i;
120
559k
    }
121
122
569k
    swap(carry, counter[i]);
123
124
569k
    if (i == fill) {
125
20.0k
      ++fill;
126
20.0k
    }
127
569k
  }
128
129
20.0k
  for (int i = 1; i < fill; ++i) {
130
13.5k
    if (!counter[i]) counter[i] = counter[i-1];
131
7.49k
    else if (counter[i-1]) counter[i] = merge(counter[i], counter[i-1], lt, nx);
132
13.5k
  }
133
134
6.52k
  return counter[fill-1];
135
6.52k
}
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
7.52k
{
140
7.52k
  return sort(first, lt, Next<N>());
141
7.52k
}
affix.cpp:aspeller::PfxEntry* acommon::sort<aspeller::PfxEntry, aspeller::AffixLess<aspeller::PfxEntry> >(aspeller::PfxEntry*, aspeller::AffixLess<aspeller::PfxEntry> const&)
Line
Count
Source
139
997
{
140
997
  return sort(first, lt, Next<N>());
141
997
}
affix.cpp:aspeller::SfxEntry* acommon::sort<aspeller::SfxEntry, aspeller::AffixLess<aspeller::SfxEntry> >(aspeller::SfxEntry*, aspeller::AffixLess<aspeller::SfxEntry> const&)
Line
Count
Source
139
6.52k
{
140
6.52k
  return sort(first, lt, Next<N>());
141
6.52k
}
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