Welcome to mirror list, hosted at ThFree Co, Russian Federation.

shash.inl « Runtime « Native « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2af4d8e730c82321925cf8d425c3af5f21c93765 (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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// disable the "Conditional expression is constant" warning
#pragma warning(disable:4127)


template <typename TRAITS>
SHash<TRAITS>::SHash()
  : m_table(nullptr),
    m_tableSize(0),
    m_tableCount(0),
    m_tableOccupied(0),
    m_tableMax(0)
{
    C_ASSERT(TRAITS::s_growth_factor_numerator > TRAITS::s_growth_factor_denominator);
    C_ASSERT(TRAITS::s_density_factor_numerator < TRAITS::s_density_factor_denominator);
}

template <typename TRAITS>
SHash<TRAITS>::~SHash()
{
    delete [] m_table;
}

template <typename TRAITS>
typename SHash<TRAITS>::count_t SHash<TRAITS>::GetCount() const 
{
    return m_tableCount;
}

template <typename TRAITS>
typename SHash<TRAITS>::count_t SHash<TRAITS>::GetCapacity() const 
{
    return m_tableMax;
}

template <typename TRAITS>
typename SHash< TRAITS>::element_t SHash<TRAITS>::Lookup(key_t key) const
{
    const element_t *pRet = Lookup(m_table, m_tableSize, key);
    return ((pRet != NULL) ? (*pRet) : TRAITS::Null());
}

template <typename TRAITS>
const typename SHash< TRAITS>::element_t* SHash<TRAITS>::LookupPtr(key_t key) const
{
    return Lookup(m_table, m_tableSize, key);
}

template <typename TRAITS>
bool SHash<TRAITS>::Add(const element_t &element)
{
    if (!CheckGrowth())
        return false;

    if (Add(m_table, m_tableSize, element))
        m_tableOccupied++;
    m_tableCount++;

    return true;
}

template <typename TRAITS>
bool SHash<TRAITS>::AddOrReplace(const element_t &element)
{
    if (!CheckGrowth())
        return false;

    AddOrReplace(m_table, m_tableSize, element);
    return true;
}

template <typename TRAITS>
void SHash<TRAITS>::Remove(key_t key)
{
    Remove(m_table, m_tableSize, key);
}

template <typename TRAITS>
void SHash<TRAITS>::Remove(Iterator& i)
{
    RemoveElement(m_table, m_tableSize, (element_t*)&(*i));
}

template <typename TRAITS>
void SHash<TRAITS>::Remove(KeyIterator& i)
{
    RemoveElement(m_table, m_tableSize, (element_t*)&(*i));
}

template <typename TRAITS>
void SHash<TRAITS>::RemovePtr(element_t * p)
{
    RemoveElement(m_table, m_tableSize, p);
}

template <typename TRAITS>
void SHash<TRAITS>::RemoveAll()
{
    delete [] m_table;

    m_table = NULL;
    m_tableSize = 0;
    m_tableCount = 0;
    m_tableOccupied = 0;
    m_tableMax = 0;
}

template <typename TRAITS>
typename SHash<TRAITS>::Iterator SHash<TRAITS>::Begin() const
{
    Iterator i(this, true);
    i.First();
    return i;
}

template <typename TRAITS>
typename SHash<TRAITS>::Iterator SHash<TRAITS>::End() const
{
    return Iterator(this, false);
}

template <typename TRAITS>
typename SHash<TRAITS>::KeyIterator SHash<TRAITS>::Begin(key_t key) const
{
    KeyIterator k(this, true);
    k.SetKey(key);
    return k;
}

template <typename TRAITS>
typename SHash<TRAITS>::KeyIterator SHash<TRAITS>::End(key_t key) const
{
    return KeyIterator(this, false);
}

template <typename TRAITS>
bool SHash<TRAITS>::CheckGrowth()
{
    if (m_tableOccupied == m_tableMax)
    {
        return Grow();
    }
        
    return true;
}

template <typename TRAITS>
bool SHash<TRAITS>::Grow()
{
    count_t newSize = (count_t) (m_tableCount 
                                 * TRAITS::s_growth_factor_numerator / TRAITS::s_growth_factor_denominator
                                 * TRAITS::s_density_factor_denominator / TRAITS::s_density_factor_numerator);
    if (newSize < TRAITS::s_minimum_allocation)
        newSize = TRAITS::s_minimum_allocation;

    // handle potential overflow
    if (newSize < m_tableCount)
    {
        TRAITS::OnFailure(ftOverflow);
        return false;
    }

    return Reallocate(newSize);
}

template <typename TRAITS>
bool SHash<TRAITS>::CheckGrowth(count_t newElements)
{
    count_t newCount = (m_tableCount + newElements);

    // handle potential overflow
    if (newCount < newElements)
    {
        TRAITS::OnFailure(ftOverflow);
        return false;
    }

    // enough space in the table?
    if (newCount < m_tableMax)
        return true;

    count_t newSize = (count_t) (newCount * TRAITS::s_density_factor_denominator / TRAITS::s_density_factor_numerator) + 1;

    // handle potential overflow
    if (newSize < newCount)
    {
        TRAITS::OnFailure(ftOverflow);
        return false;
    }

    // accelerate the growth to avoid unnecessary rehashing
    count_t newSize2 = (m_tableCount * TRAITS::s_growth_factor_numerator / TRAITS::s_growth_factor_denominator
                                     * TRAITS::s_density_factor_denominator / TRAITS::s_density_factor_numerator);

    if (newSize < newSize2)
        newSize = newSize2;

    if (newSize < TRAITS::s_minimum_allocation)
        newSize = TRAITS::s_minimum_allocation;

    return Reallocate(newSize);
}

template <typename TRAITS>
bool SHash<TRAITS>::Reallocate(count_t newTableSize)
{
    ASSERT(newTableSize >= 
                 (count_t) (GetCount() * TRAITS::s_density_factor_denominator / TRAITS::s_density_factor_numerator));

    // Allocation size must be a prime number.  This is necessary so that hashes uniformly
    // distribute to all indices, and so that chaining will visit all indices in the hash table.
    newTableSize = NextPrime(newTableSize);
    if (newTableSize == 0)
    {
        TRAITS::OnFailure(ftOverflow);
        return false;
    }

    element_t *newTable = new (nothrow) element_t [newTableSize];
    if (newTable == NULL)
    {
        TRAITS::OnFailure(ftAllocation);
        return false;
    }

    element_t *p = newTable, *pEnd = newTable + newTableSize;
    while (p < pEnd)
    {
        *p = TRAITS::Null();
        p++;
    }

    // Move all entries over to new table.

    for (Iterator i = Begin(), end = End(); i != end; i++)
    {
        const element_t & cur = (*i);
        if (!TRAITS::IsNull(cur) && !TRAITS::IsDeleted(cur))
            Add(newTable, newTableSize, cur);
    }

    // @todo:
    // We might want to try to delay this cleanup to allow asynchronous readers

    delete [] m_table;

    m_table = PTR_element_t(newTable);
    m_tableSize = newTableSize;
    m_tableMax = (count_t) (newTableSize * TRAITS::s_density_factor_numerator / TRAITS::s_density_factor_denominator);
    m_tableOccupied = m_tableCount;

    return true;
}

template <typename TRAITS>
const typename SHash<TRAITS>::element_t * SHash<TRAITS>::Lookup(PTR_element_t table, count_t tableSize, key_t key)
{
    if (tableSize == 0)
        return NULL;

    count_t hash = TRAITS::Hash(key);
    count_t index = hash % tableSize; 
    count_t increment = 0; // delay computation

    while (true)
    {
        element_t& current = table[index];
            
        if (TRAITS::IsNull(current))
            return NULL;

        if (!TRAITS::IsDeleted(current)
            && TRAITS::Equals(key, TRAITS::GetKey(current)))
        {
            return &current;
        }

        if (increment == 0)
            increment = (hash % (tableSize-1)) + 1; 

        index += increment;
        if (index >= tableSize)
            index -= tableSize;
    }
}

template <typename TRAITS>
bool SHash<TRAITS>::Add(element_t *table, count_t tableSize, const element_t &element)
{
    key_t key = TRAITS::GetKey(element);

    count_t hash = TRAITS::Hash(key);
    count_t index = hash % tableSize; 
    count_t increment = 0; // delay computation

    while (true)
    {
        element_t& current = table[index];
            
        if (TRAITS::IsNull(current))
        {
            table[index] = element;
            return true;
        }

        if (TRAITS::IsDeleted(current))
        {
            table[index] = element;
            return false;
        }

        if (increment == 0)
            increment = (hash % (tableSize-1)) + 1; 

        index += increment;
        if (index >= tableSize)
            index -= tableSize;
    }
}

template <typename TRAITS>
void SHash<TRAITS>::AddOrReplace(element_t *table, count_t tableSize, const element_t &element)
{
    ASSERT(!TRAITS::s_supports_remove);        

    key_t key = TRAITS::GetKey(element);

    count_t hash = TRAITS::Hash(key);
    count_t index = hash % tableSize; 
    count_t increment = 0; // delay computation

    while (true)
    {
        element_t& current = table[index];
        ASSERT(!TRAITS::IsDeleted(current));    
 
        if (TRAITS::IsNull(current))
        {
            table[index] = element;
            m_tableCount++;
            m_tableOccupied++;
            return;
        }
        else if (TRAITS::Equals(key, TRAITS::GetKey(current)))
        {
            table[index] = element;
            return;
        }

        if (increment == 0)
            increment = (hash % (tableSize-1)) + 1; 

        index += increment;
        if (index >= tableSize)
            index -= tableSize;
    }
}

#ifdef _MSC_VER
#pragma warning (disable: 4702) // Workaround bogus unreachable code warning
#endif
template <typename TRAITS>
void SHash<TRAITS>::Remove(element_t *table, count_t tableSize, key_t key)
{
    ASSERT(TRAITS::s_supports_remove);
    ASSERT(Lookup(table, tableSize, key) != NULL);

    count_t hash = TRAITS::Hash(key);
    count_t index = hash % tableSize; 
    count_t increment = 0; // delay computation

    while (true)
    {
        element_t& current = table[index];
            
        if (TRAITS::IsNull(current))
            return;

        if (!TRAITS::IsDeleted(current)
            && TRAITS::Equals(key, TRAITS::GetKey(current)))
        {
            table[index] = TRAITS::Deleted();
      	    m_tableCount--;
            return;
        }

        if (increment == 0)
            increment = (hash % (tableSize-1)) + 1; 

        index += increment;
        if (index >= tableSize)
            index -= tableSize;
    }
}
#ifdef _MSC_VER
#pragma warning (default: 4702)
#endif

template <typename TRAITS>
void SHash<TRAITS>::RemoveElement(element_t *table, count_t tableSize, element_t *element)
{
    ASSERT(TRAITS::s_supports_remove);
    ASSERT(table <= element && element < table + tableSize);
    ASSERT(!TRAITS::IsNull(*element) && !TRAITS::IsDeleted(*element));

    *element = TRAITS::Deleted();
    m_tableCount--;
}

template <typename TRAITS>
bool SHash<TRAITS>::IsPrime(count_t number)
{
    // This is a very low-tech check for primality, which doesn't scale very well.  
    // There are more efficient tests if this proves to be burdensome for larger
    // tables.

    if ((number&1) == 0)
        return false;

    count_t factor = 3;
    while (factor * factor <= number)
    {
        if ((number % factor) == 0)
            return false;
        factor += 2;
    }

    return true;
}

namespace
{
    const UInt32 g_shash_primes[] = {
        11,17,23,29,37,47,59,71,89,107,131,163,197,239,293,353,431,521,631,761,919,
        1103,1327,1597,1931,2333,2801,3371,4049,4861,5839,7013,8419,10103,12143,14591,
        17519,21023,25229,30293,36353,43627,52361,62851,75431,90523, 108631, 130363, 
        156437, 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403,
        968897, 1162687, 1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 
        4999559, 5999471, 7199369 };
}


// Returns a prime larger than 'number' or 0, in case of overflow
template <typename TRAITS>
typename SHash<TRAITS>::count_t SHash<TRAITS>::NextPrime(typename SHash<TRAITS>::count_t number)
{
    for (int i = 0; i < (int) (sizeof(g_shash_primes) / sizeof(g_shash_primes[0])); i++)
    {
        if (g_shash_primes[i] >= number)
            return (typename SHash<TRAITS>::count_t)(g_shash_primes[i]);
    }

    if ((number&1) == 0)
        number++;

    while (number != 1)
    {
        if (IsPrime(number))
            return number;
        number += 2;
    }

    return 0;
}

// restore "Conditional expression is constant" warning to default value
#pragma warning(default:4127)