This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_editdistance.cpp
131 lines (117 loc) · 5.63 KB
/
_editdistance.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
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
// -------
// License
// -------
//
// It is released under the MIT license.
//
// Copyright (c) 2013 Hiroyuki Tanaka
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <cstdlib>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <iostream>
#include <bitset>
#include <algorithm>
#include "./_editdistance.h"
using namespace std;
template<typename T, typename TVALUE>
unsigned int edit_distance_bpv(T &cmap, wchar_t const *vec, size_t const &vecsize, unsigned int const &tmax, unsigned int const &tlen) {
int D = tmax * 64 + tlen;
TVALUE D0, HP, HN, VP, VN;
uint64_t top = (1LL << (tlen - 1)); // 末尾のvectorに適用
uint64_t lmb = (1LL << 63);
for(size_t i = 0; i <= tmax; ++i) {
VP[i] = 0;
VN[i] = 0;
}
for(size_t i = 0; i < tmax; ++i) VP[i] = ~0;
for(size_t i = 0; i < tlen; ++i) VP[tmax] |= (1LL << i);
for(size_t i = 0; i < vecsize; ++i) {
TVALUE &PM = cmap[vec[i]];
for(size_t r = 0; r <= tmax; ++r) {
uint64_t X = PM[r];
if(r > 0 && (HN[r - 1] & lmb)) X |= 1LL;
D0[r] = (((X & VP[r]) + VP[r]) ^ VP[r]) | X | VN[r];
HP[r] = VN[r] | ~(D0[r] | VP[r]);
HN[r] = D0[r] & VP[r];
X = (HP[r] << 1LL);
if(r == 0 || HP[r - 1] & lmb) X |= 1LL;
VP[r] = (HN[r] << 1LL) | ~(D0[r] | X);
if(r > 0 && (HN[r - 1] & lmb)) VP[r] |= 1LL;
VN[r] = D0[r] & X;
}
if(HP[tmax] & top) ++D;
else if(HN[tmax] & top) --D;
}
return D;
}
/// c.f. http://handasse.blogspot.com/2009/04/c_29.html
template<typename T>
unsigned int edit_distance_dp(T const *str1, size_t const size1, T const *str2, size_t const size2) {
// vectorより固定長配列の方が速いが、文字列が長い時の保険でのみ呼ばれるのでサイズを決め打ちできない
vector< vector<uint32_t> > d(2, vector<uint32_t>(size2 + 1));
d[0][0] = 0;
d[1][0] = 1;
for (size_t i = 0; i < size2 + 1; i++) d[0][i] = i;
for (size_t i = 1; i < size1 + 1; i++) {
for (size_t j = 1; j < size2 + 1; j++) {
d[i&1][j] = min(min(d[(i-1)&1][j], d[i&1][j-1]) + 1, d[(i-1)&1][j-1] + (str1[i-1] == str2[j-1] ? 0 : 1));
}
}
return d[size1&1][size2];
}
template <size_t N>
struct varr {
uint64_t arr_[N];
uint64_t & operator[](size_t const &i) {
return arr_[i];
}
};
template<size_t N>
unsigned int edit_distance_map_(wchar_t const *a, size_t const asize, wchar_t const *b, size_t const bsize) {
typedef map<wchar_t, varr<N> > cmap_v;
cmap_v cmap;
unsigned int tmax = (asize - 1) >> 6;
unsigned int tlen = asize - tmax * 64;
for(size_t i = 0; i < tmax; ++i) {
for(size_t j = 0; j < 64; ++j) cmap[a[i * 64 + j]][i] |= (1LL << j);
}
for(size_t i = 0; i < tlen; ++i) cmap[a[tmax * 64 + i]][tmax] |= (1LL << i);
return edit_distance_bpv<cmap_v, typename cmap_v::mapped_type>(cmap, b, bsize, tmax, tlen);
}
unsigned int edit_distance(const wchar_t *a, const size_t asize, const wchar_t *b, const size_t bsize) {
if(asize == 0) return bsize;
else if(bsize == 0) return asize;
// 要素数の大きいほうがa
wchar_t const *ap, *bp;
size_t const *asizep, *bsizep;
if(asize < bsize) ap = b, bp = a, asizep = &bsize, bsizep = &asize;
else ap = a, bp = b, asizep = &asize, bsizep = &bsize;
// 必要な配列サイズを調べる
size_t vsize = ((*asizep - 1) >> 6) + 1; // 64までは1, 128までは2, ...
// bit-parallelでできそうな限界を超えたら要素数の小さい方をaとする。
if(vsize > 10) {
wchar_t const *_ = ap;
size_t const *__ = asizep;
ap = bp, bp = _, asizep = bsizep, bsizep = __;
vsize = ((*asizep - 1) >> 6) + 1;
}
if(vsize == 1) return edit_distance_map_<1>(ap, *asizep, bp, *bsizep);
else if(vsize == 2) return edit_distance_map_<2>(ap, *asizep, bp, *bsizep);
else if(vsize == 3) return edit_distance_map_<3>(ap, *asizep, bp, *bsizep);
else if(vsize == 4) return edit_distance_map_<4>(ap, *asizep, bp, *bsizep);
else if(vsize == 5) return edit_distance_map_<5>(ap, *asizep, bp, *bsizep);
else if(vsize == 6) return edit_distance_map_<6>(ap, *asizep, bp, *bsizep);
else if(vsize == 7) return edit_distance_map_<7>(ap, *asizep, bp, *bsizep);
else if(vsize == 8) return edit_distance_map_<8>(ap, *asizep, bp, *bsizep);
else if(vsize == 9) return edit_distance_map_<9>(ap, *asizep, bp, *bsizep);
else if(vsize == 10) return edit_distance_map_<10>(ap, *asizep, bp, *bsizep);
return edit_distance_dp<wchar_t>(ap, *asizep, bp, *bsizep); // dynamic programmingに任せる
}