47 edist_seq(
const Char* ptr_,
int len_) : ptr(ptr_), len(len_) { }
76 return k + maxdist + fkp_rows * (
p + 1);
80 edist_state(
const Char* ptr1,
int len1,
const Char* ptr2,
int len2,
82 : seq1(ptr1, len1), seq2(ptr2, len2), fkp(fkp_), maxdist(len2) {
87 fkp_rows = 2 * maxdist + 1;
92 memset(fkp,
unsigned(INT_MIN) >> (8 * (
sizeof(
int) - 1)),
93 sizeof(
int) * (calc_index(maxdist, maxdist - 2) + 1));
95 for (
int k = 1; k <= maxdist; ++k) {
96 set_f_kp(k, k - 1, -1);
97 set_f_kp(-k, k - 1, k - 1);
102 return fkp[calc_index(k,
p)];
106 fkp[calc_index(k,
p)] = val;
110 if (pos1 <= 0 || pos2 <= 0 || pos1 >= seq1.
len || pos2 >= seq2.
len)
112 return (seq1.
ptr[pos1 - 1] == seq2.
ptr[pos2] &&
113 seq1.
ptr[pos1] == seq2.
ptr[pos2 - 1]);
116 void edist_calc_f_kp(
int k,
int p);
122 int maxlen = get_f_kp(k,
p - 1) + 1;
123 int maxlen2 = get_f_kp(k - 1,
p - 1);
124 int maxlen3 = get_f_kp(k + 1,
p - 1) + 1;
126 if (is_transposed(maxlen, maxlen + k)) {
131 if (maxlen >= maxlen2) {
132 if (maxlen >= maxlen3) {
139 if (maxlen2 >= maxlen3) {
150 while (maxlen < seq1.len &&
151 maxlen + k < seq2.len &&
152 seq1.ptr[maxlen] == seq2.ptr[maxlen + k]) {
155 set_f_kp(k,
p, maxlen);
161 int* fkp_,
int max_distance)
163 int lendiff = len2 - len1;
172 if (len1 == 0)
return len2;
177 while (
p <= max_distance) {
178 for (
int temp_p = 0; temp_p !=
p; ++temp_p) {
179 int inc =
p - temp_p;
180 if (abs(lendiff - inc) <= temp_p) {
183 if (abs(lendiff + inc) <= temp_p) {
189 if (state.
get_f_kp(lendiff,
p) == len1)
break;
198 int max_distance)
const
204 for (
int i = 0; i != len; ++i) {
205 unsigned ch = ptr[i];
207 freqs2 |= (freqs & bit);
217 int ed_lower_bound = bits / 2;
218 if (ed_lower_bound > max_distance) {
221 return ed_lower_bound;
224 if (
rare(target.size() >
size_t{INT_MAX})) {
227 int target_size = int(target.size());
251 array =
new int[alloc_size];
254 return seqcmp_editdist<unsigned>(ptr, len, &target[0], target_size,
255 array, max_distance);
int calc(const unsigned *ptr, int len, int max_distance) const
Calculate edit distance.
unsigned long long freqs_bitmap
The type to use for the occurrence bitmaps.
edist_state & operator=(const edist_state &)=delete
Don't allow assignment.
bool is_transposed(int pos1, int pos2) const
void edist_calc_f_kp(int k, int p)
int get_f_kp(int k, int p) const
int calc_index(int k, int p) const
void set_f_kp(int k, int p, int val)
edist_state(const edist_state &)=delete
Don't allow copying.
edist_state(const Char *ptr1, int len1, const Char *ptr2, int len2, int *fkp_)
static int seqcmp_editdist(const Char *ptr1, int len1, const Char *ptr2, int len2, int *fkp_, int max_distance)
Edit distance calculation algorithm.
Various assertion macros.
Arithmetic operations with overflow checks.
std::enable_if_t< std::is_unsigned_v< T1 > &&std::is_unsigned_v< T2 > &&std::is_unsigned_v< R >, bool > add_overflows(T1 a, T2 b, R &res)
Addition with overflow checking.
std::enable_if_t< std::is_unsigned_v< T1 > &&std::is_unsigned_v< T2 > &&std::is_unsigned_v< R >, bool > mul_overflows(T1 a, T2 b, R &res)
Multiplication with overflow checking.
Count the number of set bits in an integer type.
static void add_popcount(A &accumulator, V value)
Add the number of set bits in value to accumulator.
edist_seq(const Char *ptr_, int len_)