Reputation: 1625
I am trying to create a buffer (not circle buffer) for big arrays like 100x100 with a specified size.
I can't find any good solution on the internet so i am trying do write it by myself. Here is the code:
In this exa,ple I will be using only small arrays 2x2 and buffer size is 3
A = [1 1; 1 1];
B = [2 2; 2 2];
C = [3 3; 3 3];
buffer = [C B A];
D = [4 4; 4 4];
Now i want to push D and pop A to look like [D C B]
buffer = buffer(1:2,3:6); %creating [C B]
buffer = [D buffer] %creating [D C B]
Now the question: What about the A in the memory? Is it still there or it is deleted? If I use about 1000 arrays with size [500x500] with buffer size 3 it would be really bad if I had so much trash in memory. If it is wrong, is there any other way to write such buffer?
Upvotes: 1
Views: 126
Reputation: 30579
Your "push front" syntax buffer = [D buffer]
seems fine.
As for "push back", you can either concatenate in a similar manner (buffer = [buffer D];
) or you can index past the end:
buffer(:,end:end+size(D,2)) = D; % assuming size(D,1) equals size(buffer,1)
For "pop" you either do buffer=buffer(keepRows,keepCols);
as in your example, or you can assign []
to whatever indexes you want to remove. For example, given buffer = [C B A];
, "pop front" to remove C
would be:
buffer(:,1:size(C,2)) = [];
You can remove any values this way, including center elements:
buffer(:,3:4) = []; % remove B from [C B A]
In this way, buffer
is rewritten and the removed values are lost. However, the original variable (e.g. B
) used to compose buffer
remains until you clear it. Keep in mind that when you do buffer = [C B A];
, it copies the contents of each variable when doing horizontal concatenation, rather than putting the array in a list.
I once did a pretty thorough comparison of array truncation performance with the v(end-N:end)=[]
syntax and the v=v(1:N-1)
syntax. Although that is just for a 1D vector, it might be helpful. You may also want to have a look at this article on automatic array growth performance.
Upvotes: 1
Reputation: 36710
I would suggest to use a java linked list.
import java.util.LinkedList
q=linkedList()
q.add(A);
q.add(B);
q.add(C);
%get top element
o=q.pop();
%insert new
q.add(D);
Your code requires to copy the whole buffer content, which will be slow for large buffers.
Upvotes: 1