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