Row adopts RandomAccessCollection

This commit is contained in:
Gwendal Roué committed 2018-01-12 08:18:50 +01:00
1 parent cf4654108e
commit 0d236ae32c
2 files changed
+32 -12

No files matched your search

+7
View File
@@ -1,6 +1,13 @@
Release Notes
=============
## Next Version
**New**
- `Row` adopts RandomAccessCollection
## 2.5.0
Released January 11, 2018 • [diff](https://github.com/groue/GRDB.swift/compare/v2.4.2...v2.5.0)
+25 -12
View File
@@ -762,19 +762,26 @@ extension Row : Collection {
impl.databaseValue(atUncheckedIndex: index))
}
/// Returns the position immediately after `i`.
/// Returns the index immediately after `i`.
///
/// - Precondition: `(startIndex..<endIndex).contains(i)`
public func index(after i: RowIndex) -> RowIndex {
return RowIndex(i.index + 1)
}
/// Replaces `i` with its successor.
public func formIndex(after i: inout RowIndex) {
i = RowIndex(i.index + 1)
}
extension Row: BidirectionalCollection {
/// Returns the index immediately before `i`.
///
/// - Precondition: i must be greater than startIndex
public func index(before i: RowIndex) -> RowIndex {
return RowIndex(i.index - 1)
}
}
extension Row: RandomAccessCollection {
}
/// Row adopts Equatable.
extension Row : Equatable {
@@ -789,10 +796,7 @@ extension Row : Equatable {
return false
}
var liter = lhs.makeIterator()
var riter = rhs.makeIterator()
while let (lcol, lval) = liter.next(), let (rcol, rval) = riter.next() {
for ((lcol, lval), (rcol, rval)) in zip(lhs, rhs) {
guard lcol == rcol else {
return false
}
@@ -842,22 +846,31 @@ extension Row: CustomStringConvertible {
// MARK: - RowIndex
/// Indexes to (columnName, dbValue) pairs in a database row.
/// Indexes to (ColumnName, DatabaseValue) pairs in a database row.
public struct RowIndex : Comparable {
let index: Int
init(_ index: Int) { self.index = index }
/// Equality operator
public static func == (lhs: RowIndex, rhs: RowIndex) -> Bool {
return lhs.index == rhs.index
}
// Comparison operator
public static func < (lhs: RowIndex, rhs: RowIndex) -> Bool {
return lhs.index < rhs.index
}
}
// Support for Row: RandomAccessCollection
extension RowIndex: Strideable {
public func distance(to other: RowIndex) -> Int {
return other.index - index
}
public func advanced(by n: Int) -> RowIndex {
return RowIndex(index + n)
}
}
// MARK: - RowImpl