If Push () and Pop () are considered expensive, you can save 5 of them by storing the last 5 elements in an array. If the rest of the stack can be dropped, you can save a lot more.
#ifdef NEED_REST_OF_STACK Stack newstack; #endif object[] cache=new object[5]; object tmp; int i=0; while (tmp=originalstack.Pop()) { cache[i%5]=tmp; #ifdef NEED_REST_OF_STACK if (i>4) newstack.Push(cache[(i-1)%5]);
The stack is in the source table, the result is in the cache.
source share