Skip to content

Instantly share code, notes, and snippets.

@chriswebb09
Created May 11, 2017 15:27
Show Gist options
  • Save chriswebb09/3dd5c83307c28e44423c19845564f68e to your computer and use it in GitHub Desktop.
Save chriswebb09/3dd5c83307c28e44423c19845564f68e to your computer and use it in GitHub Desktop.
import UIKit
class Playlist {
private var head: PlaylistItem?
var itemCount: Int = 0
var isEmpty: Bool? {
return head == nil
}
private var last: PlaylistItem? {
if var track = head {
while case let next? = track.next {
track = next
}
return track
} else {
return nil
}
}
func append(newPlaylistItem: PlaylistItem?) {
itemCount += 1
if let lastItem = last {
newPlaylistItem?.previous = lastItem
lastItem.next = newPlaylistItem
} else if head == nil {
head = newPlaylistItem
}
}
func printAllKeys() {
var current: PlaylistItem! = head
var i = 1
while current != nil {
i += 1
current = current.next
}
}
func playlistItem(at index: Int) -> PlaylistItem? {
if index >= 0 {
var trackItem = head
var i = index
while let trackAt = trackItem, trackItem != nil {
if i == 0 {
return trackAt
}
i -= 1
trackItem = trackAt.next
}
}
return nil
}
func reverse() {
var track = head
while let currentTrack = track {
track = currentTrack.next
swap(&currentTrack.next, &currentTrack.previous)
head = currentTrack
}
}
func removeFromPlaylist(for playlistItem: PlaylistItem?) -> iTrack? {
let previous = playlistItem?.previous
let next = playlistItem?.next
if let previous = previous {
previous.next = next
} else {
head = next
}
next?.previous = previous
playlistItem?.previous = nil
playlistItem?.next = nil
guard let trackItem = playlistItem?.track else { return nil }
return trackItem
}
func removeAll() {
var track = head
while let next = track?.next {
track?.previous = nil
track = nil
track = next
}
}
func contains(playlistItem item: PlaylistItem) -> Bool {
guard let currentTrack = head else { return false }
while currentTrack != item && currentTrack.next != nil {
guard let currentTrack = currentTrack.next else { return false }
if currentTrack == item {
return true
}
}
return false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment