This file is indexed.

/usr/include/colib/hash.h is in libiulib-dev 0.4+is+0.3-3ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// -*- C++ -*-

// Copyright 2006 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz 
// or its licensors, as applicable.
// Copyright 1995-2005 Thomas M. Breuel.
// 
// You may not use this file except under the terms of the accompanying license.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You may
// obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 
// Project: iulib -- image understanding library
// File: hash.h
// Purpose: simple hash table imlpementation
// Responsible: tmb
// Reviewer: 
// Primary Repository: 
// Web Sites: www.iupr.org, www.dfki.de


/// \file hash.h
/// \brief Simple hash table implementations.

#ifndef h_hash_
#define h_hash_

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "colib/narray.h"
#include "smartptr.h"

namespace colib {

    namespace {

        const int hash_empty = 999999;

        inline int hash_next(int size) {
            int i = 1;
            while(i<=size) i<<=1;
            return i+1;
        }

        inline int hash_value(int key,int n) {
            ASSERT(n>0);
            return int(unsigned(98.209328340918084289029384908 * key
                                + 0.2349082034802840982092834098)%n);
        }

        inline int hash_value(int key1,int key2,int n) {
            ASSERT(n>0);
            return int(unsigned(1793.02934802983402808942308402389908 * key1 +
                                34.34983902935890238058909480840238 * key2
                                + 0.8949173412390482890423908048) % n);
        }

        inline int hash_value(const char *p,int n) {
            double total = 0.0;
            while(*p) {
                total = total * 1.29008352390840238 + (*p++) * 0.209384902384;
            }
            return int(unsigned(total)%n);
        }

    }
    


    /// \brief A simple int-to-something hash class.
    ///
    /// This is not particularly high performance, but it should be good enough for most uses.

    template <class T>
    class inthash {
    private:
        struct kvp {
            int key;
            T value;
            kvp() { key = hash_empty; }
        };
        narray<kvp> data;
        int fill;
        void maybe_grow() {
            if(fill>data.length()/2)
                grow(data.length()+1);
        }

    public:

        /// Allocate a hash table with a given initial size.

        inthash(int initial=3) {
            data.resize(max(3,initial));
            fill = 0;
        }

        /// Make the hash table empty again and deallocate all the data it holds.
        
        void dealloc() {
            data.dealloc();
            data.resize(3);
            fill = 0;
        }

        /// Grow the hash table by the given amount.

        void grow_by(int n) {
            ASSERT(n>=0);
            inthash nhash(data.length()+n);
            for(int i=0;i<data.length();i++) {
                kvp &entry = data[i];
                if(entry.key==hash_empty) continue;
                nhash(entry.key) = entry.value;
            }
            move(data,nhash.data);
            fill = nhash.fill;
        }

        /// Return a reference to the location associated with the given key.

        T &operator()(int key) {
            if(key==hash_empty) throw "key value reserved for hash table implementation";
            int n = data.length();
            int base = hash_value(key,n);
            for(int i=0;i<n;i++) {
                int index = (base+i)%n;
                kvp &entry = data[index];
                if(entry.key==key)
                    return entry.value;
                if(entry.key==hash_empty) {
                    if(fill<data.length()/2) {
                        fill++;
                        entry.key = key;
                        return entry.value;
                    }
                    grow_by(hash_next(data.length())-data.length());
                    return operator()(key);
                }
            }
            throw "internal error: no empty hash bucket found";
        }

        T &at(int key) {
            return this->operator()(key);
        }

        /// Return a list of keys.

        void keys(narray<int> &result) {
            result.clear();
            for(int i=0;i<data.length();i++) {
                kvp &entry = data[i];
                if(entry.key!=hash_empty) {
                    result.push(entry.key);
                }
            }
        }
    };

/// \brief A simple pair-of-int-to-something hash class.
    ///
    /// This is not particularly high performance, but it should be good enough for most uses.

    template <class T>
    class int2hash {
    private:
        struct kvp {
            int key1,key2;
            T value;
            kvp() { key1 = hash_empty; }
        };
        narray<kvp> data;
        int fill;
        void maybe_grow() {
            if(fill>data.length()/2)
                grow(data.length()+1);
        }

    public:

        /// Allocate a hash table with a given initial size.

        int2hash(int initial=3) {
            data.dealloc();
            data.resize(max(3,initial));
            fill = 0;
        }

        /// Make the hash table empty again and deallocate all the data it holds.
        
        void dealloc() {
            data.dealloc();
            data.resize(3);
            fill = 0;
        }

        /// Grow the hash table by the given amount.

        void grow_by(int n) {
            ASSERT(n>=0);
            int2hash<T> nhash(data.length()+n);
            for(int i=0;i<data.length();i++) {
                kvp &entry = data[i];
                if(entry.key1==hash_empty) continue;
                nhash(entry.key1,entry.key2) = entry.value;
            }
            move(data,nhash.data);
            fill = nhash.fill;
        }

        /// Return a reference to the location associated with the given key.

        T &operator()(int key1,int key2) {
            if(key1==hash_empty) throw "key value reserved for hash table implementation";
            int n = data.length();
            int base = hash_value(key1,key2,n);
            for(int i=0;i<n;i++) {
                int index = (base+i)%n;
                kvp &entry = data[index];
                if(entry.key1==key1 && entry.key2==key2)
                    return entry.value;
                if(entry.key1==hash_empty) {
                    if(fill<data.length()/2) {
                        fill++;
                        entry.key1 = key1;
                        entry.key2 = key2;
                        return entry.value;
                    }
                    grow_by(hash_next(data.length())-data.length());
                    return operator()(key1,key2);
                }
            }
            throw "internal error: no empty hash bucket found";
        }
    };

    /// A string-to-something hash class.

    /// \brief A simple string-to-something hash class.
    ///
    /// This is not particularly high performance, but it should be good enough for most uses.

    template <class T>
    class strhash {
    private:
        struct kvp {
            autofree<char> key;
            T value;
        };
        narray<kvp> data;
        int fill;
        void maybe_grow() {
            if(fill>data.length()/2)
                grow(data.length()+1);
        }

    public:

        /// Allocate a hash table with a given initial size.

        strhash(int initial=3) {
            data.dealloc();
            data.resize(max(3,initial));
            fill = 0;
        }

        /// Make the hash table empty again and deallocate all the data it holds.
        
        void dealloc() {
            data.dealloc();
            data.resize(3);
            fill = 0;
        }

        /// Grow the hash table by the given amount.

        void grow_by(int n) {
            ASSERT(n>=0);
            strhash nhash(data.length()+n);
            for(int i=0;i<data.length();i++) {
                kvp &entry = data[i];
                if(entry.key.ptr()==0) continue;
                nhash(entry.key.ptr()) = entry.value;
            }
            move(data,nhash.data);
            fill = nhash.fill;
        }

        /// Return a reference to the location associated with the given key.

        T &operator()(const char *key) {
            if(key==0) throw "key value reserved for hash table implementation";
            int n = data.length();
            int base = hash_value(key,n);
            for(int i=0;i<n;i++) {
                int index = (base+i)%n;
                kvp &entry = data[index];
                if(entry.key.ptr() && !strcmp(entry.key.ptr(),key))
                    return entry.value;
                if(!entry.key) {
                    if(fill<data.length()/2) {
                        fill++;
                        entry.key = strdup(key);
                        return entry.value;
                    }
                    grow_by(hash_next(data.length())-data.length());
                    return operator()(key);
                }
            }
            throw "internal error: no empty hash bucket found";
        }
    };

}

#endif