- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathIndexSubset.cpp
150 lines (137 loc) · 4.47 KB
/
IndexSubset.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//===--- IndexSubset.cpp - Fixed-size subset of indices -------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include"swift/AST/IndexSubset.h"
#include"swift/Basic/Assertions.h"
usingnamespaceswift;
IndexSubset *
IndexSubset::getFromString(ASTContext &ctx, StringRef string) {
unsigned capacity = string.size();
llvm::SmallBitVector indices(capacity);
for (unsigned i : range(capacity)) {
if (string[i] == 'S')
indices.set(i);
elseif (string[i] != 'U')
returnnullptr;
}
returnget(ctx, indices);
}
std::string IndexSubset::getString() const {
std::string result;
result.reserve(capacity);
for (unsigned i : range(capacity))
result += contains(i) ? 'S' : 'U';
return result;
}
boolIndexSubset::isSubsetOf(IndexSubset *other) const {
assert(capacity == other->capacity);
for (autoindex : range(numBitWords))
if (getBitWord(index) & ~other->getBitWord(index))
returnfalse;
returntrue;
}
boolIndexSubset::isSupersetOf(IndexSubset *other) const {
assert(capacity == other->capacity);
for (autoindex : range(numBitWords))
if (~getBitWord(index) & other->getBitWord(index))
returnfalse;
returntrue;
}
boolIndexSubset::isDisjointWith(IndexSubset *other) const {
assert(capacity == other->capacity);
for (autoindex : range(numBitWords))
if (getBitWord(index) & other->getBitWord(index))
returnfalse;
returntrue;
}
IndexSubset *IndexSubset::adding(unsigned index, ASTContext &ctx) const {
assert(index < getCapacity());
if (contains(index))
returnconst_cast<IndexSubset *>(this);
SmallBitVector newIndices(capacity);
bool inserted = false;
for (auto curIndex : getIndices()) {
if (!inserted && curIndex > index) {
newIndices.set(index);
inserted = true;
}
newIndices.set(curIndex);
}
returnget(ctx, newIndices);
}
IndexSubset *IndexSubset::extendingCapacity(
ASTContext &ctx, unsigned newCapacity) const {
assert(newCapacity >= capacity);
if (newCapacity == capacity)
returnconst_cast<IndexSubset *>(this);
SmallBitVector indices(newCapacity);
for (autoindex : getIndices())
indices.set(index);
returnIndexSubset::get(ctx, indices);
}
voidIndexSubset::print(llvm::raw_ostream &s) const {
s << '{';
llvm::interleave(
range(capacity), [this, &s](unsigned i) { s << contains(i); },
[&s] { s << ", "; });
s << '}';
}
voidIndexSubset::dump() const {
auto &s = llvm::errs();
s << "(index_subset capacity=" << capacity << " indices=(";
interleave(getIndices(), [&s](unsigned i) { s << i; },
[&s] { s << ", "; });
s << "))\n";
}
intIndexSubset::findNext(int startIndex) const {
assert(startIndex < (int)capacity && "Start index cannot be past the end");
unsigned bitWordIndex = 0, offset = 0;
if (startIndex >= 0) {
auto indexAndOffset = getBitWordIndexAndOffset(startIndex);
bitWordIndex = indexAndOffset.first;
offset = indexAndOffset.second + 1;
}
for (; bitWordIndex < numBitWords; ++bitWordIndex, offset = 0) {
for (; offset < numBitsPerBitWord; ++offset) {
autoindex = bitWordIndex * numBitsPerBitWord + offset;
auto bitWord = getBitWord(bitWordIndex);
if (!bitWord)
break;
if (index >= capacity)
return capacity;
if (bitWord & ((BitWord)1 << offset))
returnindex;
}
}
return capacity;
}
intIndexSubset::findPrevious(int endIndex) const {
assert(endIndex >= 0 && "End index cannot be before the start");
int bitWordIndex = numBitWords - 1, offset = numBitsPerBitWord - 1;
if (endIndex < (int)capacity) {
auto indexAndOffset = getBitWordIndexAndOffset(endIndex);
bitWordIndex = (int)indexAndOffset.first;
offset = (int)indexAndOffset.second - 1;
}
for (; bitWordIndex >= 0; --bitWordIndex, offset = numBitsPerBitWord - 1) {
for (; offset >= 0; --offset) {
autoindex = bitWordIndex * (int)numBitsPerBitWord + offset;
auto bitWord = getBitWord(bitWordIndex);
if (!bitWord)
break;
if (index < 0)
return -1;
if (bitWord & ((BitWord)1 << offset))
returnindex;
}
}
return -1;
}