Skip to content

Commit

Permalink
introduce base function
Browse files Browse the repository at this point in the history
  • Loading branch information
kulics committed Oct 1, 2022
1 parent 0547a45 commit 98bbd63
Show file tree
Hide file tree
Showing 10 changed files with 1,058 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .gitignore
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
24 changes: 24 additions & 0 deletions Package.swift
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")]
)
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# SwiftyCollection
# SwiftyCollection

A description of this package.
199 changes: 199 additions & 0 deletions Sources/SwiftyCollection/ArrayList.swift
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
}
}
103 changes: 103 additions & 0 deletions Sources/SwiftyCollection/ArrayStack.swift
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
}
}
Loading

0 comments on commit 98bbd63

Please sign in to comment.