Skip to content

Commit d396933

Browse files
committed
Merge branch 'hash_functions_c' of https://github.com/perazz/stdlib into hash_functions_c
2 parents 74046fa + e0977db commit d396933

File tree

2 files changed

+80
-56
lines changed

2 files changed

+80
-56
lines changed

test/hash_functions/CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,22 @@ target_sources(
1414
waterhash.c
1515
generate_hash_arrays.cpp
1616
)
17+
1718
if(CMAKE_Fortran_COMPILER_ID MATCHES "^Intel")
19+
20+
# Set the C++ standard to prevent icpc breakage
21+
set(CMAKE_CXX_STANDARD 11)
22+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
23+
set(CMAKE_CXX_EXTENSIONS OFF)
24+
1825
set_target_properties(test_hash_functions PROPERTIES LINKER_LANGUAGE Fortran)
1926
endif()
27+
2028
if(CMAKE_Fortran_COMPILER_ID STREQUAL GNU AND CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 10.0)
2129
target_compile_options(
2230
test_hash_functions
2331
PRIVATE
2432
$<$<COMPILE_LANGUAGE:Fortran>:-fno-range-check>
25-
)
33+
)
2634
endif()
35+

test/hash_functions/generate_hash_arrays.cpp

+70-55
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include <iostream>
2-
#include <fstream>
1+
#include <stdio.h>
32

43
extern "C" {
54
#include "nmhash.h"
@@ -52,114 +51,130 @@ void SpookyHash_seed_state_test(int in_bits, const void *seed, void *state) {
5251
}
5352
}
5453

55-
using namespace std;
56-
5754
static const int SIZE = 2048;
5855
char * key_array = new char[SIZE];
5956
static const uint32_t NM_SEED = 0xdeadbeef;
6057
static const uint64_t WATER_SEED = 0xdeadbeef1eadbeef;
6158
static const uint32_t PENGY_SEED = 0xdeadbeef;
6259
static const uint64_t SPOOKY_SEED[2] = { WATER_SEED, WATER_SEED };
6360

