Coverage Report

Created: 2025-07-18 06:07

/src/openvswitch/lib/sort.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2009 Nicira, Inc.
2
 *
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
 * you may not use this file except in compliance with the License.
5
 * You may obtain a copy of the License at:
6
 *
7
 *     http://www.apache.org/licenses/LICENSE-2.0
8
 *
9
 * Unless required by applicable law or agreed to in writing, software
10
 * distributed under the License is distributed on an "AS IS" BASIS,
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
 * See the License for the specific language governing permissions and
13
 * limitations under the License.
14
 */
15
16
#include <config.h>
17
18
#include "sort.h"
19
20
#include "random.h"
21
22
static size_t
23
partition(size_t p, size_t r,
24
          int (*compare)(size_t a, size_t b, void *aux),
25
          void (*swap)(size_t a, size_t b, void *aux),
26
          void *aux)
27
0
{
28
0
    size_t x = r - 1;
29
0
    size_t i, j;
30
31
0
    i = p;
32
0
    for (j = p; j < x; j++) {
33
0
        if (compare(j, x, aux) <= 0) {
34
0
            swap(i++, j, aux);
35
0
        }
36
0
    }
37
0
    swap(i, x, aux);
38
0
    return i;
39
0
}
40
41
static void
42
quicksort(size_t p, size_t r,
43
          int (*compare)(size_t a, size_t b, void *aux),
44
          void (*swap)(size_t a, size_t b, void *aux),
45
          void *aux)
46
0
{
47
0
    size_t i, q;
48
49
0
    if (r - p < 2) {
50
0
        return;
51
0
    }
52
53
0
    i = random_range(r - p) + p;
54
0
    if (r - 1 != i) {
55
0
        swap(r - 1, i, aux);
56
0
    }
57
58
0
    q = partition(p, r, compare, swap, aux);
59
0
    quicksort(p, q, compare, swap, aux);
60
0
    quicksort(q, r, compare, swap, aux);
61
0
}
62
63
void
64
sort(size_t count,
65
     int (*compare)(size_t a, size_t b, void *aux),
66
     void (*swap)(size_t a, size_t b, void *aux),
67
     void *aux)
68
0
{
69
0
    quicksort(0, count, compare, swap, aux);
70
0
}