|
1 |
| -#include <stdio.h> |
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
2 | 3 |
|
3 | 4 | extern "C" {
|
4 | 5 | #include "nmhash.h"
|
@@ -51,130 +52,114 @@ void SpookyHash_seed_state_test(int in_bits, const void *seed, void *state) {
|
51 | 52 | }
|
52 | 53 | }
|
53 | 54 |
|
| 55 | +using namespace std; |
| 56 | + |
54 | 57 | static const int SIZE = 2048;
|
55 | 58 | char * key_array = new char[SIZE];
|
56 | 59 | static const uint32_t NM_SEED = 0xdeadbeef;
|
57 | 60 | static const uint64_t WATER_SEED = 0xdeadbeef1eadbeef;
|
58 | 61 | static const uint32_t PENGY_SEED = 0xdeadbeef;
|
59 | 62 | static const uint64_t SPOOKY_SEED[2] = { WATER_SEED, WATER_SEED };
|
60 | 63 |
|
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); |
| 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; |
74 | 69 | return 1;
|
75 | 70 | }
|
76 |
| - |
77 |
| - fclose(fin); |
| 71 | + fin.read(key_array, SIZE); |
| 72 | + fin.close(); |
78 | 73 | return 0;
|
79 | 74 | }
|
80 | 75 |
|
81 |
| -int write_nmhash32() { |
| 76 | +int write_nmhash32(){ |
82 | 77 | size_t i;
|
83 | 78 | uint32_t hash;
|
84 |
| - const char *outFileName = "c_nmhash32_array.bin"; |
85 |
| - FILE *fout = fopen(outFileName, "wb"); |
| 79 | + string outFileName = "c_nmhash32_array.bin"; |
| 80 | + std::ofstream fout( outFileName, ios::out | ios::binary ); |
86 | 81 |
|
87 |
| - if (!fout) { |
88 |
| - fprintf(stderr, "Cannot open c_nmhash32_array.bin!\n"); |
| 82 | + if (!fout){ |
| 83 | + cout << "Cannot open c_nmhash32_array.bin!" << endl; |
89 | 84 | return 1;
|
90 | 85 | }
|
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 |
| 86 | + for( i=0; i<=SIZE; i+=1 ){ |
| 87 | + hash = NMHASH32((void *) key_array, i, NM_SEED); |
| 88 | + fout.write((char *) &hash, 4); |
95 | 89 | }
|
96 |
| - |
97 |
| - fclose(fout); |
| 90 | + fout.close(); |
98 | 91 | return 0;
|
99 | 92 | }
|
100 | 93 |
|
101 |
| -int write_nmhash32x() { |
| 94 | +int write_nmhash32x(){ |
102 | 95 | size_t i;
|
103 | 96 | uint32_t hash;
|
104 |
| - const char *outFileName = "c_nmhash32x_array.bin"; |
105 |
| - FILE *fout = fopen(outFileName, "wb"); |
| 97 | + string outFileName = "c_nmhash32x_array.bin"; |
| 98 | + std::ofstream fout( outFileName, ios::out | ios::binary ); |
106 | 99 |
|
107 |
| - if (!fout) { |
108 |
| - fprintf(stderr, "Cannot open c_nmhash32x_array.bin!\n"); |
| 100 | + if (!fout){ |
| 101 | + cout << "Cannot open c_nmhash32x_array.bin!" << endl; |
109 | 102 | return 1;
|
110 | 103 | }
|
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 |
| 104 | + for( i=0; i<=SIZE; i+=1 ){ |
| 105 | + hash = NMHASH32X((void *) key_array, i, NM_SEED); |
| 106 | + fout.write((char *) &hash, 4); |
115 | 107 | }
|
116 |
| - |
117 |
| - fclose(fout); |
| 108 | + fout.close(); |
118 | 109 | return 0;
|
119 | 110 | }
|
120 | 111 |
|
121 |
| -int write_water() { |
| 112 | +int write_water(){ |
122 | 113 | uint32_t i;
|
123 | 114 | uint32_t hash;
|
124 |
| - const char *outFileName = "c_water_hash_array.bin"; |
125 |
| - FILE *fout = fopen(outFileName, "wb"); |
| 115 | + string outFileName = "c_water_hash_array.bin"; |
| 116 | + std::ofstream fout( outFileName, ios::out | ios::binary ); |
126 | 117 |
|
127 |
| - if (!fout) { |
128 |
| - fprintf(stderr, "Cannot open c_water_hash_array.bin!\n"); |
| 118 | + if (!fout){ |
| 119 | + cout << "Cannot open c_water_hash_array.bin!" << endl; |
129 | 120 | return 1;
|
130 | 121 | }
|
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 |
| 122 | + for( i=0; i<=SIZE; i+=1 ){ |
| 123 | + hash = waterhash((void *) key_array, i, WATER_SEED); |
| 124 | + fout.write((char *) &hash, 4); |
135 | 125 | }
|
136 |
| - |
137 |
| - fclose(fout); |
| 126 | + fout.close(); |
138 | 127 | return 0;
|
139 | 128 | }
|
140 | 129 |
|
141 | 130 | int write_pengy(){
|
142 | 131 | size_t i;
|
143 | 132 | uint64_t hash;
|
144 |
| - const char *outFileName = "c_pengy_hash_array.bin"; |
145 |
| - FILE *fout = fopen(outFileName, "wb"); |
| 133 | + string outFileName = "c_pengy_hash_array.bin"; |
| 134 | + std::ofstream fout( outFileName, ios::out | ios::binary ); |
146 | 135 |
|
147 |
| - if (!fout) { |
148 |
| - fprintf(stderr, "Cannot open c_pengy_hash_array.bin!\n"); |
| 136 | + if (!fout){ |
| 137 | + cout << "Cannot open c_pengy_hash_array.bin!" << endl; |
149 | 138 | return 1;
|
150 | 139 | }
|
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 |
| 140 | + for( i=0; i<=SIZE; i+=1 ){ |
| 141 | + hash = pengyhash((void *) key_array, i, PENGY_SEED); |
| 142 | + fout.write((char *) &hash, 8); |
155 | 143 | }
|
156 |
| - |
157 |
| - fclose(fout); |
| 144 | + fout.close(); |
158 | 145 | return 0;
|
159 | 146 | }
|
160 | 147 |
|
161 |
| -int write_spooky() { |
| 148 | +int write_spooky(){ |
162 | 149 | size_t i;
|
163 | 150 | uint64_t hash[2];
|
164 |
| - const char *outFileName = "c_spooky_hash_array.bin"; |
165 |
| - FILE *fout = fopen(outFileName, "wb"); |
| 151 | + string outFileName = "c_spooky_hash_array.bin"; |
| 152 | + std::ofstream fout( outFileName, ios::out | ios::binary ); |
166 | 153 |
|
167 |
| - if (!fout) { |
168 |
| - fprintf(stderr, "Cannot open c_spooky_hash_array.bin!\n"); |
| 154 | + if (!fout){ |
| 155 | + cout << "Cannot open c_spooky_hash_array.bin!" << endl; |
169 | 156 | return 1;
|
170 | 157 | }
|
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 |
| 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); |
175 | 161 | }
|
176 |
| - |
177 |
| - fclose(fout); |
| 162 | + fout.close(); |
178 | 163 | return 0;
|
179 | 164 | }
|
180 | 165 |
|
|
0 commit comments