diff --git a/lib/backend/dbiset.c b/lib/backend/dbiset.c index 649ea8866..0ac514582 100644 --- a/lib/backend/dbiset.c +++ b/lib/backend/dbiset.c @@ -152,6 +152,39 @@ int dbiIndexSetPruneSet(dbiIndexSet set, dbiIndexSet oset, int sortset) return dbiIndexSetPrune(set, oset->recs, oset->count, sortset); } +int dbiIndexSetFilter(dbiIndexSet set, dbiIndexItem recs, + unsigned int nrecs, int sorted) +{ + unsigned int from; + unsigned int to = 0; + unsigned int num = set->count; + unsigned int numCopied = 0; + size_t recsize = sizeof(*recs); + + if (num == 0 || nrecs == 0) { + set->count = 0; + return num ? 0 : 1; + } + if (nrecs > 1 && !sorted) + qsort(recs, nrecs, recsize, hdrNumCmp); + for (from = 0; from < num; from++) { + if (!bsearch(&set->recs[from], recs, nrecs, recsize, hdrNumCmp)) { + set->count--; + continue; + } + if (from != to) + set->recs[to] = set->recs[from]; /* structure assignment */ + to++; + numCopied++; + } + return (numCopied == num); +} + +int dbiIndexSetFilterSet(dbiIndexSet set, dbiIndexSet oset, int sorted) +{ + return dbiIndexSetFilter(set, oset->recs, oset->count, sorted); +} + unsigned int dbiIndexSetCount(dbiIndexSet set) { return (set != NULL) ? set->count : 0; diff --git a/lib/backend/dbiset.h b/lib/backend/dbiset.h index 506375865..da196c865 100644 --- a/lib/backend/dbiset.h +++ b/lib/backend/dbiset.h @@ -86,6 +86,28 @@ int dbiIndexSetPrune(dbiIndexSet set, dbiIndexItem recs, RPM_GNUC_INTERNAL int dbiIndexSetPruneSet(dbiIndexSet set, dbiIndexSet oset, int sorted); +/** + * Filter element(s) from set of index database items. + * @param set set of index database items + * @param recs array of items to remove from set + * @param nrecs number of items + * @param sorted recs array is already sorted? + * @return 0 success, 1 failure (no items removed) + */ +RPM_GNUC_INTERNAL +int dbiIndexSetFilter(dbiIndexSet set, dbiIndexItem recs, + unsigned int nrecs, int sorted); + +/** + * Filter (intersect) an index set with another. + * @param set set of index database items + * @param oset set of entries that should be intersected + * @param sorted oset is already sorted? + * @return 0 success, 1 failure (no items removed) + */ +RPM_GNUC_INTERNAL +int dbiIndexSetFilterSet(dbiIndexSet set, dbiIndexSet oset, int sorted); + /* Count items in index database set. */ RPM_GNUC_INTERNAL unsigned int dbiIndexSetCount(dbiIndexSet set);