-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeparateChaining.cpp
More file actions
59 lines (47 loc) · 905 Bytes
/
SeparateChaining.cpp
File metadata and controls
59 lines (47 loc) · 905 Bytes
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
#include "SeparateChaining.h"
#include <iostream>
using namespace std;
/**
* Internal method to test if a positive number is prime.
* Not an efficient algorithm.
*/
bool isPrime( int n )
{
if( n == 2 || n == 3 )
return true;
if( n == 1 || n % 2 == 0 )
return false;
for( int i = 3; i * i <= n; i += 2 )
if( n % i == 0 )
return false;
return true;
}
/**
* Internal method to return a prime number at least as large as n.
* Assumes n > 0.
*/
int nextPrime( int n )
{
if( n % 2 == 0 )
++n;
for( ; !isPrime( n ); n += 2 )
;
return n;
}
/**
* A hash routine for string objects.
*/
size_t hash( const string & key )
{
size_t hashVal = 0;
for( char ch : key )
hashVal = 37 * hashVal + ch;
return hashVal;
}
/**
* A hash routine for ints.
*/
size_t hash( long long key )
{
return key;
}