Skip to content

Commit

Permalink
relax type used by subscript operators.
Browse files Browse the repository at this point in the history
Closes #13
  • Loading branch information
dastrobu committed Sep 28, 2020
1 parent 250df44 commit 06ad7ef
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 95 deletions.
26 changes: 13 additions & 13 deletions Sources/NdArray/NdArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// full slice access
public subscript(r: UnboundedRange) -> NdArraySlice<T> {
public subscript(r: UnboundedRange) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r]
}
Expand All @@ -268,7 +268,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// partial range slice access
public subscript(r: ClosedRange<Int>) -> NdArraySlice<T> {
public subscript(r: ClosedRange<Int>) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r]
}
Expand All @@ -278,7 +278,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// partial range slice access
public subscript(r: PartialRangeThrough<Int>) -> NdArraySlice<T> {
public subscript(r: PartialRangeThrough<Int>) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r]
}
Expand All @@ -288,7 +288,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// partial range slice access
public subscript(r: PartialRangeUpTo<Int>) -> NdArraySlice<T> {
public subscript(r: PartialRangeUpTo<Int>) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r]
}
Expand All @@ -298,7 +298,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// partial range slice access
public subscript(r: PartialRangeFrom<Int>) -> NdArraySlice<T> {
public subscript(r: PartialRangeFrom<Int>) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r]
}
Expand All @@ -308,7 +308,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// range slice access
public subscript(r: Range<Int>) -> NdArraySlice<T> {
public subscript(r: Range<Int>) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r]
}
Expand All @@ -318,7 +318,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// range with stride
public subscript(r: Range<Int>, stride: Int) -> NdArraySlice<T> {
public subscript(r: Range<Int>, stride: Int) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r, stride]
}
Expand All @@ -328,7 +328,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// closed range with stride
public subscript(r: ClosedRange<Int>, stride: Int) -> NdArraySlice<T> {
public subscript(r: ClosedRange<Int>, stride: Int) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r, stride]
}
Expand All @@ -338,7 +338,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// partial range with stride
public subscript(r: PartialRangeFrom<Int>, stride: Int) -> NdArraySlice<T> {
public subscript(r: PartialRangeFrom<Int>, stride: Int) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r, stride]
}
Expand All @@ -348,7 +348,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// partial range with stride
public subscript(r: PartialRangeThrough<Int>, stride: Int) -> NdArraySlice<T> {
public subscript(r: PartialRangeThrough<Int>, stride: Int) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r, stride]
}
Expand All @@ -358,7 +358,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// partial range with stride
public subscript(r: PartialRangeUpTo<Int>, stride: Int) -> NdArraySlice<T> {
public subscript(r: PartialRangeUpTo<Int>, stride: Int) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r, stride]
}
Expand All @@ -368,7 +368,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// full range with stride
public subscript(r: UnboundedRange, stride: Int) -> NdArraySlice<T> {
public subscript(r: UnboundedRange, stride: Int) -> NdArray<T> {
get {
return NdArraySlice(self, sliced: 0)[r, stride]
}
Expand All @@ -378,7 +378,7 @@ open class NdArray<T>: CustomDebugStringConvertible,
}

/// single slice access
public subscript(i: Int) -> NdArraySlice<T> {
public subscript(i: Int) -> NdArray<T> {
get {
assert(!isEmpty)
var startIndex = [Int](repeating: 0, count: ndim)
Expand Down
140 changes: 82 additions & 58 deletions Sources/NdArray/NdArraySlice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,106 +47,130 @@ public class NdArraySlice<T>: NdArray<T> {
self.init(NdArray(copy: a), sliced: 0)
}

private func subscr(_ r: UnboundedRange) -> NdArraySlice {
let slice = NdArraySlice(self, sliced: sliced + 1)
slice.sliceDescription = self.sliceDescription
slice.sliceDescription.append("[...]")
return slice
}

/// full slice access
public override subscript(r: UnboundedRange) -> NdArraySlice<T> {
public override subscript(r: UnboundedRange) -> NdArray<T> {
get {
let slice = NdArraySlice(self, sliced: sliced + 1)
slice.sliceDescription = self.sliceDescription
slice.sliceDescription.append("[...]")
return slice
return subscr(r)
}
set {
newValue.copyTo(self[r])
}
}

private func subscr(_ r: ClosedRange<Int>) -> NdArraySlice {
let slice = self.subscr(r.lowerBound..<r.upperBound + 1)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[\(r.lowerBound)...\(r.upperBound)]")
return slice
}

/// partial range slice access
public override subscript(r: ClosedRange<Int>) -> NdArraySlice<T> {
public override subscript(r: ClosedRange<Int>) -> NdArray<T> {
get {
let slice = self[r.lowerBound..<r.upperBound + 1]
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[\(r.lowerBound)...\(r.upperBound)]")
return slice
return subscr(r)
}
set {
newValue.copyTo(self[r])
}
}

private func subscr(_ r: PartialRangeThrough<Int>) -> NdArraySlice {
let slice = self.subscr(0...r.upperBound)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[...\(r.upperBound)]")
return slice
}

/// partial range slice access
public override subscript(r: PartialRangeThrough<Int>) -> NdArraySlice<T> {
public override subscript(r: PartialRangeThrough<Int>) -> NdArray<T> {
get {
let slice = self[0...r.upperBound]
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[...\(r.upperBound)]")
return slice
return subscr(r)
}
set {
newValue.copyTo(self[r])
}
}

private func subscr(_ r: PartialRangeUpTo<Int>) -> NdArraySlice {
let slice = self.subscr(0..<r.upperBound)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[..<\(r.upperBound)]")
return slice
}

/// partial range slice access
public override subscript(r: PartialRangeUpTo<Int>) -> NdArraySlice<T> {
public override subscript(r: PartialRangeUpTo<Int>) -> NdArray<T> {
get {
let slice = self[0..<r.upperBound]
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[..<\(r.upperBound)]")
return slice
return subscr(r)
}
set {
newValue.copyTo(self[r])
}
}

private func subscr(_ r: PartialRangeFrom<Int>) -> NdArraySlice {
let slice = self.subscr(r.lowerBound..<shape[sliced])
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[\(r.lowerBound)...]")
return slice
}

/// partial range slice access
public override subscript(r: PartialRangeFrom<Int>) -> NdArraySlice<T> {
public override subscript(r: PartialRangeFrom<Int>) -> NdArray<T> {
get {
let slice = self[r.lowerBound..<shape[sliced]]
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[\(r.lowerBound)...]")
return slice
return subscr(r)
}
set {
newValue.copyTo(self[r])
}
}

/// range slice access
public override subscript(r: Range<Int>) -> NdArraySlice<T> {
get {
assert(!isEmpty)
assert(r.lowerBound >= 0, "\(r.lowerBound) >= 0")

// check for empty range
if r.lowerBound >= r.upperBound {
let slice = NdArraySlice(self, startIndex: [Int](repeating: 0, count: ndim), sliced: sliced + 1)
slice.shape[sliced] = 0
slice.count = slice.len
return slice
}
private func subscr(_ r: Range<Int>) -> NdArraySlice {
assert(!isEmpty)
assert(r.lowerBound >= 0, "\(r.lowerBound) >= 0")

let upperBound = Swift.min(r.upperBound, shape[sliced])
var startIndex = [Int](repeating: 0, count: ndim)
startIndex[sliced] = r.lowerBound
let slice = NdArraySlice(self, startIndex: startIndex, sliced: sliced + 1)
slice.shape[sliced] = upperBound - r.lowerBound
// check for empty range
if r.lowerBound >= r.upperBound {
let slice = NdArraySlice(self, startIndex: [Int](repeating: 0, count: ndim), sliced: sliced + 1)
slice.shape[sliced] = 0
slice.count = slice.len
slice.sliceDescription = self.sliceDescription
slice.sliceDescription.append("[\(r.lowerBound)..<\(r.upperBound)]")
return slice
}

let upperBound = Swift.min(r.upperBound, shape[sliced])
var startIndex = [Int](repeating: 0, count: ndim)
startIndex[sliced] = r.lowerBound
let slice = NdArraySlice(self, startIndex: startIndex, sliced: sliced + 1)
slice.shape[sliced] = upperBound - r.lowerBound
slice.count = slice.len
slice.sliceDescription = self.sliceDescription
slice.sliceDescription.append("[\(r.lowerBound)..<\(r.upperBound)]")
return slice
}

/// range slice access
public override subscript(r: Range<Int>) -> NdArray<T> {
get {
return subscr(r)
}
set {
newValue.copyTo(self[r])
}
}

/// closed range with stride
public override subscript(r: ClosedRange<Int>, stride: Int) -> NdArraySlice<T> {
public override subscript(r: ClosedRange<Int>, stride: Int) -> NdArray<T> {
get {
assert(stride > 0, "\(stride) > 0")

let slice = self[r]
let slice = self.subscr(r)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[\(r.lowerBound)...\(r.upperBound), \(stride)]")
slice.strideBy(stride, axis: sliced)
Expand All @@ -158,11 +182,11 @@ public class NdArraySlice<T>: NdArray<T> {
}

/// range with stride
public override subscript(r: Range<Int>, stride: Int) -> NdArraySlice<T> {
public override subscript(r: Range<Int>, stride: Int) -> NdArray<T> {
get {
assert(stride > 0, "\(stride) > 0")

let slice = self[r]
let slice = self.subscr(r)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[\(r.lowerBound)..<\(r.upperBound), \(stride)]")
slice.strideBy(stride, axis: sliced)
Expand All @@ -174,11 +198,11 @@ public class NdArraySlice<T>: NdArray<T> {
}

/// partial range with stride
public override subscript(r: PartialRangeFrom<Int>, stride: Int) -> NdArraySlice<T> {
public override subscript(r: PartialRangeFrom<Int>, stride: Int) -> NdArray<T> {
get {
assert(stride > 0, "\(stride) > 0")

let slice = self[r]
let slice = self.subscr(r)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[\(r.lowerBound)..., \(stride)]")
slice.strideBy(stride, axis: sliced)
Expand All @@ -190,11 +214,11 @@ public class NdArraySlice<T>: NdArray<T> {
}

/// partial range with stride
public override subscript(r: PartialRangeThrough<Int>, stride: Int) -> NdArraySlice<T> {
public override subscript(r: PartialRangeThrough<Int>, stride: Int) -> NdArray<T> {
get {
assert(stride > 0, "\(stride) > 0")

let slice = self[r]
let slice = self.subscr(r)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[...\(r.upperBound), \(stride)]")
slice.strideBy(stride, axis: sliced)
Expand All @@ -206,11 +230,11 @@ public class NdArraySlice<T>: NdArray<T> {
}

/// partial range with stride
public override subscript(r: PartialRangeUpTo<Int>, stride: Int) -> NdArraySlice<T> {
public override subscript(r: PartialRangeUpTo<Int>, stride: Int) -> NdArray<T> {
get {
assert(stride > 0, "\(stride) > 0")

let slice = self[r]
let slice = self.subscr(r)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[..<\(r.upperBound), \(stride)]")

Expand All @@ -223,11 +247,11 @@ public class NdArraySlice<T>: NdArray<T> {
}

/// full range with stride
public override subscript(r: UnboundedRange, stride: Int) -> NdArraySlice<T> {
public override subscript(r: UnboundedRange, stride: Int) -> NdArray<T> {
get {
assert(stride > 0, "\(stride) > 0")

let slice = self[r]
let slice = self.subscr(r)
slice.sliceDescription.removeLast()
slice.sliceDescription.append("[..., \(stride)]")

Expand All @@ -240,7 +264,7 @@ public class NdArraySlice<T>: NdArray<T> {
}

/// single slice access
public override subscript(i: Int) -> NdArraySlice<T> {
public override subscript(i: Int) -> NdArray<T> {
get {
assert(!isEmpty)

Expand Down
4 changes: 2 additions & 2 deletions Sources/NdArray/apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public extension NdArray {
/// - fSlice: Function to apply to each slice, if the array is neither 1d nor contiguous
internal func apply1d(f1d: (Int) throws -> Void,
fContiguous: (Int) throws -> Void,
fSlice: (NdArraySlice<T>) throws -> Void) rethrows {
fSlice: (NdArray<T>) throws -> Void) rethrows {
let n = shape.reduce(1, *)
if n == 0 {
return
Expand Down Expand Up @@ -41,7 +41,7 @@ public extension NdArray {
/// - fSlice: Function to apply to each slice, if the array is neither 1d nor contiguous
internal func apply1d(other: NdArray<T>, f1d: (Int) throws -> Void,
fContiguous: (Int) throws -> Void,
fSlice: (NdArraySlice<T>, NdArraySlice<T>) throws -> Void) rethrows {
fSlice: (NdArray<T>, NdArray<T>) throws -> Void) rethrows {
let n = shape.reduce(1, *)
if n == 0 {
return
Expand Down
Loading

0 comments on commit 06ad7ef

Please sign in to comment.