-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1300 from wakmusic/1296-duplicate-song-is-crash
🔀 :: (#1296) 플레이리스트의 중복곡 처리
- Loading branch information
Showing
8 changed files
with
104 additions
and
25 deletions.
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
2 changes: 1 addition & 1 deletion
2
Projects/Features/BaseFeature/Sources/Etc/PlayState/PlaylistItem.swift
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
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
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
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
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
33 changes: 33 additions & 0 deletions
33
Projects/Modules/Utility/Sources/Extensions/Extension+Sequence.swift
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,33 @@ | ||
import Foundation | ||
|
||
public extension Sequence where Element: Hashable { | ||
@inlinable | ||
func uniqued() -> UniquedSequence<Self, Element> { | ||
UniquedSequence(base: self, projection: { $0 }) | ||
} | ||
} | ||
|
||
public extension Sequence { | ||
@inlinable | ||
func uniqued<Subject: Hashable>( | ||
on projection: (Element) throws -> Subject | ||
) rethrows -> [Element] { | ||
var seen: Set<Subject> = [] | ||
var result: [Element] = [] | ||
for element in self { | ||
if try seen.insert(projection(element)).inserted { | ||
result.append(element) | ||
} | ||
} | ||
return result | ||
} | ||
} | ||
|
||
public extension LazySequenceProtocol { | ||
@inlinable | ||
func uniqued<Subject: Hashable>( | ||
on projection: @escaping (Element) -> Subject | ||
) -> UniquedSequence<Self, Subject> { | ||
UniquedSequence(base: self, projection: projection) | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
Projects/Modules/Utility/Sources/Utils/UniquedSequence.swift
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,54 @@ | ||
import Foundation | ||
|
||
public struct UniquedSequence<Base: Sequence, Subject: Hashable> { | ||
@usableFromInline | ||
internal let base: Base | ||
|
||
@usableFromInline | ||
internal let projection: (Base.Element) -> Subject | ||
|
||
@usableFromInline | ||
internal init(base: Base, projection: @escaping (Base.Element) -> Subject) { | ||
self.base = base | ||
self.projection = projection | ||
} | ||
} | ||
|
||
extension UniquedSequence: Sequence { | ||
public struct Iterator: IteratorProtocol { | ||
@usableFromInline | ||
internal var base: Base.Iterator | ||
|
||
@usableFromInline | ||
internal let projection: (Base.Element) -> Subject | ||
|
||
@usableFromInline | ||
internal var seen: Set<Subject> = [] | ||
|
||
@usableFromInline | ||
internal init( | ||
base: Base.Iterator, | ||
projection: @escaping (Base.Element) -> Subject | ||
) { | ||
self.base = base | ||
self.projection = projection | ||
} | ||
|
||
@inlinable | ||
public mutating func next() -> Base.Element? { | ||
while let element = base.next() { | ||
if seen.insert(projection(element)).inserted { | ||
return element | ||
} | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
@inlinable | ||
public func makeIterator() -> Iterator { | ||
Iterator(base: base.makeIterator(), projection: projection) | ||
} | ||
} | ||
|
||
extension UniquedSequence: LazySequenceProtocol where Base: LazySequenceProtocol {} |