I have a dictioary that uses a hashed compound key
struct Key: Hashable {
let row : UInt16
let column : UInt16
}
I need to insert “rows” based on that key structure. So I need to insert a new row between the existing row #9 and row #10, this means I need to increment the row component of all existing dictionary entries where the current row is >=10 … so (10 becomes 11 , 11 becomes 12).
I’m NOT sure they can be incremented in place (ie. 10 becomes 11, then that same one becomes 12 etc)
So my current process is to build a clone of the dictionary, altering the keys for it, the moving it back to the original structure
public final func insertRow(_ at: UInt16,_ items:[String]=[]) {
var tempSpreadSheet = [Key : cellRecord]()
//
// Move records into a temp dictionary, altering keys as required
//
// Step 01 : build tempSpreadSheet [key = [UInt16:UInt16]
//
for key in spreadSheet.keys {
if key.row >= at {
let newKey = Key(row:key.row+1,column: key.column)
tempSpreadSheet[newKey]=spreadSheet[key]
} else {
tempSpreadSheet[key]=spreadSheet[key]
}
}
//
// Step 02: put records back into spreadsheet
//
for key in tempSpreadSheet.keys {
spreadSheet[key]=tempSpreadSheet[key]
}
//
// Step 03 : add the new row
//
addRowAT(items,atRow:Int(at))
}
//
Is that the best way to accomplish this. If if were an array, it could be sorted and iterated in reverse, but this is a dictionary, and the physical order is unknown