forked from cubicdaiya/dtl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.hpp
215 lines (176 loc) · 5.19 KB
/
common.hpp
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
#ifndef DTL_MODERN_COMMON_HPP
#define DTL_MODERN_COMMON_HPP
#include "dtl_modern/concepts.hpp"
#include <cassert>
#include <cstdint>
#include <vector>
namespace dtl_modern
{
using i64 = std::int64_t;
using u64 = std::uint64_t;
/**
* @enum SesEdit
*
* @brief Type of edit for SES.
*/
enum class SesEdit : std::int32_t
{
Delete = -1,
Common = 0,
Add = 1,
};
/**
* @brief Get the mark of the edit type.
*
* @param edit Type of edit.
* @return A character representing the edit type.
*/
constexpr char ses_mark(SesEdit edit) noexcept
{
switch (edit) {
case SesEdit::Delete: return '-';
case SesEdit::Common: return ' ';
case SesEdit::Add: return '+';
default: [[unlikely]] return '?';
}
}
/**
* @struct ElemInfo
*
* @brief Info for Unified Format.
*/
struct ElemInfo
{
i64 m_index_before; // index of prev sequence
i64 m_index_after; // index of after sequence
SesEdit m_type; // type of edit
auto operator<=>(const ElemInfo&) const = default;
bool operator==(const ElemInfo&) const = default;
};
/**
* @struct KPoint
*
* @brief Edit graph/grid coordinate.
*/
struct KPoint
{
i64 m_x;
i64 m_y;
i64 m_k; // diagonal
auto operator<=>(const KPoint&) const = default;
bool operator==(const KPoint&) const = default;
};
/**
* @struct Point
*
* @brief Edit graph/grid coordinate without diagonal.
*/
struct Point
{
i64 m_x;
i64 m_y;
auto operator<=>(const Point&) const = default;
bool operator==(const Point&) const = default;
};
/**
* @struct EditPath
*
* @brief Edit path for a sequence.
*/
struct EditPath
{
EditPath() = default;
EditPath(u64 size, i64 value)
: m_inner(size, value)
{
}
// clang-format off
i64& operator[](u64 index) { return m_inner[index]; }
const i64& operator[](u64 index) const { return m_inner[index]; }
i64& at(i64 index) { assert(index >= 0); return m_inner[static_cast<u64>(index)]; }
const i64& at(i64 index) const { assert(index >= 0); return m_inner[static_cast<u64>(index)]; }
void add(i64 value) { m_inner.push_back(value); }
std::size_t size() const noexcept { return m_inner.size(); }
void clear() noexcept { m_inner.clear(); }
// clang-format on
std::vector<i64> m_inner;
bool operator==(const EditPath&) const = default;
};
/**
* @struct EditPathCoordinates
*
* @brief Edit path coordinates for a sequence.
*/
template <typename P = KPoint>
struct EditPathCoordinates
{
EditPathCoordinates() = default;
EditPathCoordinates(u64 size, P value)
: m_inner(size, value)
{
}
// clang-format off
P& operator[](u64 index) { return m_inner[index]; }
const P& operator[](u64 index) const { return m_inner[index]; }
P& at(i64 index) { assert(index >= 0); return m_inner[static_cast<u64>(index)]; }
const P& at(i64 index) const { assert(index >= 0); return m_inner[static_cast<u64>(index)]; }
void add(P value) { m_inner.push_back(value); }
std::size_t size() const noexcept { return m_inner.size(); }
void clear() noexcept { m_inner.clear(); }
// clang-format on
std::vector<P> m_inner;
bool operator==(const EditPathCoordinates&) const = default;
};
/**
* @struct SesElem
*
* @brief SES element; contains the element and its edit type.
*/
template <Diffable E>
struct SesElem
{
using Elem = E;
Elem m_elem;
ElemInfo m_info;
bool operator==(const SesElem&) const
requires TriviallyComparable<E>
= default;
};
/**
* @struct UniHunk
*
* @brief Structure of Unified Format Hunk.
*/
template <Diffable E>
struct UniHunk
{
using Elem = E;
i64 m_a; // @@ -a,b +c,d @@
i64 m_b;
i64 m_c;
i64 m_d;
std::vector<SesElem<Elem>> m_common_0; // anteroposterior commons on changes
std::vector<SesElem<Elem>> m_common_1;
std::vector<SesElem<Elem>> m_change; // changes
i64 m_inc_dec_count; // count of increase and decrease
bool operator==(const UniHunk&) const
requires TriviallyComparable<E>
= default;
};
/**
* @struct UniHunkSeq
*
* @brief Sequence of Unified Format Hunks.
*/
template <Diffable E>
struct UniHunkSeq
{
using Elem = E;
std::span<const UniHunk<E>> get() const { return m_inner; }
std::vector<UniHunk<E>> m_inner;
bool operator==(const UniHunkSeq&) const
requires TriviallyComparable<E>
= default;
};
}
#endif /* end of include guard: DTL_MODERN_COMMON_HPP */