| 1289 | /* Insert the new element ELEM to the re_node_set* SET. |
| 1290 | SET should not already have ELEM. |
| 1291 | return -1 if an error has occurred, return 1 otherwise. */ |
| 1292 | |
| 1293 | static int |
| 1294 | internal_function |
| 1295 | re_node_set_insert (re_node_set *set, int elem) |
| 1296 | { |
| 1297 | int idx; |
| 1298 | /* In case the set is empty. */ |
| 1299 | if (set->alloc == 0) |
| 1300 | { |
| 1301 | if (BE (re_node_set_init_1 (set, elem) == REG_NOERROR, 1)) |
| 1302 | return 1; |
| 1303 | else |
| 1304 | return -1; |
| 1305 | } |
| 1306 | |
| 1307 | if (BE (set->nelem, 0) == 0) |
| 1308 | { |
| 1309 | /* We already guaranteed above that set->alloc != 0. */ |
| 1310 | set->elems[0] = elem; |
| 1311 | ++set->nelem; |
| 1312 | return 1; |
| 1313 | } |
| 1314 | |
| 1315 | /* Realloc if we need. */ |
| 1316 | if (set->alloc == set->nelem) |
| 1317 | { |
| 1318 | int *new_elems; |
| 1319 | set->alloc = set->alloc * 2; |
| 1320 | new_elems = re_realloc (set->elems, int, set->alloc); |
| 1321 | if (BE (new_elems == NULL, 0)) |
| 1322 | return -1; |
| 1323 | set->elems = new_elems; |
| 1324 | } |
| 1325 | |
| 1326 | /* Move the elements which follows the new element. Test the |
| 1327 | first element separately to skip a check in the inner loop. */ |
| 1328 | if (elem < set->elems[0]) |
| 1329 | { |
| 1330 | idx = 0; |
| 1331 | for (idx = set->nelem; idx > 0; idx--) |
| 1332 | set->elems[idx] = set->elems[idx - 1]; |
| 1333 | } |
| 1334 | else |
| 1335 | { |
| 1336 | for (idx = set->nelem; set->elems[idx - 1] > elem; idx--) |
| 1337 | set->elems[idx] = set->elems[idx - 1]; |
| 1338 | } |
| 1339 | |
| 1340 | /* Insert the new element. */ |
| 1341 | set->elems[idx] = elem; |
| 1342 | ++set->nelem; |
| 1343 | return 1; |
| 1344 | } |
| 1345 |
no test coverage detected