File: hash64.c

package info (click to toggle)
r-cran-bit64 0.9-7-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 876 kB
  • sloc: ansic: 6,405; perl: 24; sh: 7; makefile: 5
file content (541 lines) | stat: -rw-r--r-- 13,531 bytes parent folder | download
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
# C-Code for hashing and matching
# S3 atomic 64bit integers for R
# (c) 2011 Jens Oehlschägel
# Licence: GPL2
# Provided 'as is', use at your own risk
# Created: 2011-12-11
# Last changed:  2012-10-22
#*/

/* for speed (should not really matter in this case as most time is spent in the hashing) */
// #define USE_RINTERNALS 1
#include <Rinternals.h>
#include <R.h>

//#include "timing.h"

// This multiplicator was used in Simon Urbanek's package fastmatch for 32-bit integers
//#define HASH64(X, SHIFT) (314159265358979323ULL * ((unsigned long long)(X)) >> (SHIFT))
// This multiplicator seems to work fine with 64bit integers
#define HASH64(X, SHIFT) (0x9e3779b97f4a7c13ULL * ((unsigned long long)(X)) >> (SHIFT))

SEXP hashfun_integer64(SEXP x_, SEXP bits_, SEXP ret_){
  int i, n = LENGTH(x_);
  long long * x = (long long *) REAL(x_);
  unsigned int * ret = (unsigned int *) INTEGER(ret_);
  int shift = 64 - asInteger(bits_);
  for(i=0; i<n; i++){
	ret[i] = (unsigned int) HASH64(x[i], shift);
  }
  return ret_;
}

