|
1 |
| -#include <iostream> |
2 |
| -#include <fstream> |
| 1 | +#include <stdio.h> |
3 | 2 |
|
4 | 3 | extern "C" {
|
5 | 4 | #include "nmhash.h"
|
@@ -52,114 +51,130 @@ void SpookyHash_seed_state_test(int in_bits, const void *seed, void *state) {
|
52 | 51 | }
|
53 | 52 | }
|
54 | 53 |
|
55 |
| -using namespace std; |
56 |
| - |
57 | 54 | static const int SIZE = 2048;
|
58 | 55 | char * key_array = new char[SIZE];
|
59 | 56 | static const uint32_t NM_SEED = 0xdeadbeef;
|
60 | 57 | static const uint64_t WATER_SEED = 0xdeadbeef1eadbeef;
|
61 | 58 | static const uint32_t PENGY_SEED = 0xdeadbeef;
|
62 | 59 | static const uint64_t SPOOKY_SEED[2] = { WATER_SEED, WATER_SEED };
|
63 | 60 |
|
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); |
69 | 74 | return 1;
|
70 | 75 | }
|
71 |
| - fin.read(key_array, SIZE); |
72 |
| - fin.close(); |
| 76 | + |
| 77 | + fclose(fin); |
73 | 78 | return 0;
|
74 | 79 | }
|
75 | 80 |
|
76 |
| -int write_nmhash32(){ |
| 81 | +int write_nmhash32() { |
77 | 82 | size_t i;
|
78 | 83 | 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"); |
81 | 86 |
|
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"); |
84 | 89 | return 1;
|
85 | 90 | }
|
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 |
89 | 95 | }
|
90 |
| - fout.close(); |
| 96 | + |
| 97 | + fclose(fout); |
91 | 98 | return 0;
|
92 | 99 | }
|
93 | 100 |
|
94 |
| -int write_nmhash32x(){ |
| 101 | +int write_nmhash32x() { |
95 | 102 | size_t i;
|
96 | 103 | 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"); |
99 | 106 |
|
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"); |
102 | 109 | return 1;
|
103 | 110 | }
|
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 |
107 | 115 | }
|
108 |
| - fout.close(); |
| 116 | + |
| 117 | + fclose(fout); |
109 | 118 | return 0;
|
110 | 119 | }
|
111 | 120 |
|
112 |
| -int write_water(){ |
| 121 | +int write_water() { |
113 | 122 | uint32_t i;
|
114 | 123 | 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"); |
117 | 126 |
|
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"); |
120 | 129 | return 1;
|
121 | 130 | }
|
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 |
125 | 135 | }
|
126 |
| - fout.close(); |
| 136 | + |
| 137 | + fclose(fout); |
127 | 138 | return 0;
|
128 | 139 | }
|
129 | 140 |
|
130 | 141 | int write_pengy(){
|
131 | 142 | size_t i;
|
132 | 143 | 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"); |
135 | 146 |
|
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"); |
138 | 149 | return 1;
|
139 | 150 | }
|
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 |
143 | 155 | }
|
144 |
| - fout.close(); |
| 156 | + |
| 157 | + fclose(fout); |
145 | 158 | return 0;
|
146 | 159 | }
|
147 | 160 |
|
148 |
| -int write_spooky(){ |
| 161 | +int write_spooky() { |
149 | 162 | size_t i;
|
150 | 163 | 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"); |
153 | 166 |
|
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"); |
156 | 169 | return 1;
|
157 | 170 | }
|
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 |
161 | 175 | }
|
162 |
| - fout.close(); |
| 176 | + |
| 177 | + fclose(fout); |
163 | 178 | return 0;
|
164 | 179 | }
|
165 | 180 |
|
|
0 commit comments