1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/* Philip T.L.C. Clausen Jan 2017 plan@dtu.dk */
/*
* Copyright (c) 2017, Philip Clausen, Technical University of Denmark
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _XOPEN_SOURCE 600
#include <stdlib.h>
#include "hashmap.h"
#include "hashtable.h"
#include "pherror.h"
#include "stdstat.h"
int (*hashMap_add)(HashMap *, long unsigned, unsigned);
unsigned * (*hashMapGet)(HashMap *, long unsigned);
void (*addUniqueValues)(HashMap *, long unsigned, unsigned *);
unsigned * (*updateValuePtr)(unsigned *, unsigned);
HashMap * hashMap_initialize(const long unsigned size, const unsigned kmersize, const unsigned mlen, const unsigned flag) {
HashMap *src;
src = smalloc(sizeof(HashMap));
src->size = size;
src->n = 0;
src->mask = 0xFFFFFFFFFFFFFFFF >> (64 - (mlen << 1));
src->kmersize = kmersize;
src->prefix_len = 0;
src->prefix = 0;
src->mlen = mlen;
src->flag = flag;
src->DB_size = 1;
if((size - 1) == src->mask) {
src->table = 0;
src->values = calloc(src->size, sizeof(unsigned *));
if(!src->values) {
ERROR();
}
} else {
src->values = 0;
src->table = calloc(src->size, sizeof(HashTable *));
if(!src->table) {
ERROR();
}
}
/* masking */
--src->size;
return src;
}
int megaMap_addKMA(HashMap *templates, long unsigned key, unsigned value) {
unsigned *values;
key &= templates->mask;
values = templates->values[key];
if(values == 0) {
templates->values[key] = updateValuePtr(0, value);
++templates->n;
return 1;
} else if((values = updateValuePtr(values, value))) {
templates->values[key] = values;
return 1;
}
return 0;
}
unsigned * megaMap_getValue(HashMap *templates, long unsigned key) {
return templates->values[key & templates->mask];
}
void hashMap2megaMap(HashMap *templates, HashTable *table) {
HashTable *node, *next;
templates->table = 0;
templates->size = templates->mask + 1;
templates->values = calloc(templates->size, sizeof(unsigned *));
if(!templates->values) {
ERROR();
}
--templates->size;
/* convert table */
for(node = table; node != 0; node = next) {
next = node->next;
/* move values */
templates->values[node->key & templates->mask] = node->value;
/* clean */
free(node);
}
/* clean */
templates->table = 0;
/* set pointers */
hashMap_add = &megaMap_addKMA;
hashMapGet = &megaMap_getValue;
}
unsigned * updateValue(unsigned *values, unsigned value) {
if(!values) {
values = smalloc(2 * sizeof(unsigned));
values[0] = 1;
values[1] = value;
} else if(values[*values] == value) {
return 0;
} else {
values[0]++;
values = realloc(values, (values[0] + 1) * sizeof(unsigned));
if(!values) {
ERROR();
}
values[*values] = value;
}
return values;
}
unsigned * updateShortValue(unsigned *valuesOrg, unsigned value) {
short unsigned *values;
values = (short unsigned *)(valuesOrg);
if(!values) {
values = smalloc(2 * sizeof(short unsigned));
values[0] = 1;
values[1] = value;
} else if(values[*values] == value) {
return 0;
} else {
values[0]++;
values = realloc(values, (values[0] + 1) * sizeof(short unsigned));
if(!values) {
ERROR();
}
values[*values] = value;
}
valuesOrg = (unsigned *)(values);
return valuesOrg;
}
int hashMap_addKMA(HashMap *templates, long unsigned key, unsigned value) {
unsigned *values, flag;
long unsigned index, mask;
HashTable *node, *next, *table;
if(templates->flag) {
murmur(index, key);
index &= templates->size;
} else {
index = key & templates->size;
}
/* check if key exists */
for(node = templates->table[index]; node != 0; node = node->next) {
if(key == node->key) {
if((values = updateValuePtr(node->value, value))) {
node->value = values;
return 1;
} else {
return 0;
}
}
}
/* new value check if there is space */
if(templates->n == templates->size) {
++templates->size;
/* link table */
table = 0;
index = templates->size;
while(index--) {
for(node = templates->table[index]; node != 0; node = next) {
next = node->next;
node->next = table;
table = node;
}
}
free(templates->table);
/* check for megamap */
templates->size <<= 1;
if((templates->mask + 1) <= (templates->size << 1)) {
hashMap2megaMap(templates, table);
return megaMap_addKMA(templates, key, value);
}
/* reallocate */
templates->table = calloc(templates->size, sizeof(HashTable));
if(!templates->table) {
ERROR();
}
mask = --templates->size;
flag = templates->flag;
for(node = table; node != 0; node = next) {
next = node->next;
if(flag) {
murmur(index, node->key);
index &= mask;
} else {
index = node->key & mask;
}
node->next = templates->table[index];
templates->table[index] = node;
}
if(flag) {
murmur(index, key);
index &= mask;
} else {
index = key & mask;
}
}
/* add new value */
node = smalloc(sizeof(HashTable));
/* key */
node->key = key;
/* value */
node->value = updateValuePtr(0, value);
/* push it */
node->next = templates->table[index];
templates->table[index] = node;
++templates->n;
return 1;
}
unsigned * hashMapGetValue(HashMap *templates, long unsigned key) {
long unsigned index;
HashTable *node;
if(templates->flag) {
murmur(index, key);
index &= templates->size;
} else {
index = key & templates->size;
}
for(node = templates->table[index]; node != 0; node = node->next) {
if(key == node->key) {
return node->value;
}
}
return 0;
}
void hashMap_addUniqueValues(HashMap *dest, long unsigned key, unsigned *values) {
long unsigned index;
HashTable *node;
node = smalloc(sizeof(HashTable));
node->key = key;
node->value = values;
if(dest->flag) {
murmur(index, key);
index &= dest->size;
} else {
index = key & dest->size;
}
node->next = dest->table[index];
dest->table[index] = node;
dest->n++;
}
void megaMap_addUniqueValues(HashMap *dest, long unsigned key, unsigned *values) {
dest->values[key & dest->mask] = values;
dest->n++;
}
unsigned * HU2U(unsigned *values) {
int i;
short unsigned *hu_values;
hu_values = (short unsigned *)(values);
values = realloc(values, (hu_values[0] + 1) * sizeof(unsigned));
if(!values) {
ERROR();
} else {
hu_values = (short unsigned *)(values);
}
i = *hu_values + 1;
while(i--) {
values[i] = hu_values[i];
}
return values;
}
void convertToU(HashMap *templates) {
long unsigned index;
HashTable *node;
/* convert values */
index = templates->size + 1;
if(templates->table) {
while(index--) {
for(node = templates->table[index]; node != 0; node = node->next) {
node->value = HU2U(node->value);
}
}
} else {
while(index--) {
if(templates->values[index]) {
templates->values[index] = HU2U(templates->values[index]);
}
}
}
/* set pointers */
updateValuePtr = &updateValue;
}
|