jweinst1
6/1/2016 - 8:54 PM

simple indexing table for characters in swift

simple indexing table for characters in swift

//sample holding table for indexes of characters
struct IndexTable {
    let string:String
    //table of strings mapping to their indexes in the text
    var indexes:[Character:Set<Int>]
    
    //forms the indexed character table
    init(string:String){
        self.string = string
        self.indexes = [Character:Set<Int>]()
        let chars = Array(string.characters)
        for i in 0..<chars.count {
            //needs to mutate the entry in the dictionary
            if let _ = self.indexes[chars[i]] {
                self.indexes[chars[i]]!.insert(i)
            }
            else {
                self.indexes[chars[i]] = Set<Int>()
                self.indexes[chars[i]]!.insert(i)
            }
        }
    }
}