Table of Contents

Class Chain<T>

Namespace
Gve.Text
Assembly
Gve.Text.dll

A chain data structure. This data structure consists of a linear sequence of nodes, each connected to the previous and the next node by a link. Additionally, each link has a version tag, which adds the dimension of time to this structure. Internally, this structure collects nodes and links, and once they are added, they cannot be removed (unless we are using a single version). Any modification is done by adding new nodes and links, which connect or disconnect or reorder the existing nodes. When the chain is empty, there are no nodes or links. When there is at least one node, there always are links for the head and the tail of the chain. So, the structure, when not empty, always alternates links and nodes, starting and ending with links.

public class Chain<T>

Type Parameters

T
Inheritance
Chain<T>
Inherited Members

Remarks

This structure allows to represent different states of a text in time, after operations change it. Each operation introduces a new version (unless you are overwriting the previous version), so that building the text for a version is done by just following the path defined by links among nodes. At any time, you can build the text for any version, while still using a single object to represent all of the versions. Operations usually add new links, and optionally add new nodes; anyway, both nodes and links are immutable items of an unordered set.

Fields

Gets or sets the links connecting nodes in this chain.

protected List<ChainLink<T>> _links

Field Value

List<ChainLink<T>>

_nodes

Gets or sets the nodes in this chain.

protected List<ChainNode<T>> _nodes

Field Value

List<ChainNode<T>>

Properties

Gets the links in this chain.

public IReadOnlyList<ChainLink<T>> Links { get; }

Property Value

IReadOnlyList<ChainLink<T>>

Nodes

Gets the nodes in this chain.

public IReadOnlyList<ChainNode<T>> Nodes { get; }

Property Value

IReadOnlyList<ChainNode<T>>

Methods

Adds the new version newTag to the chain copying it from the version with the specified tag.

public void AddVersion(string tag, string newTag, params ChainLink<T>?[] excludedLinks)

Parameters

tag string

The source tag.

newTag string

The new tag.

excludedLinks ChainLink<T>[]

The optional links to be excluded from the copy.

Exceptions

ArgumentNullException

tag or newTag

Append(string, string, ChainNode<T>)

Appends newNode at the end of this chain.

public void Append(string tag, string newTag, ChainNode<T> newNode)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

newNode ChainNode<T>

The new node.

Exceptions

ArgumentNullException

tag, newTag, newNode

AppendRange(string, string, IList<ChainNode<T>>)

Appends nodes at the end of this chain.

public void AppendRange(string tag, string newTag, IList<ChainNode<T>> nodes)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

nodes IList<ChainNode<T>>

The new nodes.

BuildLinksFrom(string, IList<ChainNode<T>>)

Builds the links which connect all the received nodes in their order.

public static IList<ChainLink<T>> BuildLinksFrom(string newTag, IList<ChainNode<T>> nodes)

Parameters

newTag string

The new version tag to assign to the created links.

nodes IList<ChainNode<T>>

The nodes.

Returns

IList<ChainLink<T>>

The links connecting all the nodes.

Exceptions

ArgumentNullException

newTag or nodes

Clear()

Clears this chain removing all the versions.

public void Clear()

Delete(string, string, ChainNode<T>)

Deletes the specified node unless it's not connected.

public bool Delete(string tag, string newTag, ChainNode<T> node)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

node ChainNode<T>

The node to delete.

Returns

bool

True if deleted.

Remarks

Deleting a node implies adding new links which short-circuit the connection between its left and right nodes. If current and new tags are equal, the old links will be removed.

Exceptions

ArgumentNullException

tag, newTag, node

DeleteRange(string, string, ChainNode<T>, ChainNode<T>)

Deletes the specified range of nodes unless the reference nodes are not connected.

public bool DeleteRange(string tag, string newTag, ChainNode<T> firstNode, ChainNode<T> lastNode)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

firstNode ChainNode<T>

The first node to delete.

lastNode ChainNode<T>

The last node to delete.

Returns

bool

True if deleted.

Exceptions

ArgumentNullException

tag, newTag, firstNode, lastNode

Dump(Func<ChainNode<T>, string>?)

Dumps this chain.

public string Dump(Func<ChainNode<T>, string>? dumpNode = null)

Parameters

dumpNode Func<ChainNode<T>, string>

The optional node dumper to use. If not specified, the node will be dumped via its data.

Returns

string

String.

EnumerateTagNodes(string)

Enumerates the nodes for the specified version tag, in their order.

public IEnumerable<ChainNode<T>> EnumerateTagNodes(string tag)

Parameters

tag string

The version tag.

Returns

