From 3c4e5fe12c2eac887710d7f04e9a728bc7fb4351 Mon Sep 17 00:00:00 2001 From: dahall Date: Fri, 25 Sep 2020 17:17:51 -0600 Subject: [PATCH] More work on generic History collection --- Core/Collections/History.cs | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/Core/Collections/History.cs b/Core/Collections/History.cs index 732c36d6..6a0c2081 100644 --- a/Core/Collections/History.cs +++ b/Core/Collections/History.cs @@ -29,7 +29,8 @@ namespace Vanara.Collections /// Adds the specified item as the last history entry and sets the property to it's value. /// The item to add to the history. - void Add(T item); + /// indicates to remove all items forward of the current pointer; leaves the history intact. + void Add(T item, bool removeForwardItems); /// Clears the history of all items. void Clear(); @@ -100,15 +101,15 @@ namespace Vanara.Collections /// Indicates the presence of items in the history that can be reached by calling . /// if this instance can seek backward; otherwise, . - public bool CanSeekBackward => GetCurrent()?.Previous != null; + public virtual bool CanSeekBackward => GetCurrent()?.Previous != null; /// Indicates the presence of items in the history that can be reached by calling . /// if this instance can seek forward; otherwise, . - public bool CanSeekForward => GetCurrent()?.Next != null; + public virtual bool CanSeekForward => GetCurrent()?.Next != null; /// Gets or sets the capacity of the history, or the maximum number of items that it will hold. /// The history's capacity. - public int Capacity + public virtual int Capacity { get => capacity; set @@ -132,16 +133,31 @@ namespace Vanara.Collections /// Gets the value at a pointer within the history that represents the current item. /// The current item. /// There are no items in the history. - public T Current => !(GetCurrent() is null) ? current.Value : throw new InvalidOperationException("There are no items in the history."); + public virtual T Current => !(GetCurrent() is null) ? current.Value : throw new InvalidOperationException("There are no items in the history."); /// Gets the items in the history. /// The number of items. - public int Count => activeHistory.Count; + public virtual int Count => activeHistory.Count; - /// Adds the specified item as the last history entry and sets the property to it's value. + /// + /// Adds the specified item as the last history entry and sets the property to it's value. + /// /// The item to add to the history. - public void Add(T item) + /// indicates to remove all items forward of the current pointer; leaves the history intact. + public virtual void Add(T item, bool removeForwardItems = false) { + if (removeForwardItems && CanSeekForward) + { + var ptr = activeHistory.Last; + var items = new List(); + while (ptr != current) + { + items.Add(ptr.Value); + activeHistory.RemoveLast(); + ptr = activeHistory.Last; + } + OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items)); + } var added = activeHistory.AddLast(item); OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); if (activeHistory.Count > Capacity) @@ -156,7 +172,7 @@ namespace Vanara.Collections } /// Clears the history of all items. - public void Clear() + public virtual void Clear() { if (Count == 0) return; activeHistory.Clear(); @@ -168,13 +184,13 @@ namespace Vanara.Collections /// Returns an enumerator that iterates through the collection. /// A that can be used to iterate through the collection. - public IEnumerator GetEnumerator() => activeHistory.GetEnumerator(); + public virtual IEnumerator GetEnumerator() => activeHistory.GetEnumerator(); /// Gets a specified number of items starting at a location within the history. /// The maximum number of items to retrieve. The actual number of items returned may be less if not avaialable. /// The reference point within the history at which to start fetching items. /// A read-only list of items. - public IReadOnlyList GetItems(int count, SeekOrigin origin) + public virtual IReadOnlyList GetItems(int count, SeekOrigin origin) { if (count == 0 || Count == 0) return (IReadOnlyList)new List(0); var ptr = origin switch @@ -204,7 +220,7 @@ namespace Vanara.Collections /// /// The number of items to move cannot be accomplished given the number of items in the history and the seek origin. /// - public T Seek(int count, SeekOrigin origin) + public virtual T Seek(int count, SeekOrigin origin) { if (activeHistory.Count == 0) throw new InvalidOperationException("Cannot seek on an empty history."); var ptr = origin switch @@ -226,11 +242,11 @@ namespace Vanara.Collections /// Seeks one position backwards. /// The value at the new current pointer position. - public T SeekBackward() => CanSeekBackward ? Seek(-1, SeekOrigin.Current) : default; + public virtual T SeekBackward() => CanSeekBackward ? Seek(-1, SeekOrigin.Current) : default; /// Seeks one position forwards. /// The value at the new current pointer position. - public T SeekForward() => CanSeekForward ? Seek(1, SeekOrigin.Current) : default; + public virtual T SeekForward() => CanSeekForward ? Seek(1, SeekOrigin.Current) : default; /// Returns an enumerator that iterates through a collection. /// An object that can be used to iterate through the collection.