-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,058 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/config/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// swift-tools-version: 5.7 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "SwiftyCollection", | ||
products: [ | ||
.library( | ||
name: "SwiftyCollection", | ||
targets: ["SwiftyCollection"]), | ||
], | ||
dependencies: [ | ||
], | ||
targets: [ | ||
.target( | ||
name: "SwiftyCollection", | ||
dependencies: []), | ||
.testTarget( | ||
name: "SwiftyCollectionTests", | ||
dependencies: ["SwiftyCollection"]), | ||
], | ||
swiftLanguageVersions: [SwiftVersion.version("5.7")] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# SwiftyCollection | ||
# SwiftyCollection | ||
|
||
A description of this package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Kulics Wu on 2022/9/28. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class ArrayList<Element>: SwiftyCollection, ExpressibleByArrayLiteral { | ||
public typealias ArrayLiteralElement = Element | ||
public typealias Element = Element | ||
public typealias Iterator = ArrayListIterator<Element> | ||
|
||
var data: Array<Element> | ||
var modCount: Int | ||
|
||
public init() { | ||
self.data = [] | ||
self.modCount = 0 | ||
} | ||
|
||
public init<S>(of collection: S) where S: Collection, S.Element == Element { | ||
self.data = Array(collection) | ||
self.modCount = 0 | ||
} | ||
|
||
public init(arrayLiteral elements: Element...) { | ||
self.data = elements | ||
self.modCount = 0 | ||
} | ||
|
||
private init(clone: Array<Element>) { | ||
self.data = clone | ||
self.modCount = 0 | ||
} | ||
|
||
public func makeIterator() -> Iterator { | ||
return ArrayListIterator(source: self) | ||
} | ||
|
||
public var count: Int { | ||
return self.data.count | ||
} | ||
|
||
public var startIndex: Int { 0 } | ||
public var endIndex: Int { self.data.endIndex } | ||
|
||
public func get(at index: Int) -> Element? { | ||
if index < 0 || index > self.count { | ||
return nil | ||
} | ||
return self.data[index] | ||
} | ||
|
||
public func set(_ newElement: Element, at index: Int)-> Element? { | ||
if index < 0 || index > self.count { | ||
return nil | ||
} | ||
let v = self.data[index] | ||
self.data[index] = newElement | ||
self.modCount += 1 | ||
return v | ||
} | ||
|
||
public subscript(position: Int) -> Element { | ||
get { | ||
return self.data[position] | ||
} | ||
set(newValue) { | ||
self.data[position] = newValue | ||
self.modCount += 1 | ||
} | ||
} | ||
|
||
public func append(_ newElement: Element) { | ||
self.data.append(newElement) | ||
self.modCount += 1 | ||
} | ||
|
||
public func appendAll<S>(contentsOf newElements: S) where S: Collection, Element == S.Element { | ||
self.data.append(contentsOf: newElements) | ||
self.modCount += 1 | ||
} | ||
|
||
public func prepend(_ newElement: Element) { | ||
self.data.insert(newElement, at: self.startIndex) | ||
self.modCount += 1 | ||
} | ||
|
||
public func prependAll<S>(contentsOf newElements: S) where S: Collection, Element == S.Element { | ||
self.data.insert(contentsOf: newElements, at: self.startIndex) | ||
self.modCount += 1 | ||
} | ||
|
||
public func insert(_ newElement: Element, at index: Int) { | ||
self.data.insert(newElement, at: index) | ||
self.modCount += 1 | ||
} | ||
|
||
public func insertAll<S>(contentsOf newElements: S, at index: Int) where S: Collection, Element == S.Element { | ||
self.data.insert(contentsOf: newElements, at: index) | ||
self.modCount += 1 | ||
} | ||
|
||
public func remove(at index: Int)-> Element? { | ||
if index < 0 || index > self.count { | ||
return nil | ||
} | ||
let element = self.data.remove(at: index) | ||
self.modCount += 1 | ||
return element | ||
} | ||
|
||
public func removeRange(at range: Range<Int>) { | ||
self.data.removeSubrange(range) | ||
self.modCount += 1 | ||
} | ||
|
||
public func contains(element: Element)-> Bool where Element: Equatable { | ||
return self.data.contains(element) | ||
} | ||
|
||
public func clone()-> ArrayList<Element> { | ||
return Self.init(clone: self.data) | ||
} | ||
|
||
public func clear() { | ||
self.data.removeAll(keepingCapacity: true) | ||
self.modCount += 1 | ||
} | ||
|
||
public func reverse() { | ||
self.data.reverse() | ||
self.modCount += 1 | ||
} | ||
|
||
public func sort(by compare: (Element, Element)-> Bool) { | ||
self.data.sort(by: compare) | ||
self.modCount += 1 | ||
} | ||
|
||
public func findFirst(where predicate: (Element)-> Bool)-> Element? { | ||
return self.data.first(where: predicate) | ||
} | ||
|
||
public func findLast(where predicate: (Element)-> Bool)-> Element? { | ||
return self.data.last(where: predicate) | ||
} | ||
|
||
public func toArray()-> Array<Element> { | ||
return self.data | ||
} | ||
} | ||
|
||
public struct ArrayListIterator<Element>: IteratorProtocol { | ||
public typealias Element = Element | ||
|
||
let source: ArrayList<Element> | ||
var iterator: Array<Element>.Iterator | ||
var modCount: Int | ||
|
||
init(source: ArrayList<Element>) { | ||
self.source = source | ||
self.iterator = source.data.makeIterator() | ||
self.modCount = source.modCount | ||
} | ||
|
||
public mutating func next() -> Element? { | ||
if self.modCount != self.source.modCount { | ||
fatalError("concurrent modification error") | ||
} | ||
return self.iterator.next() | ||
} | ||
} | ||
|
||
extension ArrayList: Equatable where Element: Equatable { | ||
public static func == (lhs: ArrayList<Element>, rhs: ArrayList<Element>) -> Bool { | ||
return lhs.data == rhs.data | ||
} | ||
|
||
public static func != (lhs: ArrayList<Element>, rhs: ArrayList<Element>) -> Bool { | ||
return !(lhs.data == rhs.data) | ||
} | ||
|
||
public func findFirstIndex(of element: Element)-> Int? { | ||
return self.data.firstIndex(of: element) | ||
} | ||
|
||
public func findLastIndex(of element: Element)-> Int? { | ||
return self.data.lastIndex(of: element) | ||
} | ||
} | ||
|
||
extension ArrayList: CustomStringConvertible { | ||
public var description: String { | ||
return self.data.description | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Kulics Wu on 2022/10/1. | ||
// | ||
|
||
import Foundation | ||
|
||
public final class ArrayStack<Element>: SwiftyCollection, ExpressibleByArrayLiteral { | ||
public typealias ArrayLiteralElement = Element | ||
public typealias Element = Element | ||
public typealias Iterator = ArrayStackIterator<Element> | ||
|
||
var data: Array<Element> | ||
var modCount: Int | ||
|
||
public init() { | ||
self.data = [] | ||
self.modCount = 0 | ||
} | ||
|
||
public init<S>(of collection: S) where S: Collection, S.Element == Element { | ||
self.data = Array(collection) | ||
self.modCount = 0 | ||
} | ||
|
||
public init(arrayLiteral elements: Element...) { | ||
self.data = elements | ||
self.modCount = 0 | ||
} | ||
|
||
private init(clone: Array<Element>) { | ||
self.data = clone | ||
self.modCount = 0 | ||
} | ||
|
||
public func makeIterator() -> Iterator { | ||
return ArrayStackIterator(source: self) | ||
} | ||
|
||
public var count: Int { | ||
return self.data.count | ||
} | ||
|
||
public func push(_ newElement: Element) { | ||
self.data.append(newElement) | ||
self.modCount += 1 | ||
} | ||
|
||
public func pushAll<S>(contentsOf newElements: S) where S: Collection, Element == S.Element { | ||
self.data.append(contentsOf: newElements) | ||
self.modCount += 1 | ||
} | ||
|
||
public func pop()-> Element? { | ||
let element = self.data.popLast() | ||
self.modCount += 1 | ||
return element | ||
} | ||
|
||
public func peek() -> Element? { | ||
return self.data.last | ||
} | ||
|
||
public func clone()-> ArrayStack<Element> { | ||
return Self.init(clone: self.data) | ||
} | ||
|
||
public func clear() { | ||
self.data.removeAll(keepingCapacity: true) | ||
self.modCount += 1 | ||
} | ||
|
||
public func toArray()-> Array<Element> { | ||
return self.data | ||
} | ||
} | ||
|
||
public struct ArrayStackIterator<Element>: IteratorProtocol { | ||
public typealias Element = Element | ||
|
||
let source: ArrayStack<Element> | ||
var index: Int | ||
var modCount: Int | ||
|
||
init(source: ArrayStack<Element>) { | ||
self.source = source | ||
self.index = source.data.count | ||
self.modCount = source.modCount | ||
} | ||
|
||
public mutating func next() -> Element? { | ||
if self.modCount != self.source.modCount { | ||
fatalError("concurrent modification error") | ||
} | ||
if self.index > 0 { | ||
self.index -= 1 | ||
return self.source.data[self.index] | ||
} | ||
return nil | ||
} | ||
} |
Oops, something went wrong.