IEnumerable<ChainNode<T>>

The nodes.

Exceptions

ArgumentNullException

tag

FindNode(int)

Finds the node with the specified ID.

public ChainNode<T>? FindNode(int id)

Parameters

id int

The identifier.

Returns

ChainNode<T>

Node or null if not found.

FindNodeByIndex(string, int)

Finds the node at the specified index in the string resulting from enumerating all the nodes for version tag.

public ChainNode<T>? FindNodeByIndex(string tag, int index)

Parameters

tag string

The version tag.

index int

The node index.

Returns

ChainNode<T>

Node or null if not found.

Exceptions

ArgumentNullException

tag

ArgumentOutOfRangeException

index

GetDistinctTags()

Gets the a list of unique tags from this chain.

public IList<string> GetDistinctTags()

Returns

IList<string>

List, sorted alphabetically.

GetFirstNode(string)

Gets the first node for the specified version tag.

public ChainNode<T>? GetFirstNode(string tag)

Parameters

tag string

The version tag.

Returns

ChainNode<T>

Node or null.

Exceptions

ArgumentNullException

tag

GetLastNode(string)

Gets the last node for the specified version tag.

public ChainNode<T>? GetLastNode(string tag)

Parameters

tag string

The tag.

Returns

ChainNode<T>

Node or null.

Exceptions

ArgumentNullException

tag

GetLinkCount(string?)

Gets the link count.

public int GetLinkCount(string? tag = null)

Parameters

tag string

The optional tag filter.

Returns

int

Count.

GetMaxOrdinal()

Gets the maximum ordinal among the nodes of all the versions in this chain.

public int GetMaxOrdinal()

Returns

int

Max ordinal number.

GetNextId()

Gets the next available node identifier in this chain.

public int GetNextId()

Returns

int

ID.

Gets the link following the specified link.

public ChainLink<T>? GetNextLink(string tag, ChainLink<T>? link)

Parameters

tag string

The version tag.

link ChainLink<T>

The reference link.

Returns

ChainLink<T>

Link or null if not found or input node was null.

Exceptions

ArgumentNullException

tag

GetNextNode(string, ChainNode<T>)

Gets the next node from node.

public ChainNode<T>? GetNextNode(string tag, ChainNode<T> node)

Parameters

tag string

The version tag.

node ChainNode<T>

The node.

Returns

ChainNode<T>

Node or null.

GetNodeCount(string?)

Gets the node count.

public int GetNodeCount(string? tag = null)

Parameters

tag string

The optional tag filter.

Returns

int

Count.

Gets the link following the specified node.

public ChainLink<T>? GetNodeNextLink(string tag, ChainNode<T>? node)

Parameters

tag string

The version tag.

node ChainNode<T>

The reference node or null.

Returns

ChainLink<T>

Link or null if not found or input node was null.

Exceptions

ArgumentNullException

tag

Gets the link preceding the specified node.

public ChainLink<T>? GetNodePreviousLink(string tag, ChainNode<T>? node)

Parameters

tag string

The version tag.

node ChainNode<T>

The reference node or null.

Returns

ChainLink<T>

Link or null if not found or input node was null.

Exceptions

ArgumentNullException

tag or node

Gets the link preceding the specified link.

public ChainLink<T>? GetPreviousLink(string tag, ChainLink<T>? link)

Parameters

tag string

The version tag.

link ChainLink<T>

The reference link or null.

Returns

ChainLink<T>

Link or null if not found or input node was null.

Exceptions

ArgumentNullException

tag

GetPreviousNode(string, ChainNode<T>)

Gets the previous node from node.

public ChainNode<T>? GetPreviousNode(string tag, ChainNode<T> node)

Parameters

tag string

The version tag.

node ChainNode<T>

The node.

Returns

ChainNode<T>

Node or null.

GetTagNodes(string, bool)

Gets the nodes for the specified version tag, in their order.

public IList<ChainNode<T>> GetTagNodes(string tag, bool source = false)

Parameters

tag string

The version tag.

source bool

True to collect only those nodes having their source tag equal to tag.

Returns

IList<ChainNode<T>>

The nodes.

Exceptions

ArgumentNullException

tag

HasVersion(string)

Determines whether this chain contains the specified version.

public bool HasVersion(string tag)

Parameters

tag string

The version tag.

Returns

bool

true if version is present; otherwise, false.

InsertAfter(string, string, ChainNode<T>, ChainNode<T>)

Inserts newNode after refNode, unless the reference node is not connected.

public bool InsertAfter(string tag, string newTag, ChainNode<T> refNode, ChainNode<T> newNode)

