import copy class SnapshotArray: # Constructor def __init__(self, length): self.snapid = 0 self.node_value = dict() self.node_value[0] = dict() self.ncount = length # Function set_value sets the value at a given index idx to val. def set_value(self, idx, val): if idx < self.ncount: self.node_value[self.snapid][idx] = val # This function takes no parameters and returns the snapid. # snapid is the number of times that the snapshot() function was called minus 1. def snapshot(self): self.node_value[self.snapid+1] = copy.deepcopy(self.node_value[self.snapid]) self.snapid += 1 return self.snapid - 1 def __str__(self): return str(self.node_value) def main(): num_nodes = [3, 5, 5, 3, 2] node_values = [ [[0, 5], [0, 1], [2, 3], [1, 10]], [[4, 1], [2, 21]], [[4, 12], [2, 61]], [[0, 15], [0, 5], [2, 13], [1, 100]], [[0, 32], [0, 6], [1, 2]] ] for i in range(len(num_nodes)): print(i + 1, ".\tInitializing a data structure with ", num_nodes[i], " nodes", sep = "") snapshot_arr = SnapshotArray(num_nodes[i]) for j in node_values[i]: print("\t\tSetting the value of node ", j[0], " to ", j[1], sep = "") snapshot_arr.set_value(j[0], j[1]) print("\t\tDictionary: ", snapshot_arr) print("\t\tSnap id: ", snapshot_arr.snapshot(), "\n", sep = "") print("\n", "-"*100, sep = "") if __name__ == '__main__': main()