Skip to content

Commit 379e536

Browse files
first commit
0 parents  commit 379e536

File tree

5 files changed

+481
-0
lines changed

5 files changed

+481
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm
7+
.netrc
8+
Package.resolved
9+
.vscode

Package.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// swift-tools-version:6.0
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-variablelengtharray",
7+
products: [
8+
.library(
9+
name: "swift-variablelengtharray",
10+
targets: ["VariableLengthArray"]
11+
)
12+
],
13+
targets: [
14+
.target(
15+
name: "VariableLengthArray"
16+
),
17+
.testTarget(
18+
name: "swift-variablelengtharrayTests",
19+
dependencies: ["VariableLengthArray"]
20+
)
21+
]
22+
)
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
2+
extension VLArray {
3+
public struct Joined: ~Copyable, @unchecked Sendable {
4+
public typealias Index = Int
5+
6+
@usableFromInline
7+
let _storage:UnsafeMutableBufferPointer<UnsafeMutableBufferPointer<Element>>
8+
9+
public init(repeating value: Element) {
10+
fatalError("not implemented")
11+
}
12+
13+
@inlinable
14+
public static func create<E: Error, let count: Int>(
15+
_ elements: borrowing InlineArray<count, VLArray<Element>>,
16+
closure: (inout Self) throws(E) -> Void
17+
) rethrows {
18+
try withUnsafeTemporaryAllocation(of: UnsafeMutableBufferPointer<Element>.self, capacity: elements.count, { pointer in
19+
for i in elements.indices {
20+
pointer.initializeElement(at: i, to: elements[i]._storage)
21+
}
22+
var joined = Self.init(_storage: pointer)
23+
try closure(&joined)
24+
})
25+
}
26+
27+
@inlinable
28+
public init(_storage: UnsafeMutableBufferPointer<UnsafeMutableBufferPointer<Element>>) {
29+
self._storage = _storage
30+
}
31+
32+
@inlinable
33+
public var startIndex: Index {
34+
0
35+
}
36+
@inlinable public var endIndex: Index {
37+
count
38+
}
39+
40+
@inlinable public var count: Int {
41+
_storage.count
42+
}
43+
44+
@inlinable
45+
public var capacity: Int {
46+
var c = 0
47+
for i in _storage.indices {
48+
c += _storage[i].count
49+
}
50+
return c
51+
}
52+
53+
@inlinable
54+
public var isEmpty:Bool {
55+
count == 0
56+
}
57+
@inlinable
58+
public var indices:Range<Index> {
59+
.init(uncheckedBounds: (0, endIndex))
60+
}
61+
62+
@inlinable
63+
public func index(after i: Index) -> Index {
64+
i &+ 1
65+
}
66+
67+
@inlinable
68+
public func index(before i: Index) -> Index {
69+
i &- 1
70+
}
71+
72+
@inlinable
73+
public subscript(index: Index) -> UnsafeMutableBufferPointer<Element> {
74+
get {
75+
_storage[index]
76+
}
77+
set {
78+
_storage[index] = newValue
79+
}
80+
}
81+
82+
@inlinable
83+
public func elementAt(index: Index) -> Element {
84+
var previousElements = 0
85+
for indice in _storage.indices {
86+
let e = _storage[indice]
87+
let currentElements = e.count
88+
if index < previousElements + currentElements {
89+
return e[index - previousElements]
90+
}
91+
previousElements += currentElements
92+
}
93+
fatalError("out-of-bounds")
94+
}
95+
96+
@inlinable
97+
public mutating func setElementAt(index: Index, element: Element) {
98+
var previousElements = 0
99+
for indice in _storage.indices {
100+
let e = _storage[indice]
101+
let currentElements = e.count
102+
if index < previousElements + currentElements {
103+
_storage[indice][index - previousElements] = element
104+
break
105+
}
106+
previousElements += currentElements
107+
}
108+
}
109+
110+
@inlinable
111+
public func forEachElement<E: Error>(
112+
_ yielding: (Element) throws(E) -> Void
113+
) rethrows {
114+
for i in _storage.indices {
115+
let buffer = _storage[i]
116+
for j in buffer.indices {
117+
try yielding(buffer[j])
118+
}
119+
}
120+
}
121+
122+
//@inlinable
123+
//public mutating func swapAt(_ i: Index, _ j: Index) {
124+
//}
125+
}
126+
}

0 commit comments

Comments
 (0)