public static bool ValidateChain(ChainItem chainItem) { if (chainItem.PreviousItem != null) { if (!ValidateChain(chainItem.PreviousItem) || !chainItem.PreviousItem.IsApplicable(chainItem)) { return(false); } } var hash = chainItem.ComputeHash(); return(Enumerable.SequenceEqual(chainItem.Hash, hash)); }
public static bool IsTheSameChainItems(ChainItem chainItem1, ChainItem chainItem2) { if (chainItem1 == null) { throw new ArgumentNullException("chainItem1"); } if (chainItem2 == null) { throw new ArgumentNullException("chainItem2"); } int offset = 0; ChainItem parentItem = null; ChainItem childItem = null; if (chainItem1.Index == chainItem2.Index) { return(Enumerable.SequenceEqual(chainItem1.Hash, chainItem2.Hash)); } else if (chainItem1.Index < chainItem2.Index) { offset = chainItem2.Index - chainItem1.Index; childItem = chainItem1; parentItem = chainItem2; } else if (chainItem1.Index > chainItem2.Index) { offset = chainItem1.Index - chainItem2.Index; parentItem = chainItem1; childItem = chainItem2; } while ((offset > 0) && parentItem != null) { parentItem = parentItem.PreviousItem; offset--; } return(parentItem != null && IsTheSameChainItems(parentItem, childItem)); }
public ChainItem(ChainItem previousItem, string payload) { if (previousItem == null) { throw new ArgumentNullException("previousItem"); } if (!ValidateChain(previousItem)) { throw new ArgumentException("previousItem"); } Index = previousItem.Index + 1; PreviousItem = previousItem; Payload = payload ?? string.Empty; Hash = ComputeHash(); if (!PreviousItem.IsApplicable(this)) { throw new InvalidOperationException("Текущий элемент не может быть добавлен к этой цепочке"); } }
static void Main(string[] args) { var chain = new List <ChainItem>(); var root = new ChainItem("ROOT"); chain.Add(root); var currentItem = root; for (int i = 0; i < 100; i++) { currentItem = new ChainItem(currentItem, "Item " + i.ToString()); chain.Add(currentItem); } var branchItem = new ChainItem(chain[40], "Branch"); var r1 = ChainItem.IsTheSameChainItems(chain[23], chain[45]); var r2 = ChainItem.IsTheSameChainItems(chain[23], chain[23]); var r3 = ChainItem.IsTheSameChainItems(chain[56], chain[13]); var r4 = ChainItem.IsTheSameChainItems(branchItem, chain[13]); var r5 = ChainItem.IsTheSameChainItems(branchItem, chain[56]); }
protected virtual bool IsApplicable(ChainItem nextChainItem) { return(true); }