Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/gl/lib/heap.c |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | static int heap_default_compare (void const *, void const *); | - | ||||||||||||
4 | static size_t heapify_down (void **, size_t, size_t, | - | ||||||||||||
5 | int (*) (void const *, void const *)); | - | ||||||||||||
6 | static void heapify_up (void **, size_t, | - | ||||||||||||
7 | int (*) (void const *, void const *)); | - | ||||||||||||
8 | - | |||||||||||||
9 | struct heap | - | ||||||||||||
10 | { | - | ||||||||||||
11 | void **array; | - | ||||||||||||
12 | size_t capacity; | - | ||||||||||||
13 | size_t count; | - | ||||||||||||
14 | int (*compare) (void const *, void const *); | - | ||||||||||||
15 | }; | - | ||||||||||||
16 | - | |||||||||||||
17 | - | |||||||||||||
18 | - | |||||||||||||
19 | struct heap * | - | ||||||||||||
20 | heap_alloc (int (*compare) (void const *, void const *), size_t n_reserve) | - | ||||||||||||
21 | { | - | ||||||||||||
22 | struct heap *heap = xmalloc (sizeof *heap); | - | ||||||||||||
23 | - | |||||||||||||
24 | if (n_reserve == 0
| 0-2402 | ||||||||||||
25 | n_reserve = 1; never executed: n_reserve = 1; | 0 | ||||||||||||
26 | - | |||||||||||||
27 | heap->array = xnmalloc (n_reserve, sizeof *(heap->array)); | - | ||||||||||||
28 | - | |||||||||||||
29 | heap->array[0] = | - | ||||||||||||
30 | ((void *)0) | - | ||||||||||||
31 | ; | - | ||||||||||||
32 | heap->capacity = n_reserve; | - | ||||||||||||
33 | heap->count = 0; | - | ||||||||||||
34 | heap->compare = compare
| 0-2402 | ||||||||||||
35 | - | |||||||||||||
36 | return executed 2402 times by 1 test: heap;return heap; Executed by:
executed 2402 times by 1 test: return heap; Executed by:
| 2402 | ||||||||||||
37 | } | - | ||||||||||||
38 | - | |||||||||||||
39 | - | |||||||||||||
40 | static int | - | ||||||||||||
41 | heap_default_compare (void const *a, void const *b) | - | ||||||||||||
42 | { | - | ||||||||||||
43 | return never executed: 0;return 0; never executed: return 0; | 0 | ||||||||||||
44 | } | - | ||||||||||||
45 | - | |||||||||||||
46 | - | |||||||||||||
47 | void | - | ||||||||||||
48 | heap_free (struct heap *heap) | - | ||||||||||||
49 | { | - | ||||||||||||
50 | free (heap->array); | - | ||||||||||||
51 | free (heap); | - | ||||||||||||
52 | } never executed: end of block | 0 | ||||||||||||
53 | - | |||||||||||||
54 | - | |||||||||||||
55 | - | |||||||||||||
56 | int | - | ||||||||||||
57 | heap_insert (struct heap *heap, void *item) | - | ||||||||||||
58 | { | - | ||||||||||||
59 | if (heap->capacity - 1 <= heap->count
| 0-25192 | ||||||||||||
60 | heap->array = x2nrealloc (heap->array, &heap->capacity, never executed: heap->array = x2nrealloc (heap->array, &heap->capacity, sizeof *(heap->array)); | 0 | ||||||||||||
61 | sizeof *(heap->array)); never executed: heap->array = x2nrealloc (heap->array, &heap->capacity, sizeof *(heap->array)); | 0 | ||||||||||||
62 | - | |||||||||||||
63 | heap->array[++heap->count] = item; | - | ||||||||||||
64 | heapify_up (heap->array, heap->count, heap->compare); | - | ||||||||||||
65 | - | |||||||||||||
66 | return executed 25192 times by 1 test: 0;return 0; Executed by:
executed 25192 times by 1 test: return 0; Executed by:
| 25192 | ||||||||||||
67 | } | - | ||||||||||||
68 | - | |||||||||||||
69 | - | |||||||||||||
70 | - | |||||||||||||
71 | void * | - | ||||||||||||
72 | heap_remove_top (struct heap *heap) | - | ||||||||||||
73 | { | - | ||||||||||||
74 | void *top; | - | ||||||||||||
75 | - | |||||||||||||
76 | if (heap->count == 0
| 0-22790 | ||||||||||||
77 | return never executed: return ((void *)0) ; never executed: return ((void *)0) ; | 0 | ||||||||||||
78 | ((void *)0) never executed: return ((void *)0) ; | 0 | ||||||||||||
79 | ; never executed: return ((void *)0) ; | 0 | ||||||||||||
80 | - | |||||||||||||
81 | top = heap->array[1]; | - | ||||||||||||
82 | heap->array[1] = heap->array[heap->count--]; | - | ||||||||||||
83 | heapify_down (heap->array, heap->count, 1, heap->compare); | - | ||||||||||||
84 | - | |||||||||||||
85 | return executed 22790 times by 1 test: top;return top; Executed by:
executed 22790 times by 1 test: return top; Executed by:
| 22790 | ||||||||||||
86 | } | - | ||||||||||||
87 | - | |||||||||||||
88 | - | |||||||||||||
89 | - | |||||||||||||
90 | static size_t | - | ||||||||||||
91 | heapify_down (void **array, size_t count, size_t initial, | - | ||||||||||||
92 | int (*compare) (void const *, void const *)) | - | ||||||||||||
93 | { | - | ||||||||||||
94 | void *element = array[initial]; | - | ||||||||||||
95 | - | |||||||||||||
96 | size_t parent = initial; | - | ||||||||||||
97 | while (parent <= count / 2
| 0-22790 | ||||||||||||
98 | { | - | ||||||||||||
99 | size_t child = 2 * parent; | - | ||||||||||||
100 | - | |||||||||||||
101 | if (child < count
| 0 | ||||||||||||
102 | child++; never executed: child++; | 0 | ||||||||||||
103 | - | |||||||||||||
104 | if (compare (array[child], element) <= 0
| 0 | ||||||||||||
105 | break; never executed: break; | 0 | ||||||||||||
106 | - | |||||||||||||
107 | array[parent] = array[child]; | - | ||||||||||||
108 | parent = child; | - | ||||||||||||
109 | } never executed: end of block | 0 | ||||||||||||
110 | - | |||||||||||||
111 | array[parent] = element; | - | ||||||||||||
112 | return executed 22790 times by 1 test: parent;return parent; Executed by:
executed 22790 times by 1 test: return parent; Executed by:
| 22790 | ||||||||||||
113 | } | - | ||||||||||||
114 | - | |||||||||||||
115 | - | |||||||||||||
116 | - | |||||||||||||
117 | static void | - | ||||||||||||
118 | heapify_up (void **array, size_t count, | - | ||||||||||||
119 | int (*compare) (void const *, void const *)) | - | ||||||||||||
120 | { | - | ||||||||||||
121 | size_t k = count; | - | ||||||||||||
122 | void *new_element = array[k]; | - | ||||||||||||
123 | - | |||||||||||||
124 | while (k != 1
| 0-25192 | ||||||||||||
125 | { | - | ||||||||||||
126 | array[k] = array[k/2]; | - | ||||||||||||
127 | k /= 2; | - | ||||||||||||
128 | } never executed: end of block | 0 | ||||||||||||
129 | - | |||||||||||||
130 | array[k] = new_element; | - | ||||||||||||
131 | } executed 25192 times by 1 test: end of block Executed by:
| 25192 | ||||||||||||
Switch to Source code | Preprocessed file |