2

I recently designed a data structure similar to 'Queue' and 'Stack' for a special purpose, with a fixed maximum number of objects in it and when it is full and inserted, the first inserted object drops out.

The Code:

public class AssemblyLine<T>
{      

    private long length;
    public long Length { get { return this.length; } }

    private T[] data;
    private long Pointer = 0;

    private long count = 0;
    public long Count { get { return this.count; } }

    public void Insert(T obj)
    {

        this.Data[Pointer] = obj;
        this.Next();

        if (this.count < this.length)
            this.count++;

    }

    public T[] GetLastX(long x)
    {

        long p = this.Pointer;

        if (x > this.count)
            x = this.count;

        T[] result = new T[x];
        for (int i = 0; i < x; i++)
        {

            Previous();
            result[i] = Grab();

        }

        this.Pointer = p;
        return result;

    }

    public T[] GetFirstX(long x)
    {

        long p = this.Pointer;

        if (x > this.count)
            x = this.count;

        long gap = this.length - this.count;
        this.Pointer = (this.Pointer + gap) % this.length;

        T[] result = new T[x];
        for (int i = 0; i < x; i++)
        {

            result[i] = Grab();
            Next();

        }

        this.Pointer = p;
        return result;

    }

    public void Clear()
    {

        this.data = new T[this.length];
        this.count = 0;

    }

    private void Next()
    {

        this.Pointer++;

        if (this.Pointer > this.length - 1)
            this.Pointer = 0;

    }

    private void Previous()
    {

        this.Pointer--;

        if (this.Pointer < 0)
            this.Pointer = this.length - 1;

    }

    private T Grab()
    {

        return this.data[this.Pointer];

    }

    public AssemblyLine(long Length)
    {

        this.length = Length;
        this.data = new T[Length];

    }

}

Now I am curious if its possible to get that connected to Linq, providing something like this:

  AssemblyLine<int> myAssemblyLine = new AssemblyLine(100);

  // Insert some Stuff

  List<int> myList = myAssemblyLine.Where(i => i > 5).ToList();

Any idea someone?

Martin Tausch
  • 714
  • 5
  • 20

2 Answers2

1

Almost all LINQ extension methods are declared on IEnumerable<T>, so as soon as your class implements that interface, you'll get it for free.

There are just couple methods that use non-generic IEnumerable, like Cast or OfType, but if you can get your class implement generic IEnumerable<T> it will be much better, because users won't have to call Cast<T> first, to get IEnumerable<T> and access all the other methods (as is right now the case for some legacy collections).

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

You have to implement IEnumerable<T> for .Where(). See Adding LINQ to my classes.

Once you implement IEnumerable<T> and all required methods, you will have access to these methods: http://msdn.microsoft.com/en-us/library/vstudio/system.linq.enumerable_methods(v=vs.100).aspx

Community
  • 1
  • 1
Anshul
  • 1,302
  • 3
  • 15
  • 36