/// <summary> /// Add Qubit to the set with specified time. Multiple qubits with the same time can be added. /// Complexity is log(N), where N - number of nodes. /// </summary> /// <param name="qubitId">Id of a qubit to add to the set.</param> /// <param name="time">Time of the qubit.</param> public void Add(long qubitId, ComplexTime time) { QubitTimeNode newNode = new QubitTimeNode(qubitId, time); if (QubitsSortedByTime.Add(newNode)) { // New item was added to the set, we are done. return; } if (!QubitsSortedByTime.TryGetValue(newNode, out QubitTimeNode existingNode)) { // We cannot add, but we cannot find a node with the same value. Is this a floating point glitch? Debug.Assert(false, "Cannot add a value to SortedSet<QubitTimeNode> that isn't in the set."); return; } // Add new node to the linked list as a second element. newNode.NextNode = existingNode.NextNode; existingNode.NextNode = newNode; }
/// <summary> /// Remove one qubit with given time from the set. Complexity is log(N), where N - number of nodes. /// </summary> /// <param name="time">Remove qubit with this time.</param> /// <returns>QubitId if qubit is found. Throws exception if it is not found.</returns> public long Remove(ComplexTime time) { Sample.Time = time; if (!QubitsSortedByTime.TryGetValue(Sample, out QubitTimeNode foundValue)) { throw new ApplicationException("Cannot get qubit that should be present in a qubit pool."); } if (foundValue.NextNode == null) { // Remove only node from the tree. long qubitId = foundValue.QubitId; QubitsSortedByTime.Remove(Sample); return(qubitId); } // Get second node in the list. QubitTimeNode nodeToRemove = foundValue.NextNode; // Remove second node from the list. foundValue.NextNode = nodeToRemove.NextNode; return(nodeToRemove.QubitId); }
internal QubitTimeNode(long qubitId, ComplexTime time) { QubitId = qubitId; Time = time; NextNode = null; }