非通用 Stack类声明“堆栈是作为循环缓冲区实现的。”
我不明白循环缓冲区在 Stack 用例中的应用。我也不明白堆栈如何实现为循环缓冲区。
Wikipedia说这个:
The useful property of a circular buffer is that it does not need to have its elements shuffled around when one is consumed. (If a non-circular buffer were used then it would be necessary to shift all elements when one is consumed.) In other words, the circular buffer is well suited as a FIFO buffer while a standard, non-circular buffer is well suited as a LIFO buffer.
Circular buffering makes a good implementation strategy for a queue that has fixed maximum size.
那么......堆栈是如何实现为循环缓冲区的,为什么?
最佳答案
我怀疑这是文档中的复制/粘贴/编辑错误;在反射器中查看,它不是作为循环缓冲区实现的;例如 push 是(在调整大小代码之后)基本上是:
this._array[this._size++] = obj;
偷看是:
return this._array[this._size - 1];
和流行是:
object value = this._array[--this._size];
this._array[this._size] = null;
return value;
请注意,它不使用任何类型的偏移/环绕 - 因此它实际上并未使用循环缓冲区。你的直觉看起来是正确的,但文档看起来是错误的。
关于.net - 为什么(非通用)Stack 类实现为循环缓冲区? (这到底是什么意思)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16955883/