Parameters

tag string

The tag.

newTag string

The new tag.

refNode ChainNode<T>

The reference node.

newNode ChainNode<T>

The new node.

Returns

bool

True if inserted.

Exceptions

ArgumentNullException

tag, newTag, refNode, newNodes

InsertBefore(string, string, ChainNode<T>, ChainNode<T>)

Inserts newNode before refNode, unless the reference node is not connected.

public bool InsertBefore(string tag, string newTag, ChainNode<T> refNode, ChainNode<T> newNode)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

refNode ChainNode<T>

The reference node.

newNode ChainNode<T>

The new node.

Returns

bool

True if inserted.

Exceptions

ArgumentNullException

tag, newTag, refNode, newNodes

InsertRangeAfter(string, string, ChainNode<T>, IList<ChainNode<T>>)

Inserts the specified range of nodes after refNode.

public bool InsertRangeAfter(string tag, string newTag, ChainNode<T> refNode, IList<ChainNode<T>> newNodes)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

refNode ChainNode<T>

The reference node.

newNodes IList<ChainNode<T>>

The new nodes.

Returns

bool

True if inserted.

Exceptions

ArgumentNullException

tag, newTag, refNode, newNodes

InsertRangeBefore(string, string, ChainNode<T>, IList<ChainNode<T>>)

Inserts the specified range of nodes before refNode.

public bool InsertRangeBefore(string tag, string newTag, ChainNode<T> refNode, IList<ChainNode<T>> newNodes)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

refNode ChainNode<T>

The reference node.

newNodes IList<ChainNode<T>>

The new nodes.

Returns

bool

True if inserted.

Exceptions

ArgumentNullException

tag, newTag, refNode, newNodes

IsFirstNode(string, ChainNode<T>)

Determines whether node is the first node in the version defined by tag.

public bool IsFirstNode(string tag, ChainNode<T> node)

Parameters

tag string

The tag.

node ChainNode<T>

The node.

Returns

bool

true if last node; otherwise, false.

Exceptions

ArgumentNullException

tag or node

IsLastNode(string, ChainNode<T>)

Determines whether node is the last node in the version defined by tag.

public bool IsLastNode(string tag, ChainNode<T> node)

Parameters

tag string

The tag.

node ChainNode<T>

The node.

Returns

bool

true if last node; otherwise, false.

Exceptions

ArgumentNullException

tag or node

IsNodeAfter(string, ChainNode<T>, ChainNode<T>, int, int)

Determines whether node is at any distance after refNode in the version defined by tag.

public bool IsNodeAfter(string tag, ChainNode<T> node, ChainNode<T> refNode, int min = -1, int max = -1)

Parameters

tag string

The version tag.

node ChainNode<T>

The node.

refNode ChainNode<T>

The other node.

min int

The minimum distance required between node and reference node, or -1 to just check if reference node comes after node.

max int

The maximum distance allowed between node and reference node, or -1 to just check if reference node comes after node.

Returns

bool

true if after; otherwise, false.

IsNodeBefore(string, ChainNode<T>, ChainNode<T>)

Determines whether node is at any distance before refNode in the version defined by tag.

public bool IsNodeBefore(string tag, ChainNode<T> node, ChainNode<T> refNode)

Parameters

tag string

The versopm tag.

node ChainNode<T>

The node.

refNode ChainNode<T>

The reference node.

Returns

bool

true if before; otherwise, false.

IsNodeInVersion(ChainNode<T>, string)

Determines whether node is included in the version defined by tag.

public bool IsNodeInVersion(ChainNode<T> node, string tag)

Parameters

node ChainNode<T>

The node.

tag string

The tag.

Returns

bool

true if in version; otherwise, false.

Exceptions

ArgumentNullException

node or tag

MoveAfter(string, string, ChainNode<T>, ChainNode<T>)

Moves node to the position after refNode.

public bool MoveAfter(string tag, string newTag, ChainNode<T> node, ChainNode<T> refNode)

Parameters

tag string

The tag.

newTag string

The new tag.

node ChainNode<T>

The chain node.

refNode ChainNode<T>

The reference node.

Returns

bool

True if moved.

MoveBefore(string, string, ChainNode<T>, ChainNode<T>)

Moves node to the position before refNode.

public bool MoveBefore(string tag, string newTag, ChainNode<T> node, ChainNode<T> refNode)

Parameters

tag string

The tag.

newTag string

The new tag.

node ChainNode<T>

The chain node.

refNode ChainNode<T>

The reference node.

Returns

bool

True if moved.

