Skip to content

Commit 483a378

Browse files
authored
[AutoDiff] NFC: Move some 'IndexSubset' method impls to their own file. (#27627)
- Move some 'IndexSubset' method implemenetations from AutoDiff.cpp to a new file called IndexSubset.cpp. - Fix some indentation errors caused by the earlier renaming of `AutoDiffIndexSubset`.
1 parent 68b96a2 commit 483a378

File tree

4 files changed

+131
-116
lines changed

4 files changed

+131
-116
lines changed

include/swift/AST/IndexSubset.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- IndexSubset.h - Fixed-size subset of indices ---------------------===//
1+
//===---------- IndexSubset.h - Fixed-size subset of indices --------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -82,8 +82,8 @@ class IndexSubset : public llvm::FoldingSetNode {
8282
}
8383

8484
explicit IndexSubset(const SmallBitVector &indices)
85-
: capacity((unsigned)indices.size()),
86-
numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
85+
: capacity((unsigned)indices.size()),
86+
numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
8787
std::uninitialized_fill_n(getBitWordsData(), numBitWords, 0);
8888
for (auto i : indices.set_bits()) {
8989
unsigned bitWordIndex, offset;

lib/AST/AutoDiff.cpp

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- AutoDiff.cpp - Swift Differentiable Programming ------------------===//
1+
//===--------- AutoDiff.cpp - Swift Differentiable Programming ------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -178,118 +178,6 @@ SILLinkage autodiff::getAutoDiffDerivativeFunctionLinkage(
178178
return SILLinkage::Hidden;
179179
}
180180

