Skip to content

Instantly share code, notes, and snippets.

@beno
Created July 24, 2015 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beno/d5e57d872157485dace1 to your computer and use it in GitHub Desktop.
Save beno/d5e57d872157485dace1 to your computer and use it in GitHub Desktop.
protocol Node: Hashable {
var localizedName: String {get set}
}
class File: Node {
let fileID: Int
var localizedName: String
let hashValue : Int
init(fileID: Int, localizedName: String) {
self.localizedName = localizedName
self.fileID = fileID
self.hashValue = fileID
}
}
func ==(lhs: File, rhs: File) -> Bool {
return lhs.fileID == rhs.fileID
}
func ==(lhs: Folder, rhs: Folder) -> Bool {
return lhs.folderID == rhs.folderID
}
class Folder : Node {
var files:Set<File>
var folders:Set<Folder>
var localizedName: String
let folderID: String
let hashValue : Int
var children : [AnyObject] {get {return Array(self.files) as [AnyObject] + Array(self.folders) as [AnyObject]}}
init(folderID: String, localizedName: String) {
self.localizedName = localizedName
self.folderID = folderID
self.files = []
self.folders = []
self.hashValue = 0
}
func addFile(file:File) -> Void {
self.files.insert(file)
}
func addFolder(folder:Folder) -> Void {
self.folders.insert(folder)
}
}
var topLevelFolder = Folder(folderID: "top", localizedName: "Top")
var subFolder = Folder(folderID: "subfolder", localizedName: "Sub Folder")
var subFile = File(fileID: 1, localizedName: "A File")
topLevelFolder.addFolder(subFolder)
topLevelFolder.addFile(subFile)
topLevelFolder.children.count //Should be 2
// Should not actually add subFile again.
topLevelFolder.addFile(subFile)
//print(topLevelNode.children)
topLevelFolder.children.count //Should still be 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment