Monday, 2 September 2013

What is the best way/data strcture to implement a Clipboard?

What is the best way/data strcture to implement a Clipboard?

I'm working in Unity3D with C# and working on a very simple file manager
for my game. There are simple operations that you could perform on the
files/folders, one of them is "Cut" (and maybe "Copy", for later)
For better imagining of what's happening:

For copying and pasting, I need a custom Clipboard. I thought of two data
structures to implement it with, a Stack and a Queue. The problem with the
stack, is that if I cut 'a', 'b' and 'c', they'll get pasted in reverse,
LIFO. So I thought a queue is better.
public class Clipboard
{
private SLLQueue queue; // SLL stands for SinglyLinkedList, the queue
is implemented that way...
public Clipboard()
{
queue = new SLLQueue();
}
public void Add(IComparable data)
{
queue.Clear();
Append(data);
}
public void Add(List<IComparable> data)
{
queue.Clear();
foreach (var d in data)
Append(d);
}
private void Append(IComparable data)
{
queue.Push(data);
}
public IComparable[] Clear()
{
var data = new IComparable[queue.Count];
for (int i = 0, len = queue.Count; i < len; i++)
{
data[i] = queue.Pop();
}
return data;
}
}
Is this good enough? - Is this the right data structure? - And did I
implement the Clipboard's operations the right way?
Thanks a lot for your help.

No comments:

Post a Comment