181-
IndexSubset *
182-
IndexSubset::getFromString(ASTContext &ctx, StringRef string) {
183-
if (string.size() < 0) return nullptr;
184-
unsigned capacity = string.size();
185-
llvm::SmallBitVector indices(capacity);
186-
for (unsigned i : range(capacity)) {
187-
if (string[i] == 'S')
188-
indices.set(i);
189-
else if (string[i] != 'U')
190-
return nullptr;
191-
}
192-
return get(ctx, indices);
193-
}
194-
195-
std::string IndexSubset::getString() const {
196-
std::string result;
197-
result.reserve(capacity);
198-
for (unsigned i : range(capacity))
199-
result += contains(i) ? 'S' : 'U';
200-
return result;
201-
}
202-
203-
bool IndexSubset::isSubsetOf(IndexSubset *other) const {
204-
assert(capacity == other->capacity);
205-
for (auto index : range(numBitWords))
206-
if (getBitWord(index) & ~other->getBitWord(index))
207-
return false;
208-
return true;
209-
}
210-
211-
bool IndexSubset::isSupersetOf(IndexSubset *other) const {
212-
assert(capacity == other->capacity);
213-
for (auto index : range(numBitWords))
214-
if (~getBitWord(index) & other->getBitWord(index))
215-
return false;
216-
return true;
217-
}
218-
219-
IndexSubset *IndexSubset::adding(unsigned index,
220-
ASTContext &ctx) const {
221-
assert(index < getCapacity());
222-
if (contains(index))
223-
return const_cast<IndexSubset *>(this);
224-
SmallBitVector newIndices(capacity);
225-
bool inserted = false;
226-
for (auto curIndex : getIndices()) {
227-
if (!inserted && curIndex > index) {
228-
newIndices.set(index);
229-
inserted = true;
230-
}
231-
newIndices.set(curIndex);
232-
}
233-
return get(ctx, newIndices);
234-
}
235-
236-
IndexSubset *IndexSubset::extendingCapacity(
237-
ASTContext &ctx, unsigned newCapacity) const {
238-
assert(newCapacity >= capacity);
239-
if (newCapacity == capacity)
240-
return const_cast<IndexSubset *>(this);
241-
SmallBitVector indices(newCapacity);
242-
for (auto index : getIndices())
243-
indices.set(index);
244-
return IndexSubset::get(ctx, indices);
245-
}
246-
247-
int IndexSubset::findNext(int startIndex) const {
248-
assert(startIndex < (int)capacity && "Start index cannot be past the end");
249-
unsigned bitWordIndex = 0, offset = 0;
250-
if (startIndex >= 0) {
251-
auto indexAndOffset = getBitWordIndexAndOffset(startIndex);
252-
bitWordIndex = indexAndOffset.first;
253-
offset = indexAndOffset.second + 1;
254-
}
255-
for (; bitWordIndex < numBitWords; ++bitWordIndex, offset = 0) {
256-
for (; offset < numBitsPerBitWord; ++offset) {
257-
auto index = bitWordIndex * numBitsPerBitWord + offset;
258-
auto bitWord = getBitWord(bitWordIndex);
259-
if (!bitWord)
260-
break;
261-
if (index >= capacity)
262-
return capacity;
263-
if (bitWord & ((BitWord)1 << offset))
264-
return index;
265-
}
266-
}
267-
return capacity;
268-
}
269-
270-
int IndexSubset::findPrevious(int endIndex) const {
271-
assert(endIndex >= 0 && "End index cannot be before the start");
272-
int bitWordIndex = numBitWords - 1, offset = numBitsPerBitWord - 1;
273-
if (endIndex < (int)capacity) {
274-
auto indexAndOffset = getBitWordIndexAndOffset(endIndex);
275-
bitWordIndex = (int)indexAndOffset.first;
276-
offset = (int)indexAndOffset.second - 1;
277-
}
278-
for (; bitWordIndex >= 0; --bitWordIndex, offset = numBitsPerBitWord - 1) {
279-
for (; offset < (int)numBitsPerBitWord; --offset) {
280-
auto index = bitWordIndex * (int)numBitsPerBitWord + offset;
281-
auto bitWord = getBitWord(bitWordIndex);
282-
if (!bitWord)
283-
break;
284-
if (index < 0)
285-
return -1;
286-
if (bitWord & ((BitWord)1 << offset))
287-
return index;
288-
}
289-
}
290-
return -1;
291-
}
292-
293181
Type VectorSpace::getType() const {
294182
switch (kind) {
295183
case Kind::Vector:

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ add_swift_host_library(swiftAST STATIC
5353
GenericSignatureBuilder.cpp
5454
Identifier.cpp
5555
ImportCache.cpp
56+
IndexSubset.cpp
5657
InlinableText.cpp
5758
LayoutConstraint.cpp
5859
Module.cpp

lib/AST/IndexSubset.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
//===-------- IndexSubset.cpp - Swift Differentiable Programming ----------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "swift/AST/IndexSubset.h"
14+
15+
using namespace swift;
16+
17+
IndexSubset *
18+
IndexSubset::getFromString(ASTContext &ctx, StringRef string) {
19+
if (string.size() < 0) return nullptr;
20+
unsigned capacity = string.size();
21+
llvm::SmallBitVector indices(capacity);
22+
for (unsigned i : range(capacity)) {
23+
if (string[i] == 'S')
24+
indices.set(i);
25+
else if (string[i] != 'U')
26+
return nullptr;
27+
}
28+
return get(ctx, indices);
29+
}
30+
31+
std::string IndexSubset::getString() const {
32+
std::string result;
33+
result.reserve(capacity);
34+
for (unsigned i : range(capacity))
35+
result += contains(i) ? 'S' : 'U';
36+
return result;
37+
}
38+
39+
bool IndexSubset::isSubsetOf(IndexSubset *other) const {
40+
assert(capacity == other->capacity);
41+
for (auto index : range(numBitWords))
42+
if (getBitWord(index) & ~other->getBitWord(index))
43+
return false;
44+
return true;
45+
}
46+
47+
bool IndexSubset::isSupersetOf(IndexSubset *other) const {
48+
assert(capacity == other->capacity);
49+
for (auto index : range(numBitWords))
50+
if (~getBitWord(index) & other->getBitWord(index))
51+
return false;
52+
return true;
53+
}
54+
55+
IndexSubset *IndexSubset::adding(unsigned index, ASTContext &ctx) const {
56+
assert(index < getCapacity());
57+
if (contains(index))
58+
return const_cast<IndexSubset *>(this);
59+
SmallBitVector newIndices(capacity);
60+
bool inserted = false;
61+
for (auto curIndex : getIndices()) {
62+
if (!inserted && curIndex > index) {
63+
newIndices.set(index);
64+
inserted = true;
65+
}
66+
newIndices.set(curIndex);
67+
}
68+
return get(ctx, newIndices);
69+
}
70+
71+
IndexSubset *IndexSubset::extendingCapacity(
72+
ASTContext &ctx, unsigned newCapacity) const {
73+
assert(newCapacity >= capacity);
74+
if (newCapacity == capacity)
75+
return const_cast<IndexSubset *>(this);
76+
SmallBitVector indices(newCapacity);
77+
for (auto index : getIndices())
78+
indices.set(index);
79+
return IndexSubset::get(ctx, indices);
80+
}
81+
82+
int IndexSubset::findNext(int startIndex) const {
83+
assert(startIndex < (int)capacity && "Start index cannot be past the end");
84+
unsigned bitWordIndex = 0, offset = 0;
85+
if (startIndex >= 0) {
86+
auto indexAndOffset = getBitWordIndexAndOffset(startIndex);
87+
bitWordIndex = indexAndOffset.first;
88+
offset = indexAndOffset.second + 1;
89+
}
90+
for (; bitWordIndex < numBitWords; ++bitWordIndex, offset = 0) {
91+
for (; offset < numBitsPerBitWord; ++offset) {
92+
auto index = bitWordIndex * numBitsPerBitWord + offset;
93+
auto bitWord = getBitWord(bitWordIndex);
94+
if (!bitWord)
95+
break;
96+
if (index >= capacity)
97+
return capacity;
98+
if (bitWord & ((BitWord)1 << offset))
99+
return index;
100+
}
101+
}
102+
return capacity;
103+
}
104+
105+
int IndexSubset::findPrevious(int endIndex) const {
106+
assert(endIndex >= 0 && "End index cannot be before the start");
107+
int bitWordIndex = numBitWords - 1, offset = numBitsPerBitWord - 1;
108+
if (endIndex < (int)capacity) {
109+
auto indexAndOffset = getBitWordIndexAndOffset(endIndex);
110+
bitWordIndex = (int)indexAndOffset.first;
111+
offset = (int)indexAndOffset.second - 1;
112+
}
113+
for (; bitWordIndex >= 0; --bitWordIndex, offset = numBitsPerBitWord - 1) {
114+
for (; offset < (int)numBitsPerBitWord; --offset) {
115+
auto index = bitWordIndex * (int)numBitsPerBitWord + offset;
116+
auto bitWord = getBitWord(bitWordIndex);
117+
if (!bitWord)
118+
break;
119+
if (index < 0)
120+
return -1;
121+
if (bitWord & ((BitWord)1 << offset))
122+
return index;
123+
}
124+
}
125+
return -1;
126+
}

0 commit comments

Comments
 (0)