public bool TryMoveToIndex(int index) { var result = pruningCursor.TryMoveToIndex(index); currentIndex = result ? index : -1; return(result); }
public static void MoveToIndex(this IMerkleTreePruningCursor cursor, int index) { if (!cursor.TryMoveToIndex(index)) { throw new InvalidOperationException(); } }
public static void MoveToIndex <T>(this IMerkleTreePruningCursor <T> cursor, int index) where T : IMerkleTreeNode <T> { if (!cursor.TryMoveToIndex(index)) { throw new InvalidOperationException(); } }
public static void PruneNode <T>(IMerkleTreePruningCursor <T> cursor, int index) where T : IMerkleTreeNode <T> { if (!cursor.TryMoveToIndex(index)) { return; } var node = cursor.ReadNode(); if (node.Depth != 0) { return; } if (!node.Pruned) { node = node.AsPruned(); cursor.WriteNode(node); } bool didWork; do { didWork = false; if (node.IsLeft()) { if (cursor.TryMoveRight()) { var rightNode = cursor.ReadNode(); if (node.Pruned && rightNode.Pruned && node.Depth == rightNode.Depth) { var newNode = node.PairWith(rightNode); cursor.DeleteNode(); //TODO cursor.MoveLeft(); cursor.WriteNode(newNode); node = newNode; didWork = true; } } else { if (node.Index != 0 && node.Pruned) { var newNode = node.PairWithSelf(); //cursor.MoveLeft(); cursor.WriteNode(newNode); node = newNode; didWork = true; } } } else { if (cursor.TryMoveLeft()) { var leftNode = cursor.ReadNode(); if (node.Pruned && leftNode.Pruned && node.Depth == leftNode.Depth) { var newNode = leftNode.PairWith(node); cursor.WriteNode(newNode); cursor.MoveRight(); cursor.DeleteNode(); //TODO cursor.MoveLeft(); node = newNode; didWork = true; } } } }while (didWork); }