// this function is loosely following Simon Urbanek's package 'fastmatch'
SEXP hashmap_integer64(SEXP x_, SEXP bits_, SEXP hashpos_, SEXP nunique_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  long long * x = (long long *) REAL(x_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  long long v;
  int nunique = 0;
  for(i=0; i<nx; ){
    v = x[i++];
	h = HASH64(v, shift);
	while (hashpos[h] && x[hashpos[h] - 1] != v){
		h++;
		if (h == nh) 
			h = 0;
	  }
	  if (!hashpos[h]){
      hashpos[h] = i;
      nunique++;
	  }
  }
  INTEGER(nunique_)[0] = nunique;
  return R_NilValue;
}

SEXP hashpos_integer64(SEXP x_, SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP nomatch_, SEXP ret_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  long long * x = (long long *) REAL(x_);
  long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int * ret = INTEGER(ret_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  int nomatch = asInteger(nomatch_);
  long long v;
  for(i=0; i<nx; i++){
    v = x[i];
	h = HASH64(v, shift);
    for(;;){
	  if (hashpos[h]){  // this is mostly while(hashpos[h]) but we want to catch failure for the nomatch assignment
		  if (hashdat[hashpos[h] - 1] == v){
			ret[i] = hashpos[h];
			break;
		  }
		  h++;
		  if (h == nh) 
			h = 0;
	  }else{
	    ret[i] = nomatch;
		break;
	  }
	}
  }
  return R_NilValue;
}

SEXP hashrev_integer64(SEXP x_, SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP nunique_, SEXP nomatch_, SEXP ret_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  int nd = LENGTH(hashdat_);
  long long * x = (long long *) REAL(x_);
  long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int * ret = INTEGER(ret_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  int nomatch = asInteger(nomatch_);
  int nunique = asInteger(nunique_);
  int iunique=0;
  long long v;
  for(i=0; i<nx; ){
	v = x[i++];
	h = HASH64(v, shift);
	while(hashpos[h]){
	  if (hashdat[hashpos[h] - 1] == v){
	    h = hashpos[h] - 1;
		if (!ret[h]){
			ret[h] = i;
			if (++iunique==nunique)
			  i=nx; // break out of for as well
		}
		break;
	  }
	  h++;
	  if (h == nh) 
		h = 0;
	}
  }
  if (iunique<nd){
    if (nunique<nd){ // some gaps are duplicates
	  for(i=0; i<nd; i++){
	    if (!ret[i]){
			v = hashdat[i];
			h = HASH64(v, shift);
			while(hashpos[h]){  // this is mostly while(hashpos[h]) but we want to catch failure for the nomatch assignment
			  if (hashdat[hashpos[h] - 1] == v){
			    h = ret[hashpos[h] - 1];
				if (h)
				  ret[i] = h;
				else
				  ret[i] = nomatch;
				break;
			  }
			  h++;
			  if (h == nh) 
				h = 0;
			}
		}
	  }
	}else{ // no duplicates: all gaps are nomatches
	  for(i=0; i<nd; i++)
	    if (!ret[i])
		  ret[i] = nomatch;
	}
  }
  return R_NilValue;
}

SEXP hashrin_integer64(SEXP x_, SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP nunique_, SEXP ret_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  int nd = LENGTH(hashdat_);
  long long * x = (long long *) REAL(x_);
  long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int * ret = INTEGER(ret_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  int nunique = asInteger(nunique_);
  int iunique=0;
  long long v;
  for(i=0; i<nx; ){
	v = x[i++];
	h = HASH64(v, shift);
	while(hashpos[h]){
	  if (hashdat[hashpos[h] - 1] == v){
	    h = hashpos[h] - 1;
		if (!ret[h]){
			ret[h] = TRUE;
			if (++iunique==nunique)
			  i=nx; // break out of for as well
		}
		break;
	  }
	  h++;
	  if (h == nh) 
		h = 0;
	}
  }
    if (nunique<nd){ // some gaps are duplicates
	  for(i=0; i<nd; i++){
	    if (!ret[i]){
			v = hashdat[i];
			h = HASH64(v, shift);
			while(hashpos[h]){  // this is mostly while(hashpos[h]) but we want to catch failure for the nomatch assignment
			  if (hashdat[hashpos[h] - 1] == v){
			    h = ret[hashpos[h] - 1];
				if (h)
				  ret[i] = TRUE;
				break;
			  }
			  h++;
			  if (h == nh) 
				h = 0;
			}
		}
	  }
	}
  return R_NilValue;
}

SEXP hashfin_integer64(SEXP x_, SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP ret_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  long long * x = (long long *) REAL(x_);
  long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int * ret = LOGICAL(ret_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  long long v;
  for(i=0; i<nx; i++){
    v = x[i];
	h = HASH64(v, shift);
    for(;;){
	  if (hashpos[h]){  // this is mostly while(hashpos[h]) but we want to catch failure for the nomatch assignment
		  if (hashdat[hashpos[h] - 1] == v){
			ret[i] = TRUE;
			break;
		  }
		  h++;
		  if (h == nh) 
			h = 0;
	  }else{
	    ret[i] = FALSE;
		break;
	  }
	}
  }
  return R_NilValue;
}

SEXP hashdup_integer64(SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP nunique_, SEXP ret_){
  int nu = LENGTH(ret_);
  int h, nh = LENGTH(hashpos_);
  //long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int * ret = LOGICAL(ret_);
  int nunique = asInteger(nunique_);
  for(h=0; h<nu; h++)
	ret[h] = TRUE;
  for(h=0; h<nh; h++)
    if (hashpos[h]>0){
	  ret[hashpos[h]-1] = FALSE;
	  nunique--;
	  if (nunique<1)
	    break;
	}
  return R_NilValue;
}

SEXP hashuni_integer64(SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP keep_order_, SEXP ret_){
  int h, nh = LENGTH(hashpos_);
  int u, nu = LENGTH(ret_);
  long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  long long * ret = (long long *) REAL(ret_);
  if (asLogical(keep_order_)){
      int i;
	  // int nx = LENGTH(hashdat_);
	  int bits = asInteger(bits_);
	  int shift = 64 - bits;
	  long long v;
	  for(u=0,i=0; u<nu; i++){
		v = hashdat[i];
		h = HASH64(v, shift);
		while(hashpos[h] && hashdat[hashpos[h] - 1] != v){  // this is mostly while(hashpos[h]) but we want to catch failure for the nomatch assignment
		  h++;
		  if (h == nh) 
			h = 0;
		}
		if (i == (hashpos[h] - 1)){
		  ret[u++] = v; /* unique */
		}
	  }
  }else{
	  for(u=0,h=0; u<nu; h++)
		if (hashpos[h]>0){
		  ret[u++] = hashdat[hashpos[h]-1];
		}
  }
  return R_NilValue;
}

SEXP hashmapuni_integer64(SEXP x_, SEXP bits_, SEXP hashpos_, SEXP nunique_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  int nu = 0;
  SEXP ret_;
  PROTECT_INDEX idx;
  PROTECT_WITH_INDEX(ret_ = allocVector(REALSXP, nx), &idx);
  long long * ret = (long long *) REAL(ret_);
  long long * x = (long long *) REAL(x_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  long long v;
  for(i=0; i<nx; ){
	v = x[i++];
	h = HASH64(v, shift);
	while(hashpos[h] && x[hashpos[h] - 1] != v){
		h++;
		if (h == nh) 
			h = 0;
	}
	if (!hashpos[h]){
		hashpos[h] = i;
		ret[nu++] = v;
	}
  }
  INTEGER(nunique_)[0] = nu;
  REPROTECT(ret_ = lengthgets(ret_, nu), idx);
  UNPROTECT(1);
  return ret_;
}


SEXP hashupo_integer64(SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP keep_order_, SEXP ret_){
  int h, nh = LENGTH(hashpos_);
  int u, nu = LENGTH(ret_);
  long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int * ret = INTEGER(ret_);
  if (asLogical(keep_order_)){
      int i;
	  // int nx = LENGTH(hashdat_);
	  int bits = asInteger(bits_);
	  int shift = 64 - bits;
	  long long v;
	  for(u=0,i=0; u<nu; i++){
		v = hashdat[i];
		h = HASH64(v, shift);
		while(hashpos[h] && hashdat[hashpos[h] - 1] != v){  // this is mostly while(hashpos[h]) but we want to catch failure for the nomatch assignment
		  h++;
		  if (h == nh) 
			h = 0;
		}
		if (i == (hashpos[h] - 1)){
		  ret[u++] = hashpos[h]; /* unique */
		}
	  }
  }else{
	  for(u=0,h=0; u<nu; h++)
		if (hashpos[h]>0){
		  ret[u++] = hashpos[h];
		}
  }
  return R_NilValue;
}

SEXP hashmapupo_integer64(SEXP x_, SEXP bits_, SEXP hashpos_, SEXP nunique_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  int nu = 0;
  SEXP ret_;
  PROTECT_INDEX idx;
  PROTECT_WITH_INDEX(ret_ = allocVector(INTSXP, nx), &idx);
  int * ret = INTEGER(ret_);
  long long * x = (long long *) REAL(x_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  long long v;
  for(i=0; i<nx; ){
	v = x[i++];
	h = HASH64(v, shift);
	while(hashpos[h] && x[hashpos[h] - 1] != v){
		h++;
		if (h == nh) 
			h = 0;
	}
	if (!hashpos[h]){
		hashpos[h] = i;
		ret[nu++] = hashpos[h];
	}
  }
  INTEGER(nunique_)[0] = nu;
  REPROTECT(ret_ = lengthgets(ret_, nu), idx);
  UNPROTECT(1);
  return ret_;
}



SEXP hashtab_integer641(SEXP hashdat_, SEXP bits_, SEXP hashpos_, SEXP nunique_){
  int i, nx = LENGTH(hashdat_);
  int h, nh = LENGTH(hashpos_);
  int u;
  long long * hashdat = (long long *) REAL(hashdat_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  //int * pos = INTEGER(pos_);
  SEXP ret_;
  PROTECT_INDEX idx;
  PROTECT_WITH_INDEX(ret_ = allocVector(INTSXP, nh), &idx);
  int * ret = INTEGER(ret_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  long long v;
  for(i=0; i<nh; i++)
	ret[i]=0;
  for(i=0; i<nx; i++){
	v = hashdat[i];
	h = HASH64(v, shift);
	while(hashpos[h]){  // this is mostly while(hashpos[h]) but we want to catch failure for the nomatch assignment
	  if (hashdat[hashpos[h] - 1] == v){
	    ret[h]++;
		break;
	  }
	  h++;
	  if (h == nh) 
		h = 0;
	}
  }
  for (u=0,h=0;h<nh;h++){
    if (hashpos[h]){
	  //pos[u]=hashpos[h];
	  ret[u++]=ret[h];
	}
  }
  REPROTECT(ret_ = lengthgets(ret_, u), idx);
  UNPROTECT(1);
  return ret_;
}



SEXP hashtab_integer64(SEXP x_, SEXP bits_, SEXP hashpos_, SEXP nunique_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  long long * x = (long long *) REAL(x_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  SEXP hashtab_;
  PROTECT_INDEX idx;
  PROTECT_WITH_INDEX(hashtab_ = allocVector(INTSXP, nh), &idx);
  int *hashtab = INTEGER(hashtab_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  long long v;
  int u, nu = INTEGER(nunique_)[0];

  for(i=0; i<nh; i++)
	hashtab[i]=0;
  for(i=0; i<nx; ){
    v = x[i++];
	h = HASH64(v, shift);
	while (hashpos[h] && x[hashpos[h] - 1] != v){
		h++;
		if (h == nh) 
			h = 0;
	}
	hashtab[h]++;
  }
  SEXP tabval_;
  PROTECT(tabval_ = allocVector(REALSXP, nu));
  long long * tabval = (long long *) REAL(tabval_);
  for (u=0,h=0;u<nu;h++){
    if (hashpos[h]){
	  tabval[u] = x[hashpos[h]-1];
	  hashtab[u]=hashtab[h];
	  u++;
	}
  }
  REPROTECT(hashtab_ = lengthgets(hashtab_, nu), idx);
  
  SEXP class;
  PROTECT(class = allocVector(STRSXP, 1));
  SET_STRING_ELT(class, 0, mkChar("integer64"));
  classgets(tabval_, class);
  
  SEXP ret_;
  PROTECT(ret_ = allocVector(VECSXP, 2));
  SET_VECTOR_ELT(ret_, 0, tabval_);
  SET_VECTOR_ELT(ret_, 1, hashtab_);
  
  UNPROTECT(4);
  return ret_;
}



SEXP hashmaptab_integer64(SEXP x_, SEXP bits_, SEXP hashpos_, SEXP nunique_){
  int i, nx = LENGTH(x_);
  int h, nh = LENGTH(hashpos_);
  long long * x = (long long *) REAL(x_);
  unsigned int * hashpos = (unsigned int *) INTEGER(hashpos_);
  SEXP hashtab_;
  PROTECT_INDEX idx;
  PROTECT_WITH_INDEX(hashtab_ = allocVector(INTSXP, nh), &idx);
  int *hashtab = INTEGER(hashtab_);
  int bits = asInteger(bits_);
  int shift = 64 - bits;
  long long v;
  int u, nu=0;
  for(i=0; i<nh; i++)
	hashtab[i]=0;
  for(i=0; i<nx; ){
    v = x[i++];
	h = HASH64(v, shift);
	while (hashpos[h] && x[hashpos[h] - 1] != v){
		h++;
		if (h == nh) 
			h = 0;
	}
	if (!hashpos[h]){
		hashpos[h] = i;
		nu++;
	}
	hashtab[h]++;
  }
  SEXP tabval_;
  PROTECT(tabval_ = allocVector(REALSXP, nu));
  long long * tabval = (long long *) REAL(tabval_);
  for (u=0,h=0;u<nu;h++){
    if (hashpos[h]){
	  tabval[u] = x[hashpos[h]-1];
	  hashtab[u]=hashtab[h];
	  u++;
	}
  }
  INTEGER(nunique_)[0] = nu;
  REPROTECT(hashtab_ = lengthgets(hashtab_, nu), idx);
  
  SEXP class;
  PROTECT(class = allocVector(STRSXP, 1));
  SET_STRING_ELT(class, 0, mkChar("integer64"));
  classgets(tabval_, class);
  
  SEXP ret_;
  PROTECT(ret_ = allocVector(VECSXP, 2));
  SET_VECTOR_ELT(ret_, 0, tabval_);
  SET_VECTOR_ELT(ret_, 1, hashtab_);
  
  UNPROTECT(4);
  return ret_;
}