-
Notifications
You must be signed in to change notification settings - Fork 7
/
snp_ops.pyx
624 lines (510 loc) · 20.6 KB
/
snp_ops.pyx
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# These are modified versions of sortednp:
# https://gitlab.sauerburger.com/frank/sortednp
# cython: boundscheck=False
# cython: wraparound=False
# cython: initializedcheck=False
# cython: cdivision=True
# cython: nonecheck=False
# cython: language_level=3
cimport numpy as np
import numpy as np
from enum import Enum
# cimport snp_ops
# from snp_ops cimport _galloping_search, DTYPE_t, ALL_BITS
cimport searcharray.roaringish.snp_ops
from searcharray.roaringish.snp_ops cimport _galloping_search, DTYPE_t, int64_t
cdef DTYPE_t ALL_BITS = 0xFFFFFFFFFFFFFFFF
cdef extern from "stddef.h":
# Assuming size_t is available via stddef.h for the example's simplicity
# and portability, though it's not directly used here.
int __builtin_popcountll(unsigned long long x)
cdef popcount64_arr(DTYPE_t[:] arr):
cdef np.uint64_t[:] result = np.empty(arr.shape[0], dtype=np.uint64)
# cdef int i = 0
cdef DTYPE_t* result_ptr = &result[0]
cdef DTYPE_t* arr_ptr = &arr[0]
for _ in range(arr.shape[0]):
result_ptr[0] = __builtin_popcountll(arr_ptr[0])
result_ptr += 1
arr_ptr += 1
return result
cdef popcount64_arr_naive(DTYPE_t[:] arr):
cdef np.uint64_t[:] result = np.empty(arr.shape[0], dtype=np.uint64)
cdef int i = 0
for i in range(arr.shape[0]):
result[i] = __builtin_popcountll(arr[i])
return result
def popcount64(arr):
return np.array(popcount64_arr(arr))
cdef void _binary_search(DTYPE_t[:] array,
DTYPE_t target,
DTYPE_t mask,
np.intp_t* idx_out,
np.intp_t len):
"""Write to idx_out the index of the first element in array that is
greater or equal to target (masked)."""
cdef DTYPE_t value = array[idx_out[0]]
target &= mask
# If already at correct location or beyond
if target <= value & mask:
return
cdef np.intp_t i_right = len - 1 # is always GREATER OR EQUAL
cdef np.intp_t i_left = idx_out[0] # is always LESS than value
if array[i_right] & mask < target:
idx_out[0] = i_right
return # indicate target value too large
while i_left + 1 < i_right:
idx_out[0] = (i_right + i_left) // 2 # midpoint
value = array[idx_out[0]]
if target <= value & mask:
i_right = idx_out[0]
else:
i_left = idx_out[0]
idx_out[0] = i_right
# Python wrapper for binary search
def binary_search(np.ndarray[DTYPE_t, ndim=1] array,
DTYPE_t target,
DTYPE_t mask=ALL_BITS,
start=0):
cdef np.intp_t i = start
cdef np.intp_t len = array.shape[0]
_binary_search(array, target, mask, &i, len)
return i, (array[i] & mask) == (target & mask)
cdef inline void _galloping_search(DTYPE_t[:] array,
DTYPE_t target,
DTYPE_t mask,
np.intp_t* idx_out,
np.intp_t len):
cdef DTYPE_t value = array[idx_out[0]] & mask
target &= mask
# If already at correct location or beyond
if target <= value:
return
cdef np.intp_t delta = 1
cdef DTYPE_t end = len - 1
cdef np.intp_t i_prev = idx_out[0]
while value < target:
i_prev = idx_out[0]
idx_out[0] += delta
if len <= idx_out[0]:
# Gallop jump reached end of array.
idx_out[0] = end
value = array[idx_out[0]] & mask
break
value = array[idx_out[0]] & mask
# Increase step size.
delta *= 2
cdef np.intp_t i_right = idx_out[0] + 1 # Convert pointer position to length.
idx_out[0] = i_prev # This is the lower boundary and the active counter.
# i_prev ~ i_left to save a variable
#
# _binary_search(array, target, mask, i, higher)
# Inline binary search without the checks
# length is one past current posn
# left is i_prev
while i_prev + 1 < i_right :
idx_out[0] = (i_right + i_prev) // 2 # midpoint
value = array[idx_out[0]] & mask
if target <= value:
i_right = idx_out[0]
else:
i_prev = idx_out[0]
idx_out[0] = i_right
def galloping_search(np.ndarray[DTYPE_t, ndim=1] array,
DTYPE_t target,
DTYPE_t mask=ALL_BITS,
start=0):
cdef np.intp_t i = start
cdef np.intp_t len = array.shape[0]
_galloping_search(array, target, mask, &i, len)
return i, (array[i] & mask) == (target & mask)
cdef _gallop_intersect_drop(DTYPE_t[:] lhs,
DTYPE_t[:] rhs,
DTYPE_t mask=ALL_BITS):
"""Two pointer approach to find the intersection of two sorted arrays."""
cdef DTYPE_t* lhs_ptr = &lhs[0]
cdef DTYPE_t* rhs_ptr = &rhs[0]
cdef DTYPE_t* end_lhs_ptr = &lhs[lhs.shape[0]]
cdef DTYPE_t* end_rhs_ptr = &rhs[rhs.shape[0]]
cdef DTYPE_t lhs_delta = 1
cdef DTYPE_t rhs_delta = 1
cdef DTYPE_t last = -1
cdef np.uint64_t[:] lhs_indices = np.empty(min(lhs.shape[0], rhs.shape[0]), dtype=np.uint64)
cdef np.uint64_t[:] rhs_indices = np.empty(min(lhs.shape[0], rhs.shape[0]), dtype=np.uint64)
cdef np.uint64_t* lhs_result_ptr = &lhs_indices[0]
cdef np.uint64_t* rhs_result_ptr = &rhs_indices[0]
while lhs_ptr < end_lhs_ptr and rhs_ptr < end_rhs_ptr:
lhs_delta = 1
rhs_delta = 1
# Gallop past the current element
while lhs_ptr < end_lhs_ptr and (lhs_ptr[0] & mask) < (rhs_ptr[0] & mask):
lhs_ptr += lhs_delta
lhs_delta *= 2
lhs_ptr = lhs_ptr - (lhs_delta // 2)
while rhs_ptr < end_rhs_ptr and (rhs_ptr[0] & mask) < (lhs_ptr[0] & mask):
rhs_ptr += rhs_delta
rhs_delta *= 2
rhs_ptr = rhs_ptr - (rhs_delta // 2)
# Now that we've reset, we just do the naive 2-ptr check
# Then next loop we pickup on exponential search
if (lhs_ptr[0] & mask) < (rhs_ptr[0] & mask):
lhs_ptr += 1
elif (rhs_ptr[0] & mask) < (lhs_ptr[0] & mask):
rhs_ptr += 1
else:
if (last & mask) != (lhs_ptr[0] & mask):
lhs_result_ptr[0] = lhs_ptr - &lhs[0]
rhs_result_ptr[0] = rhs_ptr - &rhs[0]
last = lhs_ptr[0]
lhs_result_ptr += 1
rhs_result_ptr += 1
lhs_ptr += 1
rhs_ptr += 1
# If delta
# Either we read past the array, or
return np.asarray(lhs_indices), np.asarray(rhs_indices), lhs_result_ptr - &lhs_indices[0]
cdef _gallop_intersect_keep(DTYPE_t[:] lhs,
DTYPE_t[:] rhs,
DTYPE_t mask=ALL_BITS):
"""Two pointer approach to find the intersection of two sorted arrays."""
cdef DTYPE_t* lhs_ptr = &lhs[0]
cdef DTYPE_t* rhs_ptr = &rhs[0]
cdef DTYPE_t* end_lhs_ptr = &lhs[lhs.shape[0]]
cdef DTYPE_t* end_rhs_ptr = &rhs[rhs.shape[0]]
cdef DTYPE_t lhs_delta = 1
cdef DTYPE_t rhs_delta = 1
cdef DTYPE_t target = -1
cdef np.uint64_t[:] lhs_indices = np.empty(max(lhs.shape[0], rhs.shape[0]), dtype=np.uint64)
cdef np.uint64_t[:] rhs_indices = np.empty(max(lhs.shape[0], rhs.shape[0]), dtype=np.uint64)
cdef np.uint64_t* lhs_result_ptr = &lhs_indices[0]
cdef np.uint64_t* rhs_result_ptr = &rhs_indices[0]
while lhs_ptr < end_lhs_ptr and rhs_ptr < end_rhs_ptr:
lhs_delta = 1
rhs_delta = 1
# Gallop past the current element
while lhs_ptr < end_lhs_ptr and (lhs_ptr[0] & mask) < (rhs_ptr[0] & mask):
lhs_ptr += lhs_delta
lhs_delta *= 2
lhs_ptr = lhs_ptr - (lhs_delta // 2)
while rhs_ptr < end_rhs_ptr and (rhs_ptr[0] & mask) < (lhs_ptr[0] & mask):
rhs_ptr += rhs_delta
rhs_delta *= 2
rhs_ptr = rhs_ptr - (rhs_delta // 2)
# Now that we've reset, we just do the naive 2-ptr check
# Then next loop we pickup on exponential search
if (lhs_ptr[0] & mask) < (rhs_ptr[0] & mask):
lhs_ptr += 1
elif (rhs_ptr[0] & mask) < (lhs_ptr[0] & mask):
rhs_ptr += 1
else:
target = lhs_ptr[0] & mask
# Store all LHS indices equal to RHS
while (lhs_ptr[0] & mask) == target and lhs_ptr < end_lhs_ptr:
lhs_result_ptr[0] = lhs_ptr - &lhs[0]; lhs_result_ptr += 1
lhs_ptr += 1
# Store all RHS equal to LHS
while (rhs_ptr[0] & mask) == target and rhs_ptr < end_rhs_ptr:
rhs_result_ptr[0] = rhs_ptr - &rhs[0]; rhs_result_ptr += 1
rhs_ptr += 1
# If delta
# Either we read past the array, or
return np.asarray(lhs_indices), np.asarray(rhs_indices), lhs_result_ptr - &lhs_indices[0], rhs_result_ptr - &rhs_indices[0]
cdef _gallop_adjacent(DTYPE_t[:] lhs,
DTYPE_t[:] rhs,
DTYPE_t mask=ALL_BITS,
DTYPE_t delta=1):
# Find all LHS / RHS indices where LHS is 1 before RHS
cdef DTYPE_t* lhs_ptr = &lhs[0]
cdef DTYPE_t* rhs_ptr = &rhs[0]
cdef DTYPE_t* end_lhs_ptr = &lhs[lhs.shape[0]]
cdef DTYPE_t* end_rhs_ptr = &rhs[rhs.shape[0]]
cdef DTYPE_t lhs_delta = 1
cdef DTYPE_t rhs_delta = 1
cdef DTYPE_t last = -1
cdef np.uint64_t[:] lhs_indices = np.empty(min(lhs.shape[0], rhs.shape[0]), dtype=np.uint64)
cdef np.uint64_t[:] rhs_indices = np.empty(min(lhs.shape[0], rhs.shape[0]), dtype=np.uint64)
cdef np.uint64_t* lhs_result_ptr = &lhs_indices[0]
cdef np.uint64_t* rhs_result_ptr = &rhs_indices[0]
# Read rhs until > delta
while rhs_ptr < end_rhs_ptr and rhs_ptr[0] & mask == 0:
rhs_ptr += 1
while lhs_ptr < end_lhs_ptr and rhs_ptr < end_rhs_ptr:
lhs_delta = 1
rhs_delta = 1
# Gallop, but instead check is:
# if value_lhs < value_rhs - delta:
# Gallop past the current element
while lhs_ptr < end_lhs_ptr and (lhs_ptr[0] & mask) < ((rhs_ptr[0] & mask) - delta):
lhs_ptr += lhs_delta
lhs_delta *= 2
lhs_ptr = lhs_ptr - (lhs_delta // 2)
while rhs_ptr < end_rhs_ptr and ((rhs_ptr[0] & mask) - delta) < (lhs_ptr[0] & mask):
rhs_ptr += rhs_delta
rhs_delta *= 2
rhs_ptr = rhs_ptr - (rhs_delta // 2)
# Now that we've reset, we just do the naive 2-ptr check
# Then next loop we pickup on exponential search
if (lhs_ptr[0] & mask) < ((rhs_ptr[0] & mask) - delta):
lhs_ptr += 1
elif ((rhs_ptr[0] & mask) - delta) < (lhs_ptr[0] & mask):
rhs_ptr += 1
else:
if (lhs_ptr[0] & mask) == ((rhs_ptr[0] & mask) - delta):
if (last & mask) != (lhs_ptr[0] & mask):
lhs_result_ptr[0] = lhs_ptr - &lhs[0]
rhs_result_ptr[0] = rhs_ptr - &rhs[0]
last = lhs_ptr[0]
lhs_result_ptr += 1
rhs_result_ptr += 1
lhs_ptr += 1
rhs_ptr += 1
# If delta
# Either we read past the array, or
return np.asarray(lhs_indices), np.asarray(rhs_indices), lhs_result_ptr - &lhs_indices[0]
def _u64(lst) -> np.ndarray:
return np.array(lst, dtype=np.uint64)
def save_input(lhs, rhs, mask):
np.save(f"lhs_{len(lhs)}.npy", lhs)
np.save(f"rhs_{len(lhs)}.npy", rhs)
np.save(f"mask_{len(lhs)}.npy", mask)
def intersect(np.ndarray[DTYPE_t, ndim=1] lhs,
np.ndarray[DTYPE_t, ndim=1] rhs,
DTYPE_t mask=ALL_BITS,
bint drop_duplicates=True):
if mask is None:
mask = ALL_BITS
if mask == 0:
raise ValueError("Mask cannot be zero")
if drop_duplicates:
# save_input(lhs, rhs, mask)
indices_lhs, indices_rhs, result_idx = _gallop_intersect_drop(lhs, rhs, mask)
return indices_lhs[:result_idx], indices_rhs[:result_idx]
else:
indices_lhs, indices_rhs, indices_lhs_idx, indices_rhs_idx = _gallop_intersect_keep(lhs, rhs, mask)
return indices_lhs[:indices_lhs_idx], indices_rhs[:indices_rhs_idx]
def adjacent(np.ndarray[DTYPE_t, ndim=1] lhs,
np.ndarray[DTYPE_t, ndim=1] rhs,
DTYPE_t mask=ALL_BITS):
if mask == 0:
raise ValueError("Mask cannot be zero")
if mask is None:
mask = ALL_BITS
delta = 1
else:
delta = (mask & -mask) # lest significant set bit on mask
indices_lhs, indices_rhs, result_idx = _gallop_adjacent(lhs, rhs, mask, delta)
return indices_lhs[:result_idx], indices_rhs[:result_idx]
cdef _scan_unique_naive(DTYPE_t[:] arr,
DTYPE_t arr_len):
cdef DTYPE_t i = 0
cdef np.uint64_t[:] result = np.empty(arr_len, dtype=np.uint64)
cdef DTYPE_t result_idx = 0
cdef DTYPE_t target = arr[i]
while i < arr_len:
target = arr[i]
result[result_idx] = target
result_idx += 1
i += 1
while i < arr_len and arr[i] == target:
i += 1
return result, result_idx
cdef _scan_unique(DTYPE_t[:] arr,
DTYPE_t arr_len):
cdef np.uint64_t[:] result = np.empty(arr_len, dtype=np.uint64)
cdef np.uint64_t* result_ptr = &result[0]
cdef DTYPE_t* arr_ptr = &arr[0]
cdef DTYPE_t* arr_end = &arr[arr_len-1]
cdef DTYPE_t* target_ptr = arr_ptr
while arr_ptr <= arr_end:
target_ptr = arr_ptr
result_ptr[0] = arr_ptr[0]
result_ptr += 1
arr_ptr += 1
while arr_ptr <= arr_end and arr_ptr[0] == target_ptr[0]:
arr_ptr += 1
return result, result_ptr - &result[0]
cdef _scan_unique_gallop(DTYPE_t[:] arr,
DTYPE_t arr_len):
cdef np.uint64_t[:] result = np.empty(arr_len, dtype=np.uint64)
cdef np.uint64_t* result_ptr = &result[0]
cdef DTYPE_t* arr_ptr = &arr[0]
cdef DTYPE_t* last_arr_ptr = &arr[0]
cdef DTYPE_t* arr_end = &arr[arr_len-1]
cdef DTYPE_t* target_ptr = arr_ptr
cdef DTYPE_t delta = 1
while arr_ptr <= arr_end:
target_ptr = arr_ptr
result_ptr[0] = arr_ptr[0]
result_ptr += 1
arr_ptr += 1
delta = 1
last_arr_ptr = arr_ptr
# Gallop to first element that is not equal
while arr_ptr <= arr_end and arr_ptr[0] == target_ptr[0]:
last_arr_ptr = arr_ptr
arr_ptr += delta
delta *= 2
# Linearly search for the first element that is not equal
arr_ptr = last_arr_ptr
if arr_ptr <= arr_end:
while arr_ptr <= arr_end and arr_ptr[0] == target_ptr[0]:
arr_ptr += 1
return result, result_ptr - &result[0]
cdef _scan_unique_shifted(DTYPE_t[:] arr,
DTYPE_t arr_len,
DTYPE_t rshift):
cdef np.uint64_t[:] result = np.empty(arr_len, dtype=np.uint64)
cdef np.uint64_t* result_ptr = &result[0]
cdef DTYPE_t* arr_ptr = &arr[0]
cdef DTYPE_t* arr_end = &arr[arr_len-1]
cdef DTYPE_t target_shifted = arr_ptr[0] >> rshift
while arr_ptr <= arr_end:
target_shifted = arr_ptr[0] >> rshift
result_ptr[0] = target_shifted
result_ptr += 1
arr_ptr += 1
while arr_ptr <= arr_end and (arr_ptr[0] >> rshift) == target_shifted:
arr_ptr += 1
return result, result_ptr - &result[0]
cdef _scan_unique_shifted_gallop(DTYPE_t[:] arr,
DTYPE_t arr_len,
DTYPE_t rshift):
cdef np.uint64_t[:] result = np.empty(arr_len, dtype=np.uint64)
cdef np.uint64_t* result_ptr = &result[0]
cdef DTYPE_t* arr_ptr = &arr[0]
cdef DTYPE_t* last_arr_ptr = &arr[0]
cdef DTYPE_t* arr_end = &arr[arr_len-1]
cdef DTYPE_t target_shifted = arr_ptr[0] >> rshift
cdef DTYPE_t delta = 1
while arr_ptr <= arr_end:
target_shifted = arr_ptr[0] >> rshift
result_ptr[0] = arr_ptr[0]
result_ptr += 1
arr_ptr += 1
delta = 1
last_arr_ptr = arr_ptr
# Gallop to first element that is not equal
while arr_ptr <= arr_end and (arr_ptr[0] >> rshift == target_shifted):
last_arr_ptr = arr_ptr
arr_ptr += delta
delta *= 2
# Linearly search for the first element that is not equal
arr_ptr = last_arr_ptr
if arr_ptr <= arr_end:
while arr_ptr <= arr_end and (arr_ptr[0] >> rshift == target_shifted):
arr_ptr += 1
return result, result_ptr - &result[0]
def unique(np.ndarray[DTYPE_t, ndim=1] arr,
DTYPE_t rshift=0):
if rshift > 0:
result, result_idx = _scan_unique_shifted(arr, arr.shape[0], rshift)
else:
result, result_idx = _scan_unique(arr, arr.shape[0])
return np.array(result[:result_idx])
cdef _merge_naive(DTYPE_t[:] lhs,
DTYPE_t[:] rhs):
cdef np.intp_t len_lhs = lhs.shape[0]
cdef np.intp_t len_rhs = rhs.shape[0]
cdef np.intp_t i_lhs = 0
cdef np.intp_t i_rhs = 0
cdef DTYPE_t value_lhs = 0
cdef DTYPE_t value_rhs = 0
# Outputs as numpy arrays
cdef np.uint64_t[:] results = np.empty(len_lhs + len_rhs, dtype=np.uint64)
cdef np.int64_t result_idx = 0
while i_lhs < len_lhs and i_rhs < len_rhs:
# Use gallping search to find the first element in the right array
value_lhs = lhs[i_lhs]
value_rhs = rhs[i_rhs]
if value_lhs < value_rhs:
results[result_idx] = value_lhs
i_lhs += 1
elif value_rhs < value_lhs:
results[result_idx] = value_rhs
i_rhs += 1
else:
results[result_idx] = value_lhs
result_idx += 1
results[result_idx] = value_rhs
i_lhs += 1
i_rhs += 1
result_idx += 1
return np.asarray(results), result_idx
cdef _merge(DTYPE_t[:] lhs,
DTYPE_t[:] rhs):
cdef np.intp_t len_lhs = lhs.shape[0]
cdef np.intp_t len_rhs = rhs.shape[0]
cdef DTYPE_t* lhs_ptr = &lhs[0]
cdef DTYPE_t* end_lhs_ptr = &lhs[len_lhs]
cdef DTYPE_t* rhs_ptr = &rhs[0]
cdef DTYPE_t* end_rhs_ptr = &rhs[len_rhs]
# Outputs as numpy arrays
cdef np.uint64_t[:] results = np.empty(len_lhs + len_rhs, dtype=np.uint64)
cdef np.uint64_t* result_ptr = &results[0]
# Copy elements from both arrays
while lhs_ptr < end_lhs_ptr and rhs_ptr < end_rhs_ptr:
if lhs_ptr[0] < rhs_ptr[0]:
result_ptr[0] = lhs_ptr[0]
lhs_ptr += 1
elif rhs_ptr[0] < lhs_ptr[0]:
result_ptr[0] = rhs_ptr[0]
rhs_ptr += 1
else:
result_ptr[0] = lhs_ptr[0]
result_ptr += 1
result_ptr[0] = rhs_ptr[0]
lhs_ptr += 1
rhs_ptr += 1
result_ptr += 1
# Copy remaining elements if one side not consumed
while lhs_ptr == end_lhs_ptr and rhs_ptr < end_rhs_ptr:
result_ptr[0] = rhs_ptr[0]
rhs_ptr += 1
result_ptr += 1
while rhs_ptr == end_rhs_ptr and lhs_ptr < end_lhs_ptr:
result_ptr[0] = lhs_ptr[0]
lhs_ptr += 1
result_ptr += 1
return np.asarray(results), result_ptr - &results[0]
cdef _merge_w_drop(DTYPE_t[:] lhs,
DTYPE_t[:] rhs):
cdef np.intp_t len_lhs = lhs.shape[0]
cdef np.intp_t len_rhs = rhs.shape[0]
cdef DTYPE_t* lhs_ptr = &lhs[0]
cdef DTYPE_t* end_lhs_ptr = &lhs[len_lhs]
cdef DTYPE_t* rhs_ptr = &rhs[0]
cdef DTYPE_t* end_rhs_ptr = &rhs[len_rhs]
# Outputs as numpy arrays
cdef np.uint64_t[:] results = np.empty(len_lhs + len_rhs, dtype=np.uint64)
cdef np.uint64_t* result_ptr = &results[0]
# Copy elements from both arrays
while lhs_ptr < end_lhs_ptr and rhs_ptr < end_rhs_ptr:
if lhs_ptr[0] < rhs_ptr[0]:
result_ptr[0] = lhs_ptr[0]
lhs_ptr += 1
elif rhs_ptr[0] < lhs_ptr[0]:
result_ptr[0] = rhs_ptr[0]
rhs_ptr += 1
else:
result_ptr[0] = lhs_ptr[0]
lhs_ptr += 1
rhs_ptr += 1
result_ptr += 1
# Copy remaining elements if one side not consumed
while lhs_ptr == end_lhs_ptr and rhs_ptr < end_rhs_ptr:
result_ptr[0] = rhs_ptr[0]
rhs_ptr += 1
result_ptr += 1
while rhs_ptr == end_rhs_ptr and lhs_ptr < end_lhs_ptr:
result_ptr[0] = lhs_ptr[0]
lhs_ptr += 1
result_ptr += 1
return np.asarray(results), result_ptr - &results[0]
def merge(np.ndarray[DTYPE_t, ndim=1] lhs,
np.ndarray[DTYPE_t, ndim=1] rhs,
bint drop_duplicates=False):
if drop_duplicates:
result, result_idx = _merge_w_drop(lhs, rhs)
else:
result, result_idx = _merge(lhs, rhs)
return np.array(result[:result_idx])