|
| 1 | +//===--- UIntBuffer.swift - Bounded Collection of Unisigned Integer -------===// |
| 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 | +// Stores a smaller unsigned integer type inside a larger one, with a limit of |
| 14 | +// 255 elements. |
| 15 | +// |
| 16 | +//===----------------------------------------------------------------------===// |
| 17 | +@_fixed_layout |
| 18 | +public struct _UIntBuffer< |
| 19 | + Storage: UnsignedInteger & FixedWidthInteger, |
| 20 | + Element: UnsignedInteger & FixedWidthInteger |
| 21 | +> { |
| 22 | + public var _storage: Storage |
| 23 | + public var _bitCount: UInt8 |
| 24 | + |
| 25 | + @inline(__always) |
| 26 | + public init(_storage: Storage, _bitCount: UInt8) { |
| 27 | + self._storage = _storage |
| 28 | + self._bitCount = _bitCount |
| 29 | + } |
| 30 | + |
| 31 | + @inline(__always) |
| 32 | + public init(containing e: Element) { |
| 33 | + _storage = Storage(extendingOrTruncating: e) |
| 34 | + _bitCount = UInt8(extendingOrTruncating: Element.bitWidth) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +extension _UIntBuffer : Sequence { |
| 39 | + public typealias SubSequence = RangeReplaceableRandomAccessSlice<_UIntBuffer> |
| 40 | + |
| 41 | + @_fixed_layout |
| 42 | + public struct Iterator : IteratorProtocol, Sequence { |
| 43 | + @inline(__always) |
| 44 | + public init(_ x: _UIntBuffer) { _impl = x } |
| 45 | + |
| 46 | + @inline(__always) |
| 47 | + public mutating func next() -> Element? { |
| 48 | + if _impl._bitCount == 0 { return nil } |
| 49 | + defer { |
| 50 | + _impl._storage = _impl._storage &>> Element.bitWidth |
| 51 | + _impl._bitCount = _impl._bitCount &- _impl._elementWidth |
| 52 | + } |
| 53 | + return Element(extendingOrTruncating: _impl._storage) |
| 54 | + } |
| 55 | + public |
| 56 | + var _impl: _UIntBuffer |
| 57 | + } |
| 58 | + |
| 59 | + @inline(__always) |
| 60 | + public func makeIterator() -> Iterator { |
| 61 | + return Iterator(self) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +extension _UIntBuffer : Collection { |
| 66 | + public typealias _Element = Element |
| 67 | + |
| 68 | + public struct Index : Comparable { |
| 69 | + @_versioned |
| 70 | + var bitOffset: UInt8 |
| 71 | + |
| 72 | + @_versioned |
| 73 | + init(bitOffset: UInt8) { self.bitOffset = bitOffset } |
| 74 | + |
| 75 | + public static func == (lhs: Index, rhs: Index) -> Bool { |
| 76 | + return lhs.bitOffset == rhs.bitOffset |
| 77 | + } |
| 78 | + public static func < (lhs: Index, rhs: Index) -> Bool { |
| 79 | + return lhs.bitOffset < rhs.bitOffset |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public var startIndex : Index { |
| 84 | + @inline(__always) |
| 85 | + get { return Index(bitOffset: 0) } |
| 86 | + } |
| 87 | + |
| 88 | + public var endIndex : Index { |
| 89 | + @inline(__always) |
| 90 | + get { return Index(bitOffset: _bitCount) } |
| 91 | + } |
| 92 | + |
| 93 | + @inline(__always) |
| 94 | + public func index(after i: Index) -> Index { |
| 95 | + return Index(bitOffset: i.bitOffset &+ _elementWidth) |
| 96 | + } |
| 97 | + |
| 98 | + @_versioned |
| 99 | + internal var _elementWidth : UInt8 { |
| 100 | + return UInt8(extendingOrTruncating: Element.bitWidth) |
| 101 | + } |
| 102 | + |
| 103 | + public subscript(i: Index) -> Element { |
| 104 | + @inline(__always) |
| 105 | + get { |
| 106 | + return Element(extendingOrTruncating: _storage &>> i.bitOffset) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +extension _UIntBuffer : BidirectionalCollection { |
| 112 | + @inline(__always) |
| 113 | + public func index(before i: Index) -> Index { |
| 114 | + return Index(bitOffset: i.bitOffset &- _elementWidth) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +extension _UIntBuffer : RandomAccessCollection { |
| 119 | + public typealias Indices = DefaultRandomAccessIndices<_UIntBuffer> |
| 120 | + public typealias IndexDistance = Int |
| 121 | + |
| 122 | + @inline(__always) |
| 123 | + public func index(_ i: Index, offsetBy n: IndexDistance) -> Index { |
| 124 | + let x = IndexDistance(i.bitOffset) &+ n &* Element.bitWidth |
| 125 | + return Index(bitOffset: UInt8(extendingOrTruncating: x)) |
| 126 | + } |
| 127 | + |
| 128 | + @inline(__always) |
| 129 | + public func distance(from i: Index, to j: Index) -> IndexDistance { |
| 130 | + return (Int(j.bitOffset) &- Int(i.bitOffset)) / Element.bitWidth |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +extension FixedWidthInteger { |
| 135 | + @inline(__always) |
| 136 | + @_versioned |
| 137 | + func _fullShiftLeft<N: FixedWidthInteger>(_ n: N) -> Self { |
| 138 | + return (self &<< ((n &+ 1) &>> 1)) &<< (n &>> 1) |
| 139 | + } |
| 140 | + @inline(__always) |
| 141 | + @_versioned |
| 142 | + func _fullShiftRight<N: FixedWidthInteger>(_ n: N) -> Self { |
| 143 | + return (self &>> ((n &+ 1) &>> 1)) &>> (n &>> 1) |
| 144 | + } |
| 145 | + @inline(__always) |
| 146 | + @_versioned |
| 147 | + static func _lowBits<N: FixedWidthInteger>(_ n: N) -> Self { |
| 148 | + return ~((~0 as Self)._fullShiftLeft(n)) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +extension Range { |
| 153 | + @inline(__always) |
| 154 | + @_versioned |
| 155 | + func _contains_(_ other: Range) -> Bool { |
| 156 | + return other.clamped(to: self) == other |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +extension _UIntBuffer : RangeReplaceableCollection { |
| 161 | + @inline(__always) |
| 162 | + public init() { |
| 163 | + _storage = 0 |
| 164 | + _bitCount = 0 |
| 165 | + } |
| 166 | + |
| 167 | + public var capacity: Int { |
| 168 | + return Storage.bitWidth / Element.bitWidth |
| 169 | + } |
| 170 | + |
| 171 | + @inline(__always) |
| 172 | + public mutating func append(_ newElement: Element) { |
| 173 | + _debugPrecondition(count + 1 <= capacity) |
| 174 | + _storage |= Storage(newElement) &<< _bitCount |
| 175 | + _bitCount = _bitCount &+ _elementWidth |
| 176 | + } |
| 177 | + |
| 178 | + @inline(__always) |
| 179 | + public mutating func replaceSubrange<C: Collection>( |
| 180 | + _ target: Range<Index>, with replacement: C |
| 181 | + ) where C._Element == Element { |
| 182 | + _debugPrecondition( |
| 183 | + (0..<_bitCount)._contains_( |
| 184 | + target.lowerBound.bitOffset..<target.upperBound.bitOffset)) |
| 185 | + |
| 186 | + let replacement1 = _UIntBuffer(replacement) |
| 187 | + |
| 188 | + let targetCount = distance( |
| 189 | + from: target.lowerBound, to: target.upperBound) |
| 190 | + let growth = replacement1.count &- targetCount |
| 191 | + _debugPrecondition(count + growth <= capacity) |
| 192 | + |
| 193 | + let headCount = distance(from: startIndex, to: target.lowerBound) |
| 194 | + let tailOffset = distance(from: startIndex, to: target.upperBound) |
| 195 | + |
| 196 | + let w = Element.bitWidth |
| 197 | + let headBits = _storage & ._lowBits(headCount &* w) |
| 198 | + let tailBits = _storage._fullShiftRight(tailOffset &* w) |
| 199 | + |
| 200 | + _storage = headBits |
| 201 | + _storage |= replacement1._storage &<< (headCount &* w) |
| 202 | + _storage |= tailBits &<< ((tailOffset &+ growth) &* w) |
| 203 | + _bitCount = UInt8( |
| 204 | + extendingOrTruncating: IndexDistance(_bitCount) &+ growth &* w) |
| 205 | + } |
| 206 | +} |
0 commit comments