-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgo_scalar.cpp
65 lines (55 loc) · 1.32 KB
/
algo_scalar.cpp
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
#include "algo_intersection.h"
size_t scalar(const uint32_t *A, const size_t lenA, const uint32_t *B,
const size_t lenB, uint32_t *out) {
const uint32_t *const initout(out);
if (lenA == 0 || lenB == 0)
return 0;
const uint32_t *endA = A + lenA;
const uint32_t *endB = B + lenB;
while (1) {
while (*A < *B) {
SKIP_FIRST_COMPARE:
if (++A == endA)
return (out - initout);
}
while (*A > *B) {
if (++B == endB)
return (out - initout);
}
if (*A == *B) {
*out++ = *A;
if (++A == endA || ++B == endB)
return (out - initout);
} else {
goto SKIP_FIRST_COMPARE;
}
}
return (out - initout); // NOTREACHED
}
size_t match_scalar(const uint32_t *A, const size_t lenA, const uint32_t *B, const size_t lenB, uint32_t *out) {
const uint32_t *initout = out;
if (lenA == 0 || lenB == 0)
return 0;
const uint32_t *endA = A + lenA;
const uint32_t *endB = B + lenB;
while (1) {
while (*A < *B) {
SKIP_FIRST_COMPARE:
if (++A == endA)
goto FINISH;
}
while (*A > *B) {
if (++B == endB)
goto FINISH;
}
if (*A == *B) {
*out++ = *A;
if (++A == endA || ++B == endB)
goto FINISH;
} else {
goto SKIP_FIRST_COMPARE;
}
}
FINISH:
return (out - initout);
}