forked from google/ced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotated_string.h
396 lines (336 loc) · 10.6 KB
/
annotated_string.h
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
// Copyright 2017 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <stdint.h>
#include <atomic>
#include "absl/strings/string_view.h"
#include "annotation.pb.h"
#include "avl.h"
#include "log.h"
union ID {
ID() { id = 0; }
ID(uint64_t i) { id = i; }
ID(uint16_t s, uint64_t i) {
site = s;
clock = i;
}
uint64_t id;
struct {
uint64_t site : 16;
uint64_t clock : (64 - 16);
};
};
inline bool operator<(ID a, ID b) { return a.id < b.id; }
inline bool operator>(ID a, ID b) { return a.id > b.id; }
inline bool operator<=(ID a, ID b) { return a.id <= b.id; }
inline bool operator>=(ID a, ID b) { return a.id >= b.id; }
inline bool operator==(ID a, ID b) { return a.id == b.id; }
inline bool operator!=(ID a, ID b) { return a.id != b.id; }
class Site {
public:
Site() : id_(id_gen_.fetch_add(1, std::memory_order_relaxed)) {
if (id_ == 0) abort();
}
Site(const Site&) = delete;
Site& operator=(const Site&) = delete;
ID GenerateID() {
return ID(id_, clock_.fetch_add(1, std::memory_order_relaxed));
}
std::pair<ID, ID> GenerateIDBlock(uint64_t n) {
assert(n > 0);
uint64_t first = clock_.fetch_add(n, std::memory_order_relaxed);
return std::make_pair(ID(id_, first), ID(id_, first + n - 1));
}
uint16_t site_id() const { return id_; }
private:
Site(uint16_t id) : id_(id) {}
const uint16_t id_;
std::atomic<uint64_t> clock_{0};
static std::atomic<uint16_t> id_gen_;
};
class AnnotatedString {
public:
AnnotatedString();
static ID Begin() { return ID(0, 1); }
static ID End() { return ID(0, 2); }
static ID MakeRawInsert(CommandSet* commands, Site* site,
absl::string_view chars, ID after, ID before);
ID MakeInsert(CommandSet* commands, Site* site, absl::string_view chars,
ID after) const {
return MakeRawInsert(commands, site, chars, after,
chars_.Lookup(after)->next);
}
ID Insert(CommandSet* commands, Site* site, absl::string_view chars,
ID after) {
const size_t start = commands->mutable_commands()->size();
ID r = MakeInsert(commands, site, chars, after);
for (size_t i = start; i < commands->mutable_commands()->size(); i++) {
Integrate(commands->mutable_commands()->Get(i));
}
return r;
}
ID Insert(Site* site, absl::string_view chars, ID after) {
CommandSet throw_away;
return Insert(&throw_away, site, chars, after);
}
static void MakeDelete(CommandSet* commands, ID id);
void MakeDelete(CommandSet* commands, ID beg, ID end) const {
AllIterator it(*this, beg);
while (it.id() != end) {
MakeDelete(commands, it.id());
it.MoveNext();
}
}
static void MakeDelDecl(CommandSet* commands, ID id);
static void MakeDelMark(CommandSet* commands, ID id);
static ID MakeDecl(CommandSet* commands, Site* site,
const Attribute& attribute);
static ID MakeMark(CommandSet* commands, Site* site,
const Annotation& annotation);
AnnotatedString Integrate(const CommandSet& commands) const;
void Integrate(const Command& command);
// return <0 if a before b, >0 if a after b, ==0 if a==b
int OrderIDs(ID a, ID b) const;
void MakeOrderedIDs(ID* a, ID* b) const {
if (OrderIDs(*a, *b) > 0) {
std::swap(*a, *b);
}
}
std::string Render() const { return Render(Begin(), End()); }
std::string Render(ID begin, ID end) const;
bool SameContentIdentity(const AnnotatedString& other) const {
return chars_.SameIdentity(other.chars_);
}
bool SameTotalIdentity(const AnnotatedString& other) const {
return chars_.SameIdentity(other.chars_) &&
attributes_by_type_.SameIdentity(other.attributes_by_type_) &&
annotations_by_type_.SameIdentity(other.annotations_by_type_);
}
// F(ID annid, ID begin, ID end, const Attribute& attr)
template <class F>
void ForEachAnnotation(Attribute::DataCase type, F&& f) const {
const auto* m = annotations_by_type_.Lookup(type);
if (!m) return;
const auto* am = attributes_by_type_.Lookup(type);
assert(am);
m->ForEach([f, am](ID id, const Annotation& ann) {
const Attribute* attr = am->Lookup(ann.attribute());
if (attr == nullptr) return;
f(id, ann.begin(), ann.end(), *attr);
});
}
// F(ID attrid, const Attribute& attr)
template <class F>
void ForEachAttribute(Attribute::DataCase type, F&& f) const {
const auto* m = attributes_by_type_.Lookup(type);
if (!m) return;
m->ForEach(f);
}
AnnotatedStringMsg AsProto() const;
private:
void IntegrateInsert(ID id, const InsertCommand& cmd);
void IntegrateDelChar(ID id);
void IntegrateDecl(ID id, const Attribute& decl);
void IntegrateDelDecl(ID id);
void IntegrateMark(ID id, const Annotation& annotation);
void IntegrateDelMark(ID id);
void IntegrateInsertChar(ID id, char c, ID after, ID before);
struct CharInfo {
bool visible;
char chr;
// next/prev in document
ID next;
ID prev;
// before/after in insert order (according to creator)
ID after;
ID before;
// cache of which annotations are on this character
AVL<ID> annotations;
};
struct LineBreak {
ID prev;
ID next;
};
AVL<ID, CharInfo> chars_;
AVL<ID, LineBreak> line_breaks_;
AVL<ID, Attribute::DataCase> attributes_;
AVL<Attribute::DataCase, AVL<ID, Attribute>> attributes_by_type_;
AVL<ID, Attribute::DataCase> annotations_;
AVL<Attribute::DataCase, AVL<ID, Annotation>> annotations_by_type_;
AVL<ID> graveyard_;
public:
class AllIterator {
public:
AllIterator(const AnnotatedString& str, ID where)
: str_(&str), pos_(where), cur_(str_->chars_.Lookup(pos_)) {}
bool is_end() const { return pos_ == End(); }
bool is_begin() const { return pos_ == Begin(); }
ID id() const { return pos_; }
char value() const { return cur_->chr; }
bool is_visible() const { return cur_->visible; }
void MoveNext() {
pos_ = cur_->next;
cur_ = str_->chars_.Lookup(pos_);
}
void MovePrev() {
pos_ = cur_->prev;
cur_ = str_->chars_.Lookup(pos_);
}
AllIterator Next() {
AllIterator i(*this);
i.MoveNext();
return i;
}
AllIterator Prev() {
AllIterator i(*this);
i.MovePrev();
return i;
}
// F(const Attribute& attr)
template <class F>
void ForEachAttrValue(F&& f) {
// Log() << "FEAV: " << pos_.id << " " << cur_->annotations.Empty();
cur_->annotations.ForEach([this, f](ID id) {
// Log() << "EXAM " << id.id << " on " << pos_.id;
const auto* dc = str_->annotations_.Lookup(id);
if (!dc) {
Log() << "no dc for " << id.id;
return;
}
const Annotation& ann =
*str_->annotations_by_type_.Lookup(*dc)->Lookup(id);
const Attribute* attr =
str_->attributes_by_type_.Lookup(*dc)->Lookup(ann.attribute());
if (!attr) {
Log() << "failed attr lookup";
return;
}
// Log() << attr->DebugString();
f(*attr);
});
}
private:
const AnnotatedString* str_;
ID pos_;
const CharInfo* cur_;
};
class Iterator {
public:
Iterator(const AnnotatedString& str, ID where) : it_(str, where) {
while (!is_begin() && !it_.is_visible()) {
it_.MovePrev();
}
}
bool is_end() const { return it_.is_end(); }
bool is_begin() const { return it_.is_begin(); }
ID id() const { return it_.id(); }
char value() const { return it_.value(); }
void MoveNext() {
if (!is_end()) it_.MoveNext();
while (!is_end() && !it_.is_visible()) it_.MoveNext();
}
void MovePrev() {
if (!is_begin()) it_.MovePrev();
while (!is_begin() && !it_.is_visible()) it_.MovePrev();
}
Iterator Next() {
Iterator i(*this);
i.MoveNext();
return i;
}
Iterator Prev() {
Iterator i(*this);
i.MovePrev();
return i;
}
// F(const Attribute& attr)
template <class F>
void ForEachAttrValue(F&& f) {
it_.ForEachAttrValue(std::forward<F>(f));
}
private:
AllIterator it_;
};
class LineIterator {
public:
LineIterator(const AnnotatedString& str, ID where) : str_(&str) {
Iterator it(str, where);
const LineBreak* lb = str.line_breaks_.Lookup(it.id());
while (lb == nullptr) {
it.MovePrev();
lb = str.line_breaks_.Lookup(it.id());
}
id_ = it.id();
}
static LineIterator FromLineNumber(const AnnotatedString& str,
int line_no) {
LineIterator it(str, Begin());
for (int i = 0; i < line_no; i++) {
it.MoveNext();
}
return it;
}
bool is_end() const { return id_ == End(); }
bool is_begin() const { return id_ == Begin(); }
void MovePrev() {
if (id_ == Begin()) return;
id_ = str_->line_breaks_.Lookup(id_)->prev;
}
void MoveNext() {
if (id_ == End()) return;
id_ = str_->line_breaks_.Lookup(id_)->next;
}
LineIterator Next() {
LineIterator tmp(*this);
tmp.MoveNext();
return tmp;
}
Iterator AsIterator() { return Iterator(*str_, id_); }
AllIterator AsAllIterator() { return AllIterator(*str_, id_); }
ID id() const { return id_; }
private:
const AnnotatedString* str_;
ID id_;
};
};
class AnnotationEditor {
public:
AnnotationEditor(Site* site) : site_(site) {}
void BeginEdit(CommandSet* commands) { commands_ = commands; }
void EndEdit();
class ScopedEdit {
public:
ScopedEdit(AnnotationEditor* editor, CommandSet* commands)
: editor_(editor) {
editor_->BeginEdit(commands);
}
~ScopedEdit() { editor_->EndEdit(); }
private:
AnnotationEditor* const editor_;
};
ID AttrID(const Attribute& attr);
ID Mark(const Annotation& anno);
ID Mark(ID beg, ID end, ID attr);
ID Mark(ID beg, ID end, const Attribute& attr) {
return Mark(beg, end, AttrID(attr));
}
private:
typedef std::unordered_map<std::string, ID> ValToID;
Site* const site_;
CommandSet* commands_ = nullptr;
ValToID last_attr2id_;
ValToID new_attr2id_;
ValToID last_ann2id_;
ValToID new_ann2id_;
};