-
Notifications
You must be signed in to change notification settings - Fork 2
/
bookmark_container.cc
56 lines (48 loc) · 1.84 KB
/
bookmark_container.cc
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
#include "bookmark.hh"
#include "bookmark_container.hh"
#include "duplication.hh"
#include <algorithm> // stable_sort, remove_if
#include <functional> // mem_fun_ref
void BookmarkContainer::report(int bookmarkIx,
const Duplication& duplication,
int instanceNr,
const Options& options) const
{
itsBookmarks[bookmarkIx].report(duplication, instanceNr, options);
}
bool BookmarkContainer::same(size_t a,
size_t b,
int longestSame,
const char* processedEnd) const
{
return itsBookmarks[a].sameAs(itsBookmarks[b], longestSame, processedEnd);
}
int BookmarkContainer::nrOfSame(int a, int b) const
{
return itsBookmarks[a].nrOfSame(itsBookmarks[b]);
}
void BookmarkContainer::sort()
{
// std::stable_sort(), which is a merge sort, has proved to be much faster
// than std::sort() in this context.
std::stable_sort(itsBookmarks.begin(), itsBookmarks.end());
}
void BookmarkContainer::clearWithin(const Duplication& d)
{
for (int i = 0; i < d.instances; ++i)
{
const char* reportStart =
itsBookmarks[d.indexOf1stInstance + i].itsProcessedText;
for (size_t ix = 0; ix < itsBookmarks.size() - 1; ++ix)
{
const char* t = itsBookmarks[ix].itsProcessedText;
if (t >= reportStart && t < reportStart + d.longestSame)
itsBookmarks[ix].clear();
}
}
// Remove all cleared bookmarks while maintaining a sorted array.
std::vector<Bookmark>::iterator newEnd =
std::remove_if(itsBookmarks.begin(), itsBookmarks.end(),
[](const Bookmark& b) { return b.isCleared(); });
itsBookmarks.resize(newEnd - itsBookmarks.begin());
}