summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/lsh/hashes.h
blob: 35053ce4e64debc503426eacd6e5d6f32d0c74e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (C) 2013  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_LSH_HAShES_Hh_
#define DLIB_LSH_HAShES_Hh_

#include "hashes_abstract.h"
#include "../hash.h"
#include "../matrix.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    class hash_similar_angles_64
    {
    public:
        hash_similar_angles_64 (
        ) : seed(0) {}

        hash_similar_angles_64 (
            const uint64 seed_ 
        ) : seed(seed_) {}

        uint64 get_seed (
        ) const { return seed; }


        typedef uint64 result_type;

        template <
            typename sparse_vector_type
            >
        typename disable_if<is_matrix<sparse_vector_type>,uint64>::type operator() (
            const sparse_vector_type& v
        ) const
        {
            typedef typename sparse_vector_type::value_type::second_type scalar_type;

            uint64 temp = 0;
            for (int i = 0; i < 64; ++i)
            {
                // compute the dot product between v and a Gaussian random vector.
                scalar_type val = 0;
                for (typename sparse_vector_type::const_iterator j = v.begin(); j != v.end(); ++j)
                    val += j->second*gaussian_random_hash(j->first, i, seed);

                if (val > 0)
                    temp |= 1;
                temp <<= 1;
            }
            return temp;
        }

        template <typename EXP>
        uint64 operator() ( 
            const matrix_exp<EXP>& v
        ) const
        {
            typedef typename EXP::type T;
            uint64 temp = 0;
            for (unsigned long i = 0; i < 64; ++i)
            {
                if (dot(matrix_cast<T>(gaussian_randm(v.size(),1,i+seed*64)), v) > 0)
                    temp |= 1;
                temp <<= 1;
            }
            return temp;
        }

        unsigned int distance (
            const result_type& a,
            const result_type& b
        ) const
        {
            return hamming_distance(a,b);
        }

    private:
        const uint64 seed;
    };

// ----------------------------------------------------------------------------------------

    class hash_similar_angles_128
    {
    public:
        hash_similar_angles_128 (
        ) : seed(0),hasher1(0), hasher2(1) {}

        hash_similar_angles_128 (
            const uint64 seed_
        ) : seed(seed_),hasher1(2*seed),hasher2(2*seed+1) {}

        uint64 get_seed (
        ) const { return seed; }

        typedef std::pair<uint64,uint64> result_type;

        template <
            typename vector_type 
            >
        result_type operator() (
            const vector_type& v
        ) const
        {
            return std::make_pair(hasher1(v), hasher2(v));
        }

        unsigned int distance (
            const result_type& a,
            const result_type& b
        ) const
        {
            return hamming_distance(a.first,b.first) + 
                   hamming_distance(a.second,b.second);
        }

    private:
        const uint64 seed;
        hash_similar_angles_64 hasher1;
        hash_similar_angles_64 hasher2;

    };

// ----------------------------------------------------------------------------------------

    class hash_similar_angles_256
    {
    public:
        hash_similar_angles_256 (
        ) : seed(0), hasher1(0), hasher2(1) {}

        hash_similar_angles_256 (
            const uint64 seed_
        ) : seed(seed_),hasher1(2*seed),hasher2(2*seed+1) {}

        uint64 get_seed (
        ) const { return seed; }

        typedef std::pair<uint64,uint64> hash128_type;
        typedef std::pair<hash128_type,hash128_type> result_type;

        template <
            typename vector_type 
            >
        result_type operator() (
            const vector_type& v
        ) const
        {
            return std::make_pair(hasher1(v), hasher2(v));
        }

        unsigned int distance (
            const result_type& a,
            const result_type& b
        ) const
        {
            return hasher1.distance(a.first,b.first) + 
                   hasher1.distance(a.second,b.second);
        }

    private:
        const uint64 seed;
        hash_similar_angles_128 hasher1;
        hash_similar_angles_128 hasher2;

    };

// ----------------------------------------------------------------------------------------

    class hash_similar_angles_512
    {
    public:
        hash_similar_angles_512 (
        ) : seed(0), hasher1(0), hasher2(1) {}

        hash_similar_angles_512 (
            const uint64 seed_
        ) : seed(seed_),hasher1(2*seed),hasher2(2*seed+1) {}

        uint64 get_seed (
        ) const { return seed; }


        typedef hash_similar_angles_256::result_type hash256_type;
        typedef std::pair<hash256_type,hash256_type> result_type;

        template <
            typename vector_type 
            >
        result_type operator() (
            const vector_type& v
        ) const
        {
            return std::make_pair(hasher1(v), hasher2(v));
        }

        unsigned int distance (
            const result_type& a,
            const result_type& b
        ) const
        {
            return hasher1.distance(a.first,b.first) + 
                   hasher1.distance(a.second,b.second);
        }

    private:
        const uint64 seed;
        hash_similar_angles_256 hasher1;
        hash_similar_angles_256 hasher2;
    };

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_LSH_HAShES_Hh_