Linq-Connection for data structure
我最近设计了一个类似于"队列"和"堆栈"的数据结构,用于特殊目的,其中包含固定的最大对象数,当它被填满并插入时,第一个插入的对象将退出。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | 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]; } } |
现在,我很好奇是否有可能将其与Linq连接起来,提供如下内容:
1 2 3 4 5 | AssemblyLine<int> myAssemblyLine = new AssemblyLine(100); // Insert some Stuff List<int> myList = myAssemblyLine.Where(i => i > 5).ToList(); |
有人知道吗?
必须为.where()实现
一旦实现了
几乎所有的linq扩展方法都在
有两种方法可以使用非泛型