| 1203 | |
| 1204 | /* Calculate the union set of the sets DEST and SRC. And store it to |
| 1205 | DEST. Return value indicate the error code or REG_NOERROR if succeeded. */ |
| 1206 | |
| 1207 | static reg_errcode_t |
| 1208 | internal_function |
| 1209 | re_node_set_merge (re_node_set *dest, const re_node_set *src) |
| 1210 | { |
| 1211 | int is, id, sbase, delta; |
| 1212 | if (src == NULL || src->nelem == 0) |
| 1213 | return REG_NOERROR; |
| 1214 | if (dest->alloc < 2 * src->nelem + dest->nelem) |
| 1215 | { |
| 1216 | int new_alloc = 2 * (src->nelem + dest->alloc); |
| 1217 | int *new_buffer = re_realloc (dest->elems, int, new_alloc); |
| 1218 | if (BE (new_buffer == NULL, 0)) |
| 1219 | return REG_ESPACE; |
| 1220 | dest->elems = new_buffer; |
| 1221 | dest->alloc = new_alloc; |
| 1222 | } |
| 1223 | |
| 1224 | if (BE (dest->nelem == 0, 0)) |
| 1225 | { |
| 1226 | dest->nelem = src->nelem; |
| 1227 | memcpy (dest->elems, src->elems, src->nelem * sizeof (int)); |
| 1228 | return REG_NOERROR; |
| 1229 | } |
| 1230 | |
| 1231 | /* Copy into the top of DEST the items of SRC that are not |
| 1232 | found in DEST. Maybe we could binary search in DEST? */ |
| 1233 | for (sbase = dest->nelem + 2 * src->nelem, |
| 1234 | is = src->nelem - 1, id = dest->nelem - 1; is >= 0 && id >= 0; ) |
| 1235 | { |
| 1236 | if (dest->elems[id] == src->elems[is]) |
| 1237 | { |
| 1238 | is--; |
| 1239 | id--; |
| 1240 | } |
| 1241 | else if (dest->elems[id] < src->elems[is]) |
| 1242 | dest->elems[--sbase] = src->elems[is--]; |
| 1243 | else /* if (dest->elems[id] > src->elems[is]) */ |
| 1244 | --id; |
| 1245 | } |
| 1246 | |
| 1247 | if (is >= 0) |
| 1248 | { |
| 1249 | /* If DEST is exhausted, the remaining items of SRC must be unique. */ |
| 1250 | sbase -= is + 1; |
| 1251 | memcpy (dest->elems + sbase, src->elems, (is + 1) * sizeof (int)); |
| 1252 | } |
| 1253 | |
| 1254 | id = dest->nelem - 1; |
| 1255 | is = dest->nelem + 2 * src->nelem - 1; |
| 1256 | delta = is - sbase + 1; |
| 1257 | if (delta == 0) |
| 1258 | return REG_NOERROR; |
| 1259 | |
| 1260 | /* Now copy. When DELTA becomes zero, the remaining |
| 1261 | DEST elements are already in place. */ |
| 1262 | dest->nelem += delta; |
no outgoing calls
no test coverage detected