Skip to content

Commit 389fa5f

Browse files
committed
stdlib: move _BitMap to its own file
1 parent 2d63879 commit 389fa5f

File tree

4 files changed

+68
-54
lines changed

4 files changed

+68
-54
lines changed

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ set(SWIFTLIB_ESSENTIAL
114114
UnicodeScalar.swift
115115
UnicodeTrie.swift.gyb
116116
Unmanaged.swift
117+
UnsafeBitMap.swift
117118
UnsafeBufferPointer.swift.gyb
118119
UnsafePointer.swift.gyb
119120
WriteBackMutableSlice.swift

stdlib/public/core/GroupInfo.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,6 @@
140140
"Process.swift",
141141
"Tuple.swift",
142142
"NewtypeWrapper.swift",
143+
"UnsafeBitMap.swift"
143144
]
144145
}

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,60 +2455,6 @@ collections = [
24552455
]
24562456
}%
24572457

2458-
/// A wrapper around a bitmap storage with room for at least `bitCount` bits.
2459-
internal struct _BitMap {
2460-
internal let values: UnsafeMutablePointer<UInt>
2461-
internal let bitCount: Int
2462-
2463-
internal static func wordIndex(_ i: Int) -> Int {
2464-
// Note: We perform the operation on UInts to get faster unsigned math
2465-
// (shifts).
2466-
return Int(bitPattern: UInt(bitPattern: i) / UInt(UInt._sizeInBits))
2467-
}
2468-
2469-
internal static func bitIndex(_ i: Int) -> UInt {
2470-
// Note: We perform the operation on UInts to get faster unsigned math
2471-
// (shifts).
2472-
return UInt(bitPattern: i) % UInt(UInt._sizeInBits)
2473-
}
2474-
2475-
internal static func sizeInWords(forCapacity bitCount: Int) -> Int {
2476-
return bitCount + Int._sizeInBytes - 1 / Int._sizeInBytes
2477-
}
2478-
2479-
internal init(storage: UnsafeMutablePointer<UInt>, bitCount: Int) {
2480-
self.bitCount = bitCount
2481-
self.values = storage
2482-
}
2483-
2484-
internal var numberOfWords: Int {
2485-
return _BitMap.sizeInWords(forCapacity: bitCount)
2486-
}
2487-
2488-
internal func initializeToZero() {
2489-
values.initialize(with: 0, count: numberOfWords)
2490-
}
2491-
2492-
internal subscript(i: Int) -> Bool {
2493-
get {
2494-
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
2495-
let word = values[_BitMap.wordIndex(i)]
2496-
let bit = word & (1 << _BitMap.bitIndex(i))
2497-
return bit != 0
2498-
}
2499-
nonmutating set {
2500-
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
2501-
let wordIdx = _BitMap.wordIndex(i)
2502-
let bitMask = 1 << _BitMap.bitIndex(i)
2503-
if newValue {
2504-
values[wordIdx] = values[wordIdx] | bitMask
2505-
} else {
2506-
values[wordIdx] = values[wordIdx] & ~bitMask
2507-
}
2508-
}
2509-
}
2510-
}
2511-
25122458
/// Header part of the native storage.
25132459
internal struct _HashedContainerStorageHeader {
25142460
internal init(capacity: Int) {

stdlib/public/core/UnsafeBitMap.swift

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
/// A wrapper around a bitmap storage with room for at least `bitCount` bits.
14+
internal struct _BitMap {
15+
internal let values: UnsafeMutablePointer<UInt>
16+
internal let bitCount: Int
17+
18+
internal static func wordIndex(_ i: Int) -> Int {
19+
// Note: We perform the operation on UInts to get faster unsigned math
20+
// (shifts).
21+
return Int(bitPattern: UInt(bitPattern: i) / UInt(UInt._sizeInBits))
22+
}
23+
24+
internal static func bitIndex(_ i: Int) -> UInt {
25+
// Note: We perform the operation on UInts to get faster unsigned math
26+
// (shifts).
27+
return UInt(bitPattern: i) % UInt(UInt._sizeInBits)
28+
}
29+
30+
internal static func sizeInWords(forCapacity bitCount: Int) -> Int {
31+
return bitCount + Int._sizeInBytes - 1 / Int._sizeInBytes
32+
}
33+
34+
internal init(storage: UnsafeMutablePointer<UInt>, bitCount: Int) {
35+
self.bitCount = bitCount
36+
self.values = storage
37+
}
38+
39+
internal var numberOfWords: Int {
40+
return _BitMap.sizeInWords(forCapacity: bitCount)
41+
}
42+
43+
internal func initializeToZero() {
44+
values.initialize(with: 0, count: numberOfWords)
45+
}
46+
47+
internal subscript(i: Int) -> Bool {
48+
get {
49+
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
50+
let word = values[_BitMap.wordIndex(i)]
51+
let bit = word & (1 << _BitMap.bitIndex(i))
52+
return bit != 0
53+
}
54+
nonmutating set {
55+
_sanityCheck(i < Int(bitCount) && i >= 0, "index out of bounds")
56+
let wordIdx = _BitMap.wordIndex(i)
57+
let bitMask = 1 << _BitMap.bitIndex(i)
58+
if newValue {
59+
values[wordIdx] = values[wordIdx] | bitMask
60+
} else {
61+
values[wordIdx] = values[wordIdx] & ~bitMask
62+
}
63+
}
64+
}
65+
}
66+

0 commit comments

Comments
 (0)