【问题标题】:Alternative for the Stack堆栈的替代方案
【发布时间】:2011-06-27 13:20:09
【问题描述】:

我正在使用 C# 在 .Net 环境中工作。我需要一些 Stack 数据结构的替代方案。某种绑定堆栈。集合中元素的数量不应超过某个固定的指定数量。而且,如果达到该数量并推送新元素,则必须删除大多数旧元素。我需要这个来存储撤消/重做策略的命令。

【问题讨论】:

    标签: c# .net data-structures stack circular-buffer


    【解决方案1】:

    circular buffer 应该可以完成这项工作;很容易包装一个列表或数组,但在 AFAIK 中没有内置任何东西。

    【讨论】:

    • 谢谢。我在这里找到了循环缓冲区的免费实现:bufferhttp://circularbuffer.codeplex.com/
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      这是一个容量受限的堆栈实现。 达到给定容量后,超出容量的堆栈底部项目将被丢弃。可以遍历包含的对象并将索引设置为特定位置(如倒带),以便在将新项目推入堆栈时一次丢弃多个条目。

      这是一个自己的实现,如果您需要返回历史并再次转发(内置),它会阻止您处理多个列表。

      public class DiscardingStack<TObject> : IEnumerable<TObject>
      {
          private readonly int capacity;
      
          private readonly List<TObject> items;
      
          private int index = -1;
      
          public DiscardingStack(int capacity)
          {
              this.capacity = capacity;
              items = new List<TObject>(capacity);
          }
      
          public DiscardingStack(int capacity, IEnumerable<TObject> collection)
              : this(capacity)
          {
              foreach (var o in collection)
              {
                  Push(o);
              }
          }
      
          public DiscardingStack(ICollection<TObject> collection)
              : this(collection.Count, collection)
          {
          }
      
          public void Clear()
          {
              if (items.Count >= 0)
              {
                  items.Clear();
                  index = items.Count - 1;
              }
          }
      
          public int Index
          {
              get { return index; }
              set
              {
                  if (index >= 0 && index < items.Count)
                  {
                      index = value;
                  }
                  else throw new InvalidOperationException();
              }
          }
      
          public int Count
          {
              get { return items.Count; }
          }
      
          public TObject Current
          {
              get { return items[index]; }
              set { index = items.IndexOf(value); }
          }
      
          public int Capacity
          {
              get { return capacity; }
          }
      
          public TObject Pop()
          {
              if (items.Count <= 0)
                  throw new InvalidOperationException();
      
              var i = items.Count - 1;
              var removed = items[i];
              items.RemoveAt(i);
      
              if (index > i)
                  index = i;
      
              return removed;
          }
      
          public void Push(TObject item)
          {
              if (index == capacity - 1)
              {
                  items.RemoveAt(0);
                  index--;
              }
              else if (index < items.Count - 1)
              {
                  var removeAt = index + 1;
                  var removeCount = items.Count - removeAt;
                  items.RemoveRange(removeAt, removeCount);
              }
      
              items.Add(item);
      
              index = items.Count - 1;
          }
      
          public TObject Peek()
          {
              return items[items.Count-1];
          }
      
          public TObject this[int i]
          {
              get { return items[i]; }
          }
      
          public IEnumerator<TObject> GetEnumerator()
          {
              return items.GetEnumerator();
          }
      
          IEnumerator IEnumerable.GetEnumerator()
          {
              return GetEnumerator();
          }
      }
      

      无论如何,如果您的列表很大(避免复制),则应该使用 LinkedList(如上所述)来构建一个在达到最大容量时丢弃元素的堆栈。所以在这种情况下,如果缓冲区最大值很高,LinkedList 的想法可能会更好,而不是包装一个 List。

      我还建议将 Push()、Pop() 等打包到接口中(例如 IStack)。遗憾的是,.Net (afaik) 中没有预定义 IStack 接口。

      【讨论】:

        【解决方案4】:

        .Net 在集合类型方面相当不足。你会发现一个收藏库here。使用 CircularQueue。

        【讨论】:

        • 谢谢。收藏库会很方便。现在我知道当我需要一些特定的收藏时该去哪里找。
        【解决方案5】:

        Framework 中没有用于此的内置类。 (我们不希望自动删除数据)。但是您可以很好地扩展 Stack 类并覆盖 Push/Pop 和其他方法以满足您的需求。

        【讨论】:

        • 这不是一个好主意,因为它们不是虚拟的。你可以向下转型,然后调用旧方法。
        • 当你降级时这显然意味着你想要基本实现......这就是它应该是的......
        • 问题是没有IStack接口。在这种情况下,将列表作为成员嵌入是“更安全的”(组合而不是继承)。否则继承可能会导致意想不到的结果。如果有人调用向下转换的 System.Collections.Generics.Stack 的 Push() 并且您的 public new Push() 实现应该触发一个事件......该事件不会被触发并且听众没有得到关于的信息堆栈更改(例如,显示项目数时)。
        猜你喜欢
        • 1970-01-01
        • 2010-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多