Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hashtable-open-addressing
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
David Nieder
hashtable-open-addressing
Commits
ac0b3d15
Commit
ac0b3d15
authored
2 years ago
by
David Nieder
Browse files
Options
Downloads
Patches
Plain Diff
hashing/probing as function objects
parent
ad3e4894
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/hashing.h
+63
-0
63 additions, 0 deletions
src/hashing.h
tests/CMakeLists.txt
+3
-0
3 additions, 0 deletions
tests/CMakeLists.txt
tests/test_hashing.cpp
+102
-0
102 additions, 0 deletions
tests/test_hashing.cpp
with
168 additions
and
0 deletions
src/hashing.h
0 → 100644
+
63
−
0
View file @
ac0b3d15
#ifndef HASHING_H
#define HASHING_H
#include
<cmath>
/*
* hashing by division
*/
template
<
typename
T
>
struct
DivHashing
{
const
size_t
m
;
DivHashing
(
size_t
table_size
)
:
m
(
table_size
)
{}
size_t
operator
()(
T
key
)
{
return
key
%
m
;
}
};
/*
* hashing by multiplication
*/
template
<
typename
T
>
struct
MulHashing
{
const
double
gratio
=
0.61803
;
const
size_t
m
;
MulHashing
(
size_t
table_size
)
:
m
(
table_size
)
{}
size_t
operator
()(
T
key
)
{
return
m
*
(
gratio
*
key
-
std
::
floor
(
gratio
*
key
));
}
};
/*
* linear probing
*/
struct
LinearProbing
{
const
size_t
h
;
const
size_t
m
;
LinearProbing
(
size_t
hash
,
size_t
table_size
)
:
h
(
hash
),
m
(
table_size
)
{}
size_t
operator
()(
size_t
i
)
{
return
(
h
+
i
)
%
m
;
}
};
/*
* quadratic probing
*/
struct
QuadraticProbing
{
const
size_t
h
;
const
size_t
m
;
QuadraticProbing
(
size_t
hash
,
size_t
table_size
)
:
h
(
hash
),
m
(
table_size
)
{}
size_t
operator
()(
size_t
i
)
{
return
(
h
+
i
*
i
)
%
m
;
}
};
/*
* double hashing
*/
struct
DoubleHashing
{
const
size_t
h
;
const
size_t
m
;
DoubleHashing
(
size_t
hash
,
size_t
table_size
)
:
h
(
hash
),
m
(
table_size
)
{}
size_t
operator
()(
size_t
i
)
{
return
(
h
+
i
*
(
h
+
1
))
%
m
;
}
};
#endif
This diff is collapsed.
Click to expand it.
tests/CMakeLists.txt
+
3
−
0
View file @
ac0b3d15
...
...
@@ -6,12 +6,15 @@ include(CTest)
# the executables that run the tests
add_executable
(
test_main test_main.cpp
)
add_executable
(
test_hashing test_hashing.cpp
)
add_executable
(
test_hashtable test_constructor.cpp test_hashtable.cpp
)
# link needed libraries
target_link_libraries
(
test_main gtest gtest_main
)
target_link_libraries
(
test_hashing gtest gtest_main
)
target_link_libraries
(
test_hashtable hashtable gtest gtest_main
)
# run with ctest
add_test
(
NAME test_main COMMAND
${
EXECUTABLE_OUTPUT_PATH
}
/test_main
)
add_test
(
NAME test_hashing COMMAND
${
EXECUTABLE_OUTPUT_PATH
}
/test_hashing
)
add_test
(
NAME test_hashtable COMMAND
${
EXECUTABLE_OUTPUT_PATH
}
/test_hashtable
)
This diff is collapsed.
Click to expand it.
tests/test_hashing.cpp
0 → 100644
+
102
−
0
View file @
ac0b3d15
#include
<gtest/gtest.h>
#include
"hashing.h"
TEST
(
hashing
,
DivHashing
)
{
DivHashing
<
unsigned
>
hash0
(
1
);
DivHashing
<
unsigned
>
hash1
(
7
);
DivHashing
<
unsigned
>
hash2
(
23
);
DivHashing
<
unsigned
>
hash3
(
512
);
DivHashing
<
unsigned
>
hash4
(
6899
);
for
(
unsigned
i
=
0
;
i
<
65535
;
i
++
)
{
EXPECT_EQ
(
i
%
1
,
hash0
(
i
));
EXPECT_EQ
(
i
%
7
,
hash1
(
i
));
EXPECT_EQ
(
i
%
23
,
hash2
(
i
));
EXPECT_EQ
(
i
%
512
,
hash3
(
i
));
EXPECT_EQ
(
i
%
6899
,
hash4
(
i
));
}
}
TEST
(
hashing
,
MulHashing
)
{
const
double
a
=
0.61803
;
using
std
::
floor
;
#define H(k,m) (floor((m*((a*k)-floor(a*k)))))
MulHashing
<
unsigned
>
hash0
(
1
);
MulHashing
<
unsigned
>
hash1
(
7
);
MulHashing
<
unsigned
>
hash2
(
23
);
MulHashing
<
unsigned
>
hash3
(
512
);
MulHashing
<
unsigned
>
hash4
(
6899
);
for
(
unsigned
i
=
0
;
i
<
65535
;
i
++
)
{
EXPECT_EQ
(
H
(
i
,
1
),
hash0
(
i
));
EXPECT_EQ
(
H
(
i
,
7
),
hash1
(
i
));
EXPECT_EQ
(
H
(
i
,
23
),
hash2
(
i
));
EXPECT_EQ
(
H
(
i
,
512
),
hash3
(
i
));
EXPECT_EQ
(
H
(
i
,
6899
),
hash4
(
i
));
}
}
TEST
(
hashing
,
LinearProbing
)
{
LinearProbing
p0
(
2
,
1
);
LinearProbing
p1
(
4
,
512
);
LinearProbing
p2
(
0
,
8
);
LinearProbing
p3
(
7
,
32
);
LinearProbing
p4
(
16
,
6899
);
for
(
unsigned
i
=
0
;
i
<
10000
;
i
++
)
{
EXPECT_LT
(
p0
(
i
),
1
);
EXPECT_EQ
(
p0
(
i
),
(
2
+
i
)
%
1
);
EXPECT_LT
(
p1
(
i
),
512
);
EXPECT_EQ
(
p1
(
i
),
(
4
+
i
)
%
512
);
EXPECT_LT
(
p2
(
i
),
8
);
EXPECT_EQ
(
p2
(
i
),
(
0
+
i
)
%
8
);
EXPECT_LT
(
p3
(
i
),
32
);
EXPECT_EQ
(
p3
(
i
),
(
7
+
i
)
%
32
);
EXPECT_LT
(
p4
(
i
),
6899
);
EXPECT_EQ
(
p4
(
i
),
(
16
+
i
)
%
6899
);
}
}
TEST
(
hashing
,
QuadraticProbing
)
{
QuadraticProbing
p0
(
2
,
1
);
QuadraticProbing
p1
(
4
,
512
);
QuadraticProbing
p2
(
0
,
8
);
QuadraticProbing
p3
(
7
,
32
);
QuadraticProbing
p4
(
16
,
6899
);
for
(
unsigned
i
=
0
;
i
<
10000
;
i
++
)
{
EXPECT_LT
(
p0
(
i
),
1
);
EXPECT_EQ
(
p0
(
i
),
(
2
+
i
*
i
)
%
1
);
EXPECT_LT
(
p1
(
i
),
512
);
EXPECT_EQ
(
p1
(
i
),
(
4
+
i
*
i
)
%
512
);
EXPECT_LT
(
p2
(
i
),
8
);
EXPECT_EQ
(
p2
(
i
),
(
0
+
i
*
i
)
%
8
);
EXPECT_LT
(
p3
(
i
),
32
);
EXPECT_EQ
(
p3
(
i
),
(
7
+
i
*
i
)
%
32
);
EXPECT_LT
(
p4
(
i
),
6899
);
EXPECT_EQ
(
p4
(
i
),
(
16
+
i
*
i
)
%
6899
);
}
}
TEST
(
hashing
,
DoubleHashing
)
{
DoubleHashing
p0
(
2
,
1
);
DoubleHashing
p1
(
4
,
512
);
DoubleHashing
p2
(
0
,
8
);
DoubleHashing
p3
(
7
,
32
);
DoubleHashing
p4
(
16
,
6899
);
for
(
unsigned
i
=
0
;
i
<
10000
;
i
++
)
{
EXPECT_LT
(
p0
(
i
),
1
);
EXPECT_EQ
(
p0
(
i
),
(
2
+
i
*
(
2
+
1
))
%
1
);
EXPECT_LT
(
p1
(
i
),
512
);
EXPECT_EQ
(
p1
(
i
),
(
4
+
i
*
(
4
+
1
))
%
512
);
EXPECT_LT
(
p2
(
i
),
8
);
EXPECT_EQ
(
p2
(
i
),
(
0
+
i
*
(
0
+
1
))
%
8
);
EXPECT_LT
(
p3
(
i
),
32
);
EXPECT_EQ
(
p3
(
i
),
(
7
+
i
*
(
7
+
1
))
%
32
);
EXPECT_LT
(
p4
(
i
),
6899
);
EXPECT_EQ
(
p4
(
i
),
(
16
+
i
*
(
16
+
1
))
%
6899
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment