Exemple #1
0
unsigned long int
mpn_hamdist (mp_srcptr p1, mp_srcptr p2, mp_size_t n)
{
  unsigned long int result = 0;
  mp_size_t i;
  for (i = 0; i < n; i++)
    result += _popcnt (p1[i] ^ p2[i]);
  return result;
}
Exemple #2
0
int special_sum(int N, int items[]) {
    int **ksums = malloc((1 + N) * sizeof(int *));
    int *kcount = calloc((1 + N), sizeof(int));

    int i;

    for(i = 0; i <= N; i++)
        ksums[i] = malloc(nchoosek(N, i) * sizeof(int));

    for(i = 1; i < (1 << N); i++) {
        int count = _popcnt(i);
        int index = kcount[count]++;

        int sum = item_sum(N, items, i);

        ksums[count][index] = sum;
    }

    for(i = 1; i < N; i++)
        qsort(ksums[i], kcount[i], sizeof(int), icmp);

    bool is_special = true;
    for(i = 1; i < N && is_special; i++) {
        // check duplicate sums
        int j;
        for(j = 1; j < kcount[i]; j++) {
            if(ksums[i][j] == ksums[i][j-1]) {
                is_special = false;
                break;
            }
        }

        // check order
        if(ksums[i][kcount[i]-1] >= ksums[i+1][0])
            is_special = false;
    }

    int ret = 0;
    if(is_special) {
        for(i = 0; i < N; i++)
            ret += items[i];
    }

    for(i = 0; i < N; i++)
        free(ksums[i]);
    free(ksums);
    free(kcount);

    return ret;
}
Exemple #3
0
typename bit_iterator<InputIt>::difference_type
count(
    bit_iterator<InputIt> first, 
    bit_iterator<InputIt> last, 
    bit_value value
)
{
    // Assertions
    _assert_range_viability(first, last);
    
    // Types and constants
    using underlying_type = typename bit_iterator<InputIt>::underlying_type;
    using difference_type = typename bit_iterator<InputIt>::difference_type;
    constexpr difference_type digits = binary_digits<underlying_type>::value;
    
    // Initialization
    difference_type result = 0;
    auto it = first.base();
    underlying_type first_value = {};
    underlying_type last_value = {};
    
    // Computation when bits belong to several underlying values
    if (first.base() != last.base()) {
        if (first.position() != 0) {
            first_value = *first.base() >> first.position();
            result = _popcnt(first_value);
            ++it;
        }
        for (; it != last.base(); ++it) {
            result += _popcnt(*it);
        }
        if (last.position() != 0) {
            last_value = *last.base() << (digits - last.position());
            result += _popcnt(last_value);
        }
    // Computation when bits belong to the same underlying value
    } else {
Exemple #4
0
constexpr T _popcnt(T src)
{
    static_assert(binary_digits<T>::value, "");
    constexpr T digits = binary_digits<T>::value;
    if (digits <= std::numeric_limits<unsigned int>::digits) {
        src = __builtin_popcount(src); 
    } else if (digits <= std::numeric_limits<unsigned long int>::digits) {
        src = __builtin_popcountl(src); 
    } else if (digits <= std::numeric_limits<unsigned long long int>::digits) {
        src = __builtin_popcountll(src); 
    } else {
        src = _popcnt(src, std::ignore);
    }
    return src;
}