C# 数据结构的Linq连接
我最近为一个特殊目的设计了一个类似于“队列”和“堆栈”的数据结构,其中有固定的最大对象数,当它已满并插入时,第一个插入的对象将退出 守则:C# 数据结构的Linq连接,c#,linq,data-structures,C#,Linq,Data Structures,我最近为一个特殊目的设计了一个类似于“队列”和“堆栈”的数据结构,其中有固定的最大对象数,当它已满并插入时,第一个插入的对象将退出 守则: public class AssemblyLine<T> { private long length; public long Length { get { return this.length; } } private T[] data; private long Pointer = 0;
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];
}
}
公共类装配线
{
私人长;
公共长长度{get{返回this.Length;}}
私有T[]数据;
私有长指针=0;
私有长计数=0;
公共长计数{get{返回this.Count;}}
公共无效插入(T obj)
{
this.Data[指针]=obj;
这个。下一个();
if(this.countthis.count)
x=这个。计数;
T[]结果=新的T[x];
对于(int i=0;ithis.count)
x=这个。计数;
长间隙=this.length-this.count;
this.Pointer=(this.Pointer+gap)%this.length;
T[]结果=新的T[x];
对于(int i=0;ithis.length-1)
指针=0;
}
私有无效以前的()
{
这个指针--;
if(this.Pointer<0)
this.Pointer=this.length-1;
}
私人T Grab()
{
返回this.data[this.Pointer];
}
公共装配线(长)
{
这个长度=长度;
this.data=新的T[长度];
}
}
现在我很好奇是否有可能将其连接到Linq,提供如下内容:
AssemblyLine<int> myAssemblyLine = new AssemblyLine(100);
// Insert some Stuff
List<int> myList = myAssemblyLine.Where(i => i > 5).ToList();
装配线myAssemblyLine=新装配线(100);
//插入一些东西
List myList=myAssemblyLine.Where(i=>i>5.ToList();
有人知道吗?几乎所有的LINQ扩展方法都是在
IEnumerable
上声明的,所以只要您的类实现了该接口,您就可以免费获得它
只有两种方法使用非泛型的
IEnumerable
,比如Cast
或of type
,但是如果你能让你的类实现泛型的IEnumerable
,它会更好,因为用户不必先调用Cast
,就可以获得IEnumerable
并访问所有其他方法(就像现在一些遗留集合的情况一样)。您必须为.Where()实现IEnumerable
。请参阅
一旦您实现了
IEnumerable
和所有必需的方法,您就可以访问这些方法:您的AssemblyLine
类必须实现IEnumerable
。您最好从队列
派生类,除非这是密封的
IEnumerable
,实际上,对于h来说是用例。对。我的评论是一天的结束,大约在10秒钟内走出工作的大门,快速扔一些东西到那里。