private static bool TryGet(TNode?node, TKey key, out TValue?value) { while (true) { if (node is null) { value = default; return(false); } switch (node.Key.CompareTo(key)) { case < 0: node = node.Right; continue; case > 0: node = node.Left; continue; case 0: value = node.Value; return(true); } } }
void Push(TNode?item) { if (item != null) { stack.Push(item); } }
/// <summary> /// Initializes a new <see cref="StandardListEnumerator{TElement, TNode}"/>. /// </summary> /// <param name="head">The head node of the list.</param> /// <param name="length">The length of the sequence.</param> public StandardListEnumerator(TNode?head, Int32 length) { Head = head; N = null; Active = true; Count = length; }
public static Boolean ContainsAll <TNode, TElement>(TNode head, ReadOnlySpan <TElement> elements) where TNode : class, IContains <TElement>, INext <TNode?> { Boolean[] found = new Boolean[elements.Length]; TNode? N = head; while (N is not null) { for (Int32 i = 0; i < elements.Length; i++) { if (N.Contains(elements[i])) { found[i] = true; } } N = N.Next; } foreach (Boolean fnd in found) { if (!fnd) { return(false); } } return(true); }
public static String ToString <TNode>(TNode?head, Int32 count, Int32 amount) where TNode : class, INext <TNode?> { StringBuilder builder = new StringBuilder(); if (head is not null) { TNode?N = head; Int32 i = 0; while (N is not null) { if (++i == count) { _ = builder.Append(N); break; } else if (i == amount) { _ = builder.Append(N).Append("..."); break; } else { _ = builder.Append(N).Append(", "); } N = N.Next; } } return($"[{builder}]"); }
public static void Replace <TNode, TElement>(TNode head, TElement search, TElement replace) where TNode : class, INext <TNode?>, IReplace <TElement> { TNode?N = head; while (N is not null) { N.Replace(search, replace); N = N.Next; } }
public static void Clear<TNode>(ref TNode? head, ref TNode? tail) where TNode : class, INext<TNode?>, IUnlink { TNode? P = null; TNode? N = head; while (N is not null) { P = N; N = N.Next; P.Unlink(); } head = null; tail = null; }
private static void TraverseInorder(TNode?node, Queue <KeyValuePair <TKey, TValue> > queue) { while (true) { if (node is null) { return; } TraverseInorder(node.Left, queue); queue.Enqueue(KeyValuePair.Create(node.Key, node.Value)); node = node.Right; } }
/// <summary> /// 指定されたインデックス番号のノードを取得します。 /// </summary> /// <typeparam name="TNode">ノードの種類です。</typeparam> /// <param name="index">取得するノードのインデックス番号です。</param> /// <param name="result">取得したノードを表すオブジェクトです。</param> /// <returns>取得に成功した場合は<see langword="true"/>、失敗した場合は<see langword="false"/>を返します。</returns> public bool TryGetNode <TNode>(int index, [NotNullWhen(true)] out TNode?result) where TNode : YNode { if (this.TryGetNode(index, out var result2)) { result = result2 as TNode; return(result is not null); } else { result = null; return(false); } }
/// <summary> /// Adds node with specified <paramref name="value"/> to the end of the list /// </summary> /// <remarks> /// Operation cost is O(1). /// </remarks> public TNode Add(string value) { if (value is null) { throw new ArgumentNullException(nameof(value)); } var node = this.CreateNode(value); if (this._head is null && this._tail is null) { this._head = node; this._tail = node; }
public static Boolean Contains <TNode, TElement>(TNode head, TElement element) where TNode : class, IContains <TElement>, INext <TNode?> { TNode?N = head; while (N is not null) { if (N.Contains(element)) { return(true); } N = N.Next; } return(false); }
/// <summary> /// Adds node with specified <paramref name="value"/> to the end of the list. /// Acts as the factory for <typeparamref name="TNode"/>. /// </summary> /// <remarks> /// Operation cost is O(1). /// </remarks> public TNode Add(string value) { if (value is null) { throw new ArgumentNullException(nameof(value)); } var node = new TNode(); // state init, hacky :-( node.Initialize(this, value); if (this._head is null && this._tail is null) { this._head = node; this._tail = node; }
public static void Dequeue <TElement, TNode>(ref TNode?head, ref TNode?tail, out TElement element) where TNode : class, IElement <TElement>, INext <TNode?>, IUnlink { if (head is not null) { TNode oldHead = head; head = head.Next; oldHead.Unlink(); element = oldHead.Element; } else { throw new InvalidOperationException("Can't dequeue an element from an empty collection."); } if (head is null) { tail = null; } }
private static bool TryGetCeiling(TNode?node, TKey?key, out TKey?ceiling) { while (true) { if (node is null) { ceiling = default; return(false); } var nodeKey = node.Key; var compareResult = nodeKey.CompareTo(key); switch (compareResult) { case < 0: node = node.Right; continue; case > 0: var leftChild = node.Left; if (leftChild is null) { ceiling = nodeKey; return(true); } if (TryGetCeiling(leftChild, key, out ceiling)) { return(true); } ceiling = nodeKey; return(true); case 0: ceiling = key; return(true); } } }
private int Rank(TNode?node, TKey?key) { while (true) { if (node is null) { return(0); } switch (node.Key.CompareTo(key)) { case > 0: node = node.Left; continue; case < 0: return(1 + Size(node.Left) + Rank(node.Right, key)); case 0: return(Size(node.Left)); } } }
private static bool TryGetFloor(TNode?node, TKey key, out TKey?floor) { while (true) { if (node is null) { floor = default; return(false); } var nodeKey = node.Key; var compareResult = nodeKey.CompareTo(key); switch (compareResult) { case > 0: node = node.Left; continue; case < 0: var rightChild = node.Right; if (rightChild is null) { floor = nodeKey; return(true); } if (TryGetFloor(rightChild, key, out floor)) { return(true); } floor = nodeKey; return(true); case 0: floor = key; return(true); } } }
private static void TraverseInorderRange(TNode?node, Queue <KeyValuePair <TKey, TValue> > queue, TKey lo, TKey hi) { if (node is null) { return; } var key = node.Key; var isLowBoundLessThanKey = lo.CompareTo(key) <= 0; if (isLowBoundLessThanKey) { TraverseInorderRange(node.Left, queue, lo, hi); } var isKeyLessThanHighBound = key.CompareTo(hi) <= 0; if (isLowBoundLessThanKey && isKeyLessThanHighBound) { queue.Enqueue(KeyValuePair.Create(node.Key, node.Value)); } if (isKeyLessThanHighBound) { TraverseInorderRange(node.Right, queue, lo, hi); } }
public static bool IsParentKind <TNode>([NotNullWhen(returnValue: true)] this SyntaxNode?node, SyntaxKind kind, [NotNullWhen(returnValue: true)] out TNode?result) where TNode : SyntaxNode { if (node.IsParentKind(kind)) { result = (TNode)node.Parent !; return(true); } result = null; return(false); }
public static unsafe void Insert <TElement, TNode>(ref TNode?head, ref TNode?tail, Int32 count, Index index, TElement element, delegate * < TElement, TNode > newNode) where TNode : class, INext <TNode?>
public static bool IsKind <TNode>([NotNullWhen(returnValue: true)] this SyntaxNode?node, SyntaxKind kind, [NotNullWhen(returnValue: true)] out TNode?result) where TNode : SyntaxNode { if (node.IsKind(kind)) { #if !CODE_STYLE result = (TNode)node; #else // The CodeStyle layer is referencing an older, unannotated version of Roslyn which doesn't know that IsKind guarantees the non-nullness // of node. So we have to silence it here. result = (TNode)node !; #endif return(true); } result = null; return(false); }
/// <summary> /// Invalidates node, detaches it from container. /// No further operations are possible on this node after invalidation. /// </summary> internal void Invalidate() { this._container = null; this._next = null; this._prev = null; }
/// <inheritdoc cref="CSharpSyntaxRewriter.Visit(SyntaxNode?)"/> public TNode?Visit <TNode>(TNode?node) where TNode : SyntaxNode { return((TNode?)base.Visit(node)); }
/// <summary> /// Adds <paramref name="node"/> to the end of this builder. No change happens if <see langword="null"/> is /// passed in. /// </summary> public SyntaxListBuilder <TNode> Add(TNode?node) { _builder.Add(node); return(this); }
private static void DoConvertToHierarchy <TEntity, TKey, TNode>(Func <TEntity, TNode?> convertor, Func <TEntity, TKey?> getKey, Func <TEntity, TKey?> getParentKey, Action <TNode, TNode> firstIsParentOfSecond, TEntity v, Dictionary <TKey, TreeNode <TEntity, TKey, TNode> > parents, Dictionary <TKey, List <TreeNode <TEntity, TKey, TNode> > > orphanage, List <TreeNode <TEntity, TKey, TNode> > explicitNoParentItems) where TKey : struct where TNode : class where TEntity : class { if (v == null) { return; } TNode?castedNode = convertor(v); if (castedNode == null) { return; } var node = new TreeNode <TEntity, TKey, TNode>(); TKey?nodeKey = getKey(v); if (!nodeKey.HasValue) { return; } node = new TreeNode <TEntity, TKey, TNode>(castedNode, getParentKey(v), nodeKey.Value); //finding parent of the node if (node.ParentKey.HasValue) { TreeNode <TEntity, TKey, TNode> parentNode; if (parents.TryGetValue(node.ParentKey.Value, out parentNode)) { firstIsParentOfSecond(parentNode.Node, node.Node); } else { //poor node :( no parent found! it should go to the orphanage if (!orphanage.TryGetValue(node.ParentKey.Value, out List <TreeNode <TEntity, TKey, TNode> >?orphanageRoom2)) { orphanageRoom2 = new List <TreeNode <TEntity, TKey, TNode> >(); orphanage.Add(node.ParentKey.Value, orphanageRoom2); } orphanageRoom2.Add(node); } } else { explicitNoParentItems.Add(node); } //checking if there are any orphans whose parent is this new node object. if (orphanage.TryGetValue(node.NodeKey, out List <TreeNode <TEntity, TKey, TNode> >?orphanageRoom)) { foreach (TreeNode <TEntity, TKey, TNode> v2 in orphanageRoom) { firstIsParentOfSecond(node.Node, v2.Node); } orphanage.Remove(node.NodeKey); //there would be no more orphans for this parent, so removing the orphanage room } parents.Add(node.NodeKey, node); }
public static unsafe void Postpend <TElement, TNode>(ref TNode?head, ref TNode?tail, TElement element, delegate * < TElement, TNode > newNode) where TNode : class, INext <TNode?>
protected static int Size(TNode?node) => node?.SubtreeSize ?? 0;
public void Add(TKey key, TValue value) { Root = Add(Root, key, value); }
public SyntaxList(TNode?node) : this((SyntaxNode?)node) { }
protected abstract TNode?Add(TNode?node, TKey key, TValue value);