MoveRangeAfter(string, string, ChainNode<T>, ChainNode<T>, ChainNode<T>)

Moves the range defined by firstNode and lastNode after refNode.

public bool MoveRangeAfter(string tag, string newTag, ChainNode<T> firstNode, ChainNode<T> lastNode, ChainNode<T> refNode)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

firstNode ChainNode<T>

The first node.

lastNode ChainNode<T>

The last node.

refNode ChainNode<T>

The reference node.

Returns

bool

True if moved.

MoveRangeBefore(string, string, ChainNode<T>, ChainNode<T>, ChainNode<T>)

Moves the range defined by firstNode and lastNode before refNode.

public bool MoveRangeBefore(string tag, string newTag, ChainNode<T> firstNode, ChainNode<T> lastNode, ChainNode<T> refNode)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

firstNode ChainNode<T>

The first node.

lastNode ChainNode<T>

The last node.

refNode ChainNode<T>

The reference node.

Returns

bool

True if moved.

RemoveVersion(string)

Removes the version with the specified tag.

public void RemoveVersion(string tag)

Parameters

tag string

The version tag.

Replace(string, string, ChainNode<T>, ChainNode<T>)

Replaces the specified node with a new one.

public bool Replace(string tag, string newTag, ChainNode<T> oldNode, ChainNode<T> newNode)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

oldNode ChainNode<T>

The old node.

newNode ChainNode<T>

The new node.

Returns

bool

True if replaced.

ReplaceRange(string, string, ChainNode<T>, ChainNode<T>, IList<ChainNode<T>>)

Replaces the range defined by firstNode and lastNode with newNodes.

public bool ReplaceRange(string tag, string newTag, ChainNode<T> firstNode, ChainNode<T> lastNode, IList<ChainNode<T>> newNodes)

Parameters

tag string

The current version tag.

newTag string

The new version tag.

firstNode ChainNode<T>

The first node.

lastNode ChainNode<T>

The last node.

newNodes IList<ChainNode<T>>

The new nodes.

Returns

bool

True if replaced.

Reset(IList<ChainNode<T>>)

Reset this chain setting the specified nodes as its base.

public void Reset(IList<ChainNode<T>> nodes)

Parameters

nodes IList<ChainNode<T>>

The nodes.

Remarks

When setting the base text nodes, use this method rather than AppendRange(string, string, IList<ChainNode<T>>), because this updates the inner ID seed.

ResetId(int)

Resets the identifier to the specified seed value. This means that the next node added to this chain will have an identifier equal to the seed plus one.

public void ResetId(int seed)

Parameters

seed int

The seed.

SupplyNodeMetadata(ChainNode<T>, string)

Supplies the identifier and the source tag to the specified node where not present.

protected void SupplyNodeMetadata(ChainNode<T> node, string sourceTag)

Parameters

node ChainNode<T>

The node.

sourceTag string

The source tag.

Exceptions

ArgumentNullException

node or sourceTag

SupplyNodeMetadata(IList<ChainNode<T>>, string)

Supply the identifiers and the source tag to the specified nodes where not present.

protected void SupplyNodeMetadata(IList<ChainNode<T>> nodes, string sourceTag)

Parameters

nodes IList<ChainNode<T>>

The nodes.

sourceTag string

The source tag.

Exceptions

ArgumentNullException

node or sourceTag

Swap(string, string, ChainNode<T>, ChainNode<T>)

Swaps node node1 with node2.

public bool Swap(string tag, string newTag, ChainNode<T> node1, ChainNode<T> node2)

Parameters

tag string

The tag.

newTag string

The new tag.

node1 ChainNode<T>

The node 1.

node2 ChainNode<T>

The node 2.

Returns

bool

True if swapped.

SwapRanges(string, string, ChainNode<T>, ChainNode<T>, ChainNode<T>, ChainNode<T>)

Swaps the specified ranges of nodes.

public bool SwapRanges(string tag, string newTag, ChainNode<T> first1Node, ChainNode<T> last1Node, ChainNode<T> first2Node, ChainNode<T> last2Node)

Parameters

tag string

The version tag.

newTag string

The new version tag.

first1Node ChainNode<T>

The first node of range 1.

last1Node ChainNode<T>

The last node of range 1.

first2Node ChainNode<T>

The first node of range 2.

last2Node ChainNode<T>

The last node of range 2.

Returns

bool

True if swapped.

Exceptions

InvalidOperationException

The two sequences to swap overlap.

ArgumentNullException

tag or newTag or first1Node or last1Node or first2Node or last2Node

ToString()

Converts to string.

public override string ToString()

Returns

string

string