Changeset 6141 for trunk/ARBDB/adhash.c
- Timestamp:
- 14/08/09 16:29:27 (3 years ago)
- Files:
-
- 1 modified
-
trunk/ARBDB/adhash.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ARBDB/adhash.c
r5907 r6141 80 80 static unsigned char bit_val[8] = { 1, 2, 4, 8, 16, 32, 64, 128 }; 81 81 82 static int bit_value(const unsigned char *era stothenes, long num) { // 'num' is odd and lowest 'num' is 382 static int bit_value(const unsigned char *eratosthenes, long num) { // 'num' is odd and lowest 'num' is 3 83 83 long bit_num = ((num-1) >> 1)-1; // 3->0 5->1 7->2 etc. 84 84 long byte_num = bit_num >> 3; // div 8 85 char byte = era stothenes[byte_num];85 char byte = eratosthenes[byte_num]; 86 86 87 87 gb_assert(bit_num >= 0); … … 92 92 return (byte & bit_val[bit_num]) ? 1 : 0; 93 93 } 94 static void set_bit_value(unsigned char *era stothenes, long num, int val) { // 'num' is odd and lowest 'num' is 3; val is 0 or 194 static void set_bit_value(unsigned char *eratosthenes, long num, int val) { // 'num' is odd and lowest 'num' is 3; val is 0 or 1 95 95 long bit_num = ((num-1) >> 1)-1; // 3->0 5->1 7->2 etc. 96 96 long byte_num = bit_num >> 3; // div 8 97 char byte = era stothenes[byte_num];97 char byte = eratosthenes[byte_num]; 98 98 99 99 gb_assert(bit_num >= 0); … … 108 108 byte &= (0xff - bit_val[bit_num]); 109 109 } 110 era stothenes[byte_num] = byte;110 eratosthenes[byte_num] = byte; 111 111 } 112 112 … … 115 115 long bits_needed = CALC_PRIMES_UP_TO/2+1; // only need bits for odd numbers 116 116 long bytes_needed = (bits_needed/8)+1; 117 unsigned char *era stothenes = GB_calloc(bytes_needed, 1); // bit = 1 means "is not a prime"117 unsigned char *eratosthenes = GB_calloc(bytes_needed, 1); // bit = 1 means "is not a prime" 118 118 long prime_count = 0; 119 119 long num; 120 120 121 printf("era stothenes' size = %li\n", bytes_needed);122 123 if (!era stothenes) {121 printf("eratosthenes' size = %li\n", bytes_needed); 122 123 if (!eratosthenes) { 124 124 GB_internal_error("out of memory"); 125 125 return; … … 127 127 128 128 for (num = 3; num <= CALC_PRIMES_UP_TO; num += 2) { 129 if (bit_value(era stothenes, num) == 0) { // is a prime number129 if (bit_value(eratosthenes, num) == 0) { // is a prime number 130 130 long num2; 131 131 prime_count++; 132 132 for (num2 = num*2; num2 <= CALC_PRIMES_UP_TO; num2 += num) { // with all multiples 133 133 if ((num2&1) == 1) { // skip even numbers 134 set_bit_value(era stothenes, num2, 1);134 set_bit_value(eratosthenes, num2, 1); 135 135 } 136 136 } … … 147 147 148 148 for (num = 3; num <= CALC_PRIMES_UP_TO; num += 2) { 149 if (bit_value(era stothenes, num) == 0) { // is a prime number149 if (bit_value(eratosthenes, num) == 0) { // is a prime number 150 150 long diff = num-last_prime; 151 151 if ((diff*PRIME_UNDENSITY)<num) { 152 set_bit_value(era stothenes, num, 1); // delete unneeded prime152 set_bit_value(eratosthenes, num, 1); // delete unneeded prime 153 153 } 154 154 else { … … 166 166 index = 0; 167 167 for (num = 3; num <= CALC_PRIMES_UP_TO; num += 2) { 168 if (bit_value(era stothenes, num) == 0) { // is a prime number168 if (bit_value(eratosthenes, num) == 0) { // is a prime number 169 169 if (printed>128) { 170 170 printf("\n "); … … 182 182 } 183 183 184 free(era stothenes);184 free(eratosthenes); 185 185 } 186 186 fflush(stdout); … … 263 263 264 264 GB_HASH *GBS_create_dynaval_hash(long user_size, GB_CASE case_sens, void (*freefun)(long)) { 265 /* like GBS_create_hash, but values stored in hash get free 'd using 'freefun'265 /* like GBS_create_hash, but values stored in hash get freed using 'freefun' 266 266 */ 267 267 GB_HASH *hs = GBS_create_hash(user_size, case_sens); … … 447 447 else e->val = val; 448 448 449 if (!copyKey) free(key); // already had an entry -> delete u sused mem449 if (!copyKey) free(key); // already had an entry -> delete unused mem 450 450 } 451 451 else if (val != 0) { // don't store 0 … … 460 460 } 461 461 else { 462 if (!copyKey) free(key); // don't need an entry -> delete u sused mem462 if (!copyKey) free(key); // don't need an entry -> delete unused mem 463 463 } 464 464 return oldval; … … 814 814 long x; 815 815 x = (key * (long long)97)%size; // make one multiplier a (long long) to avoid 816 if (x<0) x+= size; // int overflow and abort if compi eld with -ftrapv816 if (x<0) x+= size; // int overflow and abort if compiled with -ftrapv 817 817 return x; 818 818 } … … 997 997 cs->entries[i].next = cs->firstfree_entry; 998 998 cs->firstfree_entry = i; 999 /* delete all unused memory s*/999 /* delete all unused memory */ 1000 1000 if (data || ( needed_size != cs->entries[i].sizeof_data) ) { 1001 1001 free(cs->entries[i].data);
