Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Add unique_count algorithm #1612

@upsj

Description

@upsj

For the common count -> allocate -> fill pattern (e.g. count_if/copy_if) there are some fill algorithms without a corresponding count algorithm, mainly unique_copy, unique_by_key_copy and reduce_by_key. So I want to suggest adding a count_unique algorithm with basically the same interface as unique save the return type:

difference_type thrust::count_unique(
        Policy exec,
        ForwardIterator first,
        ForwardIterator last,
        BinaryPredicate binary_pred)

The name probably needs a bit of discussion, since it doesn't actually count unique elements, but runs of unique elements, but unique has the same issue, so there is some prior art for it.

@allisonvacanti suggested using cub::DeviceRunLengthEncode::Encode to implement it, so I wanted to check whether there are any other approaches to consider before starting work on a PR. Another simple approach might be

auto zip_it = zip(it, it + 1);
if (size > 0) {
    return 1 + count_if(zip_it, zip_it + size - 1, [](auto a) { !binary_pred(get<0>(a), get<1>(a)); });
} else {
    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions