/// <summary> /// Replace an existing node with a new one. /// </summary> /// <param name="inner">The inner where the node is replaced.</param> /// <param name="replaceIndex">Index for the replace operation.</param> /// <param name="nodeIndex">Index of the replacing node upon return.</param> public void Replace(IWriteableInner inner, IWriteableInsertionChildIndex replaceIndex, out IWriteableBrowsingChildIndex nodeIndex) { Contract.RequireNotNull(inner, out IWriteableInner Inner); Contract.RequireNotNull(replaceIndex, out IWriteableInsertionChildIndex ReplaceIndex); IWriteableNodeState Owner = Inner.Owner; IWriteableIndex ParentIndex = Owner.ParentIndex; Debug.Assert(Contains(ParentIndex)); Debug.Assert(IndexToState(ParentIndex) == Owner); WriteableInnerReadOnlyDictionary <string> InnerTable = Owner.InnerTable; Debug.Assert(InnerTable.ContainsKey(Inner.PropertyName)); Debug.Assert(InnerTable[Inner.PropertyName] == Inner); IndexToPositionAndNode(ReplaceIndex, out int BlockIndex, out int Index, out bool ClearNode, out Node NewNode); Action <IWriteableOperation> HandlerRedo = (IWriteableOperation operation) => RedoReplace(operation); Action <IWriteableOperation> HandlerUndo = (IWriteableOperation operation) => UndoReplace(operation); IWriteableReplaceOperation Operation = CreateReplaceOperation(Inner.Owner.Node, Inner.PropertyName, BlockIndex, Index, ClearNode, NewNode, HandlerRedo, HandlerUndo, isNested: false); Operation.Redo(); SetLastOperation(Operation); CheckInvariant(); nodeIndex = Operation.NewBrowsingIndex; }
/// <summary> /// Inserts a new node in a list or block list. /// </summary> /// <param name="inner">The inner for the list or block list where the node is inserted.</param> /// <param name="insertedIndex">Index for the insertion operation.</param> /// <param name="nodeIndex">Index of the inserted node upon return.</param> public virtual void Insert(IWriteableCollectionInner inner, IWriteableInsertionCollectionNodeIndex insertedIndex, out IWriteableBrowsingCollectionNodeIndex nodeIndex) { Contract.RequireNotNull(inner, out IWriteableCollectionInner Inner); Contract.RequireNotNull(insertedIndex, out IWriteableInsertionCollectionNodeIndex InsertedIndex); IWriteableNodeState Owner = Inner.Owner; IWriteableIndex ParentIndex = Owner.ParentIndex; Debug.Assert(Contains(ParentIndex)); Debug.Assert(IndexToState(ParentIndex) == Owner); WriteableInnerReadOnlyDictionary <string> InnerTable = Owner.InnerTable; Debug.Assert(InnerTable.ContainsKey(Inner.PropertyName)); Debug.Assert(InnerTable[Inner.PropertyName] == Inner); bool IsHandled = false; nodeIndex = null; if (Inner is IWriteableBlockListInner <IWriteableBrowsingBlockNodeIndex> AsBlockListInner && InsertedIndex is IWriteableInsertionNewBlockNodeIndex AsNewBlockIndex) { InsertNewBlock(AsBlockListInner, AsNewBlockIndex, out nodeIndex); IsHandled = true; } else if (Inner is IWriteableCollectionInner <IWriteableBrowsingCollectionNodeIndex> AsCollectionInner && InsertedIndex is IWriteableInsertionCollectionNodeIndex AsCollectionIndex) { InsertNewNode(AsCollectionInner, AsCollectionIndex, out nodeIndex); IsHandled = true; } Debug.Assert(IsHandled); }
/// <summary> /// Split an identifier with replace and insert indexes. /// </summary> /// <param name="inner">The inner where the node is replaced.</param> /// <param name="replaceIndex">Index for the replace operation.</param> /// <param name="insertIndex">Index for the insert operation.</param> /// <param name="firstIndex">Index of the replacing node upon return.</param> /// <param name="secondIndex">Index of the inserted node upon return.</param> public virtual void SplitIdentifier(IWriteableListInner inner, IWriteableInsertionListNodeIndex replaceIndex, IWriteableInsertionListNodeIndex insertIndex, out IWriteableBrowsingListNodeIndex firstIndex, out IWriteableBrowsingListNodeIndex secondIndex) { Contract.RequireNotNull(inner, out IWriteableListInner Inner); Contract.RequireNotNull(replaceIndex, out IWriteableInsertionListNodeIndex ReplaceIndex); Contract.RequireNotNull(insertIndex, out IWriteableInsertionListNodeIndex InsertIndex); IWriteableNodeState Owner = Inner.Owner; IWriteableIndex ParentIndex = Owner.ParentIndex; Debug.Assert(Contains(ParentIndex)); Debug.Assert(IndexToState(ParentIndex) == Owner); WriteableInnerReadOnlyDictionary <string> InnerTable = Owner.InnerTable; Debug.Assert(InnerTable.ContainsKey(Inner.PropertyName)); Debug.Assert(InnerTable[Inner.PropertyName] == Inner); int Index = ReplaceIndex.Index; Debug.Assert(InsertIndex.Index == Index + 1); Node ReplacingNode = ReplaceIndex.Node; Node InsertedNode = InsertIndex.Node; Action <IWriteableOperation> HandlerRedoReplace = (IWriteableOperation operation) => RedoReplace(operation); Action <IWriteableOperation> HandlerUndoReplace = (IWriteableOperation operation) => UndoReplace(operation); IWriteableReplaceOperation ReplaceOperation = CreateReplaceOperation(Inner.Owner.Node, Inner.PropertyName, -1, Index, clearNode: false, ReplacingNode, HandlerRedoReplace, HandlerUndoReplace, isNested: true); Action <IWriteableOperation> HandlerRedoInsert = (IWriteableOperation operation) => RedoInsertNewNode(operation); Action <IWriteableOperation> HandlerUndoInsert = (IWriteableOperation operation) => UndoInsertNewNode(operation); WriteableInsertNodeOperation InsertOperation = CreateInsertNodeOperation(Inner.Owner.Node, Inner.PropertyName, -1, Index + 1, InsertedNode, HandlerRedoInsert, HandlerUndoInsert, isNested: true); ReplaceOperation.Redo(); InsertOperation.Redo(); Action <IWriteableOperation> HandlerRedoRefresh = (IWriteableOperation operation) => RedoRefresh(operation); Action <IWriteableOperation> HandlerUndoRefresh = (IWriteableOperation operation) => throw new NotImplementedException(); // Undo is not possible. WriteableGenericRefreshOperation RefreshOperation = CreateGenericRefreshOperation(RootState, HandlerRedoRefresh, HandlerUndoRefresh, isNested: false); RefreshOperation.Redo(); WriteableOperationList OperationList = CreateOperationList(); OperationList.Add(ReplaceOperation); OperationList.Add(InsertOperation); WriteableOperationReadOnlyList OperationReadOnlyList = OperationList.ToReadOnly(); WriteableOperationGroup OperationGroup = CreateOperationGroup(OperationReadOnlyList, RefreshOperation); SetLastOperation(OperationGroup); CheckInvariant(); firstIndex = ReplaceOperation.NewBrowsingIndex as IWriteableBrowsingListNodeIndex; Debug.Assert(firstIndex != null); secondIndex = InsertOperation.BrowsingIndex as IWriteableBrowsingListNodeIndex; Debug.Assert(secondIndex != null); }
private protected virtual void ReplaceState(IWriteableReplaceOperation operation, IWriteableInner <IWriteableBrowsingChildIndex> inner) { Contract.RequireNotNull(inner, out IWriteableInner <IWriteableBrowsingChildIndex> Inner); IWriteableNodeState Owner = Inner.Owner; IWriteableIndex ParentIndex = Owner.ParentIndex; Debug.Assert(Contains(ParentIndex)); Debug.Assert(IndexToState(ParentIndex) == Owner); WriteableInnerReadOnlyDictionary <string> InnerTable = Owner.InnerTable; Debug.Assert(InnerTable.ContainsKey(Inner.PropertyName)); Debug.Assert(InnerTable[Inner.PropertyName] == Inner); IWriteableOptionalInner <IWriteableBrowsingOptionalNodeIndex> AsOptionalInner = Inner as IWriteableOptionalInner <IWriteableBrowsingOptionalNodeIndex>; if (AsOptionalInner != null) { IWriteableNodeState OldState = AsOptionalInner.ChildState; PruneStateChildren(OldState); if (AsOptionalInner.IsAssigned) { Stats.AssignedOptionalNodeCount--; } } Inner.Replace(operation); IWriteableBrowsingChildIndex OldBrowsingIndex = operation.OldBrowsingIndex; IWriteableBrowsingChildIndex NewBrowsingIndex = operation.NewBrowsingIndex; IWriteableNodeState ChildState = operation.NewChildState; if (AsOptionalInner != null) { if (AsOptionalInner.IsAssigned) { Stats.AssignedOptionalNodeCount++; } } else { Debug.Assert(Contains(OldBrowsingIndex)); IWriteableNodeState OldState = (IWriteableNodeState)StateTable[OldBrowsingIndex]; PruneStateChildren(OldState); } RemoveState(OldBrowsingIndex); AddState(NewBrowsingIndex, ChildState); BuildStateTable(Inner, null, NewBrowsingIndex, ChildState); }
/// <summary> /// Removes a node from a list or block list. /// </summary> /// <param name="inner">The inner for the list or block list from which the node is removed.</param> /// <param name="nodeIndex">Index for the removed node.</param> public virtual void Remove(IWriteableCollectionInner inner, IWriteableBrowsingCollectionNodeIndex nodeIndex) { Contract.RequireNotNull(inner, out IWriteableCollectionInner Inner); Contract.RequireNotNull(nodeIndex, out IWriteableBrowsingCollectionNodeIndex NodeIndex); IWriteableNodeState Owner = Inner.Owner; IWriteableIndex ParentIndex = Owner.ParentIndex; Debug.Assert(Contains(ParentIndex)); Debug.Assert(IndexToState(ParentIndex) == Owner); WriteableInnerReadOnlyDictionary <string> InnerTable = Owner.InnerTable; Debug.Assert(InnerTable.ContainsKey(Inner.PropertyName)); Debug.Assert(InnerTable[Inner.PropertyName] == Inner); IndexToPositionAndNode(NodeIndex, out int BlockIndex, out int Index, out _, out Node Node); bool IsHandled = false; if (Inner is IWriteableBlockListInner <IWriteableBrowsingBlockNodeIndex> AsBlockListInner && NodeIndex is IWriteableBrowsingExistingBlockNodeIndex ExistingBlockIndex) { if (AsBlockListInner.BlockStateList[ExistingBlockIndex.BlockIndex].StateList.Count == 1) { RemoveBlock(AsBlockListInner, BlockIndex); } else { RemoveNode(AsBlockListInner, BlockIndex, Index); } IsHandled = true; } else if (Inner is IWriteableCollectionInner <IWriteableBrowsingCollectionNodeIndex> AsCollectionInner) { RemoveNode(AsCollectionInner, BlockIndex, Index); IsHandled = true; } Debug.Assert(IsHandled); }
private protected virtual void Expand(IWriteableNodeIndex expandedIndex, WriteableOperationList operationList) { IWriteablePlaceholderNodeState State = StateTable[expandedIndex] as IWriteablePlaceholderNodeState; State = FindBestExpandReduceState(State); Debug.Assert(State != null); WriteableInnerReadOnlyDictionary <string> InnerTable = State.InnerTable; foreach (string Key in InnerTable.Keys) { IWriteableInner Value = (IWriteableInner)InnerTable[Key]; if (Value is IWriteableOptionalInner <IWriteableBrowsingOptionalNodeIndex> AsOptionalInner) { ExpandOptional(AsOptionalInner, operationList); } else if (Value is IWriteableBlockListInner <IWriteableBrowsingBlockNodeIndex> AsBlockListInner) { ExpandBlockList(AsBlockListInner, operationList); } } }