64-
int read_keys(){
65-
string inFileName = "key_array.bin";
66-
std::ifstream fin( inFileName, ios::in | ios::binary );
67-
if (!fin){
68-
cout << "Cannot open key_array.bin!" << endl;
61+
int read_keys() {
62+
const char *inFileName = "key_array.bin";
63+
FILE *fin = fopen(inFileName, "rb");
64+
65+
if (!fin) {
66+
fprintf(stderr, "Cannot open key_array.bin!\n");
67+
return 1;
68+
}
69+
70+
size_t bytesRead = fread(key_array, 1, SIZE, fin);
71+
if (bytesRead != SIZE) {
72+
fprintf(stderr, "Error reading key_array.bin! Only %zu bytes read.\n", bytesRead);
73+
fclose(fin);
6974
return 1;
7075
}
71-
fin.read(key_array, SIZE);
72-
fin.close();
76+
77+
fclose(fin);
7378
return 0;
7479
}
7580

76-
int write_nmhash32(){
81+
int write_nmhash32() {
7782
size_t i;
7883
uint32_t hash;
79-
string outFileName = "c_nmhash32_array.bin";
80-
std::ofstream fout( outFileName, ios::out | ios::binary );
84+
const char *outFileName = "c_nmhash32_array.bin";
85+
FILE *fout = fopen(outFileName, "wb");
8186

82-
if (!fout){
83-
cout << "Cannot open c_nmhash32_array.bin!" << endl;
87+
if (!fout) {
88+
fprintf(stderr, "Cannot open c_nmhash32_array.bin!\n");
8489
return 1;
8590
}
86-
for( i=0; i<=SIZE; i+=1 ){
87-
hash = NMHASH32((void *) key_array, i, NM_SEED);
88-
fout.write((char *) &hash, 4);
91+
92+
for (i = 0; i <= SIZE; i++) {
93+
hash = NMHASH32((const void *)key_array, i, NM_SEED);
94+
fwrite(&hash, sizeof(uint32_t), 1, fout); // Write 4 bytes (1 uint32_t) to the file
8995
}
90-
fout.close();
96+
97+
fclose(fout);
9198
return 0;
9299
}
93100

94-
int write_nmhash32x(){
101+
int write_nmhash32x() {
95102
size_t i;
96103
uint32_t hash;
97-
string outFileName = "c_nmhash32x_array.bin";
98-
std::ofstream fout( outFileName, ios::out | ios::binary );
104+
const char *outFileName = "c_nmhash32x_array.bin";
105+
FILE *fout = fopen(outFileName, "wb");
99106

100-
if (!fout){
101-
cout << "Cannot open c_nmhash32x_array.bin!" << endl;
107+
if (!fout) {
108+
fprintf(stderr, "Cannot open c_nmhash32x_array.bin!\n");
102109
return 1;
103110
}
104-
for( i=0; i<=SIZE; i+=1 ){
105-
hash = NMHASH32X((void *) key_array, i, NM_SEED);
106-
fout.write((char *) &hash, 4);
111+
112+
for (i = 0; i <= SIZE; i++) {
113+
hash = NMHASH32X((const void *)key_array, i, NM_SEED);
114+
fwrite(&hash, sizeof(uint32_t), 1, fout); // Write 4 bytes (1 uint32_t) to the file
107115
}
108-
fout.close();
116+
117+
fclose(fout);
109118
return 0;
110119
}
111120

112-
int write_water(){
121+
int write_water() {
113122
uint32_t i;
114123
uint32_t hash;
115-
string outFileName = "c_water_hash_array.bin";
116-
std::ofstream fout( outFileName, ios::out | ios::binary );
124+
const char *outFileName = "c_water_hash_array.bin";
125+
FILE *fout = fopen(outFileName, "wb");
117126

118-
if (!fout){
119-
cout << "Cannot open c_water_hash_array.bin!" << endl;
127+
if (!fout) {
128+
fprintf(stderr, "Cannot open c_water_hash_array.bin!\n");
120129
return 1;
121130
}
122-
for( i=0; i<=SIZE; i+=1 ){
123-
hash = waterhash((void *) key_array, i, WATER_SEED);
124-
fout.write((char *) &hash, 4);
131+
132+
for (i = 0; i <= SIZE; i++) {
133+
hash = waterhash((const void *)key_array, i, WATER_SEED);
134+
fwrite(&hash, sizeof(uint32_t), 1, fout); // Write 4 bytes (1 uint32_t) to the file
125135
}
126-
fout.close();
136+
137+
fclose(fout);
127138
return 0;
128139
}
129140

130141
int write_pengy(){
131142
size_t i;
132143
uint64_t hash;
133-
string outFileName = "c_pengy_hash_array.bin";
134-
std::ofstream fout( outFileName, ios::out | ios::binary );
144+
const char *outFileName = "c_pengy_hash_array.bin";
145+
FILE *fout = fopen(outFileName, "wb");
135146

136-
if (!fout){
137-
cout << "Cannot open c_pengy_hash_array.bin!" << endl;
147+
if (!fout) {
148+
fprintf(stderr, "Cannot open c_pengy_hash_array.bin!\n");
138149
return 1;
139150
}
140-
for( i=0; i<=SIZE; i+=1 ){
141-
hash = pengyhash((void *) key_array, i, PENGY_SEED);
142-
fout.write((char *) &hash, 8);
151+
152+
for (i = 0; i <= SIZE; i++) {
153+
hash = pengyhash((const void *)key_array, i, PENGY_SEED);
154+
fwrite(&hash, sizeof(uint64_t), 1, fout); // Write 8 bytes (1 uint64_t) to the file
143155
}
144-
fout.close();
156+
157+
fclose(fout);
145158
return 0;
146159
}
147160

148-
int write_spooky(){
161+
int write_spooky() {
149162
size_t i;
150163
uint64_t hash[2];
151-
string outFileName = "c_spooky_hash_array.bin";
152-
std::ofstream fout( outFileName, ios::out | ios::binary );
164+
const char *outFileName = "c_spooky_hash_array.bin";
165+
FILE *fout = fopen(outFileName, "wb");
153166

154-
if (!fout){
155-
cout << "Cannot open c_spooky_hash_array.bin!" << endl;
167+
if (!fout) {
168+
fprintf(stderr, "Cannot open c_spooky_hash_array.bin!\n");
156169
return 1;
157170
}
158-
for( i=0; i<=SIZE; i+=1 ){
159-
SpookyHash128_with_state_test((void *) key_array, i, (void *) SPOOKY_SEED, (void *) hash);
160-
fout.write((char *) hash, 16);
171+
172+
for (i = 0; i <= SIZE; i++) {
173+
SpookyHash128_with_state_test((const void *)key_array, i, (const void *)SPOOKY_SEED, (void *)hash);
174+
fwrite(hash, sizeof(uint64_t), 2, fout); // Write 16 bytes (2 * 8 bytes) to the file
161175
}
162-
fout.close();
176+
177+
fclose(fout);
163178
return 0;
164179
}
165180

0 commit comments

Comments
 (0)