[swift-corelibs-dev] [Issue] Collection.distance() works not very well in some situations

2017-12-02 Thread Cao, Jiannan via swift-corelibs-dev
[Issue] Collection.distance() works not very well in some situations
The question is that, whether the original Collection Protocol requires index 
from lower to higher (startIndex < endIndex).

If the original Collection Protocol requires index from lower to higher, than 
this infinity loop should not happen:

> class MyCollectionA : Collection {
> 
> var startIndex: Int {
> return 1
> }
> 
> var endIndex: Int {
> return 100
> }
> 
> func index(after i: Int) -> Int {
> return i + 2
> }
> 
> subscript(position: Int) -> Void {
> return ()
> }
> }
> 
> 
> let a = MyCollectionA()
> a.distance(from: 0, to: 100) // 50
> a.distance(from: 1, to: 100) // !! Infinity Loop
> a.count // !! Infinity Loop

if we assume the index is from lower to higher, the implementation of the 
distance(from:to:) should use "<" with "while" to count distance

> 
> 
> 
> var start = start
> var count: IndexDistance = 0
> while start < end { // instead of using !=
>   count = count + 1
>   formIndex(after: &start)
> }
> return count
>   }




If the original Collection Protocol not requires index from to higher, than the 
code below should works, but unfortunately, the code in Collection.distance 
does not allow.


> class MyCollectionB : Collection {
> 
> var startIndex: Int {
> return 100
> }
> 
> var endIndex: Int {
> return 0
> }
> 
> func index(after i: Int) -> Int {
> return i - 2
> }
> 
> // start, end, index(after i: Int) -> indices: DefaultIndices
> 
> subscript(position: Int) -> Void {
> return ()
> }
> }
> 

> let b = MyCollectionB()
> b.count // !! Fetal Error: Only BidirectionalCollections can have end come 
> before start




___
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


[swift-corelibs-dev] [Proposal] RangeReplaceableCollection should inherits from MutableCollection

2017-12-02 Thread Cao, Jiannan via swift-corelibs-dev
Hi everyone,

I'd like to discuss the relation between RangeReplaceableCollection and 
MutableCollection

MutableCollection is a Collection type that can { set } any element with 
subscript(position: Index). (and also { set } for subscript(range: 
Range))

RangeReplaceableCollection requires a function replaceRange(_:with:)

If a type conforms to RangeReplaceableCollection, it means any of its range can 
be replaced, that includes the situation to replace only one element.
So if some type conforms to RangeReplaceableCollection, it is sufficient to be 
a MutableCollection.
So I think the RangeReplaceableCollection should conforms to MutableCollection 
should inherits from MutableCollection.

Thanks!
Jiannan


___
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev