0

How to store binary data when you only care about speed?으로 대답하려면 몇 가지 비교를 작성하려고합니다. 따라서 std::bitset을 사용하고 싶습니다. 그러나 공정한 비교를 위해 1D std::bitset으로 2D를 에뮬레이트하고 싶습니다. 2D 비트 세트가 1D로 저장 될 때 XOR 비트셋

그래서 대신 가진

:

bitset<3> b1(string("010")); 
bitset<3> b2(string("111")); 

내가 사용하고 싶습니다 :

bitset<2 * 3> b1(string("010111")); 

데이터 지역성을 최적화 할 수 있습니다. 그러나, 지금은 내 최소한의 예에서 볼 수 있듯이, How should I store and compute Hamming distance between binary codes?에 문제가 있습니다 :

#include <vector> 
#include <iostream> 
#include <random> 
#include <cmath> 
#include <numeric> 
#include <bitset> 

int main() 
{ 
    const int N = 1000000; 
    const int D = 100; 
    unsigned int hamming_dist[N] = {0}; 
    std::bitset<D> q; 
    for(int i = 0; i < D; ++i) 
     q[i] = 1; 

    std::bitset<N * D> v; 
    for(int i = 0; i < N; ++i) 
     for(int j = 0; j < D; ++j) 
      v[j + i * D] = 1; 


    for(int i = 0; i < N; ++i) 
     hamming_dist[i] += (v[i * D]^q).count(); 

    std::cout << "hamming_distance = " << hamming_dist[0] << "\n"; 

    return 0; 
} 

오류 : 그것을 알고하지 않기 때문에 발생

Georgioss-MacBook-Pro:bit gsamaras$ g++ -Wall bitset.cpp -o bitset 
bitset.cpp:24:32: error: invalid operands to binary expression ('reference' (aka 
     '__bit_reference<std::__1::__bitset<1562500, 100000000> >') and 
     'std::bitset<D>') 
       hamming_dist[i] += (v[i * D]^q).count(); 
            ~~~~~~~~^~ 
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/bitset:1096:1: note: 
     candidate template ignored: could not match 'bitset' against 
     '__bit_reference' 
operator^(const bitset<_Size>& __x, const bitset<_Size>& __y) _NOEXCEPT 
^ 
1 error generated. 

가 중지! D 비트를 처리 한 후 멈추라 고 어떻게 말할 수 있습니까?


2D 을 사용하지 않고 의미합니다.

답변

1

문제는 v[i * D]이 단일 비트를 액세스한다는 것입니다. 2D 비트 배열의 개념적 모델에서 행 i 및 열 0의 비트에 액세스합니다.

그래서 v[i * D]boolqstd::bitset<D>이다 및 비트 논리 XOR 연산자 (^)를 이해하지 않는 것에 적용될.

vD 크기의 이진 벡터 시퀀스를 나타내려는 경우 대신 std::vector<std::bitset<D>>을 사용해야합니다. 또한 std::bitset<N>::set()은 모든 비트를 1으로 설정합니다.

#include <vector> 
#include <iostream> 
#include <random> 
#include <cmath> 
#include <numeric> 
#include <bitset> 

int main() 
{ 
    const int N = 1000000; 
    const int D = 100; 

    std::vector<std::size_t> hamming_dist(N); 

    std::bitset<D> q; 
    q.set(); 

    std::vector<std::bitset<D>> v(N); 
    for (int i = 0; i < N; ++i) 
    { 
     v[i].set(); 
    } 

    for (int i = 0; i < N; ++i) 
    { 
     hamming_dist[i] = (v[i]^q).count(); 
    } 

    std::cout << "hamming_distance = " << hamming_dist[0] << "\n"; 

    return 0; 
} 
+0

하지만 지금은 1D가 아닌 2D 데이터 구조입니다. 맞습니까? : / – gsamaras