@@ -748,6 +748,157 @@ struct Hamming2
748
748
749
749
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
750
750
751
+ struct DNAmmingLUT
752
+ {
753
+ typedef False is_kdtree_distance;
754
+ typedef False is_vector_space_distance;
755
+
756
+ typedef unsigned char ElementType;
757
+ typedef int ResultType;
758
+ typedef ElementType CentersType;
759
+
760
+ /* * this will count the bits in a ^ b
761
+ */
762
+ template <typename Iterator2>
763
+ ResultType operator ()(const unsigned char * a, const Iterator2 b, size_t size) const
764
+ {
765
+ static const uchar popCountTable[] =
766
+ {
767
+ 0 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 ,
768
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 ,
769
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
770
+ 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
771
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
772
+ 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
773
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
774
+ 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4
775
+ };
776
+ ResultType result = 0 ;
777
+ const unsigned char * b2 = reinterpret_cast <const unsigned char *> (b);
778
+ for (size_t i = 0 ; i < size; i++) {
779
+ result += popCountTable[a[i] ^ b2[i]];
780
+ }
781
+ return result;
782
+ }
783
+
784
+
785
+ ResultType operator ()(const unsigned char * a, const ZeroIterator<unsigned char > b, size_t size) const
786
+ {
787
+ (void )b;
788
+ static const uchar popCountTable[] =
789
+ {
790
+ 0 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 ,
791
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 ,
792
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
793
+ 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
794
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
795
+ 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
796
+ 1 , 2 , 2 , 2 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 ,
797
+ 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 2 , 3 , 3 , 3 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4 , 3 , 4 , 4 , 4
798
+ };
799
+ ResultType result = 0 ;
800
+ for (size_t i = 0 ; i < size; i++) {
801
+ result += popCountTable[a[i]];
802
+ }
803
+ return result;
804
+ }
805
+ };
806
+
807
+
808
+ template <typename T>
809
+ struct DNAmming2
810
+ {
811
+ typedef False is_kdtree_distance;
812
+ typedef False is_vector_space_distance;
813
+
814
+ typedef T ElementType;
815
+ typedef int ResultType;
816
+ typedef ElementType CentersType;
817
+
818
+ /* * This is popcount_3() from:
819
+ * http://en.wikipedia.org/wiki/Hamming_weight */
820
+ unsigned int popcnt32 (uint32_t n) const
821
+ {
822
+ n = ((n >> 1 ) | n) & 0x55555555 ;
823
+ n = (n & 0x33333333 ) + ((n >> 2 ) & 0x33333333 );
824
+ return (((n + (n >> 4 ))& 0x0F0F0F0F )* 0x01010101 ) >> 24 ;
825
+ }
826
+
827
+ #ifdef FLANN_PLATFORM_64_BIT
828
+ unsigned int popcnt64 (uint64_t n) const
829
+ {
830
+ n = ((n >> 1 ) | n) & 0x5555555555555555 ;
831
+ n = (n & 0x3333333333333333 ) + ((n >> 2 ) & 0x3333333333333333 );
832
+ return (((n + (n >> 4 ))& 0x0f0f0f0f0f0f0f0f )* 0x0101010101010101 ) >> 56 ;
833
+ }
834
+ #endif
835
+
836
+ template <typename Iterator1, typename Iterator2>
837
+ ResultType operator ()(const Iterator1 a, const Iterator2 b, size_t size, ResultType /* worst_dist*/ = -1 ) const
838
+ {
839
+ CV_DbgAssert (!(size % long_word_size_) && " vectors size must be multiple of long words size (i.e. 8)" );
840
+
841
+ #ifdef FLANN_PLATFORM_64_BIT
842
+ const uint64_t * pa = reinterpret_cast <const uint64_t *>(a);
843
+ const uint64_t * pb = reinterpret_cast <const uint64_t *>(b);
844
+ ResultType result = 0 ;
845
+ size /= long_word_size_;
846
+ for (size_t i = 0 ; i < size; ++i ) {
847
+ result += popcnt64 (*pa ^ *pb);
848
+ ++pa;
849
+ ++pb;
850
+ }
851
+ #else
852
+ const uint32_t * pa = reinterpret_cast <const uint32_t *>(a);
853
+ const uint32_t * pb = reinterpret_cast <const uint32_t *>(b);
854
+ ResultType result = 0 ;
855
+ size /= long_word_size_;
856
+ for (size_t i = 0 ; i < size; ++i ) {
857
+ result += popcnt32 (*pa ^ *pb);
858
+ ++pa;
859
+ ++pb;
860
+ }
861
+ #endif
862
+ return result;
863
+ }
864
+
865
+
866
+ template <typename Iterator1>
867
+ ResultType operator ()(const Iterator1 a, ZeroIterator<unsigned char > b, size_t size, ResultType /* worst_dist*/ = -1 ) const
868
+ {
869
+ CV_DbgAssert (!(size % long_word_size_) && " vectors size must be multiple of long words size (i.e. 8)" );
870
+
871
+ (void )b;
872
+ #ifdef FLANN_PLATFORM_64_BIT
873
+ const uint64_t * pa = reinterpret_cast <const uint64_t *>(a);
874
+ ResultType result = 0 ;
875
+ size /= long_word_size_;
876
+ for (size_t i = 0 ; i < size; ++i ) {
877
+ result += popcnt64 (*pa);
878
+ ++pa;
879
+ }
880
+ #else
881
+ const uint32_t * pa = reinterpret_cast <const uint32_t *>(a);
882
+ ResultType result = 0 ;
883
+ size /= long_word_size_;
884
+ for (size_t i = 0 ; i < size; ++i ) {
885
+ result += popcnt32 (*pa);
886
+ ++pa;
887
+ }
888
+ #endif
889
+ return result;
890
+ }
891
+
892
+ private:
893
+ #ifdef FLANN_PLATFORM_64_BIT
894
+ static const size_t long_word_size_= sizeof (uint64_t )/sizeof (unsigned char );
895
+ #else
896
+ static const size_t long_word_size_= sizeof (uint32_t )/sizeof (unsigned char );
897
+ #endif
898
+ };
899
+
900
+
901
+
751
902
template <class T >
752
903
struct HistIntersectionDistance
753
904
{
0 commit comments