实现自定义泛型列表/队列/堆栈相结合的有效方式 [英] An efficient way to implement a custom generic list/queue/stack combination

查看:129
本文介绍了实现自定义泛型列表/队列/堆栈相结合的有效方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个以上的场合的通用集合需要在时间和作为栈或队列在另一时间被处理作为列表在一个点上。对于我目前正在开发一个应用程序,它没有任何意义,使用三个不同的对象。

我能想到的最简单的解决方案是实现队列/出列/按键/上的标准列表流行/窥视功能。也(不包括在$ C $以下c)中,一个接口约束施加于T允许类保持位置/顺序索引每个列表,队列,堆

 公共类List< T>:
    System.Collections.Generic.List< T>
{
    私有对象SyncRoot上=新的对象();

    公共无效排队(T项)
    {
        锁定(this.SyncRoot)
        {
            this.Add(项目);
        }
    }

    公共牛逼出列()
    {
        牛逼项=默认(T);

        锁定(this.SyncRoot)
        {
            如果(this.Count大于0)
            {
                项目=此[0];
                this.RemoveAt(0);
            }
        }

        返回(项目);
    }

    公共无效推送(T项)
    {
        锁定(this.SyncRoot)
        {
            this.Add(项目);
        }
    }

    公共牛逼POP()
    {
        牛逼项=默认(T);

        锁定(this.SyncRoot)
        {
            如果(this.Count大于0)
            {
                项目=此[this.Count  -  1];
                this.RemoveAt(this.Count  -  1);
            }
        }

        返回(项目);
    }

    公共牛逼PeekQueue()
    {
        牛逼项=默认(T);

        锁定(this.SyncRoot)
        {
            如果(this.Count大于0)
            {
                项目=此[0];
            }
        }

        返回(项目);
    }

    公共牛逼PeekStack()
    {
        牛逼项=默认(T);

        锁定(this.SyncRoot)
        {
            如果(this.Count大于0)
            {
                项目=此[this.Count  -  1];
            }
        }

        返回(项目);
    }
}
 

  • 由于这是一个粗略的,在即时执行,我不知道哪一个角落的情况下看出来的,以便将AP preciate指针或链接到任何现有的这种实现。
  • 其次,我怀疑在非常大名单的性能。是从列表比使用说LinkedList的大型列表很好地继承了决定。就我而言,添加/删除项目比枚举列表更优先。
解决方案

下面是如何acomplish与扩展方法类似的操作... 的http://weblogs.asp.net/bsimser/archive/2011/01/13/generic-pop-and-push-for-list-lt-t-gt.aspx

  

一般流行,推动名单

     

下面是一个小片段我用它来扩展泛型列表类的有   类似capabilites的Stack类。

     

Stack类是伟大的,但它生活在自己的世界下   System.Object的。那岂不是很高兴有一个列表,可以做   相同?这里的code:

 公共静态类ExtensionMethods
{
    公共静态牛逼流行LT; T>(名单< T>的thelist)
    {
        VAR本地=的thelist [theList.Count  -  1];
        theList.RemoveAt(theList.Count  -  1);
        回到地方;
     }

     公共静态无效的推< T>(名单< T>的thelist,T项)
     {
        theList.Add(项目);
     }
}
 

  

这是一个简单的扩展,但我发现它很有用,希望你会   太!享受。

也是一个链接到扩展方法 http://msdn.microsoft.com/en-us/library/bb383977.aspx

I have found more than one occasions where a generic collection needs to be treated as a list at one point in time and as a stack or queue at another time. For an an application I'm currently developing, it does not make sense to use three separate objects.

The simplest solution I could think of was to implement Queue/Dequeue/Push/Pop/Peek functions on the standard List. Also (not included in the code below), an interface constraint is applied on T allowing the class to maintains a position/ordinal index for each list, queue and stack.

public class List<T>:
    System.Collections.Generic.List<T>
{
    private object SyncRoot = new object();

    public void Enqueue (T item)
    {
        lock (this.SyncRoot)
        {
            this.Add(item);
        }
    }

    public T Dequeue ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [0];
                this.RemoveAt(0);
            }
        }

        return (item);
    }

    public void Push (T item)
    {
        lock (this.SyncRoot)
        {
            this.Add(item);
        }
    }

    public T Pop ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [this.Count - 1];
                this.RemoveAt(this.Count - 1);
            }
        }

        return (item);
    }

    public T PeekQueue ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [0];
            }
        }

        return (item);
    }

    public T PeekStack ()
    {
        T item = default(T);

        lock (this.SyncRoot)
        {
            if (this.Count > 0)
            {
                item = this [this.Count - 1];
            }
        }

        return (item);
    }
}

  • Since this is a rough, on-the-fly implementation, I'm not sure what corner cases to look out for so would appreciate pointers or links to any existing such implementations.
  • Secondly, I am skeptical about performance on very large lists. Is the decision to inherit from List better than using say LinkedList for large lists. In my case, adding/removing items has more priority than enumerating the list.

解决方案

Here is how to acomplish similiar behavior with extension methods... http://weblogs.asp.net/bsimser/archive/2011/01/13/generic-pop-and-push-for-list-lt-t-gt.aspx

Generic Pop and Push for List

Here's a little snippet I use to extend a generic List class to have similar capabilites to the Stack class.

The Stack class is great but it lives in its own world under System.Object. Wouldn't it be nice to have a List that could do the same? Here's the code:

public static class ExtensionMethods    
{ 
    public static T Pop<T>(this List<T> theList)    
    {              
        var local = theList[theList.Count - 1];    
        theList.RemoveAt(theList.Count - 1);    
        return local;   
     }

     public static void Push<T>(this List<T> theList, T item)   
     {   
        theList.Add(item);   
     }   
}

It's a simple extension but I've found it useful, hopefully you will too! Enjoy.

also a link to extension methods http://msdn.microsoft.com/en-us/library/bb383977.aspx

这篇关于实现自定义泛型列表/队列/堆栈相结合的有效方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