| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/coreutils/src/gl/lib/randperm.c |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||||||||||||||||||||
| 2 | - | |||||||||||||||||||||||||
| 3 | - | |||||||||||||||||||||||||
| 4 | - | |||||||||||||||||||||||||
| 5 | - | |||||||||||||||||||||||||
| 6 | - | |||||||||||||||||||||||||
| 7 | - | |||||||||||||||||||||||||
| 8 | - | |||||||||||||||||||||||||
| 9 | static size_t __attribute__ ((__const__)) | - | ||||||||||||||||||||||||
| 10 | ceil_lg (size_t n) | - | ||||||||||||||||||||||||
| 11 | { | - | ||||||||||||||||||||||||
| 12 | size_t b = 0; | - | ||||||||||||||||||||||||
| 13 | for (n--; n != 0
| 13-172 | ||||||||||||||||||||||||
| 14 | b++; executed 172 times by 1 test: b++;Executed by:
| 172 | ||||||||||||||||||||||||
| 15 | return executed 13 times by 1 test: b;return b;Executed by:
executed 13 times by 1 test: return b;Executed by:
| 13 | ||||||||||||||||||||||||
| 16 | } | - | ||||||||||||||||||||||||
| 17 | - | |||||||||||||||||||||||||
| 18 | - | |||||||||||||||||||||||||
| 19 | - | |||||||||||||||||||||||||
| 20 | - | |||||||||||||||||||||||||
| 21 | - | |||||||||||||||||||||||||
| 22 | size_t | - | ||||||||||||||||||||||||
| 23 | randperm_bound (size_t h, size_t n) | - | ||||||||||||||||||||||||
| 24 | { | - | ||||||||||||||||||||||||
| 25 | - | |||||||||||||||||||||||||
| 26 | - | |||||||||||||||||||||||||
| 27 | size_t lg_n = ceil_lg (n); | - | ||||||||||||||||||||||||
| 28 | - | |||||||||||||||||||||||||
| 29 | - | |||||||||||||||||||||||||
| 30 | size_t ar = lg_n * h; | - | ||||||||||||||||||||||||
| 31 | - | |||||||||||||||||||||||||
| 32 | - | |||||||||||||||||||||||||
| 33 | size_t bound = (ar + 8 - 1) / 8; | - | ||||||||||||||||||||||||
| 34 | - | |||||||||||||||||||||||||
| 35 | return executed 13 times by 1 test: bound;return bound;Executed by:
executed 13 times by 1 test: return bound;Executed by:
| 13 | ||||||||||||||||||||||||
| 36 | } | - | ||||||||||||||||||||||||
| 37 | - | |||||||||||||||||||||||||
| 38 | - | |||||||||||||||||||||||||
| 39 | - | |||||||||||||||||||||||||
| 40 | static void | - | ||||||||||||||||||||||||
| 41 | swap (size_t *v, size_t i, size_t j) | - | ||||||||||||||||||||||||
| 42 | { | - | ||||||||||||||||||||||||
| 43 | size_t t = v[i]; | - | ||||||||||||||||||||||||
| 44 | v[i] = v[j]; | - | ||||||||||||||||||||||||
| 45 | v[j] = t; | - | ||||||||||||||||||||||||
| 46 | } executed 2104 times by 1 test: end of blockExecuted by:
| 2104 | ||||||||||||||||||||||||
| 47 | - | |||||||||||||||||||||||||
| 48 | - | |||||||||||||||||||||||||
| 49 | - | |||||||||||||||||||||||||
| 50 | - | |||||||||||||||||||||||||
| 51 | - | |||||||||||||||||||||||||
| 52 | - | |||||||||||||||||||||||||
| 53 | struct sparse_ent_ | - | ||||||||||||||||||||||||
| 54 | { | - | ||||||||||||||||||||||||
| 55 | size_t index; | - | ||||||||||||||||||||||||
| 56 | size_t val; | - | ||||||||||||||||||||||||
| 57 | }; | - | ||||||||||||||||||||||||
| 58 | - | |||||||||||||||||||||||||
| 59 | static size_t | - | ||||||||||||||||||||||||
| 60 | sparse_hash_ (void const *x, size_t table_size) | - | ||||||||||||||||||||||||
| 61 | { | - | ||||||||||||||||||||||||
| 62 | struct sparse_ent_ const *ent = x; | - | ||||||||||||||||||||||||
| 63 | return executed 8 times by 1 test: ent->index % table_size;return ent->index % table_size;Executed by:
executed 8 times by 1 test: return ent->index % table_size;Executed by:
| 8 | ||||||||||||||||||||||||
| 64 | } | - | ||||||||||||||||||||||||
| 65 | - | |||||||||||||||||||||||||
| 66 | static | - | ||||||||||||||||||||||||
| 67 | _Bool | - | ||||||||||||||||||||||||
| 68 | - | |||||||||||||||||||||||||
| 69 | sparse_cmp_ (void const *x, void const *y) | - | ||||||||||||||||||||||||
| 70 | { | - | ||||||||||||||||||||||||
| 71 | struct sparse_ent_ const *ent1 = x; | - | ||||||||||||||||||||||||
| 72 | struct sparse_ent_ const *ent2 = y; | - | ||||||||||||||||||||||||
| 73 | return executed 2 times by 1 test: ent1->index == ent2->index;return ent1->index == ent2->index;Executed by:
executed 2 times by 1 test: return ent1->index == ent2->index;Executed by:
| 2 | ||||||||||||||||||||||||
| 74 | } | - | ||||||||||||||||||||||||
| 75 | - | |||||||||||||||||||||||||
| 76 | typedef Hash_table sparse_map; | - | ||||||||||||||||||||||||
| 77 | - | |||||||||||||||||||||||||
| 78 | - | |||||||||||||||||||||||||
| 79 | - | |||||||||||||||||||||||||
| 80 | - | |||||||||||||||||||||||||
| 81 | - | |||||||||||||||||||||||||
| 82 | static sparse_map * | - | ||||||||||||||||||||||||
| 83 | sparse_new (size_t size_hint) | - | ||||||||||||||||||||||||
| 84 | { | - | ||||||||||||||||||||||||
| 85 | return executed 1 time by 1 test: hash_initialize (size_hint, return hash_initialize (size_hint, ((void *)0) , sparse_hash_, sparse_cmp_, free);Executed by:
executed 1 time by 1 test: return hash_initialize (size_hint, ((void *)0) , sparse_hash_, sparse_cmp_, free);Executed by:
| 1 | ||||||||||||||||||||||||
| 86 | ((void *)0) executed 1 time by 1 test: return hash_initialize (size_hint, ((void *)0) , sparse_hash_, sparse_cmp_, free);Executed by:
| 1 | ||||||||||||||||||||||||
| 87 | , sparse_hash_, sparse_cmp_, free); executed 1 time by 1 test: return hash_initialize (size_hint, ((void *)0) , sparse_hash_, sparse_cmp_, free);Executed by:
| 1 | ||||||||||||||||||||||||
| 88 | } | - | ||||||||||||||||||||||||
| 89 | - | |||||||||||||||||||||||||
| 90 | - | |||||||||||||||||||||||||
| 91 | - | |||||||||||||||||||||||||
| 92 | - | |||||||||||||||||||||||||
| 93 | - | |||||||||||||||||||||||||
| 94 | static void | - | ||||||||||||||||||||||||
| 95 | sparse_swap (sparse_map *sv, size_t* v, size_t i, size_t j) | - | ||||||||||||||||||||||||
| 96 | { | - | ||||||||||||||||||||||||
| 97 | struct sparse_ent_ *v1 = hash_delete (sv, &(struct sparse_ent_) {i,0}); | - | ||||||||||||||||||||||||
| 98 | struct sparse_ent_ *v2 = hash_delete (sv, &(struct sparse_ent_) {j,0}); | - | ||||||||||||||||||||||||
| 99 | - | |||||||||||||||||||||||||
| 100 | - | |||||||||||||||||||||||||
| 101 | if (!v1
| 0-2 | ||||||||||||||||||||||||
| 102 | { | - | ||||||||||||||||||||||||
| 103 | v1 = xmalloc (sizeof *v1); | - | ||||||||||||||||||||||||
| 104 | v1->index = v1->val = i; | - | ||||||||||||||||||||||||
| 105 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 106 | if (!v2
| 0-2 | ||||||||||||||||||||||||
| 107 | { | - | ||||||||||||||||||||||||
| 108 | v2 = xmalloc (sizeof *v2); | - | ||||||||||||||||||||||||
| 109 | v2->index = v2->val = j; | - | ||||||||||||||||||||||||
| 110 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 111 | - | |||||||||||||||||||||||||
| 112 | size_t t = v1->val; | - | ||||||||||||||||||||||||
| 113 | v1->val = v2->val; | - | ||||||||||||||||||||||||
| 114 | v2->val = t; | - | ||||||||||||||||||||||||
| 115 | if (!hash_insert (sv, v1)
| 0-2 | ||||||||||||||||||||||||
| 116 | xalloc_die (); never executed: xalloc_die (); | 0 | ||||||||||||||||||||||||
| 117 | if (!hash_insert (sv, v2)
| 0-2 | ||||||||||||||||||||||||
| 118 | xalloc_die (); never executed: xalloc_die (); | 0 | ||||||||||||||||||||||||
| 119 | - | |||||||||||||||||||||||||
| 120 | v[i] = v1->val; | - | ||||||||||||||||||||||||
| 121 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||||||||
| 122 | - | |||||||||||||||||||||||||
| 123 | static void | - | ||||||||||||||||||||||||
| 124 | sparse_free (sparse_map *sv) | - | ||||||||||||||||||||||||
| 125 | { | - | ||||||||||||||||||||||||
| 126 | hash_free (sv); | - | ||||||||||||||||||||||||
| 127 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||
| 128 | - | |||||||||||||||||||||||||
| 129 | - | |||||||||||||||||||||||||
| 130 | - | |||||||||||||||||||||||||
| 131 | - | |||||||||||||||||||||||||
| 132 | - | |||||||||||||||||||||||||
| 133 | - | |||||||||||||||||||||||||
| 134 | size_t * | - | ||||||||||||||||||||||||
| 135 | randperm_new (struct randint_source *r, size_t h, size_t n) | - | ||||||||||||||||||||||||
| 136 | { | - | ||||||||||||||||||||||||
| 137 | size_t *v; | - | ||||||||||||||||||||||||
| 138 | - | |||||||||||||||||||||||||
| 139 | switch (h) | - | ||||||||||||||||||||||||
| 140 | { | - | ||||||||||||||||||||||||
| 141 | case executed 2 times by 1 test: 0:case 0:Executed by:
executed 2 times by 1 test: case 0:Executed by:
| 2 | ||||||||||||||||||||||||
| 142 | v = | - | ||||||||||||||||||||||||
| 143 | ((void *)0) | - | ||||||||||||||||||||||||
| 144 | ; | - | ||||||||||||||||||||||||
| 145 | break; executed 2 times by 1 test: break;Executed by:
| 2 | ||||||||||||||||||||||||
| 146 | - | |||||||||||||||||||||||||
| 147 | case executed 3 times by 1 test: 1:case 1:Executed by:
executed 3 times by 1 test: case 1:Executed by:
| 3 | ||||||||||||||||||||||||
| 148 | v = xmalloc (sizeof *v); | - | ||||||||||||||||||||||||
| 149 | v[0] = randint_choose (r, n); | - | ||||||||||||||||||||||||
| 150 | break; executed 3 times by 1 test: break;Executed by:
| 3 | ||||||||||||||||||||||||
| 151 | - | |||||||||||||||||||||||||
| 152 | default executed 9 times by 1 test: :default:Executed by:
executed 9 times by 1 test: default:Executed by:
| 9 | ||||||||||||||||||||||||
| 153 | { | - | ||||||||||||||||||||||||
| 154 | - | |||||||||||||||||||||||||
| 155 | _Bool | - | ||||||||||||||||||||||||
| 156 | sparse = (
| 0-8 | ||||||||||||||||||||||||
| 157 | - | |||||||||||||||||||||||||
| 158 | size_t i; | - | ||||||||||||||||||||||||
| 159 | sparse_map *sv; | - | ||||||||||||||||||||||||
| 160 | - | |||||||||||||||||||||||||
| 161 | if (sparse
| 1-8 | ||||||||||||||||||||||||
| 162 | { | - | ||||||||||||||||||||||||
| 163 | sv = sparse_new (h * 2); | - | ||||||||||||||||||||||||
| 164 | if (sv ==
| 0-1 | ||||||||||||||||||||||||
| 165 | ((void *)0)
| 0-1 | ||||||||||||||||||||||||
| 166 | ) | - | ||||||||||||||||||||||||
| 167 | xalloc_die (); never executed: xalloc_die (); | 0 | ||||||||||||||||||||||||
| 168 | v = xnmalloc (h, sizeof *v); | - | ||||||||||||||||||||||||
| 169 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||||||||
| 170 | else | - | ||||||||||||||||||||||||
| 171 | { | - | ||||||||||||||||||||||||
| 172 | sv = | - | ||||||||||||||||||||||||
| 173 | ((void *)0) | - | ||||||||||||||||||||||||
| 174 | ; | - | ||||||||||||||||||||||||
| 175 | v = xnmalloc (n, sizeof *v); | - | ||||||||||||||||||||||||
| 176 | for (i = 0; i < n
| 8-2111 | ||||||||||||||||||||||||
| 177 | v[i] = i; executed 2111 times by 1 test: v[i] = i;Executed by:
| 2111 | ||||||||||||||||||||||||
| 178 | } executed 8 times by 1 test: end of blockExecuted by:
| 8 | ||||||||||||||||||||||||
| 179 | - | |||||||||||||||||||||||||
| 180 | for (i = 0; i < h
| 9-2106 | ||||||||||||||||||||||||
| 181 | { | - | ||||||||||||||||||||||||
| 182 | size_t j = i + randint_choose (r, n - i); | - | ||||||||||||||||||||||||
| 183 | if (sparse
| 2-2104 | ||||||||||||||||||||||||
| 184 | sparse_swap (sv, v, i, j); executed 2 times by 1 test: sparse_swap (sv, v, i, j);Executed by:
| 2 | ||||||||||||||||||||||||
| 185 | else | - | ||||||||||||||||||||||||
| 186 | swap (v, i, j); executed 2104 times by 1 test: swap (v, i, j);Executed by:
| 2104 | ||||||||||||||||||||||||
| 187 | } | - | ||||||||||||||||||||||||
| 188 | - | |||||||||||||||||||||||||
| 189 | if (sparse
| 1-8 | ||||||||||||||||||||||||
| 190 | sparse_free (sv); executed 1 time by 1 test: sparse_free (sv);Executed by:
| 1 | ||||||||||||||||||||||||
| 191 | else | - | ||||||||||||||||||||||||
| 192 | v = xnrealloc (v, h, sizeof *v); executed 8 times by 1 test: v = xnrealloc (v, h, sizeof *v);Executed by:
| 8 | ||||||||||||||||||||||||
| 193 | } | - | ||||||||||||||||||||||||
| 194 | break; executed 9 times by 1 test: break;Executed by:
| 9 | ||||||||||||||||||||||||
| 195 | } | - | ||||||||||||||||||||||||
| 196 | - | |||||||||||||||||||||||||
| 197 | return executed 14 times by 1 test: v;return v;Executed by:
executed 14 times by 1 test: return v;Executed by:
| 14 | ||||||||||||||||||||||||
| 198 | } | - | ||||||||||||||||||||||||
| Switch to Source code | Preprocessed file |